-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5bf3be5
add more content
siriwatknp 21d0d5f
Apply suggestions from code review
siriwatknp 9049ea0
update readme
siriwatknp 240d82b
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp a3b9db2
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp 2ae791a
update zero to pigment
siriwatknp 0d5a9b5
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp dca9a14
update readme
siriwatknp 38d5343
Merge branch 'master' of https://github.com/mui/material-ui into zero…
siriwatknp ff6f852
udpate readme
siriwatknp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
# Pigment CSS | ||||||
|
||||||
A zero-runtime CSS-in-JS library that extracts the colocated styles to their own CSS files at build-time. | ||||||
A Pigment CSS CSS-in-JS library that extracts the colocated styles to their own CSS files at build-time. | ||||||
|
||||||
- [Getting started](#getting-started) | ||||||
- [Next.js](#nextjs) | ||||||
|
@@ -18,13 +18,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) | ||||||
|
||||||
## Getting started | ||||||
|
||||||
Pigment CSS supports Next.js and Vite with support for more bundlers in future. You must install the corresponding plugin, as shown below. | ||||||
|
||||||
### Next.js | ||||||
|
||||||
#### Starter template | ||||||
|
||||||
Use the following commands to create a new Next.js project with Zero Runtime setup: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```bash | ||||||
curl https://codeload.github.com/mui/material-ui/tar.gz/master | tar -xz --strip=2 material-ui-master/examples/pigmentcss-nextjs | ||||||
cd pigmentcss-nextjs | ||||||
``` | ||||||
|
||||||
#### Manual installation | ||||||
|
||||||
```bash | ||||||
npm install @pigmentcss/react | ||||||
npm install --save-dev @pigmentcss/nextjs-plugin | ||||||
|
@@ -42,6 +55,17 @@ module.exports = withPigment({ | |||||
|
||||||
### Vite | ||||||
|
||||||
#### Starter template | ||||||
|
||||||
Use the following commands to create a new Vite project with Zero Runtime setup: | ||||||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```bash | ||||||
curl https://codeload.github.com/mui/material-ui/tar.gz/master | tar -xz --strip=2 material-ui-master/examples/pigmentcss-vite | ||||||
cd pigmentcss-vite | ||||||
``` | ||||||
|
||||||
#### Manual installation | ||||||
|
||||||
```bash | ||||||
npm install @pigmentcss/react | ||||||
npm install --save-dev @pigmentcss/vite-plugin | ||||||
|
@@ -132,7 +156,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')({ | ||||||
|
@@ -156,7 +180,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')({ | ||||||
|
@@ -177,7 +201,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. | ||||||
|
||||||
|
@@ -195,6 +219,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`: | ||||||
|
||||||
```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. | ||||||
|
@@ -451,3 +506,107 @@ declare module '@pigmentcss/react/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. | ||||||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
On the other hand, Pigment CSS extracts CSS at build time and replaces the JS code with hashed class names 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 Pigment CSS APIs. | ||||||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Here are some common patterns and how to achieve them with Pigment CSS: | ||||||
|
||||||
1. **Fixed set of styles** | ||||||
|
||||||
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 Pigment CSS, 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 the `variants` key is for fixed values of props, for example, a component's colors, sizes, and states. | ||||||
|
||||||
2. **Programatically generated styles** | ||||||
|
||||||
For Emotion and styled-components, the styles will be different on each render and instance because the styles are generated at runtime: | ||||||
|
||||||
```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 Pigment CSS, all instances will have the same styles and won't change between renders because the styles are extracted at build time. | ||||||
|
||||||
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> | ||||||
) | ||||||
} | ||||||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.