0.46.1 • Published 9 months ago

@clds/style-utils v0.46.1

Weekly downloads
-
License
-
Repository
github
Last release
9 months ago

@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

0.46.0

10 months ago

0.46.1

9 months ago

0.44.4

10 months ago

0.44.2

10 months ago

0.44.3

10 months ago

0.44.0

10 months ago

0.44.1

10 months ago

0.45.3

10 months ago

0.45.1

10 months ago

0.45.2

10 months ago

0.45.0

10 months ago

0.43.0

11 months ago

0.40.0

1 year ago

0.41.1

1 year ago

0.41.0

1 year ago

0.42.4

12 months ago

0.42.2

1 year ago

0.42.3

12 months ago

0.42.0

1 year ago

0.42.1

1 year ago

0.39.0

1 year ago

0.39.2

1 year ago

0.43.1

11 months ago

0.38.0

2 years ago

0.37.0

2 years ago

0.36.3

2 years ago

0.36.2

2 years ago

0.36.1

2 years ago

0.36.0

2 years ago

0.32.4

2 years ago

0.34.0

2 years ago

0.33.10

2 years ago

0.33.7

2 years ago

0.33.6

2 years ago

0.35.3

2 years ago

0.33.5

2 years ago

0.35.2

2 years ago

0.35.1

2 years ago

0.33.3

2 years ago

0.35.0

2 years ago

0.33.2

2 years ago

0.33.1

2 years ago

0.33.0

2 years ago

0.33.9

2 years ago

0.33.8

2 years ago

0.32.3

2 years ago

0.32.2

2 years ago

0.32.1

2 years ago

0.32.0

2 years ago

0.30.1

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.27.0

2 years ago

0.23.4

2 years ago

0.23.3

2 years ago

0.25.0

2 years ago

0.23.2

2 years ago

0.23.1

2 years ago

0.23.0

2 years ago

0.21.0

2 years ago

0.31.1

2 years ago

0.31.0

2 years ago

0.28.1

2 years ago

0.28.0

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.24.1

2 years ago

0.24.0

2 years ago

0.20.3

2 years ago

0.22.0

2 years ago

0.20.1

2 years ago

0.20.0

2 years ago

0.19.0

2 years ago

0.19.1

2 years ago

0.17.0

2 years ago

0.18.0

2 years ago

0.20.2

2 years ago

0.16.0

2 years ago

0.15.0

2 years ago

0.14.5

2 years ago

0.14.6

2 years ago

0.13.0

3 years ago

0.12.1

3 years ago

0.14.0

3 years ago

0.13.1

3 years ago

0.12.2

3 years ago

0.14.1

3 years ago

0.12.3

3 years ago

0.14.2

3 years ago

0.14.3

3 years ago

0.14.4

2 years ago

0.8.8

3 years ago

0.8.5

3 years ago

0.7.6

3 years ago

0.8.4

3 years ago

0.7.5

3 years ago

0.8.7

3 years ago

0.10.0

3 years ago

0.11.0

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.8.3

3 years ago

0.8.2

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago