Create a Modular React Component Library
A guide to building a React component library that scales
Design systems/UI libraries used to be a luxury enjoyed exclusively by large enterprises, but in recent years, due to the advent of new technologies , we've seen that changed quite drastically. Design systems have now become a tool used by organizations of all scales, from single freelance developers and small dev teams, all the way to large and mature enterprises.
Why Build a UI Libray/Design System?
UI libraries affect both ends of the development process: the coding and the final product.
UI libraries speed up development
- UI libraries maximize code reuse and speed up development. They do so by making components available to all projects in and across repositories. They also make components easier to find and understand (as they’re documented and indexed in one central location) — after all, reusable code no one knows about isn’t that useful.
- UI libraries make codebases easier to scale and maintain. A DRY codebase means fewer lines and files to go through. Fewer duplications also mean better predictability to changes in code (it may be argued that sharing code across projects makes predicting outcomes more challenging, but as you’ll see in this post, it’s only a matter of using the right tools).
- Lastly, code that is written for reuse is better code. It’s documented, testable, and pure (return values for each argument remain consistent with zero changes to their environment). Better code is more maintainable code.
UI libraries help deliver a betteruser experience
- A limited set of UI components means your users spend less time learning your product and more time enjoying it.
- Consistency in style and functionality makes it much easier for users to predict the outcomes of actions performed on your app, minimizing the chance for confusion and the frustration that follows it.
What’s a Modular Library? Why Are They Important?
A standard library is a monolith. It’s built and maintained as a single object — a single repository, a single build setup, and, usually, a single package.
Components are organized by ownership or business functionality
A modular library, or a collection, is a scope that serves only as a way to organize components by ownership or business functionality (there are currently no other implementations of modular libraries that I’m aware of other than Bit.dev , but I’m sure there will be).
Each component is versioned and built as independent code
Components published to a collection are versioned and built independently, which means they can be harvested from any codebase/repository. That gives us the freedom to publish components whenever we like and from whatever project we choose. It doesn’t have to be a full-on component-library project.
No pesky meaningless updates
Consumers of shared components only have to deal with version updates that are relevant to what they actually use as they consume each component directly and not as part of a larger project (e.g., a library).
Building a UI Library With Bit and Bit.dev
What is Bit/Bit.dev?
Bit.dev is a component hub (a private registry and a documentation site). It’s where you host, document, and manage reusable components from your different projects. It works perfectly with Bit , an open-source tool that handles component isolation and publishing (when using Bit.dev , components are published to Bit’s registry).
As mentioned earlier, components can be published to a collection from any project at all. To demonstrate that, I’ll harvest and publish components from a demo React with TS app.
1. Create a component collection in Bit.dev .
2. Install Bit globally (npm/Yarn).
$ yarn global add bit-bin
3. Log into your account.
$ bit login
4. Initialize a workspace in the project’s directory.
// To follow along with my demo app, clone and install it$ git clone https://github.com/giteden/react-ts-demo-app.git
$ cd react-ts-demo-app
$ yarn
// initialize a workspace (in your project's directory)$ bit init --package-manager yarn
Note:Notice how a new .bitmap
file has been added to your directory, and a bit
section has been added to your package.json
.
5. Track all components. In my case, components are located in the components
directory (for example, src/components/button/index.js
):
$ bit add src/components/*
Note:Placing all files under the same directory is yet another way to make your code more comprehensible to other developers (maintainers and consumers of your reusable components). It’s much easier to understand how different files relate to each other when they’re grouped under the same directory. Moreover, it’s a way to make your reusable component more mobile and autonomous.
6. Configure a compiler to make the components usable in other environments with different setups:
$ bit import bit.envs/compilers/react-typescript --compiler
Note:For other compilers, check out Bit’s ENVs collection or learn how to build your own .
7. Tag the components to build them in isolation (and lock changes):
$ bit tag --all 1.0.0
Note:Bit tracks your component dependencies by itself and doesn’t require manual configuration. Having said that, it’s good practice to check for unresolved dependency issues using $ bit status
.
8. Export all tracked components (push them to the shared collection):
$ bit export <user-name>.<collection-name>
The components are now shared and publicly available :+1:
If you followed along, go to bit.dev/<user-name>/<collection-name>
to see them rendered in Bit’s playground. Otherwise, you can check out my collection here:
Documenting Components
The process of documentation starts at writing components in your local dev environment and ends at writing examples in the component page on Bit.dev.
When using React with TypeScript, most of the job is done for us. Bit extracts props and types, forms documentation, and displays it on the component page (on B it.dev ). As wonderful as it is, it’s always good to enrich your autogenerated documentation with JSDocs descriptions.
Note:When using react-docgen
/ Bit.dev
to autogenerate docs for your reusable components, the prop’s type/interface should be defined both as a generic of React.FC<T>
and (directly) as the function’s arguments type (otherwise, your props won’t appear in the generated docs).
Writing Examples in Bit’s Playground
After exporting components, it’s best to complete the documentation process by providing an example (on the component page).
Examples enable components to render in Bit’s playground (a component that doesn’t receive its required props won’t be able to render) and guide consumers of components on how/how best to use them.
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
重新定义团队:谷歌如何工作
拉兹洛·博克 / 宋伟 / 中信出版集团 / 2015-12-1 / CNY 56.00
谷歌首席人才官拉斯洛•博克权威力作,谷歌公开认可的谷歌高层作品,首度揭秘谷歌颠覆工业时代模式的人才和团队管理的核心法则,《纽约时报》畅销榜第一名,Business Insider 2015最佳商业书籍,谷歌的创造力就在于此! 编辑推荐! 1、 谷歌人才官首次公开谷歌人才和团队管理的核心秘籍 在谷歌执掌人事多年的拉斯洛•博克是人才和团队管理的顶级专家。他加入谷歌后,谷歌的员工数从六......一起来看看 《重新定义团队:谷歌如何工作》 这本书的介绍吧!