Storybook & Atomic Design - 1.15. - Responsive Styled Components
Hey! Just so you know, this article is over 2 years old. Some of the information in it might be outdated, so take it with a grain of salt. I'm not saying it's not worth a read, but don't take everything in it as gospel. If you're curious about something, it never hurts to double-check with a more up-to-date source!
GitHub - whatjackhasmade/storybook-atomic-design-react
Contribute to whatjackhasmade/storybook-atomic-design-react development by creating an account on GitHub.
In this lesson, we'll be looking at how we can create helpful SCSS mixin inspired JavaScript references to use throughout our component styles.
So far we've been using the old-school method of writing media queries by specifically setting a hard coded breakpoint min-width value to apply our CSS rules at.
If we take a look at the header component specifically we'll see this with the following styles -
While this CSS is valid and there's nothing wrong with using the code snippet found above, we can improve it by using variable values for our breakpoints.
Advantages of a Global Media Query File
#The advantage of setting up a global file that stores our breakpoints in the form of integers, px strings, and media query strings is that we can create a single source of truth for our media queries. Again allowing us to modify these breakpoints in the future by updating one source, instead of searching every reference to a media query and replacing the values manually.
Instead of defining the media queries with a value like -
We'll instead be able to reference a global breakpoint value using -
Where
device.md'(min-width: 992px)`.mediaQueries.jsx
#To implement our responsive breakpoint values, we'll need a file to store the information in. Doing so will allow us to import these values to any of our styled-components or reference the breakpoints in other component settings (e.g. Slick slider).
As the media query values aren't visual styles or elements, and instead data, we will store the mediaQueries.jsx file in our `particles'directory.
Our mediaQueries.jsx file will contain the following -
What we're doing in this file is defining our breakpoints (xs - xxl) which are heavily inspired by Bootstrap 4's breakpoints.
Next, we set up a new object for
px'valued string.Finally, we create the device object which interpolates the
device'from `mediaQueries.jsx'in our Styled-Component and reference the object values to create a media query.Using our Media Queries
#Revisiting our Header component, we can now import the `device'value and use it in our styles to define media queried styles.
At the top of the
mediaQueries.jsx`Next, we can replace the previous references to our hardcoded media queries -
Now we have a flexible and consistent set of breakpoint values that we can use to create responsive Styled-Components in our pattern library.
If you have any components which aren't mobile-friendly and optimised across devices, please do feel free to implement the new media query logic and improve your CSS for the existing React components in your system.