Skip to content

Commit e233ec7

Browse files
docs(svelte-5-adapter): force runes mode for all examples (#7800)
* docs(examples): force runes mode for all examples * Fix simple example mounting * Fix type
1 parent 907834f commit e233ec7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+158
-2198
lines changed

examples/svelte/auto-refetching/src/routes/+page.svelte

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
createMutation,
66
} from '@tanstack/svelte-query'
77
8-
let intervalMs = 1000
9-
let value = ''
8+
let intervalMs = $state(1000)
9+
let value = $state<string>('')
1010
1111
const client = useQueryClient()
1212
1313
const endpoint = 'http://localhost:5173/api/data'
1414
15-
const todos = createQuery<{ items: string[] }>({
15+
const todos = createQuery<{ items: string[] }>(() => ({
1616
queryKey: ['refetch'],
1717
queryFn: async () => await fetch(endpoint).then((r) => r.json()),
1818
// Refetch the data every second
1919
refetchInterval: intervalMs,
20-
})
20+
}))
2121
2222
const addMutation = createMutation({
2323
mutationFn: (value: string) =>

examples/svelte/auto-refetching/svelte.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
33

44
/** @type {import('@sveltejs/kit').Config} */
55
const config = {
6-
// Consult https://github.com/sveltejs/svelte-preprocess
7-
// for more information about preprocessors
86
preprocess: vitePreprocess(),
9-
107
kit: {
118
adapter: adapter(),
129
},
10+
compilerOptions: {
11+
runes: true,
12+
},
1313
}
1414

1515
export default config

examples/svelte/basic/svelte.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
33

44
/** @type {import('@sveltejs/kit').Config} */
55
const config = {
6-
// Consult https://github.com/sveltejs/svelte-preprocess
7-
// for more information about preprocessors
86
preprocess: vitePreprocess(),
9-
107
kit: {
118
adapter: adapter(),
129
},
10+
compilerOptions: {
11+
runes: true,
12+
},
1313
}
1414

1515
export default config

examples/svelte/load-more-infinite-scroll/src/app.css

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ main {
4848
text-align: center;
4949
}
5050

51-
button {
51+
.button {
5252
border-radius: 8px;
5353
border: 1px solid transparent;
5454
padding: 0.6em 1.2em;
@@ -59,11 +59,11 @@ button {
5959
cursor: pointer;
6060
transition: border-color 0.25s;
6161
}
62-
button:hover {
62+
.button:hover {
6363
border-color: #646cff;
6464
}
65-
button:focus,
66-
button:focus-visible {
65+
.button:focus,
66+
.button:focus-visible {
6767
outline: 4px auto -webkit-focus-ring-color;
6868
}
6969

@@ -75,7 +75,7 @@ button:focus-visible {
7575
a:hover {
7676
color: #747bff;
7777
}
78-
button {
78+
.button {
7979
background-color: #f9f9f9;
8080
}
8181
}

examples/svelte/load-more-infinite-scroll/svelte.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
33

44
/** @type {import('@sveltejs/kit').Config} */
55
const config = {
6-
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
7-
// for more information about preprocessors
86
preprocess: vitePreprocess(),
9-
107
kit: {
118
adapter: adapter(),
129
},
10+
compilerOptions: {
11+
runes: true,
12+
},
1313
}
1414

1515
export default config

examples/svelte/optimistic-updates/src/routes/+page.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
ts: number
1717
}
1818
19-
let text = ''
19+
let text = $state<string>('')
2020
2121
const client = useQueryClient()
2222

examples/svelte/optimistic-updates/svelte.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'
33

44
/** @type {import('@sveltejs/kit').Config} */
55
const config = {
6-
// Consult https://github.com/sveltejs/svelte-preprocess
7-
// for more information about preprocessors
86
preprocess: vitePreprocess(),
9-
107
kit: {
118
adapter: adapter(),
129
},
10+
compilerOptions: {
11+
runes: true,
12+
},
1313
}
1414

1515
export default config
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export function ref<T>(initial: T) {
2+
let value = $state(initial)
3+
4+
return {
5+
get value() {
6+
return value
7+
},
8+
set value(newValue) {
9+
value = newValue
10+
},
11+
}
12+
}
13+
14+
export const staleTime = ref(1000)
15+
export const gcTime = ref(3000)
16+
export const errorRate = ref(0.05)
17+
export const queryTimeMin = ref(1000)
18+
export const queryTimeMax = ref(2000)
19+
20+
export const editingIndex = ref<number | null>(null)
21+
export const views = ref(['', 'fruit', 'grape'])
22+
23+
let initialId = 0
24+
const initialList = [
25+
'apple',
26+
'banana',
27+
'pineapple',
28+
'grapefruit',
29+
'dragonfruit',
30+
'grapes',
31+
].map((d) => ({ id: initialId++, name: d, notes: 'These are some notes' }))
32+
33+
export const list = ref(initialList)
34+
export const id = ref(initialId)
35+
36+
export type Todos = typeof initialList
37+
export type Todo = Todos[0]

examples/svelte/playground/src/lib/stores.ts

-26
This file was deleted.

examples/svelte/playground/src/routes/+page.svelte

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
errorRate,
77
queryTimeMin,
88
queryTimeMax,
9-
} from '../lib/stores'
9+
} from '../lib/stores.svelte'
1010
import App from './App.svelte'
1111
1212
const queryClient = useQueryClient()
1313
1414
queryClient.setDefaultOptions({
1515
queries: {
16-
staleTime: $staleTime,
17-
gcTime: $gcTime,
16+
staleTime: staleTime.value,
17+
gcTime: gcTime.value,
1818
},
1919
})
2020
</script>
@@ -29,7 +29,7 @@
2929
type="number"
3030
min="0"
3131
step="1000"
32-
bind:value={$staleTime}
32+
bind:value={staleTime.value}
3333
style="width: 100px"
3434
/>
3535
</div>
@@ -39,7 +39,7 @@
3939
type="number"
4040
min="0"
4141
step="1000"
42-
bind:value={$gcTime}
42+
bind:value={gcTime.value}
4343
style="width: 100px"
4444
/>
4545
</div>
@@ -51,7 +51,7 @@
5151
min="0"
5252
max="1"
5353
step=".05"
54-
bind:value={$errorRate}
54+
bind:value={errorRate.value}
5555
style="width: 100px"
5656
/>
5757
</div>
@@ -61,7 +61,7 @@
6161
type="number"
6262
min="1"
6363
step="500"
64-
bind:value={$queryTimeMin}
64+
bind:value={queryTimeMin.value}
6565
style="width: 100px"
6666
/>{' '}
6767
</div>
@@ -71,7 +71,7 @@
7171
type="number"
7272
min="1"
7373
step="500"
74-
bind:value={$queryTimeMax}
74+
bind:value={queryTimeMax.value}
7575
style="width: 100px"
7676
/>
7777
</div>

examples/svelte/playground/src/routes/AddTodo.svelte

+14-13
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,29 @@
77
list,
88
id,
99
type Todo,
10-
} from '../lib/stores'
10+
} from '../lib/stores.svelte'
1111
1212
const queryClient = useQueryClient()
1313
14-
let name = ''
14+
let name = $state('')
1515
1616
const postTodo = async ({ name, notes }: Omit<Todo, 'id'>) => {
1717
console.info('postTodo', { name, notes })
1818
return new Promise((resolve, reject) => {
1919
setTimeout(
2020
() => {
21-
if (Math.random() < $errorRate) {
21+
if (Math.random() < errorRate.value) {
2222
return reject(
2323
new Error(JSON.stringify({ postTodo: { name, notes } }, null, 2)),
2424
)
2525
}
26-
const todo = { name, notes, id: $id }
27-
id.set($id + 1)
28-
list.set([...$list, todo])
26+
const todo = { name, notes, id: id.value }
27+
id.value = id.value + 1
28+
list.value = [...list.value, todo]
2929
resolve(todo)
3030
},
31-
$queryTimeMin + Math.random() * ($queryTimeMax - $queryTimeMin),
31+
queryTimeMin.value +
32+
Math.random() * (queryTimeMax.value - queryTimeMin.value),
3233
)
3334
})
3435
}
@@ -42,20 +43,20 @@
4243
</script>
4344

4445
<div>
45-
<input bind:value={name} disabled={$addMutation.status === 'pending'} />
46+
<input bind:value={name} disabled={addMutation.status === 'pending'} />
4647

4748
<button
48-
onclick={() => $addMutation.mutate({ name, notes: name })}
49-
disabled={$addMutation.status === 'pending' || !name}
49+
onclick={() => addMutation.mutate({ name, notes: name })}
50+
disabled={addMutation.status === 'pending' || !name}
5051
>
5152
Add Todo
5253
</button>
5354

5455
<div>
55-
{$addMutation.status === 'pending'
56+
{addMutation.status === 'pending'
5657
? 'Saving...'
57-
: $addMutation.status === 'error'
58-
? $addMutation.error.message
58+
: addMutation.status === 'error'
59+
? addMutation.error.message
5960
: 'Saved!'}
6061
</div>
6162
</div>

examples/svelte/playground/src/routes/App.svelte

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import TodosList from './TodosList.svelte'
44
import EditTodo from './EditTodo.svelte'
55
import AddTodo from './AddTodo.svelte'
6-
import { views, editingIndex } from '../lib/stores'
6+
import { views, editingIndex } from '../lib/stores.svelte'
77
88
const queryClient = useQueryClient()
99
</script>
@@ -17,7 +17,7 @@
1717
<br />
1818
<hr />
1919

20-
{#each $views as view}
20+
{#each views.value as view}
2121
<div>
2222
<TodosList initialFilter={view} />
2323
<br />
@@ -26,14 +26,14 @@
2626

2727
<button
2828
onclick={() => {
29-
views.set([...$views, ''])
29+
views.value = [...views.value, '']
3030
}}
3131
>
3232
Add Filter List
3333
</button>
3434
<hr />
3535

36-
{#if $editingIndex !== null}
36+
{#if editingIndex.value !== null}
3737
<EditTodo />
3838
<hr />
3939
{/if}

0 commit comments

Comments
 (0)