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


  1. ```bash
  2. npm install restyle
  3. ```

[!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:

  1. ```ts
  2. import { css } from 'restyle'

  3. const [classNames, styleElement] = css({
  4.   padding: '1rem',
  5.   backgroundColor: 'peachpuff',
  6. })

  7. // classNames: 'x1y2 x3z4'
  8. // styleElement:
  9. ```

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:

  1. ```ts
  2. import { css } from 'restyle'

  3. const [classNames] = css({
  4.   padding: '1rem',
  5.   backgroundColor: 'tomato',
  6. })

  7. // Example output: 'x1y2 xfg3'
  8. ```

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:

  1. ```ts
  2. import { css } from 'restyle'

  3. const styles = {
  4.   padding: '1rem',
  5.   backgroundColor: 'rebeccapurple',
  6. }

  7. const [classNames, styleElement] = css(styles)

  8. // classNames: 'x1y2 x4z1'
  9. // Reuse class names for other elements
  10. const buttonStyles = {
  11.   ...styles,
  12.   border: '1px solid black',
  13. }

  14. const [buttonClassNames, buttonStyleElement] = css(buttonStyles)

  15. // buttonClassNames: 'x1y2 x4z1 x5a6'
  16. ```

4. On-Demand Style Injection: Styles are only added to the DOM when the component or element is rendered:

  1. ```tsx
  2. import { css } from 'restyle'

  3. export default function OnDemandStyles() {
  4.   const [classNames, styleElement] = css({
  5.     padding: '1rem',
  6.     backgroundColor: 'papayawhip',
  7.   })

  8.   return (
  9.     <>
  10.       <div className={classNames}>Hello World</div>
  11.       {styleElement}
  12.     </>
  13.   )
  14. }
  15. ```

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.

  1. ```tsx
  2. /** @jsxImportSource restyle */

  3. export default function MyComponent() {
  4.   return (
  5.     <div
  6.       css={{
  7.         padding: '1rem',
  8.         backgroundColor: 'peachpuff',
  9.       }}
  10.     >
  11.       Hello World
  12.     </div>
  13.   )
  14. }
  15. ```