@@ -5,7 +5,7 @@ import DefaultApp from "next/app";
5
5
import type { AppProps , AppContext } from "next/app" ;
6
6
import type { DocumentProps , DocumentContext } from "next/document" ;
7
7
import { startDsfrReact } from "./lib/start" ;
8
- import type { Params as startDsfrReactParams } from "./lib/start" ;
8
+ import type { Params as StartDsfrReactParams } from "./lib/start" ;
9
9
import { isBrowser } from "./lib/tools/isBrowser" ;
10
10
import { objectKeys } from "tsafe/objectKeys" ;
11
11
import marianneLightWoff2Url from "./dsfr/fonts/Marianne-Light.woff2" ;
@@ -40,14 +40,25 @@ const fontUrlByFileBasename = {
40
40
"Spectral-ExtraBold" : spectralExtraBoldWoff2Url
41
41
} as const ;
42
42
43
- export type Params = startDsfrReactParams & {
44
- /** If not provided no fonts are preloaded.
45
- * Preloading of fonts is only enabled in production.
46
- */
47
- preloadFonts ?: ( keyof typeof fontUrlByFileBasename ) [ ] ;
48
- /** Default false */
49
- doPersistDarkModePreferenceWithCookie ?: boolean ;
50
- } ;
43
+ export type Params = Params . WithDocument | Params . WithoutDocument ;
44
+ export namespace Params {
45
+ export type Common = StartDsfrReactParams & {
46
+ /** If not provided no fonts are preloaded.
47
+ * Preloading of fonts is only enabled in production.
48
+ */
49
+ preloadFonts ?: ( keyof typeof fontUrlByFileBasename ) [ ] ;
50
+ /** Default false */
51
+ doPersistDarkModePreferenceWithCookie ?: boolean ;
52
+ } ;
53
+
54
+ export type WithDocument = Common & {
55
+ doPersistDarkModePreferenceWithCookie : true ;
56
+ } ;
57
+
58
+ export type WithoutDocument = Common & {
59
+ doPersistDarkModePreferenceWithCookie ?: false ;
60
+ } ;
61
+ }
51
62
52
63
/*
53
64
const $overwriteGetInitialProps = createStatefulObservable<(() => void) | undefined>(
@@ -82,15 +93,33 @@ function readColorSchemeInCookie(cookie: string) {
82
93
} ) ( ) ;
83
94
}
84
95
85
- export function createDsfrNextIntegrationApi ( params : Params ) {
96
+ export type NextDsfrIntegrationApi = {
97
+ withAppDsfr : < AppComponent extends NextComponentType < any , any , any > > (
98
+ App : AppComponent
99
+ ) => AppComponent ;
100
+ dsfrDocumentApi : {
101
+ augmentDocumentByReadingColorSchemeFromCookie : (
102
+ Document : NextComponentType < any , any , any >
103
+ ) => void ;
104
+ getColorSchemeHtmlAttributes : (
105
+ props : DocumentProps
106
+ ) =>
107
+ | Record < never , unknown >
108
+ | Record < typeof data_fr_scheme | typeof data_fr_theme , ColorScheme > ;
109
+ } ;
110
+ } ;
111
+
112
+ export function createNextDsfrIntegrationApi ( params : Params . WithDocument ) : NextDsfrIntegrationApi ;
113
+ export function createNextDsfrIntegrationApi (
114
+ params : Params . WithoutDocument
115
+ ) : Omit < NextDsfrIntegrationApi , "dsfrDocumentApi" > ;
116
+ export function createNextDsfrIntegrationApi ( params : Params ) : NextDsfrIntegrationApi {
86
117
const {
87
118
preloadFonts = [ ] ,
88
119
doPersistDarkModePreferenceWithCookie = false ,
89
120
...startDsfrReactParams
90
121
} = params ;
91
122
92
- assert ( doPersistDarkModePreferenceWithCookie , "TODO: Support without this mode" ) ;
93
-
94
123
if ( isBrowser ) {
95
124
startDsfrReact ( startDsfrReactParams ) ;
96
125
} else {
@@ -99,11 +128,9 @@ export function createDsfrNextIntegrationApi(params: Params) {
99
128
100
129
const colorSchemeKey = "dsfrColorScheme" ;
101
130
102
- /** the App returned by witAppDsfr should be directly exported default as is */
103
131
function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
104
132
App : AppComponent
105
133
) : AppComponent {
106
- //function AppWithDsfr({ [colorSchemeKey]: colorScheme = "light", ...props }: AppProps & Record<typeof colorSchemeKey, ColorScheme | undefined>) {
107
134
function AppWithDsfr ( {
108
135
[ colorSchemeKey ] : colorScheme ,
109
136
...props
@@ -162,7 +189,7 @@ export function createDsfrNextIntegrationApi(params: Params) {
162
189
staticMethod => ( ( AppWithDsfr as any ) [ staticMethod ] = ( App as any ) [ staticMethod ] )
163
190
) ;
164
191
165
- {
192
+ if ( doPersistDarkModePreferenceWithCookie ) {
166
193
const super_getInitialProps =
167
194
App . getInitialProps ?. bind ( App ) ?? DefaultApp . getInitialProps . bind ( DefaultApp ) ;
168
195
@@ -250,7 +277,9 @@ export function createDsfrNextIntegrationApi(params: Params) {
250
277
} ;
251
278
}
252
279
253
- function getColorSchemeHtmlAttributes ( props : DocumentProps ) {
280
+ function getColorSchemeHtmlAttributes (
281
+ props : DocumentProps
282
+ ) : Record < never , unknown > | Record < typeof data_fr_scheme | typeof data_fr_theme , ColorScheme > {
254
283
const { [ colorSchemeKey ] : colorScheme } = props as DocumentProps &
255
284
Record < typeof colorSchemeKey , ColorScheme | undefined > ;
256
285
0 commit comments