|
1 | 1 | [](http://commitizen.github.io/cz-cli/) |
| 2 | +[](https://codecov.io/gh/wearehumblebee/styled-components-breakpoint) |
| 3 | +[](https://github.com/semantic-release/semantic-release) |
| 4 | +[]() |
| 5 | + |
| 6 | +## Styled Components Breakpoint 💅 |
| 7 | +This package provides a friendly API for working with breakpoints in [Styled Components](https://www.styled-components.com/). It allows you to set up any number of breakpoints with a naming convention of your choice. |
| 8 | +Once set up you'll have three main ways of interacting with your breakpoints `up` (min-width), `down` (max-width), and `only` (a range between two breakpoints). |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +### Installation |
| 13 | +```sh |
| 14 | +yarn install @humblebee/styled-components-breakpoint |
| 15 | +# or |
| 16 | +npm install @humblebee/styled-components-breakpoint |
| 17 | +``` |
| 18 | +--- |
| 19 | + |
| 20 | +### Usage and example |
| 21 | + |
| 22 | +The default export of `styled-components-breakpoint` is a function that accepts a config object with your breakpoints. This will return an instance with utility functions for each breakpoint. |
| 23 | +I'd recommend that you store this instance in a separate file that can later be imported and used by any component. |
| 24 | + |
| 25 | +Coming from form SASS I like to think of these kind of utility functions for `styled-components` in terms of `mixins`. I also think it makes seance to keep these "mixins" close to our projects themes (if any exist), to make them easier to discover when working on other styles. With this in mind the folder structure could look something like this: |
| 26 | + |
| 27 | +. |
| 28 | ++--componsnts |
| 29 | +| +--Button.js |
| 30 | ++--themes |
| 31 | +| +--mixins.js |
| 32 | + |
| 33 | + |
| 34 | +`themes/mixins.js` |
| 35 | +```javascript |
| 36 | +import styledBreakpoint from '@humblebee/styled-components-breakpoint'; |
| 37 | + |
| 38 | +// Create an instance of styled-components-breakpoint and pass it an object of breakpoints. |
| 39 | +export const breakpoint = styledBreakpoint({ |
| 40 | + xs: 320, |
| 41 | + s: 576, |
| 42 | + m: 768, |
| 43 | + l: 992, |
| 44 | + xl: 1200, |
| 45 | +}); |
| 46 | +``` |
| 47 | + |
| 48 | +`components/Button.js` |
| 49 | +```javascript |
| 50 | +// Styled Components |
| 51 | +import styled from 'styled-components'; |
| 52 | +// Our breakpoint instance/mixin |
| 53 | +import { breakpoint } from '../../theme/mixins'; |
| 54 | + |
| 55 | +const Button = styled.button` |
| 56 | + background: white; |
| 57 | + ${breakpoint.m}` |
| 58 | + background: palevioletred; |
| 59 | + ` |
| 60 | + ` |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +In the above example we create an instance of `styled-components-breakpoint` in a file called `themes/mixins.js` and export it with the name `breakpoint`. |
| 65 | +We then import `breakpoint` in a separate file called `components/Button.js` and use it inside the styling of our `styled-component` button. |
| 66 | +The function `breakpoint.m` will result in the following media query: `'@media only screen and (min-width: 768px)'`, giving our button component a `background-color` of `palevioletred` when the viewport is wider than 768px. |
| 67 | + |
| 68 | +## API |
| 69 | + |
| 70 | +Continuing on the above example, you have access to all the other breakpoints in the same manner, in our case: `breakpoint.xs`, `breakpoint.s`, `breakpoint.m`, `breakpoint.l`, `breakpoint.xl`. As mentioned in the intro you can add as many breakpoints as you like with any naming convention you prefer. If you prefer the naming convention used in Twitter Bootstrap, your config object would look like this. |
| 71 | + |
| 72 | +```javascript |
| 73 | +export const breakpoint = styledBreakpoint({ |
| 74 | + xs: 320, |
| 75 | + sm: 576, |
| 76 | + md: 768, |
| 77 | + lq: 992, |
| 78 | + xl: 1200, |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +#### breakpoint.m.up |
| 85 | + |
| 86 | +In the "Usage and example" section we made use of the function `breakpoint.m`, this is a shorthand for writing `breakpoint.up.m`. The reason for this shorthand is to encourage the usage of mobile-first breakpoints, i.e. `min-width` media queries. |
| 87 | + |
| 88 | +The functions `breakpoint.m` and `breakpoint.up.m` are the same. |
| 89 | + |
| 90 | +```javascript |
| 91 | +const Button = styled.button` |
| 92 | + background: white; |
| 93 | + ${breakpoint.m.up}` |
| 94 | + background: palevioletred; |
| 95 | + ` |
| 96 | + ` |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +#### breakpoint.m.down |
| 101 | + |
| 102 | +In contrast to `breakpoint.up`, `breakpoint.down` goes the opposite direction and returns a `max-width` media query. The example below would return the media query `@media only screen and (min-width: 768px)`. |
| 103 | + |
| 104 | +```javascript |
| 105 | +const Button = styled.button` |
| 106 | + background: white; |
| 107 | + ${breakpoint.m.down}` |
| 108 | + background: palevioletred; |
| 109 | + ` |
| 110 | + ` |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +#### breakpoint.m.only(breakpoint?) |
| 115 | + |
| 116 | +Unlike `up` and `down`, the `only` function accepts an optional breakpoint argument in the form of a string. This argument is used to return a range media query, between the breakpoint used in the executing function and the passed argument. |
| 117 | +For example, executing `breakpoint.m.only('xl')`, will return a range between the `m` and `xl` breakpoints. |
| 118 | +```javascript |
| 119 | +`breakpoint.m.only('xl')` |
| 120 | +// Will return: |
| 121 | +// @media only screen and (min-width: 768px) and (max-width: 1200px) |
| 122 | +``` |
| 123 | + |
| 124 | +It works just as well to pass a smaller breakpoint than the executing function. For example starting at breakpoint `m` and going down to `s`. |
| 125 | +```javascript |
| 126 | +`breakpoint.m.only('s')` |
| 127 | +// Will return: |
| 128 | +// @media only screen and (max-width: 768px) and (min-width: 576px) |
| 129 | +``` |
| 130 | + |
| 131 | +If no argument is passed the next upper breakpoint will be used implicitly. For example, executing `breakpoint.m.only()`, will return a range between the `m` and `l` breakpoints. |
| 132 | + |
| 133 | +```javascript |
| 134 | +`breakpoint.m.only()` |
| 135 | +// Will return: |
| 136 | +// @media only screen and (min-width: 768px) and (max-width: 992px) |
| 137 | +``` |
| 138 | + |
| 139 | +**Important! Even if no breakpoint argument is passed to `only`, it is still necessary to actually execute the function. If not no media query will be returned.** |
| 140 | + |
| 141 | +**Correct: breakpoint.m.only()** |
| 142 | + |
| 143 | +**Wrong: breakpoint.m.only** |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +Happy coding! Wishes the bees at [Humblebee](https://humblebee.se) 🐝 |
0 commit comments