-
-
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
[system] Reexport Pigment CSS from index file #43218
Merged
mnajdova
merged 8 commits into
mui:next
from
siriwatknp:types/material-pigment-css-theme
Aug 7, 2024
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
25e373f
fix migrating to pigment content
siriwatknp 4c9fb41
move theme.ts into subpath
siriwatknp 8b30acc
move theme export to index
siriwatknp 3a57447
Merge branch 'docs/migration-pigment' into types/material-pigment-css…
siriwatknp c31a122
add TypeScript docs
siriwatknp dbaf9a2
fix non-breaking space
siriwatknp 08493ad
revert leaked changes from another PR
mnajdova ba1e130
Revert remaining leak
DiegoAndai 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 | ||||
---|---|---|---|---|---|---|
|
@@ -76,7 +76,9 @@ const nextConfig = { | |||||
/** | ||||||
* @type {import('@pigment-css/nextjs-plugin').PigmentOptions} | ||||||
*/ | ||||||
const pigmentConfig = {}; // we will refer to this later | ||||||
const pigmentConfig = { | ||||||
transformLibraries: ['@mui/material'], | ||||||
}; | ||||||
|
||||||
export default withPigment(nextConfig, pigmentConfig); | ||||||
``` | ||||||
|
@@ -91,7 +93,9 @@ const nextConfig = { | |||||
/** | ||||||
* @type {import('@pigment-css/nextjs-plugin').PigmentOptions} | ||||||
*/ | ||||||
const pigmentConfig = {}; // we will refer to this later | ||||||
const pigmentConfig = { | ||||||
transformLibraries: ['@mui/material'], | ||||||
}; | ||||||
|
||||||
module.exports = withPigment(nextConfig, pigmentConfig); | ||||||
``` | ||||||
|
@@ -143,7 +147,12 @@ Next, open vite config file, usually named `vite.config.js`, and add the plugin: | |||||
import { defineConfig } from 'vite'; | ||||||
import { pigment } from '@pigment-css/vite-plugin'; | ||||||
|
||||||
const pigmentConfig = {}; // we will refer to this later | ||||||
/** | ||||||
* @type {import('@pigment-css/nextjs-plugin').PigmentOptions} | ||||||
*/ | ||||||
const pigmentConfig = { | ||||||
transformLibraries: ['@mui/material'], | ||||||
}; | ||||||
|
||||||
export default defineConfig({ | ||||||
plugins: [ | ||||||
|
@@ -153,6 +162,10 @@ export default defineConfig({ | |||||
}); | ||||||
``` | ||||||
|
||||||
:::warning | ||||||
There is an [issue with `pnpm`](https://github.com/mui/pigment-css/issues/176) that prevents the plugin from working correctly. Consider using `npm` or `yarn` instead. | ||||||
::: | ||||||
|
||||||
Finally, add the Pigment CSS stylesheet to the top of the main file. | ||||||
|
||||||
```diff title="src/main.(js|tsx)" | ||||||
|
@@ -173,14 +186,14 @@ Integrating Pigment CSS with Material UI requires you to configure the theme t | |||||
Add the following code to your [Next.js](#nextjs) or [Vite](#vite) config file: | ||||||
|
||||||
```diff | ||||||
+import { extendTheme } from '@mui/material'; | ||||||
+import { createTheme } from '@mui/material'; | ||||||
|
||||||
+const pigmentConfig = { | ||||||
+ theme: extendTheme(), | ||||||
+ theme: createTheme(…parameters if any), | ||||||
+}; | ||||||
``` | ||||||
|
||||||
If you don't have a custom theme, you are ready to go. Start a development server by running: | ||||||
If you have a custom theme, follow the [theme migration](#migrating-custom-theme), otherwise you are ready to go. Start a development server by running: | ||||||
|
||||||
<codeblock storageKey="package-manager"> | ||||||
|
||||||
|
@@ -200,6 +213,35 @@ pnpm dev | |||||
|
||||||
Open the browser and navigate to the localhost URL, you should see the app running with Pigment CSS. | ||||||
|
||||||
### Typescript | ||||||
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
Fixing this in #43751 |
||||||
|
||||||
If you are using TypeScript, you need to extend the Pigment CSS theme types with Material UI `Theme`. | ||||||
Add the following code to a file that is included in your `tsconfig.json`: | ||||||
|
||||||
```ts | ||||||
// e.g. App.tsx | ||||||
import { Theme } from '@mui/material/styles'; | ||||||
|
||||||
declare module '@mui/material-pigment-css' { | ||||||
interface ThemeArgs { | ||||||
theme: Theme; | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
Then, verify that the types is correctly picked up by Pigment CSS with the following code: | ||||||
|
||||||
```ts | ||||||
// e.g. App.tsx | ||||||
import { styled } from '@mui/material-pigment-css'; | ||||||
|
||||||
const TestThemeTypes = styled('div')(({ theme }) => ({ | ||||||
color: theme.palette.primary.main, | ||||||
})); | ||||||
``` | ||||||
|
||||||
You should see no TypeScript errors in your editor. Finally, remove the test code. | ||||||
|
||||||
## How it works | ||||||
|
||||||
When a Pigment CSS plugin is configured through a framework bundler, it intercepts the styling APIs that Material UI uses and replaces them with those from Pigment CSS instead. Pigment CSS then extracts the styles at build time and injects them into the stylesheet. | ||||||
|
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 +1,2 @@ | ||
export * from '@pigment-css/react'; | ||
export * from '@pigment-css/react/theme'; |
This file was deleted.
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.
Some of these seem to have leaked from #43217?