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

[system] Reexport Pigment CSS from index file #43218

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
};
Copy link
Member

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?


export default withPigment(nextConfig, pigmentConfig);
```
Expand All @@ -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);
```
Expand Down Expand Up @@ -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: [
Expand All @@ -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)"
Expand All @@ -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">

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Typescript
### TypeScript

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.
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material-pigment-css/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from '@pigment-css/react';
export * from '@pigment-css/react/theme';
1 change: 0 additions & 1 deletion packages/mui-material-pigment-css/src/theme.ts

This file was deleted.