Components are the building blocks of your website. They should be efficiently reusable to improve development speed and consistency.
To streamline your web design process and create visually appealing user interfaces, it’s essential to have a robust component library in place.
In this blog post, we’ll walk you through the process of building a component library with Anima and Storybook. In just a few steps, without code, you will have a ready-to-use component library for your website.
We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!
Before diving into the details, let’s briefly introduce the two core tools we’ll be using in this tutorial:
Navigate to the root directory of your react app and initialize the storybook using the below command,
npx sb init
The above command will do the following,
stories
directory in the src
folder with a basic Button componentNote — You can change the location of
stories
and also rename it. Make sure you are updating the path inside.storybook/main.ts
file’s stories field.
Prerequisites — Logged in on Anima
For creating a Figma design to React code, we will use Anima with the React plugin in Figma.
Steps to set anima with react in Figma
download selection
to download the codeNow it is time to create reusable components with storybooks, which you can use in your website or web application.
Steps to include anima component in storybook
stories
(created at step 1) of your project<anima-code-directory>/src/components/<component-name — i.e. Button>
to the stories.Note — If you are using typescript, covert files to typescript.
/*
We're constantly improving the code you see.
Please share your feedback here: https://form.asana.com/?k=uvp-HPgd3_hyoXRBw1IcNg&d=1152665201300829
*/
import PropTypes from "prop-types";
import React from "react";
import "./style.css";
export const Btn = ({ className, divClassName, text = "Content" }) => {
return (
<button className={`btn ${className}`}>
<div className={`content ${divClassName}`}>{text}</div>
</button>
);
};
Btn.propTypes = {
text: PropTypes.string,
};
You can customize components like adding click events or custom classes like below in the typescript,
import PropTypes from 'prop-types';
import { useEffect, useReducer } from 'react';
import './style.css';
interface Props {
text?: string;
state?: string; //'default' | 'hovered' | 'disabled'
classes?: string;
onClick?: any;
}
export const Button = ({
text = 'Content',
state = 'default',
classes = '',
onClick,
}: Props): JSX.Element => {
const [stateProp, dispatch] = useReducer(reducer, {
prevState: state,
state,
});
useEffect(() => {
dispatch({ type: 'default', newState: state });
}, [state]);
const handleClick = (e: any) => {
if (state === 'disabled') return;
onClick ? onClick(e) : '';
};
return (
<button
type="button"
className={`btn content ${stateProp.state} ${classes}`}
onMouseLeave={() => {
dispatch({ type: 'mouse_leave', newState: stateProp.prevState });
}}
onMouseEnter={() => {
dispatch({
type: 'mouse_enter',
newState: stateProp.state === 'disabled' ? 'disabled' : 'hovered',
});
}}
onClick={handleClick}
>
{text}
</button>
);
};
function reducer(state: any, action: any) {
switch (action.type) {
case 'mouse_enter':
return {
prevState: state.state,
state: action.newState,
};
case 'default':
return {
prevState: action.newState,
state: action.newState,
};
case 'mouse_leave':
return {
state: state.prevState,
};
}
return state;
}
Button.propTypes = {
text: PropTypes.string,
classes: PropTypes.string,
state: PropTypes.oneOf(['default', 'hovered', 'disabled']),
priority: PropTypes.oneOf(['primary', 'secondary']),
};
Run below command,
npm run storybook
You will be redirected to http://localhost:6006/ and You will see the components you created.
Create as many components as you need and you will have a library of reusable components.
Here is an example of a Button element we created,
You can use this component anywhere in the code as below,
import { Button } from "../stories/Button";
// define required properties
<Button state="hovered" text="Hello button"/>
By leveraging the power of design-to-code conversion with Anima and the interactive component development environment provided by Storybook, you can streamline your web development workflow and create beautiful, consistent components. As your library grows, continue to add new components and stories to keep it up-to-date and useful for your team.
We’re Grateful to have you with us on this journey!
Suggestions and feedback are more than welcome!
Please reach us at Canopas Twitter handle @canopas_eng with your content or feedback. Your input enriches our content and fuels our motivation to create more valuable and informative articles for you.
Keep exploring and growing!!