Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support shadow dom #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/mode-watcher/src/lib/mode-watcher-full.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
export let trueNonce: string = "";
export let initConfig: Parameters<typeof setInitialMode>[0];
export let themeColors: ThemeColors = undefined;

$: window.__modeWatcherTo = initConfig.to;
</script>

<svelte:head>
Expand Down
2 changes: 2 additions & 0 deletions packages/mode-watcher/src/lib/mode-watcher.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
export let themeStorageKey: string = "mode-watcher-theme";
export let modeStorageKey: string = "mode-watcher-mode";
export let disableHeadScriptInjection = false;
export let to: ModeWatcherProps["to"] = document.documentElement;

$: disableTransitionsStore.set(disableTransitions);
$: themeColorsStore.set(themeColors);
Expand Down Expand Up @@ -67,6 +68,7 @@
defaultTheme,
modeStorageKey,
themeStorageKey,
to,
});

$: trueNonce = typeof window === "undefined" ? nonce : "";
Expand Down
9 changes: 8 additions & 1 deletion packages/mode-watcher/src/lib/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ type SetInitialModeArgs = {
defaultTheme?: string;
modeStorageKey?: string;
themeStorageKey?: string;
to?: HTMLElement | HTMLHtmlElement | null;
};

declare global {
interface Window {
__modeWatcherTo?: HTMLElement | HTMLHtmlElement | null;
}
}

/** Used to set the mode on initial page load to prevent FOUC */
export function setInitialMode({
defaultMode = "system",
Expand All @@ -56,7 +63,7 @@ export function setInitialMode({
modeStorageKey = "mode-watcher-mode",
themeStorageKey = "mode-watcher-theme",
}: SetInitialModeArgs) {
const rootEl = document.documentElement;
const rootEl = window.__modeWatcherTo ?? document.documentElement;
const mode = localStorage.getItem(modeStorageKey) || defaultMode;
const theme = localStorage.getItem(themeStorageKey) || defaultTheme;
const light =
Expand Down
4 changes: 2 additions & 2 deletions packages/mode-watcher/src/lib/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function createDerivedMode() {
const sanitizedLightClassNames = sanitizeClassNames($lightClassNames);

function update() {
const htmlEl = document.documentElement;
const htmlEl = window.__modeWatcherTo ?? document.documentElement;
const themeColorEl = document.querySelector('meta[name="theme-color"]');
if (derivedMode === "light") {
if (sanitizedDarkClassNames.length)
Expand Down Expand Up @@ -251,7 +251,7 @@ function createDerivedTheme() {
if (!isBrowser) return undefined;

function update() {
const htmlEl = document.documentElement;
const htmlEl = window.__modeWatcherTo ?? document.documentElement;
htmlEl.setAttribute("data-theme", $theme);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/mode-watcher/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export type ModeWatcherProps = {
*/
themeStorageKey?: string;

/**
* The element to apply the mode to.
*
* @defaultValue `document.documentElement`
*/
to?: HTMLElement | HTMLHtmlElement | null;

/**
* An optional nonce to use for the injected script tag to allow-list mode-watcher
* if you are using a Content Security Policy.
Expand Down
Loading