Restyle leverages React's new ability to hoist style elements by generating atomic CSS on-demand to provide a flexible and efficient styling solution for React components.
Read more
Here's a high-level overview of how it works:
Styles Parsing: Restyle takes a styles object of CSS and parses it, generating atomic class names for each unique style property and value pair:
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:
import { css } from 'restyle'const [classNames] = css({ padding: '1rem', backgroundColor: 'tomato',})// Example output: 'x1y2 xfg3'
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:
The styled utility is a higher-order function that takes an HTML element tag name or a component that accepts a className prop and a initial styles object that returns a styled component that can accept a css prop:
import Link from 'next/link'import { styled } from 'restyle'const StyledLink = styled(Link, { color: 'rebeccapurple', textDecoration: 'none',})
Style Props
The second argument to the styled utility also accepts a style resolver function that returns an object of styles based on the props passed to the component, referred to as style props. This is useful for creating components that accept dynamic styles based on props.
For example, you can create a Grid component that accepts gridTemplateColumns and gridTemplateRows props:
[!IMPORTANT]
A proxy is used to differentiate between style props and those passed directly to the component. Only style props should be accessed from the first parameter of the style resolver function to ensure proper filtering.
To access the props passed to the component, you can use the second parameter of the style resolver function. This is useful for applying styles based on props that are not style props:
The css function returns a tuple of class names and the style tags to render. You can use the class names to apply styles to an element and the style tag to inject the styles into the head of the document:
The css function is most useful for components. However, you can use the css prop to style elements directly. The pragma will take care of applying the class names and injecting the style tag.
First, configure the pragma in your tsconfig.json file:
Use the GlobalStyles component to inject global styles into the document. This is useful for setting default styles for the body, headings, etc. This component accepts an object of styles and injects them into the head of the document based on their order in the object as well as when they are rendered in the react tree. Note, styles will not be removed when the component is unmounted. React makes no guarantees about when styles are removed from the document.
Variants can be achieved by using the style props pattern. For example, you can create an Alert component that accepts a variant prop that applies different styles based on the variant: