9 min read — Published 6 months ago

How To Create React Component Library With Typescript & Publish It

Learn how to create React component library using TypeScript with this guide. Plus, discover the process of publishing it for others to use!

UI Frameworks
Featured Image

Are you often overwhelmed by the process of creating consistent and tailored UI components in your React projects? The world of UI Frameworks can be daunting, especially when trying to maintain a cohesive look and feel for your web applications. What if there was a way to simplify this process and build your own React component library to ensure a consistent design system across all your projects? This blog will provide valuable insights on achieving just that, offering a step-by-step guide to creating your React component library.

Introducing Magic UI's simple yet powerful solution—a React component library designed to help you streamline the UI development process and create reusable components easily. With this tool, you can effortlessly construct your own React component library, ensuring continuity and efficiency in your projects.

What Is A Component Library?

Create React Component LibraryCreate React Component Library

A component library is a collection of pre-designed and pre-built user interface elements used to create user interfaces for digital products, such as websites and applications that have a unified look.

These libraries include a range of pre-made UI elements, such as buttons, forms, navigation menus, icons, and more, each designed with a consistent look and feel. UI component libraries are particularly useful in collaborative design and development environments, as they help ensure that all team members are using the same source of truth and that the end product maintains a professional and polished appearance. 

6 Reasons Why You Should Build A Component Library

Create React Component LibraryCreate React Component Library

Building a component library offers many benefits that make the design process more efficient and quality-driven. These benefits include the following:

1. Consistency Across Projects

When you have a component library, you ensure a uniform look and feel across all your projects. This eliminates the need to recreate buttons, forms, and other elements when starting a new project. With pre-designed and pre-tested components at your disposal, you maintain consistent and professional designs.

2. Efficiency and Speed

By utilizing a library of components, you save significant time. Instead of starting from scratch whenever you design a new interface, you can quickly grab pre-made components, make any necessary tweaks, and kickstart your project. This speeds up the development process substantially.

3. Quality Control

When you build a component once and thoroughly test it, you ensure it functions perfectly. This results in fewer bugs and a more reliable user experience. By reusing vetted components, you enhance the overall quality of your projects.

4. Scalability

Your component library can grow as your projects do. Adding new components to your library as needed allows you to meet the evolving needs of your expanding projects without starting from square one.

5. Collaboration

A component library becomes a shared resource if you work in a team. This shared resource ensures that all team members use the same components, simplifying collaboration and reducing inconsistencies in project development.

6. Focus on Creativity

With your design basics covered by a component library, you can dedicate more time to the creative aspects of your design work. This enables you to focus on user experience, innovative features, and the overall aesthetics of your projects.

In essence, a component library serves as a designer's toolkit, containing ready-to-use, reliable, and beautifully crafted components that smoothen, accelerate, and standardize the design process. An investment in efficiency and quality, a component library pays dividends and enhances the outcome of every project.

Design-Focused UI Library for Streamlined Development

MagicUI is a free and open-source UI library that we designed specifically for design engineers. It offers a collection of over 20 animated components built with React, TypeScript, Tailwind CSS, and Framer Motion. We provide a range of visually appealing and interactive elements that can be easily integrated into web applications, allowing us to create stunning user interfaces with minimal effort. 

MagicUI components are highly customizable, enabling seamless adaptation to match our desired branding and design requirements. With our focus on animation and a design-centric approach, MagicUI aims to bridge the gap between design and development, empowering us to craft captivating digital experiences. Along with our free component library, with MagicUI Pro, you can save thousands of hours and create a beautiful landing page, and convert your visitors into customers with our website templates. 

Use our React component library for free today at https://magicui.design/docs.

Tools Required To Create React Component Library

Create React Component LibraryCreate React Component Library

React

React is a JavaScript library for building user interfaces. It allows developers to create reusable UI components that can be used to build interactive user interfaces. React is the foundation of the component library we will build, so you should have experience working with it.

Typescript

Typescript is a superset of JavaScript that adds static type definitions to the language. It is useful for adding type safety to your codebase, preventing bugs and making your code easier to maintain. We will set up Typescript in our project to add type safety to our React components.

Storybook

Storybook is a tool for developing UI components in isolation. It allows you to build components outside of your application, making the development process faster and more efficient. Storybook is useful for testing components and documenting how they should be used.

How To Create React Component Library

Create React Component LibraryCreate React Component Library

Setting Up the Project

We must create a package.json file using the command npm init. Now, let’s proceed with installing the necessary dependencies. Unlike a regular project, here we will differentiate between regular dependencies and dev dependencies and peer dependencies. Regular dependencies are the ones packaged in the bundle that contribute to the size of our library. On the other hand, dev dependencies are only necessary during development. On the other hand, peer dependencies indicate that a certain package needs to be pre-installed to use the library. 

For instance, if a library called “avi-lib” has peer dependencies such as react and react-dom, your project must have react and react-dom pre-installed. Now, let’s install react, react-dom, typescript, @types/react, and @types/react-dom as dev dependencies using the command yarn i -D react react-dom typescript @types/react @types/react-dom. Next, we shall add react and react-dom as peer dependencies with appropriate versions:

```json peerDependencies: { "react": ">=16.0.0", "react-dom": ">=16.0.0" } ```

After this, it’s time to initialize a tsconfig file using the command npx tsc --init. Within the tsconfig.json file, enable jsx using "jsx": "react".

Creating the Component

Once the setup is complete, create a directory named src to hold all the code. Inside the src directory, create another directory named components. Inside this directory, create a file named Button.tsx. Open the file and add a simple button component:

```typescript import React from 'react'; export interface ButtonProps { children: React.ReactNode; onClick: () => void; } export const Button = ({ children, onClick }: ButtonProps) => { return <button onClick={onClick}>{children}</button>; }; </code></pre><p>Within the src directory, create an index.ts file and add the following:</p><pre><code>typescript export { Button, ButtonProps } from "./components/Button" </code></pre><h2><strong>Setting Up Storybook</strong></h2><p>To visualize how our components look in isolation, we’ll <a target="_blank" rel="noopener noreferrer nofollow" href="https://storybook.js.org/"><u>install Storybook</u></a>. It’s a tool that’s almost indispensable for component libraries. Storybook lets you preview your components, add documentation, and even host the components for others to test. Use <code>npx storybook init</code> to set up Storybook. By default, Storybook creates sample stories at /src/stories, but you can remove them as they’re just examples of what you can do with Storybook. Let’s create a simple story for our Button:</p><pre><code>typescript // ./src/components/Button/button.stories.tsx import { Button } from "./button"; export default { title: "Button", component: Button, }; export const Default = () => <Button>Click me!</Button>; </code></pre><p>To preview the components, run <code>npm run storybook</code>.</p><h2><strong>Internal Documentation</strong></h2><p>Documenting your components is a good practice for the users of your library. While it’s not covered in this tutorial, you can include documentation in a README file or on a docs website.</p><h2><strong>Bundling the Library</strong></h2><p>Once the components are ready, it’s time to bundle the library. This allows you to pack everything in a single file. Bundling also enables users to drag and drop a single file into their projects or use it on CDNs. To bundle the component, a bundler is required. In this case, we’ll use tsup as it doesn’t need a configuration for this use case. Install tsup using <code>yarn add -D tsup</code>. Add the following script to your package.json file:</p><pre><code>json scripts: { "build": "tsup src/index.ts --dts" }

Dillion Verma


Share this post


© 2024 Magic UI

Made with Engyne