UNPKG

1.73 kBPlain TextView Raw
1import { isBrowser } from "./tools/isBrowser";
2import { assert } from "tsafe/assert";
3import type { ColorScheme } from "./useIsDark";
4import { startClientSideIsDarkLogic } from "./useIsDark/client";
5import { Deferred } from "./tools/Deferred";
6
7type Params = {
8 defaultColorScheme: ColorScheme | "system";
9 verbose: boolean;
10 nextParams:
11 | {
12 doPersistDarkModePreferenceWithCookie: boolean;
13 registerEffectAction: (effect: () => void) => void;
14 }
15 | undefined;
16 doCheckNonce: boolean;
17 trustedTypesPolicyName: string;
18};
19
20let isStarted = false;
21
22export async function start(params: Params) {
23 const { defaultColorScheme, verbose, nextParams, doCheckNonce, trustedTypesPolicyName } =
24 params;
25
26 assert(isBrowser);
27
28 if (isStarted) {
29 return;
30 }
31
32 isStarted = true;
33
34 const registerEffectAction: (action: () => void) => void =
35 nextParams === undefined ? action => action() : nextParams.registerEffectAction;
36
37 startClientSideIsDarkLogic({
38 "colorSchemeExplicitlyProvidedAsParameter": defaultColorScheme,
39 "doPersistDarkModePreferenceWithCookie":
40 nextParams === undefined ? false : nextParams.doPersistDarkModePreferenceWithCookie,
41 registerEffectAction,
42 doCheckNonce,
43 trustedTypesPolicyName
44 });
45
46 // @ts-expect-error
47 window.dsfr = {
48 verbose,
49 "mode": "react"
50 };
51
52 // @ts-expect-error
53 await import("./dsfr/dsfr.module.min");
54
55 dDsfrLoaded.resolve();
56
57 registerEffectAction(() => {
58 // @ts-expect-error
59 window.dsfr.start();
60 });
61}
62
63const dDsfrLoaded = new Deferred<void>();
64
65export const prDsfrLoaded = dDsfrLoaded.pr;