🔧Initial setup
Setup @codegouvfr/react-dsfr in your project or start from a template
If you already had the DSFR installed in your project, let's start from scratch:
Remove
@gouvfr/dsfrfrom your dependencies.Remove the import of
dsfr.css, dsfr.module.js the favicon and the fonts.Remove
the data-fr-scheme(anddata-fr-theme) attribude from your<html/>tag
yarn add @codegouvfr/react-dsfrnpm install --save @codegouvfr/react-dsfrpnpm add @codegouvfr/react-dsfrAnd add this file to the root of your project, to enable pre & post scripts with pnpm:
enable-pre-post-scripts=trueWhen we say Yarn we usually refer to Yarn 1.x as most dev teams (Including me) havent upgraded to the newest version (for good reasons).
If you want to use Yarn Berry you be aware that pre- post- scripts aren't supported.
So you must do something like "dev": "copy-dsfr-to-public && next dev" (same thing for start)
Also you must configure it so it uses node_modules (sorry)
If you want a more complete starter with a Ready yo use stack that includes routing, autentication, internationalisation ect. you can also use this starter instead:
Add these three scripts to your package.json:
"scripts": {
"predev": "react-dsfr update-icons",
"prebuild": "react-dsfr update-icons"
}Trigger the execution of the postinstall script by running:
yarn # Or 'npm install' or 'pnpm install'Add the following tags in the <head />
<link rel="apple-touch-icon" href="./node_modules/@codegouvfr/react-dsfr/favicon/apple-touch-icon.png" />
<link rel="icon" href="./node_modules/@codegouvfr/react-dsfr/favicon/favicon.svg" type="image/svg+xml" />
<link rel="shortcut icon" href="./node_modules/@codegouvfr/react-dsfr/favicon/favicon.ico" type="image/x-icon" />
<link rel="manifest" href="./node_modules/@codegouvfr/react-dsfr/favicon/manifest.webmanifest" crossorigin="use-credentials" />
<link rel="stylesheet" href="./node_modules/@codegouvfr/react-dsfr/main.css" />import React from "react";
import ReactDOM from "react-dom/client";
import { App } from "./App";
import { startReactDsfr } from "@codegouvfr/react-dsfr/spa";
startReactDsfr({ defaultColorScheme: "system" });
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
);You're all set! Next step for you is to setup the integration with your routing library (react-router for example).
🔀Integration with routing libsyarn add @codegouvfr/react-dsfr
yarn add --dev sass
npx degit https://github.com/garronej/react-dsfr-next-appdir-demo/src/dsfr-bootstrap src/dsfr-bootstrapimport type { NextConfig } from "next";
const nextConfig: NextConfig = {
/* config options here */
webpack: config => {
config.module.rules.push({
test: /\.woff2$/,
type: "asset/resource"
});
return config;
}
};
export default nextConfig;
"scripts": {
"predev": "react-dsfr update-icons",
"prebuild": "react-dsfr update-icons"
}import { getHtmlAttributes, DsfrHead } from "../dsfr-bootstrap/server-only-index";
import { DsfrProvider } from "../dsfr-bootstrap";
export default function RootLayout({ children }: { children: React.JSX.Element; }) {
const lang = undefined; // Can be "fr" or "en" ...
return (
<html {...getHtmlAttributes({ lang })} >
<head>
<DsfrHead />
</head>
<body>
<DsfrProvider lang={lang}>
{children}
</DsfrProvider>
</body>
</html>
);
}import { StartDsfrOnHydration } from "../dsfr-bootstrap";
export default function Page() {
return (
<>
{/* Important: You must mount this component on every pages of your App! */}
<StartDsfrOnHydration />
<h1>Welcome!</h1>
</>
);
}# If you plan to use MUI:
yarn add @mui/material @emotion/react @emotion/server @emotion/styled @mui/material @emotion/react/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
webpack: config => {
config.module.rules.push({
test: /\.woff2$/,
type: "asset/resource"
});
return config;
},
//This option requires Next 13.1 or newer, if you can't update you can use this plugin instead: https://github.com/martpie/next-transpile-modules
transpilePackages: [
"@codegouvfr/react-dsfr",
"tss-react" // This is for MUI or if you use htts://tss-react.dev
],
output: "export"
};
module.exports = nextConfig"scripts": {
"predev": "react-dsfr update-icons",
"prebuild": "react-dsfr update-icons"
}import type { AppProps } from "next/app";
import { createNextDsfrIntegrationApi } from "@codegouvfr/react-dsfr/next-pagesdir";
import Link from "next/link";
// Only in TypeScript projects
declare module "@codegouvfr/react-dsfr/next-pagesdir" {
interface RegisterLink {
Link: typeof Link;
}
}
const {
withDsfr,
dsfrDocumentApi
} = createNextDsfrIntegrationApi({
defaultColorScheme: "system",
Link
});
export { dsfrDocumentApi };
function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
export default withDsfr(App);import { Html, Head, Main, NextScript, DocumentProps } from "next/document";
import { dsfrDocumentApi } from "./_app";
const {
getColorSchemeHtmlAttributes,
augmentDocumentForDsfr
} = dsfrDocumentApi;
export default function Document(props: DocumentProps) {
return (
<Html {...getColorSchemeHtmlAttributes(props)}>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
);
}
augmentDocumentForDsfr(Document);The create-react-app project is no longer being maintained. If you are starting a new project you'll probably be beter off with Vite.
Add these three scripts to your package.json:
"scripts": {
...
"postinstall": "react-dsfr copy-static-assets",
"predev": "react-dsfr update-icons",
"prebuild": "react-dsfr update-icons"
},
...
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!@codegouvfr/react-dsfr)"
]
}Trigger the execution of the postinstall script by running:
yarn # Or 'npm install' or 'pnpm install'Add the following code in the <head />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/dsfr/favicon/apple-touch-icon.png" />
<link rel="icon" href="%PUBLIC_URL%/dsfr/favicon/favicon.svg" type="image/svg+xml" />
<link rel="shortcut icon" href="%PUBLIC_URL%/dsfr/favicon/favicon.ico" type="image/x-icon" />
<link rel="manifest" href="%PUBLIC_URL%/dsfr/favicon/manifest.webmanifest" crossorigin="use-credentials" />
<link rel="stylesheet" href="%PUBLIC_URL%/dsfr/utility/icons/icons.min.css" />
<link rel="stylesheet" href="%PUBLIC_URL%/dsfr/dsfr.min.css" />import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import { startReactDsfr } from "@codegouvfr/react-dsfr/spa";
startReactDsfr({ defaultColorScheme: "system" });
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);You're all set! Next step for you is to setup de integration with your routing library (react-router for example)
🔀Integration with routing libsYour framework isn't supported? Let's get in touch! Note: We don't have a custom integration for it but react-dsfr has been reported working with Gatsby.
Avoiding or flash of unstyled text (FOUT)
You can avoid having a flash of unstyled text by preloading the font variant used on your homepage (look in the network tab of your browser dev tools what are the font downloaded initially).
Add the following tags in the <head />
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Light.woff2" as="font" crossorigin="anonymous" />-->
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Light_Italic.woff2" as="font" crossorigin="anonymous" />-->
<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Regular.woff2" as="font" crossorigin="anonymous" />
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Regular_Italic.woff2" as="font" crossorigin="anonymous" />-->
<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Medium.woff2" as="font" crossorigin="anonymous" />
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Medium_Italic.woff2" as="font" crossorigin="anonymous" />-->
<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Bold.woff2" as="font" crossorigin="anonymous" />
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Marianne-Bold_Italic.woff2" as="font" crossorigin="anonymous" />-->
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Spectral-Regular.woff2" as="font" crossorigin="anonymous" />-->
<!--<link rel="preload" href="./node_modules/@codegouvfr/react-dsfr/dsfr/fonts/Spectral-ExtraBold.woff2" as="font" crossorigin="anonymous" />-->import { getHtmlAttributes, DsfrHead } from "../dsfr-bootstrap/server-only-index";
import { DsfrProvider } from "../dsfr-bootstrap";
export default function RootLayout({ children }: { children: React.JSX.Element; }) {
const lang = undefined; // Can be "fr" or "en" ...
return (
<html {...getHtmlAttributes({ lang })} >
<head>
<DsfrHead
preloadFonts={[
//"Marianne-Light",
//"Marianne-Light_Italic",
"Marianne-Regular",
//"Marianne-Regular_Italic",
"Marianne-Medium",
//"Marianne-Medium_Italic",
"Marianne-Bold"
//"Marianne-Bold_Italic",
//"Spectral-Regular",
//"Spectral-ExtraBold"
]}
/>
</head>
<body>
<DsfrProvider lang={lang}>
{children}
</DsfrProvider>
</body>
</html>
);
}import type { AppProps } from "next/app";
import { createNextDsfrIntegrationApi } from "@codegouvfr/react-dsfr/next-pagesdir";
import Link from "next/link";
// Only in TypeScript projects
declare module "@codegouvfr/react-dsfr/next-pagesdir" {
interface RegisterLink {
Link: typeof Link;
}
}
const {
withDsfr,
dsfrDocumentApi
} = createNextDsfrIntegrationApi({
defaultColorScheme: "system",
Link,
preloadFonts: [
//"Marianne-Light",
//"Marianne-Light_Italic",
"Marianne-Regular",
//"Marianne-Regular_Italic",
"Marianne-Medium",
//"Marianne-Medium_Italic",
"Marianne-Bold",
//"Marianne-Bold_Italic",
//"Spectral-Regular",
//"Spectral-ExtraBold"
]
});
export { dsfrDocumentApi };
function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
export default withDsfr(App);Add the following code in the <head />
<%
[
//"Marianne-Light",
//"Marianne-Light_Italic",
"Marianne-Regular",
//"Marianne-Regular_Italic",
"Marianne-Medium",
//"Marianne-Medium_Italic",
"Marianne-Bold",
//"Marianne-Bold_Italic",
//"Spectral-Regular",
//"Spectral-ExtraBold"
].forEach(function(name){ %>
<link rel="preload" href="%PUBLIC_URL%/dsfr/fonts/<%=name%>.woff2" as="font" crossorigin="anonymous" />
<% }); %>Last updated
Was this helpful?
