Restyle
The simplest way to add CSS styles to your React components.
README
restyle
The simplest way to add CSS styles to your React components.
Features
- Zero-runtime CSS in JS
- Generates atomic class names
- De-duplicates styles
- Works in Server and Client Components
- Encourages encapsulation
- Supports css prop with JSX pragma
- Loads styles on demand
- Allows shipping CSS in NPM packages
- Small core 1.38kb minified and gzipped
Installation
- ```bash
- npm install restyle
- ```
[!IMPORTANT]
This library requires a React Canary version since it utilizes the new style hoisting feature.
How it Works
Restyle leverages React's new ability to [hoist style elements](https://react.dev/reference/react-dom/components/style#rendering-an-inline-css-stylesheet) by generating atomic CSS on-demand to provide a flexible and efficient styling solution for React components. Here's a high-level overview of how it operates:
1. Styles Parsing: Restyle takes a styles object of CSS and parses it, generating atomic class names for each unique style property and value pair:
- ```ts
- import { css } from 'restyle'
- const [classNames, styleElement] = css({
- padding: '1rem',
- backgroundColor: 'peachpuff',
- })
- // classNames: 'x1y2 x3z4'
- // styleElement:
- ```
2. Class Names Generation and Deduplication: Atomic class names are generated using a hashing function to ensure uniqueness and prevent collisions. Class names are cached per request, optimizing performance and reducing the overall size of the generated CSS:
- ```ts
- import { css } from 'restyle'
- const [classNames] = css({
- padding: '1rem',
- backgroundColor: 'tomato',
- })
- // Example output: 'x1y2 xfg3'
- ```
3. Atomic CSS: By breaking down styles into atomic units, it allows for highly reusable class names, making it easy to manage and override styles while reducing the overall size of the CSS produced:
- ```ts
- import { css } from 'restyle'
- const styles = {
- padding: '1rem',
- backgroundColor: 'rebeccapurple',
- }
- const [classNames, styleElement] = css(styles)
- // classNames: 'x1y2 x4z1'
- // Reuse class names for other elements
- const buttonStyles = {
- ...styles,
- border: '1px solid black',
- }
- const [buttonClassNames, buttonStyleElement] = css(buttonStyles)
- // buttonClassNames: 'x1y2 x4z1 x5a6'
- ```
4. On-Demand Style Injection: Styles are only added to the DOM when the component or element is rendered:
- ```tsx
- import { css } from 'restyle'
- export default function OnDemandStyles() {
- const [classNames, styleElement] = css({
- padding: '1rem',
- backgroundColor: 'papayawhip',
- })
- return (
- <>
- <div className={classNames}>Hello World</div>
- {styleElement}
- </>
- )
- }
- ```
5. Integration with JSX Pragma: Easily add support for the css prop via the JSX pragma, allowing colocated inline CSS styles directly on JSX elements.
- ```tsx
- /** @jsxImportSource restyle */
- export default function MyComponent() {
- return (
- <div
- css={{
- padding: '1rem',
- backgroundColor: 'peachpuff',
- }}
- >
- Hello World
- </div>
- )
- }
- ```
探客时代
