@ramstack/alpinegear-bound 1.4.3 → 1.4.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +382 -401
- package/alpinegear-bound.esm.js +369 -369
- package/alpinegear-bound.esm.min.js +1 -1
- package/alpinegear-bound.js +371 -371
- package/alpinegear-bound.min.js +1 -1
- package/package.json +9 -3
package/README.md
CHANGED
|
@@ -1,401 +1,382 @@
|
|
|
1
|
-
# @ramstack/alpinegear-bound
|
|
2
|
-
[](https://www.npmjs.com/package/@ramstack/alpinegear-bound)
|
|
3
|
-
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
4
|
-
|
|
5
|
-
`@ramstack/alpinegear-bound` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-bound` directive.
|
|
6
|
-
|
|
7
|
-
This directive allows for two-way binding between input elements and their associated data properties.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
```html
|
|
84
|
-
<
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
```html
|
|
118
|
-
<
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
<
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
</
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
```
|
|
181
|
-
🚀 [Live demo | Alpine.js x-bound: Binding
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
>
|
|
193
|
-
>
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
>
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
>
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
<
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
>
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
>
|
|
347
|
-
|
|
348
|
-
>
|
|
349
|
-
>
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
**[@ramstack/alpinegear-hotkey](https://www.npmjs.com/package/@ramstack/alpinegear-hotkey)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/hotkey))<br>
|
|
385
|
-
Provides the `x-hotkey` directive, which allows you to easily handle keyboard shortcuts within your Alpine.js components or application.
|
|
386
|
-
|
|
387
|
-
**[@ramstack/alpinegear-router](https://www.npmjs.com/package/@ramstack/alpinegear-router)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/router))<br>
|
|
388
|
-
Provides the `x-router` and `x-route` directives, which enable client-side navigation and routing functionality within your Alpine.js application.
|
|
389
|
-
|
|
390
|
-
**[@ramstack/alpinegear-dialog](https://www.npmjs.com/package/@ramstack/alpinegear-dialog)** ([README](https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/dialog))<br>
|
|
391
|
-
Provides a headless dialog directive for Alpine.js based on the native HTML `<dialog>` element.
|
|
392
|
-
It supports declarative composition, value-based close semantics, and both modal and non-modal dialogs,
|
|
393
|
-
with optional Promise-based imperative control.
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
## Contributions
|
|
397
|
-
Bug reports and contributions are welcome.
|
|
398
|
-
|
|
399
|
-
## License
|
|
400
|
-
This package is released as open source under the **MIT License**.
|
|
401
|
-
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|
|
1
|
+
# @ramstack/alpinegear-bound
|
|
2
|
+
[](https://www.npmjs.com/package/@ramstack/alpinegear-bound)
|
|
3
|
+
[](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE)
|
|
4
|
+
|
|
5
|
+
`@ramstack/alpinegear-bound` is a plugin for [Alpine.js](https://alpinejs.dev/) that provides the `x-bound` directive.
|
|
6
|
+
|
|
7
|
+
This directive allows for two-way binding between input elements and their associated data properties.
|
|
8
|
+
It works similarly to the binding provided by [Svelte](https://svelte.dev/docs/element-directives#bind-property)
|
|
9
|
+
and also supports synchronizing values between two `Alpine.js` data properties.
|
|
10
|
+
|
|
11
|
+
> [!Note]
|
|
12
|
+
> This package is part of the **[`@ramstack/alpinegear-main`](https://www.npmjs.com/package/@ramstack/alpinegear-main)** bundle.
|
|
13
|
+
> If you are using the main bundle, you don't need to install this package separately.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
### Using CDN
|
|
18
|
+
To include the CDN version of this plugin, add the following `<script>` tag before the core `alpine.js` file:
|
|
19
|
+
|
|
20
|
+
```html
|
|
21
|
+
<!-- alpine.js plugin -->
|
|
22
|
+
<script src="https://cdn.jsdelivr.net/npm/@ramstack/alpinegear-bound@1/alpinegear-bound.min.js" defer></script>
|
|
23
|
+
|
|
24
|
+
<!-- alpine.js -->
|
|
25
|
+
<script src="https://cdn.jsdelivr.net/npm/alpinejs@3/dist/cdn.min.js" defer></script>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Using NPM
|
|
29
|
+
Alternatively, you can install the plugin via `npm`:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install --save @ramstack/alpinegear-bound
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Then initialize it in your bundle:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import Alpine from "alpinejs";
|
|
39
|
+
import bound from "@ramstack/alpinegear-bound";
|
|
40
|
+
|
|
41
|
+
Alpine.plugin(bound);
|
|
42
|
+
Alpine.start();
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
The syntax for binding properties is `x-bound:property="dataprop"`.
|
|
48
|
+
|
|
49
|
+
Let's take the following example:
|
|
50
|
+
|
|
51
|
+
```html
|
|
52
|
+
<div x-data="{ name: '' }">
|
|
53
|
+
<input x-bound:value="name" />
|
|
54
|
+
Hello <span x-text="name"></span>!
|
|
55
|
+
|
|
56
|
+
<button @click="name = 'John'">Change Name</button>
|
|
57
|
+
</div>
|
|
58
|
+
```
|
|
59
|
+
🚀 [Live demo | Alpine.js x-bound: Basic usage](https://jsfiddle.net/rameel/8cw23y7o/)
|
|
60
|
+
|
|
61
|
+
In this example, we bind the `name` property to the `value` property of the `<input>` element.
|
|
62
|
+
Since `x-bound` provides two-way binding, any changes to `name` will be reflected in the `<input>` element,
|
|
63
|
+
as will occur when the `button` is clicked.
|
|
64
|
+
|
|
65
|
+
### Shorthand Syntax
|
|
66
|
+
The `x-bound` directive also supports shorthand syntax: `&`.
|
|
67
|
+
|
|
68
|
+
So the previous example could be written as follows:
|
|
69
|
+
|
|
70
|
+
```html
|
|
71
|
+
<input &value="name" />
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
If the element's property name matches the bound data property, you can simplify this further:
|
|
75
|
+
```html
|
|
76
|
+
<input x-bound:value />
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
In this example, the repetition of the `value` in `x-bound:value="value"` is redundant,
|
|
80
|
+
so we can simply shorten it to `<input x-bound:value>`. Since we can use `&` instead of `x-bound`,
|
|
81
|
+
the example can be written as follows:
|
|
82
|
+
|
|
83
|
+
```html
|
|
84
|
+
<input &value />
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
More examples:
|
|
88
|
+
|
|
89
|
+
```html
|
|
90
|
+
<div x-data="{ name: '', text: '', yes: true, methods: [] }">
|
|
91
|
+
<input &value="name" />
|
|
92
|
+
<textarea &value="text"></textarea>
|
|
93
|
+
<input &checked="yes" type="checkbox" />
|
|
94
|
+
<select &value="methods">
|
|
95
|
+
...
|
|
96
|
+
</select>
|
|
97
|
+
</div>
|
|
98
|
+
```
|
|
99
|
+
🚀 [Live demo | Alpine.js x-bound: Shorthand Syntax](https://jsfiddle.net/rameel/9ys23n4z/)
|
|
100
|
+
|
|
101
|
+
### Binding Numeric Inputs
|
|
102
|
+
|
|
103
|
+
For `<input>` elements with `type="number"` and `type="range"`, values are automatically coerced into numbers.
|
|
104
|
+
If the `<input>` value is empty or invalid, the bound property will be set to `null`.
|
|
105
|
+
|
|
106
|
+
```html
|
|
107
|
+
<input &value="number" type="number" />
|
|
108
|
+
```
|
|
109
|
+
🚀 [Live demo | Alpine.js x-bound: Bind Numeric Inputs](https://jsfiddle.net/rameel/e160vsta/)
|
|
110
|
+
|
|
111
|
+
### Binding `<input type="file">`
|
|
112
|
+
|
|
113
|
+
For `<input>` elements with `type="file"`, the binding is applied to the `files` property,
|
|
114
|
+
resulting in a [FileList](https://developer.mozilla.org/en-US/docs/Web/API/FileList) object being assigned,
|
|
115
|
+
containing the list of selected files.
|
|
116
|
+
|
|
117
|
+
```html
|
|
118
|
+
<input &files type="file" accept="image/jpeg" />
|
|
119
|
+
```
|
|
120
|
+
🚀 [Live demo | Alpine.js x-bound: Bind Files](https://jsfiddle.net/rameel/phy2zn0a/)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
### Binding `<select>`
|
|
124
|
+
|
|
125
|
+
To bind the value of a `<select>` element, use the `value` property:
|
|
126
|
+
```html
|
|
127
|
+
<div x-data="{ fruit: '' }">
|
|
128
|
+
<select &value="fruit">
|
|
129
|
+
<option value="" disabled>Select...</option>
|
|
130
|
+
<option>Apple</option>
|
|
131
|
+
<option>Banana</option>
|
|
132
|
+
<option>Orange</option>
|
|
133
|
+
<option>Grape</option>
|
|
134
|
+
<option>Mango</option>
|
|
135
|
+
</select>
|
|
136
|
+
|
|
137
|
+
<p>
|
|
138
|
+
Fruit: <span x-text="fruit"></span>
|
|
139
|
+
</p>
|
|
140
|
+
</div>
|
|
141
|
+
```
|
|
142
|
+
🚀 [Live demo | Alpine.js x-bound: Binding select](https://jsfiddle.net/rameel/fs12bo5m/)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
For a `<select multiple>` element, the data property is an array containing the values of the selected options.
|
|
146
|
+
|
|
147
|
+
```html
|
|
148
|
+
<div x-data="{ pets: ['goldfish', 'parrot'] }">
|
|
149
|
+
<select &value="pets" multiple>
|
|
150
|
+
<option value="cat">Cat</option>
|
|
151
|
+
<option value="goldfish">Goldfish</option>
|
|
152
|
+
<option value="parrot">Parrot</option>
|
|
153
|
+
<option value="spider">Spider</option>
|
|
154
|
+
</select>
|
|
155
|
+
|
|
156
|
+
Pets: <span x-text="pets"></span>
|
|
157
|
+
</div>
|
|
158
|
+
```
|
|
159
|
+
🚀 [Live demo | Alpine.js x-bound: Multiple select](https://jsfiddle.net/rameel/kq0xseo1/)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
### Binding `<details>`
|
|
163
|
+
|
|
164
|
+
The directive also allows binding to the `open` property of `<details>` elements:
|
|
165
|
+
|
|
166
|
+
```html
|
|
167
|
+
<div x-data="{ open: true }">
|
|
168
|
+
<details &open>
|
|
169
|
+
<summary>Details</summary>
|
|
170
|
+
<p>Something small enough to escape casual notice.</p>
|
|
171
|
+
</details>
|
|
172
|
+
|
|
173
|
+
<p>
|
|
174
|
+
<label>
|
|
175
|
+
<input &checked="open" type="checkbox" />
|
|
176
|
+
Open / Close
|
|
177
|
+
</label>
|
|
178
|
+
</p>
|
|
179
|
+
</div>
|
|
180
|
+
```
|
|
181
|
+
🚀 [Live demo | Alpine.js x-bound: Binding details](https://jsfiddle.net/rameel/fw2bkLqv/)
|
|
182
|
+
|
|
183
|
+
### Binding `<img>` sizes
|
|
184
|
+
|
|
185
|
+
You can bind the `naturalWidth` and `naturalHeight` properties of an image after it loads:
|
|
186
|
+
|
|
187
|
+
```html
|
|
188
|
+
<img src="..." &naturalWidth="width" &naturalHeight="height" />
|
|
189
|
+
```
|
|
190
|
+
🚀 [Live demo | Alpine.js x-bound: Binding image sizes](https://jsfiddle.net/rameel/q4vb1d0w/)
|
|
191
|
+
|
|
192
|
+
> [!TIP]
|
|
193
|
+
> If you prefer using `kebab-case` for multi-word properties like `naturalWidth`, you can write it as `natural-width`. It will be automatically normalized internally:
|
|
194
|
+
> ```html
|
|
195
|
+
> <img src="..." &natural-width="width" &natural-height="height" />
|
|
196
|
+
> ```
|
|
197
|
+
|
|
198
|
+
> [!NOTE]
|
|
199
|
+
> As HTML attributes are case-insensitive, corresponding properties can be written as follows:
|
|
200
|
+
> ```html
|
|
201
|
+
> <img src="..." &naturalwidth="width" &naturalheight="height" />
|
|
202
|
+
> ```
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
> [!NOTE]
|
|
206
|
+
> The `naturalWidth` and `naturalHeight` properties are read-only and reflect the original image dimensions, available after the image has loaded.
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
### Binding `<video>` sizes
|
|
210
|
+
|
|
211
|
+
You can bind the `videoWidth` and `videoHeight` properties of a video after it loads:
|
|
212
|
+
|
|
213
|
+
```html
|
|
214
|
+
<video &videoWidth="width" &videoHeight="height">
|
|
215
|
+
<source src="..." type="video/mp4">
|
|
216
|
+
</video>
|
|
217
|
+
```
|
|
218
|
+
🚀 [Live demo | Alpine.js x-bound: Binding video sizes](https://jsfiddle.net/rameel/nah2pfcx/)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
### Binding `contenteditable` elements
|
|
222
|
+
|
|
223
|
+
For `contenteditable` elements, you can bind the following properties:
|
|
224
|
+
- [innerHTML](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML)
|
|
225
|
+
- [innerText](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText)
|
|
226
|
+
- [textContent](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
|
|
227
|
+
|
|
228
|
+
```html
|
|
229
|
+
<div &innerHtml="html" contenteditable="true"></div>
|
|
230
|
+
```
|
|
231
|
+
🚀 [Live demo | Alpine.js x-bound: Contenteditable bindings](https://jsfiddle.net/rameel/n5sj0rdz/)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
### Binding block-level element sizes
|
|
235
|
+
|
|
236
|
+
You can bind to the following properties to get the **width** and **height** of block-level elements,
|
|
237
|
+
measured with a `ResizeObserver`. The values will update whenever the element's size changes:
|
|
238
|
+
|
|
239
|
+
- [clientHeight](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight)
|
|
240
|
+
- [clientWidth](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth)
|
|
241
|
+
- [offsetHeight](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight)
|
|
242
|
+
- [offsetWidth](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth)
|
|
243
|
+
|
|
244
|
+
```html
|
|
245
|
+
<div &client-width="width" &client-height="height"></div>
|
|
246
|
+
```
|
|
247
|
+
🚀 [Live demo | Alpine.js x-bound: Binding element dimensions](https://jsfiddle.net/rameel/jc4eu921/)
|
|
248
|
+
|
|
249
|
+
> [!NOTE]
|
|
250
|
+
> These properties are read-only.
|
|
251
|
+
|
|
252
|
+
> [!IMPORTANT]
|
|
253
|
+
> Elements with `display: inline` don't have an explicit width or height (unless they are intrinsically sized, like `<img>` or `<canvas>`). Therefore, a `ResizeObserver` cannot track their size. If you need to observe their size, change their `display` style to something like `inline-block`.
|
|
254
|
+
>
|
|
255
|
+
> Also keep in mind that CSS transforms do not trigger `ResizeObserver` updates.
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
### Binding group of `<input type="radio">` and `<input type="checkbox">`
|
|
259
|
+
The group of `<input>` elements that should function together can utilize the `group` bound property.
|
|
260
|
+
|
|
261
|
+
```html
|
|
262
|
+
<div x-data="{ pets: ['goldfish', 'parrot'], contact: 'Email' }">
|
|
263
|
+
|
|
264
|
+
<!-- grouped checkboxes are similar to "select multiple"
|
|
265
|
+
and use an array for selected options -->
|
|
266
|
+
<input &group="pets" type="checkbox" value="cat" />
|
|
267
|
+
<input &group="pets" type="checkbox" value="goldfish" />
|
|
268
|
+
<input &group="pets" type="checkbox" value="parrot" />
|
|
269
|
+
<input &group="pets" type="checkbox" value="spider" />
|
|
270
|
+
|
|
271
|
+
<!-- grouped radio inputs are mutually exclusive -->
|
|
272
|
+
<input &group="contact" type="radio" value="Email" />
|
|
273
|
+
<input &group="contact" type="radio" value="Phone" />
|
|
274
|
+
<input &group="contact" type="radio" value="Mail" />
|
|
275
|
+
|
|
276
|
+
</div>
|
|
277
|
+
```
|
|
278
|
+
🚀 [Live demo | Alpine.js x-bound: Binding group inputs](https://jsfiddle.net/rameel/f5jpry7b/)
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
### Binding `input[type="checkbox"]:indeterminate` property
|
|
282
|
+
The `x-bound` directive supports binding the `indeterminate` property of `<input type="checkbox">` elements,
|
|
283
|
+
allowing you to control the checkbox's indeterminate state (a state where the checkbox is neither checked nor unchecked,
|
|
284
|
+
typically represented visually with a dash or similar indicator).
|
|
285
|
+
|
|
286
|
+
```html
|
|
287
|
+
<div x-data="{ checked: false, indeterminate: true }">
|
|
288
|
+
<input type="checkbox" &checked &indeterminate />
|
|
289
|
+
|
|
290
|
+
<template x-match>
|
|
291
|
+
<span x-case="indeterminate">Waiting...</span>
|
|
292
|
+
<span x-case="checked">Checked</span>
|
|
293
|
+
<span x-default>Unchecked</span>
|
|
294
|
+
</template>
|
|
295
|
+
</div>
|
|
296
|
+
```
|
|
297
|
+
🚀 [Live demo | Alpine.js x-bound: Binding indeterminate](https://jsfiddle.net/rameel/o8ubzac0/)
|
|
298
|
+
|
|
299
|
+
This is useful for scenarios like selecting a subset of items in a list, such as in a table header checkbox:
|
|
300
|
+
```html
|
|
301
|
+
<table>
|
|
302
|
+
<thead>
|
|
303
|
+
<tr>
|
|
304
|
+
<th>
|
|
305
|
+
<input type="checkbox"
|
|
306
|
+
&indeterminate="isPartialSelected"
|
|
307
|
+
&checked="isAllSelected" />
|
|
308
|
+
</th>
|
|
309
|
+
<th>Value</th>
|
|
310
|
+
</tr>
|
|
311
|
+
</thead>
|
|
312
|
+
...
|
|
313
|
+
</table>
|
|
314
|
+
```
|
|
315
|
+
🚀 [Live demo | Alpine.js x-bound: Binding indeterminate (table)](https://jsfiddle.net/rameel/ryvhw3jt/)
|
|
316
|
+
|
|
317
|
+
In this example, the `indeterminate` property of the checkbox is bound to the `isPartialSelected` data property.
|
|
318
|
+
When `isPartialSelected` is `true`, the checkbox will be in the indeterminate state.
|
|
319
|
+
|
|
320
|
+
> [!NOTE]
|
|
321
|
+
> The `indeterminate` binding is one-way. Changes to the indeterminate property in the DOM (e.g., via user interaction or programmatically) do not update the bound data property.
|
|
322
|
+
|
|
323
|
+
### Binding `Alpine` data properties
|
|
324
|
+
|
|
325
|
+
The directive also supports synchronizing values between two data properties.
|
|
326
|
+
|
|
327
|
+
```html
|
|
328
|
+
<div x-data="{ number: 5 }">
|
|
329
|
+
<div x-data="{ count: 0 }" &count="number">
|
|
330
|
+
<button @click="count++">Increment</button>
|
|
331
|
+
</div>
|
|
332
|
+
|
|
333
|
+
Number: <span x-text="number"></span>
|
|
334
|
+
</div>
|
|
335
|
+
```
|
|
336
|
+
🚀 [Live demo | Alpine.js x-bound: Binding data properties](https://jsfiddle.net/rameel/972qyomn/)
|
|
337
|
+
|
|
338
|
+
In this example, we bind the outer `number` property to the inner `count` property. Since `number` is initially set to `5`,
|
|
339
|
+
the `count` property is also set to `5` when the binding occurs.
|
|
340
|
+
|
|
341
|
+
By default, the binding is two-way, so changes in `count` are reflected in `number` and vice versa.
|
|
342
|
+
|
|
343
|
+
But what if we want changes to propagate in one direction only? For this case, the `x-bound` directive
|
|
344
|
+
provides three modifiers to control data flow:
|
|
345
|
+
|
|
346
|
+
> [!TIP]
|
|
347
|
+
> - **`inout`**: Binding works in both directions. This means that changes in one property are automatically reflected in the other and vice versa. This modifier is used by default.
|
|
348
|
+
>
|
|
349
|
+
> Example: If we have the property `&count="number"`, then changing the value of `count` will also change the value of `number`, and vice versa.
|
|
350
|
+
>
|
|
351
|
+
> - **`in`**: Binding works in one direction only — from the source property to the target property. This means that changes in the source property are passed to the target property, but changes in the target property do not affect the source property.
|
|
352
|
+
>
|
|
353
|
+
> Example: If we have `&count.in="number"`, then changes in `number` will be passed to `count`, but changes in `count` will not be reflected in `number`.
|
|
354
|
+
>
|
|
355
|
+
> - **`out`**: Binding works in one direction only — from the target property to the source property. This means that changes in the target property are passed to the source property, but changes in the source property do not affect the target property.
|
|
356
|
+
>
|
|
357
|
+
> Example: If we have `&count.out="number"`, then changes in `count` will be passed to `number`, but changes in `number` will not be reflected in `count`.
|
|
358
|
+
|
|
359
|
+
> [!NOTE]
|
|
360
|
+
> The default behavior (`inout`) can also be achieved using the `x-model` and `x-modelable` directives.
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
## Source code
|
|
364
|
+
You can find the source code for this plugin on GitHub:
|
|
365
|
+
|
|
366
|
+
https://github.com/rameel/ramstack.alpinegear.js/tree/main/src/plugins/bound
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
## Related packages
|
|
370
|
+
This package is part of **[AlpineGear](https://github.com/rameel/ramstack.alpinegear.js)** —
|
|
371
|
+
a collection of utilities and directives for [Alpine.js](https://alpinejs.dev).
|
|
372
|
+
|
|
373
|
+
You can find the full list of related packages and their documentation here:
|
|
374
|
+
https://github.com/rameel/ramstack.alpinegear.js
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
## Contributions
|
|
378
|
+
Bug reports and contributions are welcome.
|
|
379
|
+
|
|
380
|
+
## License
|
|
381
|
+
This package is released as open source under the **MIT License**.
|
|
382
|
+
See the [LICENSE](https://github.com/rameel/ramstack.alpinegear.js/blob/main/LICENSE) file for more details.
|