@@ -5,7 +5,7 @@ import DefaultApp from "next/app";
55import type { AppProps , AppContext } from "next/app" ;
66import type { DocumentProps , DocumentContext } from "next/document" ;
77import { startDsfrReact } from "./lib/start" ;
8- import type { Params as startDsfrReactParams } from "./lib/start" ;
8+ import type { Params as StartDsfrReactParams } from "./lib/start" ;
99import { isBrowser } from "./lib/tools/isBrowser" ;
1010import { objectKeys } from "tsafe/objectKeys" ;
1111import marianneLightWoff2Url from "./dsfr/fonts/Marianne-Light.woff2" ;
@@ -40,14 +40,25 @@ const fontUrlByFileBasename = {
4040 "Spectral-ExtraBold" : spectralExtraBoldWoff2Url
4141} as const ;
4242
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+ }
5162
5263/*
5364const $overwriteGetInitialProps = createStatefulObservable<(() => void) | undefined>(
@@ -82,15 +93,33 @@ function readColorSchemeInCookie(cookie: string) {
8293 } ) ( ) ;
8394}
8495
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 {
86117 const {
87118 preloadFonts = [ ] ,
88119 doPersistDarkModePreferenceWithCookie = false ,
89120 ...startDsfrReactParams
90121 } = params ;
91122
92- assert ( doPersistDarkModePreferenceWithCookie , "TODO: Support without this mode" ) ;
93-
94123 if ( isBrowser ) {
95124 startDsfrReact ( startDsfrReactParams ) ;
96125 } else {
@@ -99,11 +128,9 @@ export function createDsfrNextIntegrationApi(params: Params) {
99128
100129 const colorSchemeKey = "dsfrColorScheme" ;
101130
102- /** the App returned by witAppDsfr should be directly exported default as is */
103131 function withAppDsfr < AppComponent extends NextComponentType < any , any , any > > (
104132 App : AppComponent
105133 ) : AppComponent {
106- //function AppWithDsfr({ [colorSchemeKey]: colorScheme = "light", ...props }: AppProps & Record<typeof colorSchemeKey, ColorScheme | undefined>) {
107134 function AppWithDsfr ( {
108135 [ colorSchemeKey ] : colorScheme ,
109136 ...props
@@ -162,7 +189,7 @@ export function createDsfrNextIntegrationApi(params: Params) {
162189 staticMethod => ( ( AppWithDsfr as any ) [ staticMethod ] = ( App as any ) [ staticMethod ] )
163190 ) ;
164191
165- {
192+ if ( doPersistDarkModePreferenceWithCookie ) {
166193 const super_getInitialProps =
167194 App . getInitialProps ?. bind ( App ) ?? DefaultApp . getInitialProps . bind ( DefaultApp ) ;
168195
@@ -250,7 +277,9 @@ export function createDsfrNextIntegrationApi(params: Params) {
250277 } ;
251278 }
252279
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 > {
254283 const { [ colorSchemeKey ] : colorScheme } = props as DocumentProps &
255284 Record < typeof colorSchemeKey , ColorScheme | undefined > ;
256285
0 commit comments