Skip to content

Commit 262b281

Browse files
docs: add inline documentation for svelte/reactivity (#15722)
* docs: add inline documentation for svelte/reactivity * map * set * more * dedupe * tweak * typo * Apply suggestions from code review Co-authored-by: bytecodemanipulator <[email protected]> --------- Co-authored-by: bytecodemanipulator <[email protected]>
1 parent fc61262 commit 262b281

File tree

7 files changed

+322
-19
lines changed

7 files changed

+322
-19
lines changed

documentation/docs/98-reference/21-svelte-reactivity.md

+1-19
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,6 @@
22
title: svelte/reactivity
33
---
44

5-
Svelte provides reactive versions of various built-ins like `SvelteMap`, `SvelteSet` and `SvelteURL`. These can be imported from `svelte/reactivity` and used just like their native counterparts.
6-
7-
```svelte
8-
<script>
9-
import { SvelteURL } from 'svelte/reactivity';
10-
11-
const url = new SvelteURL('https://example.com/path');
12-
</script>
13-
14-
<!-- changes to these... -->
15-
<input bind:value={url.protocol} />
16-
<input bind:value={url.hostname} />
17-
<input bind:value={url.pathname} />
18-
19-
<hr />
20-
21-
<!-- will update `href` and vice versa -->
22-
<input bind:value={url.href} />
23-
```
5+
Svelte provides reactive versions of various built-ins like [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map), [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) that can be used just like their native counterparts, as well as a handful of additional utilities for handling reactivity.
246

257
> MODULE: svelte/reactivity

packages/svelte/src/reactivity/date.js

+32
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,38 @@ import { active_reaction, get, set_active_reaction } from '../internal/client/ru
55

66
var inited = false;
77

8+
/**
9+
* A reactive version of the built-in [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) object.
10+
* Reading the date (whether with methods like `date.getTime()` or `date.toString()`, or via things like [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat))
11+
* in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived)
12+
* will cause it to be re-evaluated when the value of the date changes.
13+
*
14+
* ```svelte
15+
* <script>
16+
* import { SvelteDate } from 'svelte/reactivity';
17+
*
18+
* const date = new SvelteDate();
19+
*
20+
* const formatter = new Intl.DateTimeFormat(undefined, {
21+
* hour: 'numeric',
22+
* minute: 'numeric',
23+
* second: 'numeric'
24+
* });
25+
*
26+
* $effect(() => {
27+
* const interval = setInterval(() => {
28+
* date.setTime(Date.now());
29+
* }, 1000);
30+
*
31+
* return () => {
32+
* clearInterval(interval);
33+
* };
34+
* });
35+
* </script>
36+
*
37+
* <p>The time is {formatter.format(date)}</p>
38+
* ```
39+
*/
840
export class SvelteDate extends Date {
941
#time = source(super.getTime());
1042

packages/svelte/src/reactivity/map.js

+41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,47 @@ import { get } from '../internal/client/runtime.js';
55
import { increment } from './utils.js';
66

77
/**
8+
* A reactive version of the built-in [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) object.
9+
* Reading contents of the map (by iterating, or by reading `map.size` or calling `map.get(...)` or `map.has(...)` as in the [tic-tac-toe example](https://svelte.dev/playground/0b0ff4aa49c9443f9b47fe5203c78293) below) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived)
10+
* will cause it to be re-evaluated as necessary when the map is updated.
11+
*
12+
* Note that values in a reactive map are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).
13+
*
14+
* ```svelte
15+
* <script>
16+
* import { SvelteMap } from 'svelte/reactivity';
17+
* import { result } from './game.js';
18+
*
19+
* let board = new SvelteMap();
20+
* let player = $state('x');
21+
* let winner = $derived(result(board));
22+
*
23+
* function reset() {
24+
* player = 'x';
25+
* board.clear();
26+
* }
27+
* </script>
28+
*
29+
* <div class="board">
30+
* {#each Array(9), i}
31+
* <button
32+
* disabled={board.has(i) || winner}
33+
* onclick={() => {
34+
* board.set(i, player);
35+
* player = player === 'x' ? 'o' : 'x';
36+
* }}
37+
* >{board.get(i)}</button>
38+
* {/each}
39+
* </div>
40+
*
41+
* {#if winner}
42+
* <p>{winner} wins!</p>
43+
* <button onclick={reset}>reset</button>
44+
* {:else}
45+
* <p>{player} is next</p>
46+
* {/if}
47+
* ```
48+
*
849
* @template K
950
* @template V
1051
* @extends {Map<K, V>}

packages/svelte/src/reactivity/set.js

+31
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,37 @@ var set_like_methods = ['difference', 'intersection', 'symmetricDifference', 'un
1010
var inited = false;
1111

1212
/**
13+
* A reactive version of the built-in [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) object.
14+
* Reading contents of the set (by iterating, or by reading `set.size` or calling `set.has(...)` as in the [example](https://svelte.dev/playground/53438b51194b4882bcc18cddf9f96f15) below) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived)
15+
* will cause it to be re-evaluated as necessary when the set is updated.
16+
*
17+
* Note that values in a reactive set are _not_ made [deeply reactive](https://svelte.dev/docs/svelte/$state#Deep-state).
18+
*
19+
* ```svelte
20+
* <script>
21+
* import { SvelteSet } from 'svelte/reactivity';
22+
* let monkeys = new SvelteSet();
23+
*
24+
* function toggle(monkey) {
25+
* if (monkeys.has(monkey)) {
26+
* monkeys.delete(monkey);
27+
* } else {
28+
* monkeys.add(monkey);
29+
* }
30+
* }
31+
* </script>
32+
*
33+
* {#each ['🙈', '🙉', '🙊'] as monkey}
34+
* <button onclick={() => toggle(monkey)}>{monkey}</button>
35+
* {/each}
36+
*
37+
* <button onclick={() => monkeys.clear()}>clear</button>
38+
*
39+
* {#if monkeys.has('🙈')}<p>see no evil</p>{/if}
40+
* {#if monkeys.has('🙉')}<p>hear no evil</p>{/if}
41+
* {#if monkeys.has('🙊')}<p>speak no evil</p>{/if}
42+
* ```
43+
*
1344
* @template T
1445
* @extends {Set<T>}
1546
*/

packages/svelte/src/reactivity/url-search-params.js

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ import { increment } from './utils.js';
55

66
export const REPLACE = Symbol();
77

8+
/**
9+
* A reactive version of the built-in [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) object.
10+
* Reading its contents (by iterating, or by calling `params.get(...)` or `params.getAll(...)` as in the [example](https://svelte.dev/playground/b3926c86c5384bab9f2cf993bc08c1c8) below) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived)
11+
* will cause it to be re-evaluated as necessary when the params are updated.
12+
*
13+
* ```svelte
14+
* <script>
15+
* import { SvelteURLSearchParams } from 'svelte/reactivity';
16+
*
17+
* const params = new SvelteURLSearchParams('message=hello');
18+
*
19+
* let key = $state('key');
20+
* let value = $state('value');
21+
* </script>
22+
*
23+
* <input bind:value={key} />
24+
* <input bind:value={value} />
25+
* <button onclick={() => params.append(key, value)}>append</button>
26+
*
27+
* <p>?{params.toString()}</p>
28+
*
29+
* {#each params as [key, value]}
30+
* <p>{key}: {value}</p>
31+
* {/each}
32+
* ```
33+
*/
834
export class SvelteURLSearchParams extends URLSearchParams {
935
#version = source(0);
1036
#url = get_current_url();
@@ -23,6 +49,7 @@ export class SvelteURLSearchParams extends URLSearchParams {
2349

2450
/**
2551
* @param {URLSearchParams} params
52+
* @internal
2653
*/
2754
[REPLACE](params) {
2855
if (this.#updating) return;

packages/svelte/src/reactivity/url.js

+27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,33 @@ export function get_current_url() {
1010
return current_url;
1111
}
1212

13+
/**
14+
* A reactive version of the built-in [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) object.
15+
* Reading properties of the URL (such as `url.href` or `url.pathname`) in an [effect](https://svelte.dev/docs/svelte/$effect) or [derived](https://svelte.dev/docs/svelte/$derived)
16+
* will cause it to be re-evaluated as necessary when the URL changes.
17+
*
18+
* The `searchParams` property is an instance of [SvelteURLSearchParams](https://svelte.dev/docs/svelte/svelte-reactivity#SvelteURLSearchParams).
19+
*
20+
* [Example](https://svelte.dev/playground/5a694758901b448c83dc40dc31c71f2a):
21+
*
22+
* ```svelte
23+
* <script>
24+
* import { SvelteURL } from 'svelte/reactivity';
25+
*
26+
* const url = new SvelteURL('https://example.com/path');
27+
* </script>
28+
*
29+
* <!-- changes to these... -->
30+
* <input bind:value={url.protocol} />
31+
* <input bind:value={url.hostname} />
32+
* <input bind:value={url.pathname} />
33+
*
34+
* <hr />
35+
*
36+
* <!-- will update `href` and vice versa -->
37+
* <input bind:value={url.href} size="65" />
38+
* ```
39+
*/
1340
export class SvelteURL extends URL {
1441
#protocol = source(super.protocol);
1542
#username = source(super.username);

0 commit comments

Comments
 (0)