To use the DSFR design system in a Next.js project using the Pages Router, use createNextDsfrIntegrationApi to generate an integration object. This object provides a Higher-Order Component (HOC) withDsfr to wrap your App component and a dsfrDocumentApi to configure your _document.tsx.
Configuration Options
When calling createNextDsfrIntegrationApi, you can pass the following parameters:
| Parameter | Type | Default | Description |
|---|
defaultColorScheme | ColorScheme | 'system' | Required | Sets the initial color scheme ('light', 'dark', or 'system'). |
verbose | boolean | false | Enables verbose logging. |
Link | Function | <a> | Custom Link component to use for DSFR links. |
preloadFonts | (keyof typeof fontUrlByFileBasename)[] | [] | List of font basenames to preload (only in production). |
doPersistDarkModePreferenceWithCookie | boolean | false | If true, dark mode preference is persisted in cookies. |
useLang | () => string | () => 'fr' | Function that returns the current language string. |
trustedTypesPolicyName | string | 'react-dsfr' | Custom policy name for Trusted Types (for CSP). |
doDisableFavicon | boolean | false | If true, prevents the library from injecting its default favicons. |
Implementation Steps
- Wrap your App: Use
withDsfr in _app.tsx. - Augment your Document: Use
augmentDocumentForDsfr in _document.tsx to handle server-side color scheme detection. - Apply HTML Attributes: Use
getColorSchemeHtmlAttributes in _document.tsx to inject data-fr-scheme and data-fr-theme into the <html> tag.
import { createNextDsfrIntegrationApi } from '@codegouvfr/react-dsfr/next-pagesdir';
const { withDsfr, dsfrDocumentApi } = createNextDsfrIntegrationApi({
defaultColorScheme: 'system',
doPersistDarkModePreferenceWithCookie: true,
preloadFonts: ['some-font-basename'],
});
// In _app.tsx
export default withDsfr(function App(props) {
return <Component {...props} />;
});
// In _document.tsx
// Use dsfrDocumentApi.augmentDocumentForDsfr and dsfrDocumentApi.getColorSchemeHtmlAttributes