UNPKG

2.13 kBPlain TextView Raw
1import { start } from "./start";
2import type { RegisterLink, RegisteredLinkProps } from "./link";
3import { setLink } from "./link";
4import { setUseLang } from "./i18n";
5import type { ColorScheme } from "./useIsDark";
6import { assert } from "tsafe/assert";
7
8export type { RegisterLink, RegisteredLinkProps };
9
10export function startReactDsfr(params: {
11 defaultColorScheme: ColorScheme | "system";
12 /** Default: false */
13 verbose?: boolean;
14 /** Default: <a /> */
15 Link?: Function;
16 /** Default: ()=> "fr" */
17 useLang?: () => string;
18 /**
19 * When set, the value will be used as the nonce attribute of subsequent script tags.
20 *
21 * @see https://developer.mozilla.org/fr/docs/Web/HTML/Global_attributes/nonce
22 */
23 nonce?: string;
24 /**
25 * Enable Trusted Types with a custom policy name.
26 *
27 * `<trustedTypesPolicyName>` and `<trustedTypesPolicyName>-asap` should be set in your Content-Security-Policy header.
28 *
29 * For example:
30 * ```txt
31 * With a policy name of "react-dsfr":
32 * Content-Security-Policy:
33 * require-trusted-types-for 'script';
34 * trusted-types react-dsfr react-dsfr-asap nextjs nextjs#bundler;
35 * ```
36 *
37 * @see https://developer.mozilla.org/fr/docs/Web/HTTP/Headers/Content-Security-Policy/trusted-types
38 * @see {@link DEFAULT_TRUSTED_TYPES_POLICY_NAME}
39 * @default "react-dsfr"
40 */
41 trustedTypesPolicyName?: string;
42}) {
43 const {
44 defaultColorScheme,
45 verbose = false,
46 Link,
47 useLang,
48 nonce,
49 trustedTypesPolicyName = "react-dsfr"
50 } = params;
51
52 if (Link !== undefined) {
53 setLink({ "Link": Link as any });
54 }
55
56 if (useLang !== undefined) {
57 setUseLang({ useLang });
58 }
59
60 assert(nonce !== "", "nonce cannot be an empty string");
61
62 const doCheckNonce = nonce !== undefined;
63 if (doCheckNonce) {
64 window.ssrNonce = nonce;
65 }
66
67 start({
68 defaultColorScheme,
69 verbose,
70 doCheckNonce,
71 trustedTypesPolicyName,
72 "nextParams": undefined
73 });
74}
75
76export { setUseLang };