Io ho un terribile problema. Sono parte di una famiglia storicamente e culturalmente di Sinistra. Per me, quindi, Vauro è un caro compagno di lotte passate. Anche se io, in quelle lotte passate, non…
One of the beauties of React is component reusability, which is a huge time saver. Most times, creating new components for a project may mean that you may have to do the same for every new project which will ultimately result in unavoidable code duplication.
Having a component library ensures that reusable components can be packaged and shared across multiple applications, meaning that you do not have to worry about code duplication, makes it faster to build new applications and helps speed up scalable application development. Components can easily be customised at the project level for each application they belong to. It also means that issues such as bug fixes and adding new features can be done at source and pulled through on every project that needs it.
In this post, I explain how to create a React component library using the following tools:-
Create a new React project with CRA using npx
for the latest version
The next step is to install dependencies required by our project:
After installing all dependencies, you should create a configuration file for babel babel.config.js
at the root of your project and add the following:
We need to add some documentation for our library. In this case, I create a docs
folder at the project root and create an introduction.md
file
And added some content about our library:
The next step is for us to create a theme for our project. You can create as many project specific themes as you want, especially if your have multiple projects that need to use different styles.
For this demo, I will be creating a theme for a project1
with specific and shared theme functions.
First we create our theme
folder, then create files for our theme functions and our ThemeProvider
high order component:
Modify the color.ts
file
Next, modify the fontSize.ts
file
Also, modify the shared fonts fontFamily.ts
file
And, add some custom breakpoints in breakpoint.ts
We need to export our project themes:
We can specify and customise which themes we want for each project.
Next, we export our ThemeProvider.ts
We also need to define a layout for our Styleguide. So, we create a ThemeWrapper
component to serve as a top level wrapper component and customises the layout.
And we enter the following:
The ThemeProvider
component accepts a theme
prop, which it passes it down to all child components.
Next we modify the script commands in our package.json
file
Running yarn run styleguide
will start the style guide dev server, while yarn run styleguide:build
builds a production-ready HTML version.
We modify Text.tsx
atomic component as follows:
Next, we modify RichText.tsx
atomic component
Next, we create our Section.tsx
molecule component. Molecules are a combination of two or more Atomic components
We can see our Introduction
but not our new components.
We need to create README files for our new components:
Then add a few examples such as the following to the Text.md
:
And alsoRichText.md
After a hot reload, Styleguidist will generate the components from the markup:
To add tests to our library, we need to install some dependencies:
Next, we need to add some tests for our components. First, we create a higher order shallow wrapper component:
Update the shallowWithTheme
component as follows:
Next, we create our test files
And we enter the following to theText.test.tsx
, RichText.test.tsx
and Section.test.tsx
files:
We need to update our package.json
with our test commands:
Running yarn run test
will run the full suite of tests; yarn run test:watch
watches test files for changes, yarn run test:update
updates our tests and yarn run test:no-warnings
runs tests in silent mode.
To add linting to our project, we need to install a few dependencies:
We then need to set up a configuration file for ESLint:
This generates a configuration file .eslintrc.js
. I have added some modifications with our plugins and rules:
Next, we need to create an .eslintignore
file which tells ESLint which directories or files to ignore and add the following:
Next, we need to also update our package.json
for by adding our lint command to the script field and adding new fields for husky
and lint-staged
:
We can run yarn run lint
to lint test our files. Also, every changed file to will also be lint tested before it is committed.
In order to use our library as npm module, we need to export our themes and components and compile our typescript files into plain Javascript before we can publish as a module and import into our projects.
First, we create our export file src/index.ts
and add the following:
Next, we need to install the following modules :
And update our script field inpackage.json
with our postinstall
and build
command.
This means that everytime we run yarn install
, the build
command will be triggered, creating a new build of our project. Next, we also need to modify our Typescript configuration file tsconfig.json
to specific our source foldersrc
and compiled destination dist
folder and also which directories or files to exclude from typechecks.
Next, we run yarn run build
to generate compiled javascript files in the destination dist
folder.
We also need to run yarn run styleguide:build
to generate a production-build of a HTML-static library in the styleguide
folder. Your project structure should now look like this:
Next we updatepackage.json
by adding new fields to point to the compiled source as below:
I have opted to manually add the configuration file .circleci/config.yml
to my project:
We also need to update the README.md
file as below:
To use our library in any project, we simply need to install by running yarn add rcl-demo
or npm install rcl-demo
.
Next we need to import the. ThemeProvider
and theme
as a high order component wrapper in our application:
And we can import any component and use anywhere in the project
Clicking on theDeploy site
button should trigger the deployment:
As soon as deployment is finished, we can preview our static library:
Having a component library is definitely the way to go if you want to have sharable and reusable components and prevent code duplication across different projects. It also means we can also fix bugs or add new features to our components with less effort.
Information and Communication Technology (ICT) atau yang dikenal dengan teknologi informasi dan komunikasi(TIK) merupakan sesuatu yang sangat penting dan menyatu dengan kehidupan masyarakat saat ini…