Styling components with CSS-in-JS libraries in Javascript Storybook

CSS-in-JS libraries have become very popular for styling components in modern JavaScript applications. They allow developers to write CSS code directly in JavaScript files, offering more flexibility and encapsulation. In this blog post, we will explore how to use CSS-in-JS libraries in JavaScript Storybook, a tool for building UI components in isolation.

What is CSS-in-JS?

CSS-in-JS is a technique where CSS styles are written and applied directly within JavaScript files. This allows developers to define styles in a more component-oriented way, ensuring that styles are encapsulated and won’t conflict with other components.

There are several popular CSS-in-JS libraries available, including styled-components, emotion, and @material-ui/styles. These libraries provide a range of features and syntax options, but the core idea remains the same - writing CSS in JavaScript.

Using CSS-in-JS libraries in Storybook

Storybook is a tool for building UI components in isolation. It helps developers develop, test, and showcase components independently of the application context. Adding CSS-in-JS styles to Storybook can be done in a few simple steps.

  1. Install the CSS-in-JS library of your choice:
// For styled-components
npm install styled-components

// For emotion
npm install @emotion/react @emotion/styled

// For @material-ui/styles
npm install @material-ui/styles
  1. Import the library into your Storybook stories:
// For styled-components
import styled from 'styled-components';

// For emotion
import { css } from '@emotion/react';

// For @material-ui/styles
import { makeStyles } from '@material-ui/styles';
  1. Use the CSS-in-JS syntax to apply styles within your components:
// For styled-components
const Button = styled.button`
  background-color: #ff0000;
  color: #ffffff;
  padding: 1rem;
`;

// For emotion
const Button = styled.button`
  ${({ primary }) => css`
    background-color: ${primary ? '#ff0000' : '#000000'};
    color: #ffffff;
    padding: 1rem;
  `}
`;

// For @material-ui/styles
const useStyles = makeStyles({
  button: {
    backgroundColor: '#ff0000',
    color: '#ffffff',
    padding: '1rem',
  },
});

const Button = () => {
  const classes = useStyles();
  return <button className={classes.button}>Click me</button>;
};
  1. Use the styled components or styles within your Storybook stories:
// For styled-components
export const PrimaryButton = () => <Button>Click me</Button>;

// For emotion
export const PrimaryButton = () => <Button primary>Click me</Button>;

// For @material-ui/styles
export const PrimaryButton = () => <Button />;
PrimaryButton.parameters = {
  css: `
    .button {
      background-color: #ff0000 !important;
    }
  `,
};

By using CSS-in-JS libraries in Storybook, you can easily create and showcase styled components in isolation. This helps you develop and test UI components independently while ensuring styles are encapsulated and consistent.

#javascript #cssinjs