Skip to content

Commit 4914569

Browse files
committed
[nextjs] Update the withPigment API to accept config
through `pigment` key from the nextConfig object itself instead of as a 2nd argument to the call. This is a standard approach followed by other nextjs plugins Fixes: mui#83
1 parent 70dd90a commit 4914569

File tree

3 files changed

+57
-42
lines changed

3 files changed

+57
-42
lines changed

README.md

+40-36
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,9 @@ For example, in Next.js, you can define a theme in the `next.config.js` file lik
531531
```js
532532
const { withPigment } = require('@pigment-css/nextjs-plugin');
533533

534-
module.exports = withPigment(
535-
{
536-
// ...other nextConfig
537-
},
538-
{
534+
module.exports = withPigment({
535+
// ...other nextConfig
536+
pigment: {
539537
theme: {
540538
colors: {
541539
primary: 'tomato',
@@ -550,9 +548,12 @@ module.exports = withPigment(
550548
// ...more keys and values, it's free style!
551549
},
552550
},
553-
);
551+
});
554552
```
555553

554+
> [!NOTE]
555+
> The previous API to configure theming was to pass the data in the above `pigment` key as the second argument, ie, `withPigment(nextConfig, pigmentConfig)`. But it has been changed to follow what Next.js community follows.
556+
556557
### Accessing theme values
557558

558559
A callback can be used with **styled()** and **css()** APIs to access the theme values:
@@ -572,11 +573,9 @@ Pigment CSS can generate CSS variables from the theme values when you wrap your
572573
```js
573574
const { withPigment, extendTheme } = require('@pigment-css/nextjs-plugin');
574575

575-
module.exports = withPigment(
576-
{
577-
// ...nextConfig
578-
},
579-
{
576+
module.exports = withPigment({
577+
// ...nextConfig
578+
pigment: {
580579
theme: extendTheme({
581580
colors: {
582581
primary: 'tomato',
@@ -590,7 +589,7 @@ module.exports = withPigment(
590589
},
591590
}),
592591
},
593-
);
592+
});
594593
```
595594

596595
The `extendTheme` utility goes through the theme and creates a `vars` object which represents the tokens that refer to CSS variables.
@@ -798,18 +797,23 @@ To support right-to-left (RTL) languages, add the `dir="rtl"` attribute to your
798797
```js
799798
const { withPigment } = require('@pigment-css/nextjs-plugin');
800799

801-
// ...
802-
module.exports = withPigment(nextConfig, {
803-
theme: yourCustomTheme,
804-
// CSS output option
805-
css: {
806-
// Specify your default CSS authoring direction
807-
defaultDirection: 'ltr',
808-
// Generate CSS for the opposite of the `defaultDirection`
809-
// This is set to `false` by default
810-
generateForBothDir: true,
800+
/** @type {import('@pigment-css/nextjs-plugin').WithPigmentOptions} */
801+
const nextConfig = {
802+
pigment: {
803+
theme: yourCustomTheme,
804+
// CSS output option
805+
css: {
806+
// Specify your default CSS authoring direction
807+
defaultDirection: 'ltr',
808+
// Generate CSS for the opposite of the `defaultDirection`
809+
// This is set to `false` by default
810+
generateForBothDir: true,
811+
},
811812
},
812-
});
813+
};
814+
815+
// ...
816+
module.exports = withPigment(nextConfig);
813817
```
814818

815819
### Vite
@@ -1136,12 +1140,12 @@ Next, they must set up Pigment CSS in their project:
11361140
// framework config file, for example next.config.js
11371141
const { withPigment } = require('@pigment-css/nextjs-plugin');
11381142

1139-
module.exports = withPigment(
1140-
{
1141-
// ... Your nextjs config.
1143+
module.exports = withPigment({
1144+
// ... Your nextjs config.
1145+
pigment: {
1146+
transformLibraries: ['your-package-name'],
11421147
},
1143-
{ transformLibraries: ['your-package-name'] },
1144-
);
1148+
});
11451149
```
11461150

11471151
Finally, they must import the stylesheet in the root layout file:
@@ -1167,9 +1171,9 @@ Developers can customize the component's styles using the theme's `styleOverride
11671171
For example, the custom theme below sets the background color of the statistics component's root slot to `tomato`:
11681172

11691173
```js
1170-
module.exports = withPigment(
1171-
{ ...nextConfig },
1172-
{
1174+
module.exports = withPigment({
1175+
...nextConfig,
1176+
pigment: {
11731177
theme: {
11741178
components: {
11751179
PigmentStat: {
@@ -1188,15 +1192,15 @@ module.exports = withPigment(
11881192
},
11891193
},
11901194
},
1191-
);
1195+
});
11921196
```
11931197

11941198
Developers can also access theme values and apply styles based on the component's props using the `variants` key:
11951199

11961200
```js
1197-
module.exports = withPigment(
1198-
{ ...nextConfig },
1199-
{
1201+
module.exports = withPigment({
1202+
...nextConfig,
1203+
pigment: {
12001204
theme: {
12011205
// user defined colors
12021206
colors: {
@@ -1229,7 +1233,7 @@ module.exports = withPigment(
12291233
},
12301234
},
12311235
},
1232-
);
1236+
});
12331237
```
12341238

12351239
## How Pigment CSS works

apps/pigment-css-next-app/next.config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ const pigmentOptions = {
106106
displayName: true,
107107
};
108108

109-
/** @type {import('next').NextConfig} */
109+
/** @type {import('@pigment-css/nextjs-plugin').WithPigmentOptions} */
110110
const nextConfig = {
111111
eslint: {
112112
ignoreDuringBuilds: true,
@@ -118,6 +118,7 @@ const nextConfig = {
118118
buildActivity: true,
119119
buildActivityPosition: 'bottom-right',
120120
},
121+
pigment: pigmentOptions,
121122
};
122123

123-
module.exports = withPigment(nextConfig, pigmentOptions);
124+
module.exports = withPigment(nextConfig);

packages/pigment-css-nextjs-plugin/src/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,25 @@ import type { NextConfig } from 'next';
33
import { findPagesDir } from 'next/dist/lib/find-pages-dir';
44
import { webpack as webpackPlugin, extendTheme, type PigmentOptions } from '@pigment-css/unplugin';
55

6-
export { type PigmentOptions };
6+
export interface WithPigmentOptions extends NextConfig {
7+
pigment?: PigmentOptions;
8+
}
79

810
const extractionFile = path.join(
911
path.dirname(require.resolve('../package.json')),
1012
'zero-virtual.css',
1113
);
1214

13-
export function withPigment(nextConfig: NextConfig, pigmentConfig?: PigmentOptions) {
14-
const { babelOptions = {}, asyncResolve, ...rest } = pigmentConfig ?? {};
15+
export function withPigment(
16+
{ pigment, ...nextConfig }: WithPigmentOptions,
17+
pigmentConfig?: PigmentOptions,
18+
) {
19+
const { babelOptions = {}, asyncResolve, ...rest } = pigment ?? pigmentConfig ?? {};
20+
if (pigmentConfig) {
21+
console.warn(
22+
'Passing Pigment CSS config through the 2nd argument is deprecated and will be removed in a future stable version. Instead, pass the config through the `pigment` key in your next.js config.',
23+
);
24+
}
1525
if (process.env.TURBOPACK === '1') {
1626
// eslint-disable-next-line no-console
1727
console.log(
@@ -103,4 +113,4 @@ export function withPigment(nextConfig: NextConfig, pigmentConfig?: PigmentOptio
103113
};
104114
}
105115

106-
export { extendTheme };
116+
export { extendTheme, type PigmentOptions };

0 commit comments

Comments
 (0)