Getting started with Storybook
Greetings to our tech-savvy TrickyKeys community! Our captivating UI didn't just materialize out of thin air. Let's embark on a journey to understand our alliance with Storybook, and how we can leverage an integration with Chromatic for visual regression testing and component previewing.
Why Storybook?
For those new to the scene, Storybook is a workshop for building UI components and pages in isolation that supercharges component-driven development.
At TrickyKeys, we're big believers in the principle of separation of concerns, especially when it comes to our components. By leveraging Storybook, we focus first on creating "dumb" components — those that are purely presentational, devoid of any embedded business logic. This allows our team to concentrate solely on the component's design, ensuring that its rendering to the DOM is pixel-perfect and consistent across various scenarios.
Once we've nailed the aesthetic and functional aspects of these components, we can confidently integrate them with "smart" components or pages. This strategy ensures a clear delineation between design and functionality, streamlining our development process and ensuring that both design fidelity and functional integrity are maintained.
Getting Storybook up and running
To install Storybook to your repository, first open a terminal and navigate to your project:
cd /path/to/your/repo
Alternatively, if you're creating a new project you can start a new React app using the following command (don't forget to navigate to the new directory you create):
npx create-react-app my-app
cd my-app
Next, install storybook into your repository:
npx sb init
Finally, launch Storybook:
npm run storybook
It's that simple! Now let's take a look at how we can add our components into our Storybook environment.
Adding new components
Create your component
First things first, build your component as you usually would in your preferred framework or library (React, Vue, Angular, etc.). Let's kick off with something simple, a basic button component in React:
import React from 'react';
// Styles
const buttonStyles = {
padding: '10px 20px',
borderRadius: '4px',
border: 'none',
color: 'white',
backgroundColor: '#007BFF',
cursor: 'pointer',
fontSize: '16px',
};
// Button Component
const Button = ({ onClick, children, ...props }) => {
return (
<button style={buttonStyles} onClick={onClick} {...props}>
{children}
</button>
);
};
export default Button;
Create story file
For each component you want to showcase, create a corresponding .stories.js (or .stories.tsx for TypeScript) file. This is where you'll define the various "stories" or states of your component:
import Button from './Button.jsx';
export default {
title: 'Button',
component: Button,
};
export const Primary = () => <Button>Click me</Button>;
Structure with folders
As your component library grows, it's useful to organize stories using folders. For instance, Button.stories.js might live under a components directory, resulting in a structure like:
View & iterate
Now you've successfully added a component into Storybook, you can view your the component in the Storybook environment. You should find it much simpler to quickly review your components in isolation, and iterate on it without the distraction of your other beautiful UI components!
Chromatic: The Game Changer
Chromatic builds upon Storybook, offering a cloud service for UI testing and review. It's a tool we can't rave enough about.
Integration simplicity
Chromatic seamlessly meshes with Storybook. After setting up Storybook, installing Chromatic is as easy as:
npm install --save-dev chromatic
Then, link your project:
Visual regression testing
With every PR, Chromatic automatically captures snapshots, making visual regressions easy to spot. Not only that, but Chromatic also offers a visual diff tool, so any changes made are explicitly highlighted. If a component changes visually, we get notified.
Component previews
Reviewing components just got 100x easier! Each Storybook build gets it's own unique link, enabling engineers and designers to preview components without diving into the codebase or waiting for local setups. Instant feedback is now a reality.
To make things even simpler, Chromatic even supplies a GitHub action to automatically deploy preview environment on each PR commit, meaning your latest Storybook build is always just one click away!
Our workflow enhanced
Using Storybook and Chromatic, we realized several advantages:
- Consistency: Every PR is automatically checked for visual anomalies, ensuring a consistent UI.
- Time-saving: Instead of manual UI checks, we rely on automated visual tests. This expedited our release cycles.
- Collaboration Boost: Designers can review actual components, not just mockups. This strengthened our designer-developer synergy.
In Conclusion
With Storybook as our design playground and Chromatic safeguarding our visuals, our UI/UX process became streamlined and efficient. Every component crafted and every design decision made, bore the hallmark of precision.
Want more insights? Stick around! The TrickyKeys development journey is filled with learnings and experiences we can’t wait to share.