This article is outdated. Learn how to build a modular React component library here.
In this tutorial, I'll demonstrate how to publish React components from any application to Bit's registry. Components will be published as independent packages that later could be installed using NPM, Yarn or Bit.
Bit is a tool, platform, and registry dedicated to publishing independent JS components.
Bit also enables cross-repo collaboration on independent components - but that's for another post.
package.json
file. Instead, Bit will analyze each component's dependencies and configure each package.json
, accordingly.a. Clone and install this React to-do app ( Source | Bit | The app )
$ git clone https://github.com/giteden/todo-react-ts.git
$ cd todo-react-ts
$ npm install
b. Install Bit globally
$ npm install bit-bin --global
c. Initialize a new Bit workspace (in the project's root directory)
$ bit add src/components/*
This is where Bit goes through the unintelligible code, analyzes it, and manages it as independent components. Read more about it here.
$ bit import bit.envs/compilers/react-typescript --compiler
As mentioned at the beginning of this tutorial, we can import Bit's React with TypeScript compiler so that we don't have to configure the build setup for each package, ourselves.
Check out Bit's compilers here.
$ bit tag --all 1.0.0
Here, Bit will build, test (if there were any test files), and version each component/package.
Bit tags each component with a semantic version number (Major.Minor.Patch). It automatically bumps the patch number on every new “export” (publish). It’s up to you to manually set a version number that describes changes more drastic than a patch. Read more about it here.
a. Create a new Bit account
b. Create a new component collection in Bit's component hub
c. Login to Bit in your terminal
d. Publish ("export") all tagged components
$ bit export <username>.<collection-name>
🎉 Great. You now have a shared component collection. Use it in future projects, and share it with your team. 🎉
Also, you can check out my collection (eden.todo-react) here.
Bit auto-generates documentation out of prop-types, JSDocs, and TypeScript. And, of course, you can always add an .md file to your component directory and manually write your docs.
So, for example -
This RemovableListItem component:
import React, {useState} from 'react';
import styles from './RemovableListItem.module.scss';
type RemovableListItemProps = {
/** The item's text */
text: string,
id: string,
/** A callback function for the "X" click event */
removeItem: (id: string) => void
}
const RemovableListItem = ({text, id, removeItem} : RemovableListItemProps) => {
const [isCompleted, setIsCompleted] = useState(false);
return(
<li className={styles.listItem}>
<span data-iscompleted={isCompleted} className={styles.text} onClick={() => setIsCompleted(!isCompleted)}>{text}</span>
<button className={styles.delete} onClick={() => removeItem(id)}>X</button>
</li>
)
}
export default RemovableListItem;
Will produce this documentation (in the component's page on Bit's component hub):
Also, notice how I've added a code example in Bit's playground (in the component's page):
And, as mentioned earlier, we can take full control over the documentation with our own markdown files.
For example, the Button component has an .md file under its component directory. Bit tracked this file as part of the Button component, and rendered it in the component's page:
|-- components
|-- Button
|-- Button.jsx
|-- Button.module.scss
|-- Button.md
|-- index.ts
### The Button Component
The Button components recieves all attributes of an HTML button component.
This is now displayed under the component playground: