Skip to content

Commit c3e8191

Browse files
committed
Merge branch 'main' of github.com:codegouvfr/react-dsfr into feature/nonce-trusted-types-csp
2 parents 9370199 + 563faa1 commit c3e8191

File tree

9 files changed

+110
-61
lines changed

9 files changed

+110
-61
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ yarn
8282
yarn storybook
8383

8484
# Starting test apps
85-
yarn start-cra # For testing in in a Create React App setup
85+
yarn start-cra # For testing in a Create React App setup
8686
yarn start-vite # For testing in a Vite setup
8787
yarn start-next-pagesdir # For testing in a Next.js 13 PagesDir setup (the default setup)
8888
yarn start-next-appdir # For testing in a Next.js 13 AppDir setup

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codegouvfr/react-dsfr",
3-
"version": "0.75.8",
3+
"version": "0.76.2",
44
"description": "French State Design System React integration library",
55
"repository": {
66
"type": "git",

scripts/build/cssToTs/breakpoints.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,15 @@ export function generateBreakpointsTsCode(rawCssCode: string): string {
149149
`import { assert } from "tsafe/assert";`,
150150
`import type { Extends } from "tsafe";`,
151151
``,
152-
`export const BreakpointsValuesUnit = "em";`,
152+
`export const breakpointsValuesUnit = "em";`,
153153
``,
154154
`export const breakpointKeys = ["xs", ${sortedKeys
155155
.map(key => `"${key}"`)
156156
.join(", ")}] as const;`,
157157
``,
158158
`export type BreakpointKeys = typeof breakpointKeys[number];`,
159159
``,
160-
`export const BreakpointsValues = {`,
160+
`export const breakpointsValues = {`,
161161
JSON.stringify(
162162
Object.fromEntries(
163163
(["xs", ...sortedKeys] as const).map(key => [
@@ -176,7 +176,7 @@ export function generateBreakpointsTsCode(rawCssCode: string): string {
176176
.join("\n"),
177177
`} as const;`,
178178
``,
179-
`assert<Extends<typeof BreakpointsValues, Record<BreakpointKeys, number>>>();`,
179+
`assert<Extends<typeof breakpointsValues, Record<BreakpointKeys, number>>>();`,
180180
``
181181
].join("\n");
182182
}

scripts/link-in-integration-apps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const projectDirPath = getProjectRoot();
99

1010
const commonThirdPartyDeps = (() => {
1111
const namespaceModuleNames: string[] = ["@emotion", "@mui"];
12-
const standaloneModuleNames = ["react", "@types/react"];
12+
const standaloneModuleNames = ["react", "react-dom", "@types/react"];
1313

1414
return [
1515
...namespaceModuleNames

src/Header/Header.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export type HeaderProps = {
6363
Record<
6464
| "root"
6565
| "body"
66+
| "container"
6667
| "bodyRow"
6768
| "brand"
6869
| "brandTop"
@@ -179,7 +180,7 @@ export const Header = memo(
179180
{...rest}
180181
>
181182
<div className={cx(fr.cx("fr-header__body" as any), classes.body)}>
182-
<div className={fr.cx("fr-container")}>
183+
<div className={cx(fr.cx("fr-container"), classes.container)}>
183184
<div className={cx(fr.cx("fr-header__body-row"), classes.bodyRow)}>
184185
<div
185186
className={cx(

src/fr/breakpoints.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BreakpointKeys } from "./generatedFromCss/breakpoints";
22
import {
3-
BreakpointsValues as values,
4-
BreakpointsValuesUnit as unit,
3+
breakpointsValues as values,
4+
breakpointsValuesUnit as unit,
55
breakpointKeys as keys
66
} from "./generatedFromCss/breakpoints";
77
import { type Equals, assert } from "tsafe/assert";
@@ -43,21 +43,62 @@ export const breakpoints = {
4343
.between(key, keys[keys.indexOf(key) + 1])
4444
.replace("@media", "@media not all and");
4545
},
46+
"values": (() => {
47+
const out = {
48+
...(() => {
49+
const key = "xs" satisfies BreakpointKeys;
50+
51+
return { [key]: `${values[key]}${unit}` as const };
52+
})(),
53+
...(() => {
54+
const key = "sm" satisfies BreakpointKeys;
55+
56+
return { [key]: `${values[key]}${unit}` as const };
57+
})(),
58+
...(() => {
59+
const key = "md" satisfies BreakpointKeys;
60+
61+
return { [key]: `${values[key]}${unit}` as const };
62+
})(),
63+
...(() => {
64+
const key = "lg" satisfies BreakpointKeys;
65+
66+
return { [key]: `${values[key]}${unit}` as const };
67+
})(),
68+
...(() => {
69+
const key = "xl" satisfies BreakpointKeys;
70+
71+
return { [key]: `${values[key]}${unit}` as const };
72+
})()
73+
} as const;
74+
75+
assert<Equals<keyof typeof out, BreakpointKeys>>();
76+
77+
return out;
78+
})(),
79+
"valuesUnit": unit,
80+
"emValues": (() => {
81+
assert<Equals<typeof unit, "em">>();
82+
83+
return values;
84+
})(),
4685
/**
4786
* Returns the breakpoint values in px.
4887
*
4988
* Warning: It reflects the values at a given time, if the root font size changes so will the breakpointsValues.
5089
* Plus this function is rather expensive to call.
5190
* If you're in react you should use the
52-
* import { useBreakpointsValues } from "@codegouvfr/react-dsfr/useBreakpointsValues";
91+
* import { useBreakpointsValuesPx } from "@codegouvfr/react-dsfr/useBreakpointsValuesPx";
5392
*/
54-
"getBreakpointsValues": (): BreakpointsValues => {
93+
"getPxValues": (): BreakpointsValues => {
5594
assert<Equals<typeof unit, "em">>();
5695

5796
const factor = getBaseFontSizePx();
5897

5998
return Object.fromEntries(
6099
Object.entries(values).map(([key, value]) => [key, value * factor])
61100
) as any;
62-
}
101+
},
102+
/** @deprecated use breakpoints.values if you're ok with getting the value in em or breakpoints.getPxValues() if you want the value in pixel */
103+
"getBreakpointsValues": () => breakpoints.getPxValues()
63104
};

src/useBreakpointsValues.ts

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,7 @@
1-
import { useReducer, useEffect, useMemo } from "react";
2-
import { assert } from "tsafe/assert";
3-
import { breakpoints, type BreakpointKeys, type BreakpointsValues } from "./fr/breakpoints";
1+
import { type BreakpointKeys, type BreakpointsValues } from "./fr/breakpoints";
2+
import { useBreakpointsValuesPx } from "./useBreakpointsValuesPx";
43

54
export type { BreakpointKeys, BreakpointsValues };
65

7-
/** Return the breakpoint values in px, the values ger refreshed
8-
* when the base font size change. */
9-
export function useBreakpointsValues() {
10-
const [breakpointsValuesDependency, triggerRefresh] = useReducer(() => ({}), {});
11-
12-
useEffect(() => {
13-
const htmlElement = document.querySelector("html");
14-
15-
assert(htmlElement !== null);
16-
17-
// Create a new MutationObserver to detect changes in the base font size
18-
const observer = new MutationObserver(mutationsList => {
19-
if (
20-
mutationsList.find(
21-
mutation =>
22-
mutation.target === htmlElement &&
23-
mutation.attributeName === "style" &&
24-
(mutation.oldValue ?? "").indexOf("font-size") !== -1
25-
) !== undefined
26-
) {
27-
triggerRefresh();
28-
}
29-
});
30-
31-
// Observe changes to the style attribute of the html element
32-
observer.observe(htmlElement, {
33-
"attributes": true,
34-
"attributeOldValue": true,
35-
"attributeFilter": ["style"]
36-
});
37-
38-
return () => {
39-
observer.disconnect();
40-
};
41-
}, []);
42-
43-
const breakpointsValues = useMemo(
44-
() => breakpoints.getBreakpointsValues(),
45-
[breakpointsValuesDependency]
46-
);
47-
48-
return { breakpointsValues };
49-
}
6+
/** @deprecated Use import { useBreakpointsValuesPx } from "@codegouvfr/react-dsfr/useBreakpointsValuesPx"; instead */
7+
export const useBreakpointsValues = useBreakpointsValuesPx;

src/useBreakpointsValuesPx.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { useReducer, useEffect, useMemo } from "react";
2+
import { assert } from "tsafe/assert";
3+
import { breakpoints, type BreakpointKeys, type BreakpointsValues } from "./fr/breakpoints";
4+
5+
export type { BreakpointKeys, BreakpointsValues };
6+
7+
/** Return the breakpoint values in px, the values ger refreshed
8+
* when the base font size change. */
9+
export function useBreakpointsValuesPx() {
10+
const [breakpointsValuesDependency, triggerRefresh] = useReducer(() => ({}), {});
11+
12+
useEffect(() => {
13+
const htmlElement = document.querySelector("html");
14+
15+
assert(htmlElement !== null);
16+
17+
// Create a new MutationObserver to detect changes in the base font size
18+
const observer = new MutationObserver(mutationsList => {
19+
if (
20+
mutationsList.find(
21+
mutation =>
22+
mutation.target === htmlElement &&
23+
mutation.attributeName === "style" &&
24+
(mutation.oldValue ?? "").indexOf("font-size") !== -1
25+
) !== undefined
26+
) {
27+
triggerRefresh();
28+
}
29+
});
30+
31+
// Observe changes to the style attribute of the html element
32+
observer.observe(htmlElement, {
33+
"attributes": true,
34+
"attributeOldValue": true,
35+
"attributeFilter": ["style"]
36+
});
37+
38+
return () => {
39+
observer.disconnect();
40+
};
41+
}, []);
42+
43+
const breakpointsValues = useMemo(
44+
() => breakpoints.getPxValues(),
45+
[breakpointsValuesDependency]
46+
);
47+
48+
return { breakpointsValues };
49+
}

test/runtime/scripts/breakpoints/generateBreakpointsTsCode.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ it("Generation of TS code for breakpoints", () => {
4545
import { assert } from "tsafe/assert";
4646
import type { Extends } from "tsafe";
4747
48-
export const BreakpointsValuesUnit = "em";
48+
export const breakpointsValuesUnit = "em";
4949
5050
export const breakpointKeys = ["xs", "sm", "md", "lg", "xl"] as const;
5151
5252
export type BreakpointKeys = typeof breakpointKeys[number];
5353
54-
export const BreakpointsValues = {
54+
export const breakpointsValues = {
5555
"xs": 0,
5656
"sm": 36,
5757
"md": 48,
5858
"lg": 62,
5959
"xl": 78
6060
} as const;
6161
62-
assert<Extends<typeof BreakpointsValues, Record<BreakpointKeys, number>>>();
62+
assert<Extends<typeof breakpointsValues, Record<BreakpointKeys, number>>>();
6363
`.replace(/^\n/, "");
6464

6565
const got = generateBreakpointsTsCode(input);

0 commit comments

Comments
 (0)