fix: fix issue (caused by svelte5 migration) where colour scheme preference wasn't persisting
This commit is contained in:
parent
2fecf0c540
commit
be256f7ed3
3 changed files with 21 additions and 22 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { tweened } from 'svelte/motion'
|
import { tweened } from "svelte/motion";
|
||||||
import { expoOut } from 'svelte/easing'
|
import { expoOut } from "svelte/easing";
|
||||||
import {
|
import {
|
||||||
colourSchemeStore,
|
colourSchemeStore,
|
||||||
lightColourScheme,
|
lightColourScheme,
|
||||||
|
|
@ -19,13 +19,13 @@
|
||||||
|
|
||||||
const iconMovement = tweened(0, {
|
const iconMovement = tweened(0, {
|
||||||
duration: 300,
|
duration: 300,
|
||||||
easing: expoOut
|
easing: expoOut,
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<nav>
|
<nav>
|
||||||
<div class="left">
|
<div class="left">
|
||||||
<a href="/" class="home">Thomas Wilson</a>
|
<a href="/" class="home">TWC</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="right">
|
<div class="right">
|
||||||
|
|
@ -37,7 +37,6 @@
|
||||||
width="24"
|
width="24"
|
||||||
height="24"
|
height="24"
|
||||||
style="transform: rotate({$iconMovement}deg)"
|
style="transform: rotate({$iconMovement}deg)"
|
||||||
|
|
||||||
/>
|
/>
|
||||||
</button>
|
</button>
|
||||||
<a href="/blog" class="blog">/blog</a>
|
<a href="/blog" class="blog">/blog</a>
|
||||||
|
|
|
||||||
|
|
@ -5,24 +5,31 @@
|
||||||
colourSchemes,
|
colourSchemes,
|
||||||
darkColourScheme,
|
darkColourScheme,
|
||||||
lightColourScheme,
|
lightColourScheme,
|
||||||
} from "../stores/colourSchemeStore.ts";
|
} from "../stores/colourSchemeStore.js";
|
||||||
import { browser } from "$app/environment";
|
import { browser } from "$app/environment";
|
||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
interface Props {
|
interface Props {
|
||||||
children?: import('svelte').Snippet;
|
children?: import("svelte").Snippet;
|
||||||
}
|
}
|
||||||
|
|
||||||
let { children }: Props = $props();
|
let { children }: Props = $props();
|
||||||
|
|
||||||
onMount(() => {
|
$effect(() => {
|
||||||
const prefersDarkmode: boolean = window.matchMedia(
|
const prefersDarkmode: boolean = window.matchMedia(
|
||||||
"(prefers-color-scheme: dark)"
|
"(prefers-color-scheme: dark)"
|
||||||
).matches;
|
).matches;
|
||||||
|
|
||||||
const colourSchemeName: string | null =
|
const colourSchemeName: string | null =
|
||||||
localStorage.getItem("colourScheme");
|
localStorage.getItem("colourScheme");
|
||||||
|
|
||||||
const colourScheme = colourSchemes?.[colourSchemeName];
|
const colourScheme = colourSchemes?.[colourSchemeName];
|
||||||
|
|
||||||
|
console.log({
|
||||||
|
prefersDarkmode,
|
||||||
|
colourSchemeName,
|
||||||
|
colourScheme,
|
||||||
|
});
|
||||||
|
|
||||||
if (colourScheme) {
|
if (colourScheme) {
|
||||||
colourSchemeStore.set(colourScheme);
|
colourSchemeStore.set(colourScheme);
|
||||||
} else if (prefersDarkmode) {
|
} else if (prefersDarkmode) {
|
||||||
|
|
@ -40,10 +47,6 @@
|
||||||
value.background
|
value.background
|
||||||
);
|
);
|
||||||
|
|
||||||
// document.documentElement.style.setProperty(
|
|
||||||
// "--colour-scheme-back""
|
|
||||||
// )
|
|
||||||
|
|
||||||
document.documentElement.style.setProperty(
|
document.documentElement.style.setProperty(
|
||||||
"--colour-scheme-text",
|
"--colour-scheme-text",
|
||||||
value.text
|
value.text
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import { writable } from "svelte/store";
|
import { writable } from 'svelte/store';
|
||||||
import { browser } from '$app/environment'
|
|
||||||
|
|
||||||
type ColourSchemeName = 'light' | 'dark';
|
type ColourSchemeName = 'light' | 'dark';
|
||||||
|
|
||||||
|
|
@ -16,7 +15,7 @@ export const lightColourScheme: ColourScheme = {
|
||||||
background: 'white',
|
background: 'white',
|
||||||
backgroundAccent: '#f8f9fa',
|
backgroundAccent: '#f8f9fa',
|
||||||
text: '#212529',
|
text: '#212529',
|
||||||
textAccent: '#495057'
|
textAccent: '#495057',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const darkColourScheme: ColourScheme = {
|
export const darkColourScheme: ColourScheme = {
|
||||||
|
|
@ -24,14 +23,12 @@ export const darkColourScheme: ColourScheme = {
|
||||||
background: '#212529',
|
background: '#212529',
|
||||||
backgroundAccent: '#343a40',
|
backgroundAccent: '#343a40',
|
||||||
text: '#f8f9fa',
|
text: '#f8f9fa',
|
||||||
textAccent: '#ced4da'
|
textAccent: '#ced4da',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const colourSchemes: Record<ColourSchemeName, ColourScheme> = {
|
export const colourSchemes: Record<ColourSchemeName, ColourScheme> = {
|
||||||
light: lightColourScheme,
|
light: lightColourScheme,
|
||||||
dark: darkColourScheme
|
dark: darkColourScheme,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const colourSchemeStore = writable<ColourScheme>(lightColourScheme);
|
export const colourSchemeStore = writable<ColourScheme>(lightColourScheme);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue