mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
chore(examples): extract Theme component and persist to localStorage
This commit is contained in:
parent
67c01e9148
commit
f9b2f69966
3 changed files with 64 additions and 3 deletions
|
@ -15,10 +15,10 @@
|
|||
import Header from "./components/Header.svelte";
|
||||
import Theme from "./components/Theme.svelte";
|
||||
|
||||
let theme = "white";
|
||||
let theme = "g10";
|
||||
</script>
|
||||
|
||||
<Theme theme="{theme}">
|
||||
<Theme persist bind:theme>
|
||||
<Header />
|
||||
<Content style="background: none; padding: 1rem">
|
||||
<Grid>
|
||||
|
|
|
@ -8,6 +8,19 @@
|
|||
import Notification20 from "carbon-icons-svelte/lib/Notification20";
|
||||
import UserAvatar20 from "carbon-icons-svelte/lib/UserAvatar20";
|
||||
import AppSwitcher20 from "carbon-icons-svelte/lib/AppSwitcher20";
|
||||
import { getContext } from "svelte";
|
||||
|
||||
const ctx = getContext("Theme");
|
||||
|
||||
$: if (ctx) {
|
||||
ctx.dark.subscribe((value) => {
|
||||
console.log("dark mode?", value);
|
||||
});
|
||||
ctx.light.subscribe((value) => {
|
||||
console.log("light mode?", value);
|
||||
});
|
||||
ctx.updateVar("--cds-productive-heading-06-font-size", "4rem");
|
||||
}
|
||||
</script>
|
||||
|
||||
<Header company="IBM" platformName="Carbon Components Svelte" href="/">
|
||||
|
|
|
@ -1,7 +1,55 @@
|
|||
<script>
|
||||
export let persist = false;
|
||||
export let persistKey = "theme";
|
||||
export let theme = "white";
|
||||
export const themes = ["white", "g10", "g90", "g100"];
|
||||
|
||||
$: document.documentElement.setAttribute("theme", theme);
|
||||
import { onMount, afterUpdate, setContext } from "svelte";
|
||||
import { writable, derived } from "svelte/store";
|
||||
|
||||
const isValidTheme = (value) => themes.includes(value);
|
||||
const isDark = (value) =>
|
||||
isValidTheme(value) && (value === "g90" || value === "g100");
|
||||
|
||||
const dark = writable(isDark(theme));
|
||||
const light = derived(dark, (_) => !_);
|
||||
|
||||
setContext("Theme", {
|
||||
updateVar: (name, value) => {
|
||||
document.documentElement.style.setProperty(name, value);
|
||||
},
|
||||
dark,
|
||||
light,
|
||||
});
|
||||
|
||||
onMount(() => {
|
||||
try {
|
||||
const persisted_theme = localStorage.getItem(persistKey);
|
||||
|
||||
if (isValidTheme(persisted_theme)) {
|
||||
theme = persisted_theme;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
});
|
||||
|
||||
afterUpdate(() => {
|
||||
if (isValidTheme(theme)) {
|
||||
document.documentElement.setAttribute("theme", theme);
|
||||
if (persist) {
|
||||
localStorage.setItem(persistKey, theme);
|
||||
}
|
||||
} else {
|
||||
console.warn(
|
||||
`"${theme}" is not a valid Carbon theme. Choose from available themes: ${JSON.stringify(
|
||||
themes
|
||||
)}`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
$: dark.set(isDark(theme));
|
||||
</script>
|
||||
|
||||
<slot />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue