π CSS in JS
Compatibility with solutions like styled-components, emotion and TSS.
At build time react-dsfr parses the official dsfr.css files and spits out a typed JavaScript representation of the DSFR. In particular, its colors options and decisions, the spacing system and the breakpoints values.
This enables to write DSFR compliant CSS in JS code, since we are able to expose function that are the equivalent of the DSFR utility classes.
Checkout the color selection tool.
You can use the style props on native react components but you won't be able to use the fr.breakpoint utility that enable to write responsive code.
import { fr } from "@codegouvfr/react-dsfr";
export type Props = {
className?: string;
};
export const MyComponent =(props: Props) => {
const { className } = props;
return (
<div
className={className}
style={{
padding: fr.spacing("10v"),
//SEE: https://components.react-dsfr.codegouv.studio/?path=/docs/%F0%9F%8E%A8-color-helper--page
backgroundColor: fr.colors.decisions.background.alt.blueFrance.active
}}
>
<span
className={fr.cx("fr-p-1v")}
style={{
...fr.spacing("margin", { "topBottom": "3v" })
}}
>
Hello World
</span>
</div>
);
};
tss-react
You can also use TSS to apply global styles:
Optionally, if you want to have access to isDark in your styles, but this is not nessesary because fr.colors uses CSS variables by default.
spacing
For ensuring the spacing between elements is consistent throughout the website.
The above code is equivalent to:
Which is in turn equivalent to:

breakpoints
For writing responsive UIs with media query (@media).

colors
Using the theme object that holds the colors decisions and options.
π£π£π£π£π£π£ There is a tool at your disposal to help you pick your colors. Use it! It's great! π£π£π£π£π£π£π£
Using CSS variables (recommended)
This approad is React agnostic and yield the best performances.
Using HEX color code
Some third party libraries might require you to provide explicit value as colors.
When CSS variable references doesn't work you can do:
useIsDark()
You can access the active mode (isDark: true/false) in the theme object. However, if you want to manually switch the mode, you can use setIsDark(true/false) .
If you want to use the isDark value in your styles:
useBreakpointsValuesPx()
It returns the values in pixel of the different breakpoint ("xs", "md", "lg", "xl") based on the current root font size.
It can be used to do stuffs like this, geting the number of column of a responsive layout in JavaScript:
Be carefull though, favor using fr.breakpoints over client size mesurement and computation.
On the backend you can't know ahead of time the size of the screen of your users so this kind of approach will result in a flickering in SSR setups.
For example, your backend has no clue the size of the device making the request so it renders for a 1080p screen but the device making the request was, in fact, an iPhone and the first print is fully broken, the app becomes usable only after hydration.
Long story short, use this only if you are building an SPA.
Last updated
Was this helpful?
