New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@clds/style-utils

Package Overview
Dependencies
Maintainers
0
Versions
141
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@clds/style-utils

---

  • 0.53.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
981
increased by156.14%
Maintainers
0
Weekly downloads
 
Created
Source

@clds/style-utils


npm version

This is a utility package that contains helper methods to ease the work with styled-components

media

Available media:

Breakpoint nameMin widthMax width
phone0px576px
tablet577px768px
desktop769px1024px
wide1025px1280px

You should use it when your component should change it's style based on the browser resolution.
This is preffered over using hooks when possible, css is much more performant.
If you use hooks to pass properties to styled components, a new CSS class will be created on each change - this should be avoided.

import { media } from '@clds/style-utils';

const ModalBox = styled.div`
  height: 220px;
  width: 520px;

  @media ${media.tablet()} {
    width: 280px;
  }
`;

By default it will use a range, meaning that if you will use tablet from the example above the media query will be as follows:

(min-width: <MOBILE_RESOLUTION>) and (max-width: <TABLET_RESOLUTION>)

You can use the options in the following way to allow different behaviors:

import { media } from '@clds/style-utils';

const ModalBox = styled.div`
  height: 220px;
  width: 520px;

  @media ${media.tablet({ minOnly: true })} {
    width: 280px;
  }
`;

Because only the query body is generated you will have full intellisense support from IDE in it's content. (hence @media is used, to prevent breaking out from styled component template)
Additional benefit is that you can combine the queries:

import { media } from '@clds/style-utils';

const ModalBox = styled.div`
  height: 220px;
  width: 520px;

  @media ${media.tablet({ minOnly: true })} and ${media.wide({
      maxOnly: true,
    })} {
    width: 280px;
  }
`;

Usage with Hooks

import { useDesktop, usePhone } from '@clds/style-utils';

const View = () => {
  const isPhone = usePhone();
  const isDesktopAndBelow = useDesktop({ maxOnly: true });

  if (isPhone) return <div>This is phone</div>;
  if (isDesktopAndBelow) return <div>This is desktop, tablet or phone</div>;
  return <div>This is above desktop</div>;
};

Available Hooks

  • usePhone
  • useTablet
  • useDesktop
  • useWide

Options

NameTypeDescriptiondefault
maxOnlybooleanMedia query will only have max-widthfalse
minOnlybooleanMedia query will only have min-widthfalse

checkerboard

A function that returns a checkerboard background style

Options

NameTypeDescriptiondefault
color1stringThe first color to use-
color2stringThe second color to usetransparent
sizenumberThe size (in pixels) of the squares15

createStylesheetsFromUrls

A function that creates stylesheet link elements from given urls and appends them to document head.

To identify these link elements, a custom attribute is been added (cld-dynamic-stylesheet). This allows recycling existing link elements by replacing their href when needed, and disposing un-necessary link elements.

For the given input -

import { createStylesheetsFromUrls } from '@clds/style-utils';

const urls = ['https://some-style-sheet.com'];

createStylesheetsFromUrls(urls);

Output will be -

<link
  href="https://some-style-sheet.com"
  rel="stylesheet"
  cld-dynamic-stylesheet="cld"
/>

Versioning

This library follows Semantic Versioning.

License

See LICENSE

FAQs

Package last updated on 17 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc