@alpinejs/docs 3.9.6-revision.1 → 3.10.0-revision.1
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/package.json +1 -1
- package/src/en/directives/bind.md +2 -1
- package/src/en/directives/for.md +1 -1
- package/src/en/directives/model.md +1 -1
- package/src/en/directives/on.md +2 -1
- package/src/en/directives/teleport.md +1 -1
- package/src/en/essentials/installation.md +1 -1
- package/src/en/essentials/state.md +1 -1
- package/src/en/magics/id.md +1 -1
- package/src/en/magics/nextTick.md +19 -0
- package/src/en/plugins/focus.md +2 -2
- package/src/en/plugins/intersect.md +1 -1
- package/src/en/plugins/mask.md +143 -0
- package/src/en/plugins/morph.md +2 -2
- package/src/en/upgrade-guide.md +1 -1
package/package.json
CHANGED
|
@@ -159,7 +159,7 @@ And like most expressions in Alpine, you can always use the result of a JavaScri
|
|
|
159
159
|
|
|
160
160
|
`x-bind` allows you to bind an object of different directives and attributes to an element.
|
|
161
161
|
|
|
162
|
-
The object keys can be anything you would normally write as an attribute name in Alpine. This includes Alpine directives and modifiers, but also plain HTML attributes. The object values are either plain strings, or in the case of dynamic Alpine
|
|
162
|
+
The object keys can be anything you would normally write as an attribute name in Alpine. This includes Alpine directives and modifiers, but also plain HTML attributes. The object values are either plain strings, or in the case of dynamic Alpine directives, callbacks to be evaluated by Alpine.
|
|
163
163
|
|
|
164
164
|
```alpine
|
|
165
165
|
<div x-data="dropdown()">
|
|
@@ -174,6 +174,7 @@ The object keys can be anything you would normally write as an attribute name in
|
|
|
174
174
|
open: false,
|
|
175
175
|
|
|
176
176
|
trigger: {
|
|
177
|
+
['x-ref']: 'trigger',
|
|
177
178
|
['@click']() {
|
|
178
179
|
this.open = true
|
|
179
180
|
},
|
package/src/en/directives/for.md
CHANGED
|
@@ -33,7 +33,7 @@ There are two rules worth noting about `x-for`:
|
|
|
33
33
|
<a name="keys"></a>
|
|
34
34
|
## Keys
|
|
35
35
|
|
|
36
|
-
It is important to specify keys for each `x-for` iteration if you are going to be re-ordering items. Without dynamic keys, Alpine may have a hard time keeping track of what re-orders and will cause odd side-effects.
|
|
36
|
+
It is important to specify unique keys for each `x-for` iteration if you are going to be re-ordering items. Without dynamic keys, Alpine may have a hard time keeping track of what re-orders and will cause odd side-effects.
|
|
37
37
|
|
|
38
38
|
```alpine
|
|
39
39
|
<ul x-data="{ colors: [
|
|
@@ -288,7 +288,7 @@ Color: <span x-text="color"></span>
|
|
|
288
288
|
<a name="lazy"></a>
|
|
289
289
|
### `.lazy`
|
|
290
290
|
|
|
291
|
-
On text inputs, by default, `x-model` updates the property on every
|
|
291
|
+
On text inputs, by default, `x-model` updates the property on every keystroke. By adding the `.lazy` modifier, you can force an `x-model` input to only update the property when user focuses away from the input element.
|
|
292
292
|
|
|
293
293
|
This is handy for things like real-time form-validation where you might not want to show an input validation error until the user "tabs" away from a field.
|
|
294
294
|
|
package/src/en/directives/on.md
CHANGED
|
@@ -89,6 +89,7 @@ For easy reference, here is a list of common keys you may want to listen for.
|
|
|
89
89
|
| `.caps-lock` | Caps Lock |
|
|
90
90
|
| `.equal` | Equal, `=` |
|
|
91
91
|
| `.period` | Period, `.` |
|
|
92
|
+
| `.slash` | Foward Slash, `/` |
|
|
92
93
|
|
|
93
94
|
<a name="custom-events"></a>
|
|
94
95
|
## Custom events
|
|
@@ -165,7 +166,7 @@ In the above example, clicking the button WON'T log the message. This is because
|
|
|
165
166
|
|
|
166
167
|
In the above example, after showing the dropdown contents by clicking the "Toggle" button, you can close the dropdown by clicking anywhere on the page outside the content.
|
|
167
168
|
|
|
168
|
-
This is because `.outside` is listening for clicks that
|
|
169
|
+
This is because `.outside` is listening for clicks that DON'T originate from the element it's registered on.
|
|
169
170
|
|
|
170
171
|
> It's worth noting that the `.outside` expression will only be evaluated when the element it's registered on is visible on the page. Otherwise, there would be nasty race conditions where clicking the "Toggle" button would also fire the `@click.outside` handler when it is not visible.
|
|
171
172
|
|
|
@@ -101,7 +101,7 @@ To make this experience more seamless, you can "forward" events by simply regist
|
|
|
101
101
|
|
|
102
102
|
Notice how we are now able to listen for events dispatched from within the teleported element from outside the `<template>` element itself?
|
|
103
103
|
|
|
104
|
-
Alpine does this by looking for event listeners registered on `<template x-teleport...>` and stops those events from
|
|
104
|
+
Alpine does this by looking for event listeners registered on `<template x-teleport...>` and stops those events from propagating past the live, teleported, DOM element. Then, it creates a copy of that event and re-dispatches it from `<template x-teleport...>`.
|
|
105
105
|
|
|
106
106
|
<a name="nesting"></a>
|
|
107
107
|
## Nesting
|
|
@@ -33,7 +33,7 @@ This is by far the simplest way to get started with Alpine. Include the followin
|
|
|
33
33
|
Notice the `@3.x.x` in the provided CDN link. This will pull the latest version of Alpine version 3. For stability in production, it's recommended that you hardcode the latest version in the CDN link.
|
|
34
34
|
|
|
35
35
|
```alpine
|
|
36
|
-
<script defer src="https://unpkg.com/alpinejs@3.
|
|
36
|
+
<script defer src="https://unpkg.com/alpinejs@3.10.0/dist/cdn.min.js"></script>
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
That's it! Alpine is now available for use inside your page.
|
|
@@ -54,7 +54,7 @@ Although this may seem obvious to some, it's worth mentioning that Alpine data c
|
|
|
54
54
|
<a name="data-less-alpine"></a>
|
|
55
55
|
### Data-less Alpine
|
|
56
56
|
|
|
57
|
-
Sometimes you may want to use Alpine functionality, but don't need any reactive data. In these cases, you can opt
|
|
57
|
+
Sometimes you may want to use Alpine functionality, but don't need any reactive data. In these cases, you can opt out of passing an expression to `x-data` entirely. For example:
|
|
58
58
|
|
|
59
59
|
```alpine
|
|
60
60
|
<button x-data @click="alert('I\'ve been clicked!')">Click Me</button>
|
package/src/en/magics/id.md
CHANGED
|
@@ -34,7 +34,7 @@ Now let's say you want to have those same two input elements, but this time you
|
|
|
34
34
|
|
|
35
35
|
This presents a problem, you now need to be able to reference the same ID twice. One for the `<label>`'s `for` attribute, and the other for the `id` on the input.
|
|
36
36
|
|
|
37
|
-
Here
|
|
37
|
+
Here is a way that you might think to accomplish this and is totally valid:
|
|
38
38
|
|
|
39
39
|
```alpine
|
|
40
40
|
<div x-data="{ id: $id('text-input') }">
|
|
@@ -21,3 +21,22 @@ title: nextTick
|
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
In the above example, rather than logging "Hello" to the console, "Hello World!" will be logged because `$nextTick` was used to wait until Alpine was finished updating the DOM.
|
|
24
|
+
|
|
25
|
+
<a name="promises"></a>
|
|
26
|
+
|
|
27
|
+
## Promises
|
|
28
|
+
|
|
29
|
+
`$nextTick` returns a promise, allowing the use of `$nextTick` to pause an async function until after pending dom updates. When used like this, `$nextTick` also does not require an argument to be passed.
|
|
30
|
+
|
|
31
|
+
```alpine
|
|
32
|
+
<div x-data="{ title: 'Hello' }">
|
|
33
|
+
<button
|
|
34
|
+
@click="
|
|
35
|
+
title = 'Hello World!';
|
|
36
|
+
await $nextTick();
|
|
37
|
+
console.log($el.innerText);
|
|
38
|
+
"
|
|
39
|
+
x-text="title"
|
|
40
|
+
></button>
|
|
41
|
+
</div>
|
|
42
|
+
```
|
package/src/en/plugins/focus.md
CHANGED
|
@@ -187,7 +187,7 @@ Here is nesting in action:
|
|
|
187
187
|
<a name="inert"></a>
|
|
188
188
|
#### .inert
|
|
189
189
|
|
|
190
|
-
When building things like dialogs/modals, it's recommended to hide all the other elements on the page from
|
|
190
|
+
When building things like dialogs/modals, it's recommended to hide all the other elements on the page from screen readers when trapping focus.
|
|
191
191
|
|
|
192
192
|
By adding `.inert` to `x-trap`, when focus is trapped, all other elements on the page will receive `aria-hidden="true"` attributes, and when focus trapping is disabled, those attributes will also be removed.
|
|
193
193
|
|
|
@@ -218,7 +218,7 @@ By adding `.inert` to `x-trap`, when focus is trapped, all other elements on the
|
|
|
218
218
|
<a name="noscroll"></a>
|
|
219
219
|
#### .noscroll
|
|
220
220
|
|
|
221
|
-
When building dialogs/modals with Alpine, it's recommended that you disable
|
|
221
|
+
When building dialogs/modals with Alpine, it's recommended that you disable scrolling for the surrounding content when the dialog is open.
|
|
222
222
|
|
|
223
223
|
`x-trap` allows you to do this automatically with the `.noscroll` modifiers.
|
|
224
224
|
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
---
|
|
2
|
+
order: 1
|
|
3
|
+
title: Mask
|
|
4
|
+
description: Automatically format text fields as users type
|
|
5
|
+
graph_image: https://alpinejs.dev/social_mask.jpg
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Mask Plugin
|
|
9
|
+
|
|
10
|
+
Alpine's Mask plugin allows you to automatically format a text input field as a user types.
|
|
11
|
+
|
|
12
|
+
This is useful for many different types of inputs: phone numbers, credit cards, dollar amounts, account numbers, dates, etc.
|
|
13
|
+
|
|
14
|
+
<a name="installation"></a>
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
<div x-data="{ expanded: false }">
|
|
18
|
+
<div class=" relative">
|
|
19
|
+
<div x-show="! expanded" class="absolute inset-0 flex justify-start items-end bg-gradient-to-t from-white to-[#ffffff66]"></div>
|
|
20
|
+
<div x-show="expanded" x-collapse.min.80px class="markdown">
|
|
21
|
+
|
|
22
|
+
You can use this plugin by either including it from a `<script>` tag or installing it via NPM:
|
|
23
|
+
|
|
24
|
+
### Via CDN
|
|
25
|
+
|
|
26
|
+
You can include the CDN build of this plugin as a `<script>` tag, just make sure to include it BEFORE Alpine's core JS file.
|
|
27
|
+
|
|
28
|
+
```alpine
|
|
29
|
+
<!-- Alpine Plugins -->
|
|
30
|
+
<script defer src="https://unpkg.com/@alpinejs/mask@3.x.x/dist/cdn.min.js"></script>
|
|
31
|
+
|
|
32
|
+
<!-- Alpine Core -->
|
|
33
|
+
<script defer src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Via NPM
|
|
37
|
+
|
|
38
|
+
You can install Mask from NPM for use inside your bundle like so:
|
|
39
|
+
|
|
40
|
+
```shell
|
|
41
|
+
npm install @alpinejs/mask
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then initialize it from your bundle:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import Alpine from 'alpinejs'
|
|
48
|
+
import mask from '@alpinejs/mask'
|
|
49
|
+
|
|
50
|
+
Alpine.plugin(mask)
|
|
51
|
+
|
|
52
|
+
...
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
</div>
|
|
56
|
+
</div>
|
|
57
|
+
<button :aria-expanded="expanded" @click="expanded = ! expanded" class="text-aqua-600 font-medium underline">
|
|
58
|
+
<span x-text="expanded ? 'Hide' : 'Show more'">Show</span> <span x-text="expanded ? '↑' : '↓'">↓</span>
|
|
59
|
+
</button>
|
|
60
|
+
</div>
|
|
61
|
+
|
|
62
|
+
<a name="x-mask"></a>
|
|
63
|
+
## x-mask
|
|
64
|
+
|
|
65
|
+
The primary API for using this plugin is the `x-mask` directive.
|
|
66
|
+
|
|
67
|
+
Let's start by looking at the following simple example of a date field:
|
|
68
|
+
|
|
69
|
+
```alpine
|
|
70
|
+
<input x-mask="99/99/9999" placeholder="MM/DD/YYYY">
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
<!-- START_VERBATIM -->
|
|
74
|
+
<div class="demo">
|
|
75
|
+
<input x-data x-mask="99/99/9999" placeholder="MM/DD/YYYY">
|
|
76
|
+
</div>
|
|
77
|
+
<!-- END_VERBATIM -->
|
|
78
|
+
|
|
79
|
+
Notice how the text you type into the input field must adhere to the format provided by `x-mask`. In addition to enforcing numeric characters, the forward slashes `/` are also automatically added if a user doesn't type them first.
|
|
80
|
+
|
|
81
|
+
The following wildcard characters are supported in masks:
|
|
82
|
+
|
|
83
|
+
| Wildcard | Description |
|
|
84
|
+
| -------------------------- | --------------------------- |
|
|
85
|
+
| `*` | Any character |
|
|
86
|
+
| `a` | Only alpha characters (a-z, A-Z) |
|
|
87
|
+
| `9` | Only numeric characters (0-9) |
|
|
88
|
+
|
|
89
|
+
<a name="mask-functions"></a>
|
|
90
|
+
## Custom Mask Functions
|
|
91
|
+
|
|
92
|
+
Sometimes simple mask literals (i.e. `(999) 999-9999`) are not sufficient. In these cases, `x-mask:function` allows you to dynamically generate masks on the fly based on user input.
|
|
93
|
+
|
|
94
|
+
Here's an example of a credit card input that needs to change it's mask based on if the number starts with the numbers "34" or "37" (which means it's an Amex card and therefore has a different format).
|
|
95
|
+
|
|
96
|
+
```alpine
|
|
97
|
+
<input x-mask:function="
|
|
98
|
+
$input.startsWith('34') || $input.startsWith('37')
|
|
99
|
+
? '9999 999999 99999' : '9999 9999 9999 9999'
|
|
100
|
+
">
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
As you can see in the above example, every time a user types in the input, that value is passed to the function as `$input`. Based on the `$input`, a different mask is utilized in the field.
|
|
104
|
+
|
|
105
|
+
Try it for yourself by typing a number that starts with "34" and one that doesn't.
|
|
106
|
+
|
|
107
|
+
<!-- START_VERBATIM -->
|
|
108
|
+
<div class="demo">
|
|
109
|
+
<input x-data x-mask:function="
|
|
110
|
+
$input.startsWith('34') || $input.startsWith('37')
|
|
111
|
+
? '9999 999999 99999' : '9999 9999 9999 9999'
|
|
112
|
+
">
|
|
113
|
+
</div>
|
|
114
|
+
<!-- END_VERBATIM -->
|
|
115
|
+
|
|
116
|
+
<a name="money-inputs"></a>
|
|
117
|
+
## Money Inputs
|
|
118
|
+
|
|
119
|
+
Because writing your own custom mask function for money inputs is fairly complex, Alpine offers a prebuilt one and makes it available as `$money()`.
|
|
120
|
+
|
|
121
|
+
Here is a fully functioning money input mask:
|
|
122
|
+
|
|
123
|
+
```alpine
|
|
124
|
+
<input x-mask:function="$money($input)">
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
<!-- START_VERBATIM -->
|
|
128
|
+
<div class="demo" x-data>
|
|
129
|
+
<input type="text" x-mask:function="$money($input)" placeholder="0.00">
|
|
130
|
+
</div>
|
|
131
|
+
<!-- END_VERBATIM -->
|
|
132
|
+
|
|
133
|
+
If you wish to swap the periods for commas and vice versa (as is required in certain currencies), you can do so using the second optional parameter:
|
|
134
|
+
|
|
135
|
+
```alpine
|
|
136
|
+
<input x-mask:function="$money($input, ',')">
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
<!-- START_VERBATIM -->
|
|
140
|
+
<div class="demo" x-data>
|
|
141
|
+
<input type="text" x-mask:function="$money($input, ',')" placeholder="0.00">
|
|
142
|
+
</div>
|
|
143
|
+
<!-- END_VERBATIM -->
|
package/src/en/plugins/morph.md
CHANGED
|
@@ -137,7 +137,7 @@ Here's an example of using `Alpine.morph()` to update an Alpine component with n
|
|
|
137
137
|
|
|
138
138
|
The "Morph" plugin works by comparing two DOM trees, the live element, and the passed in HTML.
|
|
139
139
|
|
|
140
|
-
Morph walks both trees
|
|
140
|
+
Morph walks both trees simultaneously and compares each node and its children. If it finds differences, it "patches" (changes) the current DOM tree to match the passed in HTML's tree.
|
|
141
141
|
|
|
142
142
|
While the default algorithm is very capable, there are cases where you may want to hook into its lifecycle and observe or change its behavior as it's happening.
|
|
143
143
|
|
|
@@ -249,4 +249,4 @@ By adding keys to each node, we can accomplish this like so:
|
|
|
249
249
|
|
|
250
250
|
Now that there are "keys" on the `<li>`s, Morph will match them in both trees and move them accordingly.
|
|
251
251
|
|
|
252
|
-
You can configure what Morph considers a "key" with the `key:`
|
|
252
|
+
You can configure what Morph considers a "key" with the `key:` configuration option. [More on that here](#lifecycle-hooks)
|
package/src/en/upgrade-guide.md
CHANGED
|
@@ -299,7 +299,7 @@ In Alpine V2 for below code
|
|
|
299
299
|
</div>
|
|
300
300
|
```
|
|
301
301
|
|
|
302
|
-
after clicking button all `$refs` were displayed. However in Alpine V3 it's possible to access only `$refs` for elements created statically, so only first ref will be returned as expected.
|
|
302
|
+
after clicking button all `$refs` were displayed. However, in Alpine V3 it's possible to access only `$refs` for elements created statically, so only first ref will be returned as expected.
|
|
303
303
|
|
|
304
304
|
|
|
305
305
|
<a name="no-ie-11"></a>
|