@@ -6,20 +6,12 @@ import DefaultApp from "next/app";
66import type { AppProps , AppContext } from "next/app" ;
77import type { DocumentProps , DocumentContext } from "next/document" ;
88import { rootColorSchemeStyleTagId , data_fr_scheme , data_fr_theme } from "./useIsDark/constants" ;
9+ import { getScriptToRunAsap } from "./useIsDark/scriptToRunAsap" ;
910import { SsrIsDarkProvider } from "./useIsDark/server" ;
1011import type { ColorScheme } from "./useIsDark" ;
1112import { isBrowser } from "./tools/isBrowser" ;
1213import { objectKeys } from "tsafe/objectKeys" ;
13- import marianneLightWoff2Url from "./dsfr/fonts/Marianne-Light.woff2" ;
14- import marianneItalicWoff2Url from "./dsfr/fonts/Marianne-Light_Italic.woff2" ;
15- import marianneRegularWoff2Url from "./dsfr/fonts/Marianne-Regular.woff2" ;
16- import marianneRegularItalicWoff2Url from "./dsfr/fonts/Marianne-Regular_Italic.woff2" ;
17- import marianneMediumWoff2Url from "./dsfr/fonts/Marianne-Medium.woff2" ;
18- import marianneMediumItalicWoff2Url from "./dsfr/fonts/Marianne-Medium_Italic.woff2" ;
19- import marianneBoldWoff2Url from "./dsfr/fonts/Marianne-Bold.woff2" ;
20- import marianneBoldItalicWoff2Url from "./dsfr/fonts/Marianne-Bold_Italic.woff2" ;
21- import spectralRegularWoff2Url from "./dsfr/fonts/Spectral-Regular.woff2" ;
22- import spectralExtraBoldWoff2Url from "./dsfr/fonts/Spectral-ExtraBold.woff2" ;
14+ import { fontUrlByFileBasename } from "./next-appdir/fontUrlByFileBasename" ;
2315import AppleTouchIcon from "./dsfr/favicon/apple-touch-icon.png" ;
2416import FaviconSvg from "./dsfr/favicon/favicon.svg" ;
2517import FaviconIco from "./dsfr/favicon/favicon.ico" ;
@@ -35,19 +27,6 @@ import "./dsfr/utility/icons/icons.css";
3527
3628export type { RegisterLink , RegisteredLinkProps } ;
3729
38- const fontUrlByFileBasename = {
39- "Marianne-Light" : marianneLightWoff2Url ,
40- "Marianne-Light_Italic" : marianneItalicWoff2Url ,
41- "Marianne-Regular" : marianneRegularWoff2Url ,
42- "Marianne-Regular_Italic" : marianneRegularItalicWoff2Url ,
43- "Marianne-Medium" : marianneMediumWoff2Url ,
44- "Marianne-Medium_Italic" : marianneMediumItalicWoff2Url ,
45- "Marianne-Bold" : marianneBoldWoff2Url ,
46- "Marianne-Bold_Italic" : marianneBoldItalicWoff2Url ,
47- "Spectral-Regular" : spectralRegularWoff2Url ,
48- "Spectral-ExtraBold" : spectralExtraBoldWoff2Url
49- } as const ;
50-
5130export type CreateNextDsfrIntegrationApiParams = {
5231 defaultColorScheme : ColorScheme | "system" ;
5332 /** Default: false */
@@ -199,95 +178,7 @@ export function createNextDsfrIntegrationApi(
199178 ) }
200179 < script
201180 dangerouslySetInnerHTML = { {
202- "__html" : `
203-
204- {
205-
206- window.ssrWasPerformedWithIsDark = "${ defaultColorScheme } " === "dark";
207-
208- const isDark = (() => {
209-
210- const isDarkExplicitlyProvidedAsParameter = (() => {
211- if ("${ defaultColorScheme } " === "system") {
212- return undefined;
213- }
214-
215- switch ("${ defaultColorScheme } ") {
216- case "dark": return true;
217- case "light": return false;
218- }
219- })();
220-
221- const isDarkFromLocalStorage = (() => {
222- const colorSchemeReadFromLocalStorage = localStorage.getItem("scheme");
223-
224- if (colorSchemeReadFromLocalStorage === null) {
225- return undefined;
226- }
227-
228- if (colorSchemeReadFromLocalStorage === "system") {
229- return undefined;
230- }
231-
232- switch (colorSchemeReadFromLocalStorage) {
233- case "dark":
234- return true;
235- case "light":
236- return false;
237- }
238- })();
239-
240- const isDarkFromOsPreference = (() => {
241- if (!window.matchMedia) {
242- return undefined;
243- }
244-
245- return window.matchMedia("(prefers-color-scheme: dark)").matches;
246- })();
247-
248- const isDarkFallback = false;
249-
250- return (
251- isDarkFromLocalStorage ??
252- isDarkExplicitlyProvidedAsParameter ??
253- isDarkFromOsPreference ??
254- isDarkFallback
255- );
256-
257- })();
258-
259- ["${ data_fr_scheme } ", "${ data_fr_theme } "].forEach(attr => document.documentElement.setAttribute(attr, isDark ? "dark" : "light"));
260-
261- {
262-
263- const element = document.createElement("style");
264-
265- element.id = "${ rootColorSchemeStyleTagId } ";
266-
267- element.innerHTML = \`:root { color-scheme: \${isDark ? "dark" : "light"}; }\`;
268-
269- document.head.appendChild(element);
270-
271- }
272-
273- {
274-
275- const element = document.createElement("meta");
276-
277- element.name = "theme-color";
278-
279- element.content = isDark ? "${
280- getColors ( true ) . decisions . background . default . grey . default
281- } " : "${
282- getColors ( false ) . decisions . background . default . grey . default
283- } ";
284-
285- document.head.appendChild(element);
286-
287- }
288-
289- }
290- `
181+ "__html" : getScriptToRunAsap ( defaultColorScheme )
291182 } }
292183 />
293184 </ Head >
0 commit comments