mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-17 11:11:25 +00:00
* docs: remove .bx--content override * docs(ui-shell): add note on UI Shell theming * docs(grid): touch up grid examples * docs(theme): wrap localStorage in try catch So that the site still works in Safari with all cookies blocked. * docs: use prefixed all.css Without vendor prefixes, some styles are lost in Safari. * docs: remove svelte-preprocess from svelte.config.js
30 lines
744 B
Svelte
30 lines
744 B
Svelte
<script>
|
|
export let persist = false;
|
|
export let persistKey = "carbon-theme";
|
|
export const themes = ["white", "g10", "g80", "g90", "g100"];
|
|
|
|
import { onMount, afterUpdate } from "svelte";
|
|
import { theme } from "../store";
|
|
|
|
const isValidTheme = (value) => themes.includes(value);
|
|
|
|
onMount(() => {
|
|
try {
|
|
const persisted_theme = localStorage.getItem(persistKey);
|
|
if (isValidTheme(persisted_theme)) theme.set(persisted_theme);
|
|
} catch (e) {}
|
|
});
|
|
|
|
afterUpdate(() => {
|
|
if (isValidTheme($theme)) {
|
|
document.documentElement.setAttribute("theme", $theme);
|
|
if (persist) {
|
|
try {
|
|
localStorage.setItem(persistKey, $theme);
|
|
} catch (e) {}
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<slot />
|