File tree Expand file tree Collapse file tree 13 files changed +104
-68
lines changed Expand file tree Collapse file tree 13 files changed +104
-68
lines changed Original file line number Diff line number Diff line change 19
19
/>
20
20
21
21
< script >
22
- document . documentElement . classList . add (
23
- JSON . parse ( localStorage . getItem ( 'svelte:theme' ) ) ?. current ??
24
- ( window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ? 'dark' : 'light' )
25
- ) ;
26
-
27
22
document . documentElement . classList . add (
28
23
`font-${ localStorage . getItem ( 'svelte:font' ) ?? 'elegant' } `
29
24
) ;
Original file line number Diff line number Diff line change 5
5
import { afterNavigate , goto , replaceState } from ' $app/navigation' ;
6
6
import type { Gist } from ' $lib/db/types' ;
7
7
import { Repl } from ' @sveltejs/repl' ;
8
- import { theme } from ' @sveltejs/site-kit/stores ' ;
8
+ import { theme } from ' @sveltejs/site-kit/state ' ;
9
9
import { mapbox_setup } from ' ../../../../config.js' ;
10
10
import AppControls from ' ./AppControls.svelte' ;
11
11
import { compress_and_encode_text , decode_and_decompress_text } from ' ./gzip.js' ;
240
240
injectedJS ={mapbox_setup }
241
241
{onchange }
242
242
{download }
243
- previewTheme ={$ theme .current }
243
+ previewTheme ={theme .current }
244
244
/>
245
245
</div >
246
246
{/if }
Original file line number Diff line number Diff line change 1
1
<script lang =" ts" >
2
2
import { browser } from ' $app/environment' ;
3
3
import { afterNavigate , replaceState } from ' $app/navigation' ;
4
- import { theme } from ' @sveltejs/site-kit/stores ' ;
4
+ import { theme } from ' @sveltejs/site-kit/state ' ;
5
5
import { Repl } from ' @sveltejs/repl' ;
6
6
import { mapbox_setup } from ' ../../../../../config.js' ;
7
7
import { page } from ' $app/state' ;
50
50
{relaxed }
51
51
can _escape
52
52
injectedJS ={mapbox_setup }
53
- previewTheme ={$ theme .current }
53
+ previewTheme ={theme .current }
54
54
embedded
55
55
/>
56
56
{/if }
Original file line number Diff line number Diff line change 5
5
<script lang =" ts" >
6
6
import { browser , dev } from ' $app/environment' ;
7
7
import { afterNavigate } from ' $app/navigation' ;
8
- import { theme , type Theme } from ' @sveltejs/site-kit/stores ' ;
8
+ import { theme } from ' @sveltejs/site-kit/state ' ;
9
9
import { onMount } from ' svelte' ;
10
10
import Chrome from ' ./Chrome.svelte' ;
11
11
import Loading from ' ./Loading.svelte' ;
43
43
clearTimeout (timeout );
44
44
});
45
45
46
- function change_theme(theme : Theme ) {
47
- if (! iframe ) return ;
48
-
49
- try {
50
- const url = new URL (iframe .src );
51
-
52
- url .searchParams .set (' theme' , theme .current );
53
-
54
- iframe .src = url .href ;
55
- } catch {}
56
- }
57
-
58
46
let timeout: any ;
59
47
60
48
async function handle_message(e : MessageEvent ) {
97
85
parentNode ?.removeChild (iframe );
98
86
99
87
const url = new URL (src );
100
- url .searchParams .set (' theme' , $ theme .current );
88
+ url .searchParams .set (' theme' , theme .current );
101
89
102
90
iframe .src = url .href ;
103
91
parentNode ?.appendChild (iframe );
112
100
$effect (() => {
113
101
if (adapter_state .base ) set_iframe_src (adapter_state .base + (path = exercise .path ));
114
102
});
103
+
115
104
$effect (() => {
116
- change_theme ($theme );
105
+ if (! iframe ) return ;
106
+
107
+ try {
108
+ const url = new URL (iframe .src );
109
+
110
+ url .searchParams .set (' theme' , theme .current );
111
+
112
+ iframe .src = url .href ;
113
+ } catch {}
117
114
});
118
115
</script >
119
116
Original file line number Diff line number Diff line change 4
4
import Viewer from ' @sveltejs/repl/viewer' ;
5
5
// @ts-expect-error TODO types
6
6
import Console , { type Log } from ' @sveltejs/repl/console' ;
7
- import { theme } from ' @sveltejs/site-kit/stores ' ;
7
+ import { theme } from ' @sveltejs/site-kit/state ' ;
8
8
import Chrome from ' ./Chrome.svelte' ;
9
9
import Loading from ' ./Loading.svelte' ;
10
10
import { adapter_state , update } from ' ./adapter.svelte' ;
37
37
can _escape
38
38
onLog ={(l : Log []) => (logs = l )}
39
39
{bundle }
40
- theme ={$ theme .current }
40
+ theme ={theme .current }
41
41
injectedCSS =" @import '/tutorial/shared.css';"
42
42
/>
43
43
{/if }
Original file line number Diff line number Diff line change 5
5
let { runes }: { runes: boolean } = $props ();
6
6
7
7
const { workspace, svelteVersion } = get_repl_context ();
8
- const majorVersion = Number (svelteVersion .split (" . " )[0 ]);
8
+ const majorVersion = Number (svelteVersion .split (' . ' )[0 ]);
9
9
</script >
10
10
11
11
<Dropdown align =" right" >
19
19
<div class =" popup" >
20
20
{#if Number .isInteger (majorVersion ) && majorVersion < 5 }
21
21
<p >
22
- <a href =" /blog/runes" >Runes</a > are available from Svelte 5 onwards, and this playground is
23
- using Svelte {svelteVersion }.
22
+ <a href =" /blog/runes" >Runes</a > are available from Svelte 5 onwards, and this playground
23
+ is using Svelte {svelteVersion }.
24
24
</p >
25
25
{:else if workspace .current .name .endsWith (' .svelte.js' )}
26
26
<p >
Original file line number Diff line number Diff line change 73
73
"./actions" : {
74
74
"default" : " ./src/lib/actions/index.ts"
75
75
},
76
+ "./state" : {
77
+ "default" : " ./src/lib/state/index.ts"
78
+ },
76
79
"./stores" : {
77
80
"default" : " ./src/lib/stores/index.ts"
78
81
},
Original file line number Diff line number Diff line change 1
1
<script lang =" ts" >
2
- import { on } from ' svelte/events' ;
3
- import { theme } from ' ../stores' ;
2
+ import { theme } from ' ../state' ;
4
3
5
4
function toggle() {
6
- const next = $theme .current === ' light' ? ' dark' : ' light' ;
7
- const system = window .matchMedia (' (prefers-color-scheme: dark)' ).matches ? ' dark' : ' light' ;
8
-
9
- $theme .preference = next === system ? ' system' : next ;
10
- $theme .current = next ;
5
+ theme .current = theme .current === ' light' ? ' dark' : ' light' ;
11
6
}
12
7
13
8
$effect (() => {
14
- if ($theme .preference === ' system' ) {
15
- const query = window .matchMedia (' (prefers-color-scheme: dark)' );
16
-
17
- return on (query , ' change' , (e ) => {
18
- $theme .current = e .matches ? ' dark' : ' light' ;
19
- });
20
- }
9
+ document .documentElement .classList .remove (' light' , ' dark' );
10
+ document .documentElement .classList .add (theme .current );
21
11
});
22
12
</script >
23
13
14
+ <svelte:head >
15
+ <script >
16
+ {
17
+ const theme = localStorage .getItem (' sv:theme' );
18
+
19
+ document .documentElement .classList .add (
20
+ theme === ' system'
21
+ ? window .matchMedia (' (prefers-color-scheme: dark)' ).matches
22
+ ? ' dark'
23
+ : ' light'
24
+ : theme
25
+ );
26
+ }
27
+ </script >
28
+ </svelte:head >
29
+
24
30
<button
25
31
onclick ={toggle }
26
32
class =" raised icon"
27
33
type =" button"
28
- aria-pressed ={$ theme .current === ' dark' }
34
+ aria-pressed ={theme .current === ' dark' }
29
35
aria-label =" Toggle dark mode"
30
36
></button >
31
37
Original file line number Diff line number Diff line change
1
+ import { on } from 'svelte/events' ;
2
+ import { createSubscriber } from 'svelte/reactivity' ;
3
+
4
+ export class Persisted < T extends string = string > {
5
+ #key: string ;
6
+ #storage: Storage | undefined ;
7
+ #fallback: T ;
8
+ #version = $state ( 0 ) ;
9
+
10
+ #subscribe = createSubscriber ( ( update ) => {
11
+ return on ( window , 'storage' , ( e ) => {
12
+ if ( e . key === this . #key) {
13
+ update ( ) ;
14
+ }
15
+ } ) ;
16
+ } ) ;
17
+
18
+ constructor (
19
+ key : string ,
20
+ fallback : T ,
21
+ storage = typeof localStorage === 'undefined' ? undefined : localStorage
22
+ ) {
23
+ this . #key = key ;
24
+ this . #fallback = fallback ;
25
+ this . #storage = storage ;
26
+ }
27
+
28
+ get current ( ) {
29
+ this . #subscribe( ) ; // handle cross-tab updates
30
+ this . #version; // handle same-tab updates
31
+
32
+ return ( this . #storage?. getItem ( this . #key) as T ) ?? this . #fallback;
33
+ }
34
+
35
+ set current ( v : T ) {
36
+ this . #storage?. setItem ( this . #key, v ) ;
37
+ this . #version += 1 ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ export { Persisted } from './Persisted.svelte' ;
2
+ export { theme } from './theme.svelte' ;
You can’t perform that action at this time.
0 commit comments