Storybook & Atomic Design - 1.6. - Styling Our Button
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.
At the moment our Button component doesn't have any styles, other than that of browsers default applied to it.
In this lesson, we are going to cover how we can use the `styled-components'package to write isolated styles that apply to any instance of our Button component, with dynamic CSS properties generated based on the props that are provided to a component.
Style File
#To continue with our separation of component file focus, we will create a new styles file to live within our button directory next to our documentation and markup files.
I've used the name `button.styles.jsx'for our Button component styles.
Inside this file, we will write the following code and then go through what is happening -
So, first of all, we are importing the
styled-components'allowing us to tap into the CSS-in-JS library.We next create a constant value named StyledButton and assign it to be a styled component. We then specify the HTML element we want to return to our component with a
followed by the name of the HTML element, in our case, this will be a `button'HTML element.We then use template literal string (
) ``characters to open up the file for our CSS. Then we start writing our CSS for our component inside of these characters.In my CSS I've included a display property and a padding property to give the button some room to breathe.
I've also included some colour values, specifically a white text colour and a background colour that taps into the values we provided our `ThemeProvider'component at the start of this chapter when setting up our Storybook global wrapper component.
We can tap into the values as the CSS we are writing is in a template literal string, by using the `${JS HERE}'syntax we can break in and out of writing a string or using JavaScript functions and properties.
The StyledButton string value has access to props anytime we interpolate within the string and forward the value of props into what we want to return in our CSS.
For anyone unfamiliar with styled-components this can take some getting used to but it gives us the great ability in writing isolated CSS that doesn't break out into other components.
With our new Styled-Component, we now need to import it into our Button component to make use of the CSS rules we've created.
Accessing Component Props in our Styles
#In the example above, I've shown how we can tap into our global theme prop values and use them within our components CSS.
We can take it a step further and instead of accessing global props, we can access the props fed directly to the Button component itself.
The syntax for doing so is very similar to that of accessing the global theme object.
But before we do that, let's add a new prop value to our Button component called
.The `variant'prop will be used to switch between component styles.
We may have a single Button Component that shares different colour instances. For example a primary, secondary, and tertiary button.
The reason we use the prop name
theme'is that the ThemeProvider'component and so the `Button'component already has this prop name being used.We could try the prop name `type'but again this is already used by the HTML button element to determine if a button is a standard button, reset button or submit button.
Instead, we use the term `variant'as you have different variants of a button.
With our new prop available to our component, we will revisit our Button style file and extend it with a function which accepts the props available to our Button component and dynamically returns a background colour based on the values it finds.
Note: only the props we make available with `ThemeProvider'or by directly passing to our Styled-Component will be available in the props object. Which in this situation would only include onClick, theme and variant.
Now when we call on our Button Component, if we have specified the prop
secondaryThemeProvider'under the key 'secondary'.Dynamically Extending Our Styled Button to an Anchor Tag
#We're almost done creating our styled React Button component, but we've got one task left to complete.
At the moment, we are only returning a styled HTML `button'with our Styled-Components logic.
However, as mentioned in the previous video, we won't always be returning a
. When we provide a value to the a'HTML element to our application.Fortunately, we can extend the styled-component defined as 'StyledButton' with the following code to return a new styled-component defined as 'StyledLinkButton'.

styled-components: API Reference
API Reference of styled-components
We now have three exports from our `button.styles.jsx'file.
Two named exports of
StyledLinkButton`.With a default export of
.We can import these into our Button React component using -
By replacing our anchor element with a `StyledLinkButton'we can now return an anchor element that shares the same styles as our button element. Creating consistency between the two elements whilst still making benefit of the browser's support for each elements functionality.
We now have a styled React Button component which dynamically changes colour based on the props we pass in at build-time. We're also making use of our `ThemeProvider'to supply consistent colour values throughout our application to keep the maintenance of brand colours easier.
The component will swap between using either an anchor element or button element based on the href prop value being defined, keeping the application accessible.