Skip to content

Commit b053790

Browse files
committed
Take into account defaultColorScheme
1 parent 9b0f1aa commit b053790

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/lib/nextJs.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const $overwriteGetInitialProps = createStatefulObservable<(() => void) | undefi
5151
() => undefined
5252
);
5353

54+
let defaultColorScheme: ColorScheme | "system";
55+
5456
export function getColorSchemeSsrUtils() {
5557
$overwriteGetInitialProps.current?.();
5658

@@ -63,7 +65,17 @@ export function getColorSchemeSsrUtils() {
6365
function readColorSchemeFromCookie(ctx: DocumentContext) {
6466
const cookie = ctx.req?.headers.cookie;
6567

66-
colorScheme = cookie === undefined ? undefined : readColorSchemeInCookie(cookie);
68+
colorScheme =
69+
(cookie === undefined ? undefined : readColorSchemeInCookie(cookie)) ??
70+
(() => {
71+
switch (defaultColorScheme) {
72+
case "light":
73+
case "dark":
74+
return defaultColorScheme;
75+
case "system":
76+
return undefined;
77+
}
78+
})();
6779

6880
isNextTickCleared = false;
6981

@@ -107,7 +119,10 @@ export function withAppDsfr<AppComponent extends NextComponentType<any, any, any
107119

108120
if (isBrowser) {
109121
startDsfrReact(startDsfrReactParams);
122+
} else {
123+
defaultColorScheme = startDsfrReactParams.defaultColorScheme;
110124
}
125+
111126
function AppWithDsfr(props: AppProps) {
112127
return (
113128
<>
@@ -133,6 +148,7 @@ export function withAppDsfr<AppComponent extends NextComponentType<any, any, any
133148
href={faviconWebmanifestUrl}
134149
crossOrigin="use-credentials"
135150
/>
151+
<style>{`:root { color-scheme: ${$colorScheme.current}; }`}</style>
136152
</Head>
137153
<App {...(props as any)} />
138154
</>
@@ -155,7 +171,16 @@ export function withAppDsfr<AppComponent extends NextComponentType<any, any, any
155171
const cookie = appContext.ctx.req?.headers.cookie;
156172

157173
return cookie === undefined ? undefined : readColorSchemeInCookie(cookie);
158-
})() ?? "light";
174+
})() ??
175+
(() => {
176+
switch (startDsfrReactParams.defaultColorScheme) {
177+
case "dark":
178+
case "light":
179+
return startDsfrReactParams.defaultColorScheme;
180+
case "system":
181+
return "light";
182+
}
183+
})();
159184
}
160185

161186
return { ...initialProps };

0 commit comments

Comments
 (0)