Skip to content

Commit de902dc

Browse files
committed
Respect the preferred colors passed as param
1 parent 9ae5f25 commit de902dc

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/lib/darkMode.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ const useIsDarkServerSide: UseIsDark = () => {
5959
export const useIsDark = isBrowser ? useIsDarkClientSide : useIsDarkServerSide;
6060

6161
function getCurrentIsDarkFromHtmlAttribute(): boolean {
62-
const colorSchemeReadFromHtmlAttribute = document.documentElement.getAttribute(data_fr_theme);
62+
const colorSchemeFromHtmlAttribute = document.documentElement.getAttribute(data_fr_theme);
6363

64-
switch (colorSchemeReadFromHtmlAttribute) {
64+
switch (colorSchemeFromHtmlAttribute) {
6565
case null:
6666
case "light":
6767
return false;

src/next.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ export namespace Params {
6969
};
7070
}
7171

72-
let defaultColorScheme: ColorScheme | "system";
73-
7472
function readColorSchemeInCookie(cookie: string) {
7573
const parsedCookies = Object.fromEntries(
7674
cookie
@@ -130,8 +128,6 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
130128

131129
if (isBrowser) {
132130
startDsfrReact(startDsfrReactParams);
133-
} else {
134-
defaultColorScheme = startDsfrReactParams.defaultColorScheme;
135131
}
136132

137133
if (langIfNoProvider !== undefined) {
@@ -140,19 +136,18 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
140136

141137
const colorSchemeKey = "dsfrColorScheme";
142138

143-
const isDarkFromHtmlAttribute = (() => {
139+
const colorSchemeFromHtmlAttribute = (() => {
144140
if (!isBrowser) {
145141
return undefined;
146142
}
147143

148-
const colorSchemeReadFromHtmlAttribute =
149-
document.documentElement.getAttribute(data_fr_theme);
144+
const colorSchemeFromHtmlAttribute = document.documentElement.getAttribute(data_fr_theme);
150145

151-
if (colorSchemeReadFromHtmlAttribute === null) {
146+
if (colorSchemeFromHtmlAttribute === null) {
152147
return undefined;
153148
}
154149

155-
return colorSchemeReadFromHtmlAttribute as ColorScheme;
150+
return colorSchemeFromHtmlAttribute as ColorScheme;
156151
})();
157152

158153
function withDsfr<AppComponent extends NextComponentType<any, any, any>>(
@@ -163,7 +158,16 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
163158
...props
164159
}: AppProps & Record<typeof colorSchemeKey, ColorScheme | undefined>) {
165160
if (colorScheme === undefined) {
166-
colorScheme = isBrowser ? isDarkFromHtmlAttribute ?? "light" : "light";
161+
const colorSchemeExplicitlyProvidedAsParameter =
162+
startDsfrReactParams.defaultColorScheme === "system"
163+
? undefined
164+
: startDsfrReactParams.defaultColorScheme;
165+
166+
colorScheme = isBrowser
167+
? colorSchemeFromHtmlAttribute ??
168+
colorSchemeExplicitlyProvidedAsParameter ??
169+
"light"
170+
: colorSchemeExplicitlyProvidedAsParameter ?? "light";
167171
}
168172

169173
useEffect(() => {
@@ -276,10 +280,10 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
276280
const colorScheme =
277281
(cookie === undefined ? undefined : readColorSchemeInCookie(cookie)) ??
278282
(() => {
279-
switch (defaultColorScheme) {
283+
switch (startDsfrReactParams.defaultColorScheme) {
280284
case "light":
281285
case "dark":
282-
return defaultColorScheme;
286+
return startDsfrReactParams.defaultColorScheme;
283287
case "system":
284288
return undefined;
285289
}

0 commit comments

Comments
 (0)