Skip to content

Commit d71aea8

Browse files
committed
feat: Add nonce to support CSP
1 parent 90c4619 commit d71aea8

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

lib/src/client/core/core.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import { useEffect } from "react";
55
export interface CoreProps {
66
/** force apply CSS transition property to all the elements during theme switching. E.g., `all .3s` */
77
t?: string;
8+
/** The nonce value for your Content Security Policy. */
9+
nonce?: string;
810
}
911

1012
/** Modify transition globally to avoid patched transitions */
11-
const modifyTransition = (themeTransition = "none") => {
13+
const modifyTransition = (themeTransition = "none", nonce = "") => {
1214
const css = document.createElement("style");
1315
/** split by ';' to prevent CSS injection */
1416
css.textContent = `*{transition:${themeTransition.split(";")[0]} !important;}`;
17+
nonce && css.setAttribute("nonce", nonce);
1518
document.head.appendChild(css);
1619

1720
return () => {
@@ -33,7 +36,7 @@ const modifyTransition = (themeTransition = "none") => {
3336
*
3437
* @source - Source code
3538
*/
36-
export const Core = ({ t }: CoreProps) => {
39+
export const Core = ({ t, nonce }: CoreProps) => {
3740
const [{ m: mode, s: systemMode }, setThemeState] = useStore();
3841
const resolvedMode = mode === SYSTEM ? systemMode : mode; // resolvedMode is the actual mode that will be used
3942

@@ -58,7 +61,7 @@ export const Core = ({ t }: CoreProps) => {
5861
}, []);
5962

6063
useEffect(() => {
61-
const restoreTransitions = modifyTransition(t);
64+
const restoreTransitions = modifyTransition(t, nonce);
6265
const serverTargetEl = document.querySelector("[data-ndm]");
6366
// We need to always update documentElement to support Tailwind configuration
6467
// skipcq: JS-D008, JS-0042 -> map keyword is shorter
@@ -79,7 +82,7 @@ export const Core = ({ t }: CoreProps) => {
7982
localStorage.setItem(COOKIE_KEY, mode);
8083
if (serverTargetEl)
8184
document.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=31536000;SameSite=Strict;`;
82-
}, [resolvedMode, systemMode, mode, t]);
85+
}, [resolvedMode, systemMode, mode, t, nonce]);
8386

8487
return null;
8588
};

0 commit comments

Comments
 (0)