npm run test
npm run build
To run a live-reload Storybook server on your local machine:
npm run storybook
To export your Storybook as static files:
npm run storybook:export
Instead of copy pasting components to create a new component, you can instead run this command to generate all the files you need to start building out a new component. To use it:
npm run generate YourComponentName
This will generate:
/src
/YourComponentName
YourComponentName.tsx
YourComponentName.stories.tsx
YourComponentName.test.tsx
YourComponentName.types.ts
YourComponentName.css
The default templates for each file can be modified under util/templates
.
Don't forget to add the component to your index.ts
exports if you want the library to export the component!
First, make sure you have an NPM account and are logged into NPM using the npm login
command.
Then update the name
field in package.json
to reflect your NPM package name in your private or public NPM registry. Then run:
npm publish
The "prepublishOnly": "npm run build"
script in package.json
will execute before publish occurs, ensuring the build/
directory and the compiled component library exist.
I recommend you host the component library using NPM. However, if you don't want to use NPM, you can use GitHub to host it instead.
You'll need to remove build/
from .gitignore
, build the component library (npm run build
), add, commit and push the contents of build
.
You can then install your library into other projects by running:
npm i --save git+https://github.com/taraldefi/ui-library.git#branch-name
OR
npm i --save github:taraldefi/ui-library#branch-name
Let's say you created a public NPM package called tariala-ui-library
with the TestComponent
component created in this repository.
First, you'll need to import the index.css
CSS file distributed by the package. This should be done at the root of your project (in index.js
or App.tsx
of your React app) and will look like:
import 'tariala-ui-library/build/index.css';
...
Usage of components (after the library installed as a dependency into another project) will look like:
import React from "react";
import { TestComponent } from "tariala-ui-library";
const App = () => (
<div className="app-container">
<h1>Hello I'm consuming the component library</h1>
<TestComponent heading={'Some heading'} content={<div>Some content</div>} />
</div>
);
export default App;
The example component TestComponent
respects the user's dark mode operating system preferences and renders the component in the appropriate theme.
This is achieved by using the media query: @media (prefers-color-scheme: dark)
in combination with CSS variables. The colours that change depending on dark mode preference can be found in src/index.css
. Example usage of these variables can be found within src/TestComponent/TestComponent.css
.
Read https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme for more details.
The Rollup plugin rollup-plugin-postcss
supports Sass, Less and Stylus:
- For Sass, install less:
yarn add node-sass --dev
- For Stylus, install stylus:
yarn add stylus --dev
- For Less, install less:
yarn add less --dev
If you want to use CSS Modules, update postcss
in rollup-config.js
to:
postcss({
modules: true
})
Add the following library to your component library @rollup/plugin-image:
npm i -D @rollup/plugin-image
Then add it to rollup-config.js
:
...
plugins:[
...,
image(),
...
]
...
You can then import and render images in your components like:
import logo from "./rollup.png";
export const ImageComponent = () => (
<div>
<img src={logo} />
</div>
);
Add the following library to your component library @rollup/plugin-json:
npm i -D @rollup/plugin-json
Then add it to rollup-config.js
:
...
plugins:[
...,
json(),
...
]
...
You can then import and use JSON as ES6 Modules:
import data from "./some-data.json";
export const JsonDataComponent = () => <div>{data.description}</div>;
Checkout the official Rollup plugin list for additional helpful plugins.