Skip to content

Commit ef78eca

Browse files
committed
docs: improve function bindings documentation with examples
- Added a hopefully clearer explanation of what function bindings are that is more approachable for newcomers - Included practical examples for common use cases
1 parent 432763a commit ef78eca

File tree

1 file changed

+81
-1
lines changed

1 file changed

+81
-1
lines changed

documentation/docs/03-template-syntax/12-bind.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,33 @@ Most bindings are _two-way_, meaning that changes to the value will affect the e
1919

2020
## Function bindings
2121

22-
You can also use `bind:property={get, set}`, where `get` and `set` are functions, allowing you to perform validation and transformation:
22+
Function bindings allow you to define custom getter and setter logic while keeping the clean `bind:` syntax. Instead of just binding directly to a variable, you provide two functions: one to get the value, and one to set it.
23+
24+
```svelte
25+
<input bind:value={
26+
() => value, // getter
27+
(v) => { value = v; } // setter
28+
} />
29+
```
30+
31+
Function bindings can act as "interceptors" for your data flow, allowing you to add custom logic when values are get or set:
32+
33+
```svelte
34+
<Input bind:value={
35+
() => name,
36+
(v) => {
37+
// Custom logic here!
38+
name = v;
39+
}
40+
} />
41+
```
42+
43+
This allows you to perform validation and transformations on inputs:
44+
45+
46+
### Practical examples
47+
48+
#### Lowercase input
2349

2450
```svelte
2551
<input bind:value={
@@ -28,6 +54,60 @@ You can also use `bind:property={get, set}`, where `get` and `set` are functions
2854
/>
2955
```
3056

57+
#### Format-as-you-type phone number
58+
59+
```svelte
60+
<script>
61+
let phoneNumber = $state('');
62+
63+
function formatPhone(value) {
64+
// Remove all non-digits
65+
const digits = value.replace(/\D/g, '');
66+
67+
// Format as (XXX) XXX-XXXX
68+
if (digits.length <= 3) return digits;
69+
if (digits.length <= 6) return `(${digits.slice(0, 3)}) ${digits.slice(3)}`;
70+
return `(${digits.slice(0, 3)}) ${digits.slice(3, 6)}-${digits.slice(6, 10)}`;
71+
}
72+
</script>
73+
74+
<input
75+
type="tel"
76+
placeholder="(555) 123-4567"
77+
bind:value={
78+
() => phoneNumber,
79+
(v) => { phoneNumber = formatPhone(v); }
80+
}
81+
/>
82+
```
83+
84+
#### Input validation with error handling
85+
86+
```svelte
87+
<script>
88+
let email = $state('');
89+
let emailError = $state('');
90+
</script>
91+
92+
<Input
93+
bind:value={
94+
() => email,
95+
(newEmail) => {
96+
// Validate on every change
97+
if (newEmail && !newEmail.includes('@')) {
98+
emailError = 'Please enter a valid email';
99+
} else {
100+
emailError = '';
101+
}
102+
email = newEmail;
103+
}
104+
}
105+
error={emailError}
106+
/>
107+
```
108+
109+
### Readonly bindings
110+
31111
In the case of readonly bindings like [dimension bindings](#Dimensions), the `get` value should be `null`:
32112

33113
```svelte

0 commit comments

Comments
 (0)