@codegouvfr/react-dsfr
Version:
French State Design System React integration library
48 lines (35 loc) • 1.38 kB
text/typescript
import { isBrowser } from "./tools/isBrowser";
import { assert } from "tsafe/assert";
import type { ColorScheme } from "./useIsDark";
import { startClientSideIsDarkLogic } from "./useIsDark/client";
type Params = {
defaultColorScheme: ColorScheme | "system";
verbose: boolean;
nextParams:
| {
doPersistDarkModePreferenceWithCookie: boolean;
registerEffectAction: (effect: () => void) => void;
}
| undefined;
};
let isStarted = false;
export async function start(params: Params) {
const { defaultColorScheme, verbose, nextParams } = params;
assert(isBrowser);
if (isStarted) {
return;
}
isStarted = true;
const registerEffectAction: (action: () => void) => void =
nextParams === undefined ? action => action() : nextParams.registerEffectAction;
startClientSideIsDarkLogic({
"colorSchemeExplicitlyProvidedAsParameter": defaultColorScheme,
"doPersistDarkModePreferenceWithCookie":
nextParams === undefined ? false : nextParams.doPersistDarkModePreferenceWithCookie,
registerEffectAction
});
(window as any).dsfr = { verbose, "mode": "react" };
await import("./dsfr/dsfr.module" as any);
const { dsfr } = window as unknown as { dsfr: { start: () => void } };
registerEffectAction(() => dsfr.start());
}