Skip to content

Commit 25116c5

Browse files
committed
Add useContext
1 parent 651deea commit 25116c5

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<script>
2+
</script>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { PageLoad } from "./$types"
2+
3+
export const load = (async ({ params }) => {
4+
return {
5+
title: "useContext",
6+
react: (await import("./react.jsx?raw")).default,
7+
svelte: (await import("./svelte.svelte?raw")).default,
8+
}
9+
}) satisfies PageLoad
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React, { createContext } from 'react'
2+
import Toolbar from './Toolbar'
3+
4+
const themes = {
5+
light: {
6+
foreground: "#000000",
7+
background: "#eeeeee"
8+
},
9+
dark: {
10+
foreground: "#ffffff",
11+
background: "#222222"
12+
}
13+
}
14+
15+
export const ThemeContext = createContext(themes.light)
16+
17+
export default function App() {
18+
return (
19+
<ThemeContext.Provider value={themes.dark}>
20+
<Toolbar />
21+
</ThemeContext.Provider>
22+
)
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<script>
2+
import { setContext } from "svelte"
3+
import Toolbar from "./Toolbar.svelte"
4+
5+
const themes = {
6+
light: {
7+
foreground: "#000000",
8+
background: "#eeeeee"
9+
},
10+
dark: {
11+
foreground: "#ffffff",
12+
background: "#222222"
13+
}
14+
}
15+
16+
setContext("ThemeContext", themes.dark)
17+
</script>
18+
19+
<Toolbar />

src/routes/+layout.svelte

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
let tile4Url = "/useRef"
1414
let tile5Url = "/useReducer"
1515
let tile6Url = "/useCallback"
16+
let tile7Url = "/useContext"
1617
</script>
1718

1819
<!-- App Shell -->
@@ -97,6 +98,14 @@
9798
href={tile6Url}
9899
class={tile6Url === $page.url.pathname ? "bg-primary-500" : ""}><Icon icon="code" /></AppRailTile
99100
>
101+
<AppRailTile
102+
label="useContext"
103+
title="useContext"
104+
value={6}
105+
tag="a"
106+
href={tile7Url}
107+
class={tile7Url === $page.url.pathname ? "bg-primary-500" : ""}><Icon icon="code" /></AppRailTile
108+
>
100109
</svelte:fragment>
101110

102111
<svelte:fragment slot="trail">

0 commit comments

Comments
 (0)