Skip to content

Commit f4f958d

Browse files
committed
Fix initial render client side Next
1 parent 8466394 commit f4f958d

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
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 colorSchemeReadFromDom = document.documentElement.getAttribute(data_fr_theme);
62+
const colorSchemeReadFromHtmlAttribute = document.documentElement.getAttribute(data_fr_theme);
6363

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

src/next.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
140140

141141
const colorSchemeKey = "dsfrColorScheme";
142142

143+
const isDarkFromHtmlAttribute = (() => {
144+
if (!isBrowser) {
145+
return undefined;
146+
}
147+
148+
const colorSchemeReadFromHtmlAttribute =
149+
document.documentElement.getAttribute(data_fr_theme);
150+
151+
if (colorSchemeReadFromHtmlAttribute === null) {
152+
return undefined;
153+
}
154+
155+
return colorSchemeReadFromHtmlAttribute as ColorScheme;
156+
})();
157+
143158
function withDsfr<AppComponent extends NextComponentType<any, any, any>>(
144159
App: AppComponent
145160
): AppComponent {
@@ -148,7 +163,7 @@ export function createNextDsfrIntegrationApi(params: Params): NextDsfrIntegratio
148163
...props
149164
}: AppProps & Record<typeof colorSchemeKey, ColorScheme | undefined>) {
150165
if (colorScheme === undefined) {
151-
colorScheme = "light";
166+
colorScheme = isBrowser ? isDarkFromHtmlAttribute ?? "light" : "light";
152167
}
153168

154169
useEffect(() => {

0 commit comments

Comments
 (0)