mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-18 11:36:36 +00:00
24 lines
647 B
Svelte
24 lines
647 B
Svelte
<script>
|
|
export let persist = false;
|
|
export let persistKey = "carbon-theme";
|
|
export const themes = ["white", "g10", "g90", "g100"];
|
|
|
|
import { onMount, afterUpdate } from "svelte";
|
|
import { theme } from "../store";
|
|
|
|
const isValidTheme = (value) => themes.includes(value);
|
|
|
|
onMount(() => {
|
|
const persisted_theme = localStorage.getItem(persistKey);
|
|
if (isValidTheme(persisted_theme)) theme.set(persisted_theme);
|
|
});
|
|
|
|
afterUpdate(() => {
|
|
if (isValidTheme($theme)) {
|
|
document.documentElement.setAttribute("theme", $theme);
|
|
if (persist) localStorage.setItem(persistKey, $theme);
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<slot />
|