Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs][pigment] Add example and guide section #41249

Merged
merged 10 commits into from Mar 8, 2024
162 changes: 159 additions & 3 deletions packages/zero-runtime/README.md
Expand Up @@ -2,6 +2,7 @@

A zero-runtime CSS-in-JS library that extracts the colocated styles to their own CSS files at build-time.

- [Starter template](#starter-template)
- [Getting started](#getting-started)
- [Next.js](#nextjs)
- [Vite](#vite)
Expand All @@ -18,6 +19,26 @@ A zero-runtime CSS-in-JS library that extracts the colocated styles to their own
- [Color schemes](#color-schemes)
- [Switching color schemes](#switching-color-schemes)
- [TypeScript](#typescript)
- [How-to guides](#how-to-guides)
- [Coming from Emotion or styled-components](#coming-from-emotion-or-styled-components)
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved

## Starter template
danilo-leal marked this conversation as resolved.
Show resolved Hide resolved

Use the following commands to create a new project with the latest version of the framework and zero-runtime (for existing projects, see [Getting started](#getting-started)):
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

**Next.js template**:

```bash
curl https://codeload.github.com/mui/material-ui/tar.gz/master | tar -xz --strip=2 material-ui-master/examples/zero-nextjs
cd zero-nextjs
```

**Vite template**:

```bash
curl https://codeload.github.com/mui/material-ui/tar.gz/master | tar -xz --strip=2 material-ui-master/examples/zero-vite
cd zero-vite
```

## Getting started

Expand Down Expand Up @@ -132,7 +153,7 @@ Use the `variants` key to define styles for a combination of the component's pro

Each variant is an object with `props` and `style` keys. The styles are applied when the component's props match the `props` object.

**Example 1**: A button component with `small` and `large` sizes:
**Example 1** A button component with `small` and `large` sizes:

```jsx
const Button = styled('button')({
Expand All @@ -156,7 +177,7 @@ const Button = styled('button')({
<Button size="small">Small button</Button>; // padding: 0.5rem
```

**Example 2**: A button component with variants and colors:
**Example 2** A button component with variants and colors:

```jsx
const Button = styled('button')({
Expand All @@ -177,7 +198,7 @@ const Button = styled('button')({
</Button>;
```

**Example 3**: Apply styles based on a condition:
**Example 3** Apply styles based on a condition:

The value of the `props` can be a function that returns a boolean. If the function returns `true`, the styles are applied.

Expand All @@ -195,6 +216,37 @@ const Button = styled('button')({
});
```

Note that the `props` function will not work if it is inside another closure, for example, inside an array.map:
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

```jsx
const Button = styled('button')({
border: 'none',
padding: '0.75rem',
// ...other base styles
variants: ['red', 'blue', 'green'].map((item) => ({
props: (props) => {
// ❌ Cannot access `item` in this closure
return props.color === item && !props.disabled;
},
style: { backgroundColor: 'tomato' },
})),
});
```

Instead, use plain objects to define the variants:

```jsx
const Button = styled('button')({
border: 'none',
padding: '0.75rem',
// ...other base styles
variants: ['red', 'blue', 'green'].map((item) => ({
props: { color: item, disabled: false },
style: { backgroundColor: 'tomato' },
})),
});
```

#### Styling based on runtime values

> 💡 This approach is recommended when the value of a prop is **unknown** ahead of time or possibly unlimited values, e.g. styling based on the user's input.
Expand Down Expand Up @@ -451,3 +503,107 @@ declare module '@mui/zero-runtime/theme' {
}
}
```

## How-to guides

### Coming from Emotion or styled-components

Emotion and styled-components are runtime CSS-in-JS libraries. What you write in your styles is what you get in the final bundle which means that the styles can be as dynamic as you want with the trade-offs of bundle size and performance overhead.

Zero-runtime, on the other hand, extracts CSS at build time and replaces the JS code with hashed classnames and some CSS variables. This means that it has to know all of the styles to be extracted ahead of time, so there are rules and limitations that you need to be aware of when using javascript callbacks or variables in zero-runtime APIs.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

Here are some common patterns and how to achieve them with zero-runtime:

1. A fixed set of styles
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

In Emotion or styled-components, you can use props to create styles conditionally:

```js
const Flex = styled('div')((props) => ({
display: 'flex',
...(props.vertical // ❌ Zero Runtime will throw an error
? {
flexDirection: 'column',
paddingBlock: '1rem',
}
: {
paddingInline: '1rem',
}),
}));
```

But in zero-runtime, you need to define all of the styles ahead of time using the `variants` key:

```js
const Flex = styled('div')((props) => ({
display: 'flex',
variants: [
{
props: { vertical: true },
style: {
flexDirection: 'column',
paddingBlock: '1rem',
},
},
{
props: { vertical: false },
style: {
paddingInline: '1rem',
},
},
],
}));
```

> 💡 Keep in mind that `variants` is for fixed values of props, for example component's colors, sizes, states.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

2. Programatically generated styles
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

For emotion and styled-components, the styles will be different on each render and instances because the styles are generated at runtime:
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

```js
function randomBetween(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function generateBubbleVars() {
return `
--x: ${randomBetween(10, 90)}%;
--y: ${randomBetween(15, 85)}%;
`;
}

function App() {
return (
<div>
{[...Array(10)].map((_, index) => (
<div key={index} className={css`${generateBubbleVars()}`} />
))}
</div>
)
}
```

However, in zero-runtime, all of the instances will have the same styles and won't change between renders because the styles are extracted at build time.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

To achieve the same result, you need to move the dynamic logic to props and pass the value to CSS variables instead:

```js
function randomBetween(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

const Bubble = styled('div')({
'--x': props => props.x,
'--y': props => props.y,
});

function App() {
return (
<div>
{[...Array(10)].map((_, index) => (
<Bubble key={index} x={`${randomBetween(10, 90)}%`} y={`${randomBetween(15, 85)}%`} />
))}
</div>
)
}
```