-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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-css] Add guide for Pigment CSS quickstart #43395
Merged
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
b7157fd
[docs] Add guide for Pigment CSS quickstart
alelthomas 2733723
add snippet for global styles
alelthomas b091ee9
non-breaking spaces pigment-css.md
alelthomas d5b9169
fix code snippet typo
alelthomas 858ce82
apply copy edit changes
alelthomas c2337c8
change description
alelthomas b7de169
change description to be more generic
alelthomas cdc89b8
Merge branch 'mui:master' into pigment-css-quickstart
alelthomas 8e7ed78
fix indentation
alelthomas 24e4b21
add prerequisites
alelthomas 9cc0c9a
move to Experimental APIs
alelthomas 5330f6d
add more context to each section
alelthomas 30ebd1d
non-breaking space
alelthomas 8c3f0e2
sx prop definition
alelthomas 31feabe
Merge branch 'mui:master' into pigment-css-quickstart
alelthomas 35ce82d
copy edits
alelthomas 86817c8
add framework tabs
alelthomas 74260a2
remove next.js distinction
alelthomas 15ae51e
Merge branch 'mui:master' into pigment-css-quickstart
alelthomas 1084219
remove trailing space
alelthomas 0c3c3d7
final commit
alelthomas 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
334 changes: 334 additions & 0 deletions
334
docs/data/material/experimental-api/pigment-css/pigment-css.md
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,334 @@ | ||||||||||||||
# Getting started with Pigment CSS | ||||||||||||||
|
||||||||||||||
<p class="description">Learn how to get started customizing your components using Pigment CSS.</p> | ||||||||||||||
|
||||||||||||||
<iframe width="100%" height="400" src="https://www.youtube.com/embed/UVeDpUey5Es?si=w8OtdStXHtWWIODa" title="YouTube video player: Getting Started with Pigment CSS" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> | ||||||||||||||
|
||||||||||||||
[Pigment CSS](https://github.com/mui/pigment-css) is a zero-runtime CSS-in-JS library that pre-compiles at build time, making it compatible with React Server Components and providing you with significant performance improvements over other styling engines. | ||||||||||||||
|
||||||||||||||
Pigment CSS is compatible with any React component library and can be used with Vite and Next.js. | ||||||||||||||
|
||||||||||||||
Check out the [Migrating to Pigment CSS](/material-ui/migration/migrating-to-pigment-css/) guide if you have an existing Material UI project. | ||||||||||||||
|
||||||||||||||
## Prerequisites | ||||||||||||||
|
||||||||||||||
- Node.js | ||||||||||||||
- A package manager | ||||||||||||||
- A Next.js or Vite project | ||||||||||||||
|
||||||||||||||
If using Next.js, you can fast track your Pigment CSS installation and project creation with the following commands: | ||||||||||||||
|
||||||||||||||
```bash | ||||||||||||||
curl https://codeload.github.com/mui/pigment-css/tar.gz/master | tar -xz --strip=2 pigment-css-master/examples/pigment-css-nextjs-ts | ||||||||||||||
cd pigment-css-nextjs-ts | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
For Vite: | ||||||||||||||
|
||||||||||||||
```bash | ||||||||||||||
curl https://codeload.github.com/mui/pigment-css/tar.gz/master | tar -xz --strip=2 pigment-css/examples/pigment-css-vite-ts | ||||||||||||||
cd pigment-css-vite-ts | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
## Manual installation | ||||||||||||||
|
||||||||||||||
To get started on an existing project, install Pigment CSS with the following command: | ||||||||||||||
|
||||||||||||||
**Next.js** | ||||||||||||||
|
||||||||||||||
```bash | ||||||||||||||
npm install @pigment-css/react | ||||||||||||||
npm install --save-dev @pigment-css/nextjs-plugin | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
**Vite** | ||||||||||||||
|
||||||||||||||
```bash | ||||||||||||||
npm install @pigment-css/react@next | ||||||||||||||
npm install --save-dev @pigment-css/vite-plugin@next | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
Next, head over to your config file and import the `withPigment` plugin: | ||||||||||||||
|
||||||||||||||
**Next.js** | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
// next.config.js | ||||||||||||||
import { withPigment } from '@pigment-css/nextjs-plugin'; | ||||||||||||||
|
||||||||||||||
export default withPigment({ nextConfig }); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
**Vite** | ||||||||||||||
|
||||||||||||||
```ts | ||||||||||||||
// main.tsx | ||||||||||||||
import { pigment } from '@pigment-css/vite-plugin'; | ||||||||||||||
|
||||||||||||||
export default defineConfig({ | ||||||||||||||
plugins: [ | ||||||||||||||
pigment(), | ||||||||||||||
// ... Your other plugins. | ||||||||||||||
], | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
Finally, import the Pigment CSS stylesheet in your `layout.tsx` (Next.js) or `main.tsx` (Vite) file: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import '@pigment-css/react/styles.css'; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
## Usage | ||||||||||||||
|
||||||||||||||
Pigment CSS addresses the needs of modern React development: it's compatible with React Server Components and lets you reap the benefits of CSS-in-JS—all without runtime performance costs. | ||||||||||||||
|
||||||||||||||
With Pigment CSS, you can create locally scoped reusable styles, themes, CSS variables, and more. | ||||||||||||||
|
||||||||||||||
### Styling | ||||||||||||||
|
||||||||||||||
Pigment CSS simplifies the creation and definition of styles through various APIs: | ||||||||||||||
|
||||||||||||||
- `css`: for reusable styles | ||||||||||||||
- `globalCss`: for global styles | ||||||||||||||
- `keyframes`: for reusable animation keyframes | ||||||||||||||
- `styled`: for styled components | ||||||||||||||
|
||||||||||||||
#### Creating reusable styles | ||||||||||||||
|
||||||||||||||
Use the `css` API to create reusable styles: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import { css } from '@pigment-css/react'; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
You can do this either with the `template` or `object` syntax: | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
**Template syntax** | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
const bodyBackground = css` | ||||||||||||||
background-color: #000; | ||||||||||||||
color: #fff; | ||||||||||||||
`; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
**Object syntax** | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
const mainClass = css({ | ||||||||||||||
display: '#000', | ||||||||||||||
color: '#fff', | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
#### Creating global styles | ||||||||||||||
|
||||||||||||||
If you'd like some CSS to be loaded by every page, you can define global styles by using the `globalCSS` API. | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
These should be defined at the top level of your JavaScript file: | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import { globalCSS } from '@pigment-css/react'; | ||||||||||||||
|
||||||||||||||
globalCss` | ||||||||||||||
body { | ||||||||||||||
margin: 0; | ||||||||||||||
padding: 0; | ||||||||||||||
} | ||||||||||||||
`; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
#### Creating styled components | ||||||||||||||
|
||||||||||||||
Keeping your styles scoped to your components ensures that only the necessary CSS is loaded and stays modular, easily readable, and maintainable. Additionally, you can apply conditional styling to your components based on props or runtime values. | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
Use the `styled` API to create styled components: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import { styled } from '@pigment-css/react'; | ||||||||||||||
|
||||||||||||||
const Heading = styled('div')({ | ||||||||||||||
fontSize: '2rem', | ||||||||||||||
color: '#9FADBC', | ||||||||||||||
fontWeight: 'bold', | ||||||||||||||
margin: '1rem', | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
**Styling based on props** | ||||||||||||||
|
||||||||||||||
You can add different styling options based on props by using the `variants` key. This approach is recommended when the value of the prop is known at build time. | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
Each `variant` is an object with `props` and `style` keys: | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import { styled } from '@pigment-css/react'; | ||||||||||||||
|
||||||||||||||
const Heading = styled('div')({ | ||||||||||||||
fontSize: '2rem', | ||||||||||||||
color: '#9FADBC', | ||||||||||||||
fontWeight: 'bold', | ||||||||||||||
margin: '1rem', | ||||||||||||||
variants: [ | ||||||||||||||
{ | ||||||||||||||
props: { variant: 'success' }, | ||||||||||||||
style: { color: '#23AD79' }, | ||||||||||||||
}, | ||||||||||||||
{ | ||||||||||||||
props: { size: 'small' }, | ||||||||||||||
style: { fontSize: '1.5rem' }, | ||||||||||||||
}, | ||||||||||||||
], | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
**Styling based on runtime values** | ||||||||||||||
|
||||||||||||||
When the value of a prop is unknown ahead of time, you can style your components based on runtime values: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
const Heading = styled('h1')({ | ||||||||||||||
color: ({ isError }) => (isError ? 'red' : 'black'), | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
### Themes | ||||||||||||||
|
||||||||||||||
Pigment CSS supports theming to apply consistent styles and values across your application. | ||||||||||||||
You can create themes by defining them in your config file: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import { withPigment } from '@pigment-css/nextjs-plugin'; | ||||||||||||||
|
||||||||||||||
export default withPigment(nextConfig, { | ||||||||||||||
theme: { | ||||||||||||||
colors: { | ||||||||||||||
primary: 'tomato', | ||||||||||||||
secondary: 'cyan', | ||||||||||||||
}, | ||||||||||||||
spacing: { | ||||||||||||||
unit: 8, | ||||||||||||||
}, | ||||||||||||||
typography: { | ||||||||||||||
fontFamily: 'Inter, sans-serif', | ||||||||||||||
}, | ||||||||||||||
// ...more keys and values, it's free style! | ||||||||||||||
}, | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
To access your themes, use a callback with the `styled()` and `css()` APIs: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
const Heading = styled('h1')(({ theme }) => ({ | ||||||||||||||
color: theme.colors.primary, | ||||||||||||||
fontSize: theme.spacing.unit * 4, | ||||||||||||||
fontFamily: theme.typography.fontFamily, | ||||||||||||||
})); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
#### CSS variables support | ||||||||||||||
|
||||||||||||||
Pigment CSS generates CSS variables from the theme values when they're wrapped by the `extendTheme` utility, creating a `vars` object: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
import { withPigment, extendTheme } from '@pigment-css/nextjs-plugin'; | ||||||||||||||
|
||||||||||||||
export default withPigment(nextConfig, { | ||||||||||||||
theme: extendTheme({ | ||||||||||||||
colors: { | ||||||||||||||
primary: 'tomato', | ||||||||||||||
secondary: 'cyan', | ||||||||||||||
}, | ||||||||||||||
spacing: { | ||||||||||||||
unit: 8, | ||||||||||||||
}, | ||||||||||||||
typography: { | ||||||||||||||
fontFamily: 'Inter, sans-serif', | ||||||||||||||
}, | ||||||||||||||
}), | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
#### Color schemes | ||||||||||||||
|
||||||||||||||
You can use the `colorSchemes` key within the `extendTheme` utility to assign different values based on different conditions, such as switching between dark mode and light mode: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
extendTheme({ | ||||||||||||||
colorSchemes: { | ||||||||||||||
light: { | ||||||||||||||
colors: { | ||||||||||||||
background: '#f9f9f9', | ||||||||||||||
foreground: '#121212', | ||||||||||||||
}, | ||||||||||||||
}, | ||||||||||||||
dark: { | ||||||||||||||
colors: { | ||||||||||||||
background: '#212121', | ||||||||||||||
foreground: '#fff', | ||||||||||||||
}, | ||||||||||||||
}, | ||||||||||||||
}, | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
Pigment CSS uses the `prefers-color-scheme` media query by default to switch between color schemes: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
const colorScheme = css` | ||||||||||||||
background-color: ${({ theme }) => theme.colorSchemes.dark.colors.background}; | ||||||||||||||
|
||||||||||||||
color: ${({ theme }) => theme.colorSchemes.dark.colors.foreground}; | ||||||||||||||
|
||||||||||||||
@media (prefers-color-scheme: light) { | ||||||||||||||
background-color: ${({ theme }) => theme.colorSchemes.light.colors.background}; | ||||||||||||||
color: ${({ theme }) => theme.colorSchemes.light.colors.foreground}; | ||||||||||||||
} | ||||||||||||||
`; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
You can also customize the behavior by providing a `getSelector` function: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
extendTheme({ | ||||||||||||||
colorSchemes: { | ||||||||||||||
Comment on lines
+298
to
+300
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. Wrong pragma and wrong indentation, should be:
Suggested change
|
||||||||||||||
light: { ... }, | ||||||||||||||
dark: { ... }, | ||||||||||||||
}, | ||||||||||||||
+ getSelector: (colorScheme) => colorScheme ? `.theme-${colorScheme}` : ':root', | ||||||||||||||
}); | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
### The sx prop | ||||||||||||||
|
||||||||||||||
Pigment CSS introduces the `sx` prop, letting you provide inline, one-off custom styles to any element. | ||||||||||||||
alelthomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
At build time, Pigment CSS replaces the `sx` prop with `className` and `style` props. | ||||||||||||||
The `sx` prop works with all Material UI components as well as HTML elements and any third-party JSX components. | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
<div sx={{ display: 'flex', flexDirection: 'column' }}> | ||||||||||||||
|
||||||||||||||
<AnyComponent sx={{ fontSize: 12, color: 'red' }} />; | ||||||||||||||
``` | ||||||||||||||
|
||||||||||||||
If you use the `sx` prop on an HTML element, you'll need to augment the `HTMLAttributes` interface: | ||||||||||||||
|
||||||||||||||
```js | ||||||||||||||
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. Should get the correct color highlighting:
Suggested change
|
||||||||||||||
type Theme = { | ||||||||||||||
// your theme type | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
declare global { | ||||||||||||||
namespace React { | ||||||||||||||
interface HTMLAttributes<T> { | ||||||||||||||
sx?: | ||||||||||||||
| React.CSSProperties | ||||||||||||||
| ((theme: Theme) => React.CSSProperties) | ||||||||||||||
| ReadonlyArray<React.CSSProperties | ((theme: Theme) => React.CSSProperties)>; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
``` |
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as React from 'react'; | ||
import MarkdownDocs from 'docs/src/modules/components/MarkdownDocs'; | ||
import * as pageProps from 'docs/data/material/experimental-api/pigment-css/pigment-css.md?muiMarkdown'; | ||
|
||
export default function Page() { | ||
return <MarkdownDocs {...pageProps} />; | ||
} |
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.
Fixing those in #44353