Skip to content

Commit bcedc18

Browse files
Improving Browser Polyfills docs for Next.js (#716)
* Creating sample doc * Adding docs for all packages
1 parent 4315b12 commit bcedc18

File tree

8 files changed

+112
-2
lines changed

8 files changed

+112
-2
lines changed

plugins/css-blank-pseudo/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,21 @@ Please note that using a class, leverages `classList` under the hood which
186186
might not be supported on some old browsers such as IE9, so you may need
187187
to polyfill `classList` in those cases.
188188

189+
### Using with Next.js
190+
191+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
192+
193+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
194+
195+
```jsx
196+
useEffect(async () => {
197+
const cssBlankPseudoInit = (await import('css-blank-pseudo/browser')).default;
198+
cssBlankPseudoInit();
199+
}, []);
200+
```
201+
202+
We recommend you load the polyfill as high up on your Next application as possible, such as your `pages/_app.ts` file.
203+
189204
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
190205
[css-url]: https://cssdb.org/#blank-pseudo-class
191206
[discord]: https://discord.gg/bUadyRwkJS

plugins/css-blank-pseudo/docs/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,20 @@ Please note that using a class, leverages `classList` under the hood which
146146
might not be supported on some old browsers such as IE9, so you may need
147147
to polyfill `classList` in those cases.
148148

149+
### Using with Next.js
150+
151+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
152+
153+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
154+
155+
```jsx
156+
useEffect(async () => {
157+
const cssBlankPseudoInit = (await import('<packageName>/browser')).default;
158+
cssBlankPseudoInit();
159+
}, []);
160+
```
161+
162+
We recommend you load the polyfill as high up on your Next application as possible, such as your `pages/_app.ts` file.
163+
149164
<linkList>
150165
[Selectors Level 4]: <specUrl>

plugins/css-has-pseudo/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ By default the polyfill will not emit errors or warnings.
198198
cssHasPseudo(document, { debug: true });
199199
```
200200

201-
202201
### Browser Dependencies
203202

204203
Web API's:
@@ -271,6 +270,21 @@ HTML might look like this :
271270
```
272271

273272

273+
### Using with Next.js
274+
275+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
276+
277+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
278+
279+
```jsx
280+
useEffect(async () => {
281+
const cssHasPseudo = (await import('css-has-pseudo/browser')).default;
282+
cssHasPseudo(document);
283+
}, []);
284+
```
285+
286+
We recommend you load the polyfill as high up on your Next application as possible, such as your `pages/_app.ts` file.
287+
274288
## How it works
275289

276290
The [PostCSS Has Pseudo] clones rules containing `:has()`,

plugins/css-has-pseudo/docs/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ By default the polyfill will not emit errors or warnings.
174174
cssHasPseudo(document, { debug: true });
175175
```
176176

177-
178177
### Browser Dependencies
179178

180179
Web API's:
@@ -198,6 +197,21 @@ ECMA Script:
198197

199198
<corsWarning>
200199

200+
### Using with Next.js
201+
202+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
203+
204+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
205+
206+
```jsx
207+
useEffect(async () => {
208+
const cssHasPseudo = (await import('<packageName>/browser')).default;
209+
cssHasPseudo(document);
210+
}, []);
211+
```
212+
213+
We recommend you load the polyfill as high up on your Next application as possible, such as your `pages/_app.ts` file.
214+
201215
## How it works
202216

203217
The [<humanReadableName>] clones rules containing `:has()`,

plugins/css-prefers-color-scheme/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,19 @@ HTML might look like this :
288288
```
289289

290290

291+
### Using with Next.js
292+
293+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
294+
295+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
296+
297+
```jsx
298+
useEffect(async () => {
299+
const prefersColorSchemeInit = (await import('css-prefers-color-scheme/browser')).default;
300+
const prefersColorScheme = prefersColorSchemeInit();
301+
}, []);
302+
```
303+
291304
## How does it work?
292305

293306
[Prefers Color Scheme] is a [PostCSS] plugin that transforms `prefers-color-scheme` queries into `color` queries.

plugins/css-prefers-color-scheme/docs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ ECMA Script:
164164

165165
<corsWarning>
166166

167+
### Using with Next.js
168+
169+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
170+
171+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
172+
173+
```jsx
174+
useEffect(async () => {
175+
const prefersColorSchemeInit = (await import('<packageName>/browser')).default;
176+
const prefersColorScheme = prefersColorSchemeInit();
177+
}, []);
178+
```
179+
167180
## How does it work?
168181

169182
[<humanReadableName>] is a [PostCSS] plugin that transforms `prefers-color-scheme` queries into `color` queries.

plugins/postcss-focus-within/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,19 @@ focusWithinInit({ replaceWith: '.focus-within });
179179
180180
This option should be used if it was changed at PostCSS configuration level.
181181
182+
### Using with Next.js
183+
184+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
185+
186+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
187+
188+
```jsx
189+
useEffect(async () => {
190+
const focusWithinInit = (await import('postcss-focus-within/browser')).default;
191+
focusWithinInit();
192+
}, []);
193+
```
194+
182195
[cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
183196
[css-url]: https://cssdb.org/#focus-within-pseudo-class
184197
[discord]: https://discord.gg/bUadyRwkJS

plugins/postcss-focus-within/docs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,18 @@ focusWithinInit({ replaceWith: '.focus-within });
141141
142142
This option should be used if it was changed at PostCSS configuration level.
143143
144+
### Using with Next.js
145+
146+
Given that Next.js imports packages both on the browser and on the server, you need to make sure that the package is only imported on the browser.
147+
148+
As outlined in the [Next.js documentation](https://nextjs.org/docs/advanced-features/dynamic-import#with-external-libraries), you need to load the package with a dynamic import:
149+
150+
```jsx
151+
useEffect(async () => {
152+
const focusWithinInit = (await import('<packageName>/browser')).default;
153+
focusWithinInit();
154+
}, []);
155+
```
156+
144157
<linkList>
145158
[Selectors Level 4 specification]: <specUrl>

0 commit comments

Comments
 (0)