bind-get-set
Dominic Gannaway 21 hours ago
parent a42ba71f46
commit 5d76cddc05

@ -24,7 +24,6 @@ export function BindDirective(node, context) {
validate_no_const_assignment(node, node.expression, context.state.scope, true); validate_no_const_assignment(node, node.expression, context.state.scope, true);
const assignee = node.expression; const assignee = node.expression;
const left = object(assignee); const left = object(assignee);
if (left === null) { if (left === null) {

@ -248,8 +248,7 @@
<input <input
autofocus autofocus
spellcheck={false} spellcheck={false}
bind:value bind:value={input_value}
input_value}
on:focus={select_input} on:focus={select_input}
on:blur={close_edit} on:blur={close_edit}
on:keydown={(e) => { on:keydown={(e) => {

@ -21,8 +21,7 @@
<div class="editor-wrapper"> <div class="editor-wrapper">
<div class="editor notranslate" translate="no"> <div class="editor notranslate" translate="no">
<CodeMirror <CodeMirror
bind:this bind:this={$module_editor}
$module_editor}
diagnostics={() => { diagnostics={() => {
if (error) { if (error) {
return [ return [

@ -77,8 +77,7 @@
</script> </script>
<li <li
bind:this bind:this={list_item_el}
list_item_el}
class:marked={!is_root && is_leaf} class:marked={!is_root && is_leaf}
on:mouseover={handle_mark_text} on:mouseover={handle_mark_text}
on:focus={handle_mark_text} on:focus={handle_mark_text}

@ -11,32 +11,32 @@
<div class="option"> <div class="option">
<span class="key">generate:</span> <span class="key">generate:</span>
<input id="client" type="radio" bind:group $compile_options.generate} value="client" /> <input id="client" type="radio" bind:group={$compile_options.generate} value="client" />
<label for="client"><span class="string">"client"</span></label> <label for="client"><span class="string">"client"</span></label>
<input id="server" type="radio" bind:group $compile_options.generate} value="server" /> <input id="server" type="radio" bind:group={$compile_options.generate} value="server" />
<label for="server"><span class="string">"server"</span>,</label> <label for="server"><span class="string">"server"</span>,</label>
</div> </div>
<div class="option"> <div class="option">
<span class="key">css:</span> <span class="key">css:</span>
<input id="injected" type="radio" bind:group $compile_options.css} value="injected" /> <input id="injected" type="radio" bind:group={$compile_options.css} value="injected" />
<label for="injected"><span class="string">"injected"</span></label> <label for="injected"><span class="string">"injected"</span></label>
<input id="external" type="radio" bind:group $compile_options.css} value="external" /> <input id="external" type="radio" bind:group={$compile_options.css} value="external" />
<label for="external"><span class="string">"external"</span>,</label> <label for="external"><span class="string">"external"</span>,</label>
</div> </div>
<label class="option"> <label class="option">
<span class="key">dev:</span> <span class="key">dev:</span>
<Checkbox bind:checked $compile_options.dev} /> <Checkbox bind:checked={$compile_options.dev} />
<span class="boolean">{$compile_options.dev}</span>, <span class="boolean">{$compile_options.dev}</span>,
</label> </label>
<label class="option"> <label class="option">
<span class="key">hmr:</span> <span class="key">hmr:</span>
<Checkbox bind:checked $compile_options.hmr} /> <Checkbox bind:checked={$compile_options.hmr} />
<span class="boolean">{$compile_options.hmr}</span>, <span class="boolean">{$compile_options.hmr}</span>,
</label> </label>
}); });

@ -77,8 +77,7 @@
<!-- component viewer --> <!-- component viewer -->
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'result'}> <div class="tab-content" class:visible={selected?.type !== 'md' && view === 'result'}>
<Viewer <Viewer
bind:error bind:error={runtimeError}
runtimeError}
{status} {status}
{relaxed} {relaxed}
{injectedJS} {injectedJS}
@ -90,11 +89,11 @@
<!-- js output --> <!-- js output -->
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'js'}> <div class="tab-content" class:visible={selected?.type !== 'md' && view === 'js'}>
{#if embedded} {#if embedded}
<CodeMirror bind:this js_editor} readonly /> <CodeMirror bind:this={js_editor} readonly />
{:else} {:else}
<PaneWithPanel pos="50%" panel="Compiler options"> <PaneWithPanel pos="50%" panel="Compiler options">
<div slot="main"> <div slot="main">
<CodeMirror bind:this js_editor} readonly /> <CodeMirror bind:this={js_editor} readonly />
</div> </div>
<div slot="panel-body"> <div slot="panel-body">
@ -106,7 +105,7 @@
<!-- css output --> <!-- css output -->
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'css'}> <div class="tab-content" class:visible={selected?.type !== 'md' && view === 'css'}>
<CodeMirror bind:this css_editor} readonly /> <CodeMirror bind:this={css_editor} readonly />
</div> </div>
<!-- ast output --> <!-- ast output -->

@ -254,8 +254,7 @@
<iframe <iframe
title="Result" title="Result"
class:inited class:inited
bind:this bind:this={iframe}
iframe}
sandbox={[ sandbox={[
'allow-popups-to-escape-sandbox', 'allow-popups-to-escape-sandbox',
'allow-scripts', 'allow-scripts',

@ -338,7 +338,7 @@
<svelte:window on:beforeunload={before_unload} /> <svelte:window on:beforeunload={before_unload} />
<div class="container" class:toggleable={$toggleable} bind:clientWidth width}> <div class="container" class:toggleable={$toggleable} bind:clientWidth={width}>
<div class="viewport" class:output={show_output}> <div class="viewport" class:output={show_output}>
<SplitPane <SplitPane
--color="var(--sk-text-4)" --color="var(--sk-text-4)"
@ -374,7 +374,7 @@
</div> </div>
{#if $toggleable} {#if $toggleable}
<InputOutputToggle bind:checked show_output} /> <InputOutputToggle bind:checked={show_output} />
{/if} {/if}
</div> </div>

@ -88,8 +88,7 @@
/> />
<Repl <Repl
bind:this bind:this={repl}
repl}
autocomplete={true} autocomplete={true}
on:add={change_from_editor} on:add={change_from_editor}
on:change={change_from_editor} on:change={change_from_editor}

@ -193,7 +193,7 @@ To run _side-effects_ when the component is mounted to the DOM, and when values
}); });
</script> </script>
<canvas bind:this canvas} width="100" height="100" /> <canvas bind:this={canvas} width="100" height="100" />
``` ```
The function passed to `$effect` will run when the component mounts, and will re-run after any changes to the values it reads that were declared with `$state` or `$derived` (including those passed in with `$props`). Re-runs are batched (i.e. changing `color` and `size` in the same moment won't cause two separate runs), and happen after any DOM updates have been applied. The function passed to `$effect` will run when the component mounts, and will re-run after any changes to the values it reads that were declared with `$state` or `$derived` (including those passed in with `$props`). Re-runs are batched (i.e. changing `color` and `size` in the same moment won't cause two separate runs), and happen after any DOM updates have been applied.
@ -346,12 +346,12 @@ You might be tempted to do something convoluted with effects to link one value t
</script> </script>
<label> <label>
<input type="range" bind:value spent} max={total} /> <input type="range" bind:value={spent} max={total} />
{spent}/{total} spent {spent}/{total} spent
</label> </label>
<label> <label>
<input type="range" bind:value left} max={total} /> <input type="range" bind:value={left} max={total} />
{left}/{total} left {left}/{total} left
</label> </label>
``` ```
@ -414,12 +414,12 @@ If you need to use bindings, for whatever reason (for example when you want some
</script> </script>
<label> <label>
<input type="range" bind:value spent} max={total} /> <input type="range" bind:value={spent} max={total} />
{spent}/{total} spent {spent}/{total} spent
</label> </label>
<label> <label>
<input type="range" bind:value left.value} max={total} /> <input type="range" bind:value={left.value} max={total} />
{left.value}/{total} left {left.value}/{total} left
</label> </label>
``` ```
@ -467,7 +467,7 @@ In rare cases, you may need to run code _before_ the DOM updates. For this we ca
}); });
</script> </script>
<div bind:this div}> <div bind:this={div}>
{#each messages as message} {#each messages as message}
<p>{message}</p> <p>{message}</p>
{/each} {/each}
@ -627,7 +627,7 @@ or array using [fine-grained reactivity](/docs/fine-grained-reactivity) will cau
</script> </script>
<button onclick={() => count++}>Increment</button> <button onclick={() => count++}>Increment</button>
<input bind:value message} /> <input bind:value={message} />
``` ```
`$inspect` returns a property `with`, which you can invoke with a callback, which will then be invoked instead of `console.log`. The first argument to the callback is either `"init"` or `"update"`, all following arguments are the values passed to `$inspect`. [Demo:](/#H4sIAAAAAAAACkVQ24qDMBD9lSEUqlTqPlsj7ON-w7pQG8c2VCchmVSK-O-bKMs-DefKYRYx6BG9qL4XQd2EohKf1opC8Nsm4F84MkbsTXAqMbVXTltuWmp5RAZlAjFIOHjuGLOP_BKVqB00eYuKs82Qn2fNjyxLtcWeyUE2sCRry3qATQIpJRyD7WPVMf9TW-7xFu53dBcoSzAOrsqQNyOe2XUKr0Xi5kcMvdDB2wSYO-I9vKazplV1-T-d6ltgNgSG1KjVUy7ZtmdbdjqtzRcphxMS1-XubOITJtPrQWMvKnYB15_1F7KKadA_AQAA) `$inspect` returns a property `with`, which you can invoke with a callback, which will then be invoked instead of `console.log`. The first argument to the callback is either `"init"` or `"update"`, all following arguments are the values passed to `$inspect`. [Demo:](/#H4sIAAAAAAAACkVQ24qDMBD9lSEUqlTqPlsj7ON-w7pQG8c2VCchmVSK-O-bKMs-DefKYRYx6BG9qL4XQd2EohKf1opC8Nsm4F84MkbsTXAqMbVXTltuWmp5RAZlAjFIOHjuGLOP_BKVqB00eYuKs82Qn2fNjyxLtcWeyUE2sCRry3qATQIpJRyD7WPVMf9TW-7xFu53dBcoSzAOrsqQNyOe2XUKr0Xi5kcMvdDB2wSYO-I9vKazplV1-T-d6ltgNgSG1KjVUy7ZtmdbdjqtzRcphxMS1-XubOITJtPrQWMvKnYB15_1F7KKadA_AQAA)

@ -25,7 +25,7 @@ Forces any pending effects (including DOM updates) to be applied immediately, ra
} }
</script> </script>
<span bind:this element}>{count}</span> <span bind:this={element}>{count}</span>
<button {onclick}>update</button> <button {onclick}>update</button>
``` ```
@ -140,14 +140,14 @@ Svelte provides reactive `SvelteMap`, `SvelteSet`, `SvelteDate` and `SvelteURL`
</script> </script>
<!-- changes to these... --> <!-- changes to these... -->
<input bind:value url.protocol} /> <input bind:value={url.protocol} />
<input bind:value url.hostname} /> <input bind:value={url.hostname} />
<input bind:value url.pathname} /> <input bind:value={url.pathname} />
<hr /> <hr />
<!-- will update `href` and vice versa --> <!-- will update `href` and vice versa -->
<input bind:value url.href} /> <input bind:value={url.href} />
``` ```
## `svelte/events` ## `svelte/events`

@ -32,8 +32,8 @@ In Svelte 4, reactivity centres on the _component_ and the top-level state decla
{#each todos as todo} {#each todos as todo}
<div> <div>
<input bind:value todo.text} /> <input bind:value={todo.text} />
<input type="checkbox" bind:checked todo.done} /> <input type="checkbox" bind:checked={todo.done} />
</div> </div>
{/each} {/each}

@ -228,7 +228,7 @@ This is [no longer true in Svelte 5](/#H4sIAAAAAAAAE4WQwU7DMAyGX8VESANpXe8lq9Q8A
let Thing = $state(); let Thing = $state();
</script> </script>
<select bind:value Thing}> <select bind:value={Thing}>
<option value={A}>A</option> <option value={A}>A</option>
<option value={B}>B</option> <option value={B}>B</option>
</select> </select>

Loading…
Cancel
Save