@@ -69,8 +69,6 @@ export namespace Params {
6969 } ;
7070}
7171
72- let defaultColorScheme : ColorScheme | "system" ;
73-
7472function 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