@alpinejs/docs 3.13.8-revision.1 → 3.13.8-revision.3
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/plugins/anchor.md +3 -3
- package/src/en/plugins/mask.md +1 -1
- package/src/en/plugins/sort.md +259 -0
package/package.json
CHANGED
package/src/en/plugins/anchor.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
order: 5
|
|
3
3
|
title: Anchor
|
|
4
|
-
description: Anchor an element's positioning to another element on the
|
|
4
|
+
description: Anchor an element's positioning to another element on the page
|
|
5
5
|
graph_image: https://alpinejs.dev/social_anchor.jpg
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
# Anchor Plugin
|
|
9
9
|
|
|
10
|
-
Alpine's Anchor plugin allows you easily anchor an element's positioning to another element on the page.
|
|
10
|
+
Alpine's Anchor plugin allows you to easily anchor an element's positioning to another element on the page.
|
|
11
11
|
|
|
12
12
|
This functionality is useful when creating dropdown menus, popovers, dialogs, and tooltips with Alpine.
|
|
13
13
|
|
|
@@ -56,7 +56,7 @@ The primary API for using this plugin is the `x-anchor` directive.
|
|
|
56
56
|
|
|
57
57
|
To use this plugin, add the `x-anchor` directive to any element and pass it a reference to the element you want to anchor it's position to (often a button on the page).
|
|
58
58
|
|
|
59
|
-
By default, `x-anchor` will set the
|
|
59
|
+
By default, `x-anchor` will set the element's CSS to `position: absolute` and the appropriate `top` and `left` values. If the anchored element is normally displayed below the reference element but doesn't have room on the page, it's styling will be adjusted to render above the element.
|
|
60
60
|
|
|
61
61
|
For example, here's a simple dropdown anchored to the button that toggles it:
|
|
62
62
|
|
package/src/en/plugins/mask.md
CHANGED
|
@@ -116,7 +116,7 @@ Try it for yourself by typing a number that starts with "34" and one that doesn'
|
|
|
116
116
|
</div>
|
|
117
117
|
<!-- END_VERBATIM -->
|
|
118
118
|
|
|
119
|
-
`x-mask:dynamic` also accepts a function as a result of the expression and will automatically pass it the `$input` as the
|
|
119
|
+
`x-mask:dynamic` also accepts a function as a result of the expression and will automatically pass it the `$input` as the first parameter. For example:
|
|
120
120
|
|
|
121
121
|
```alpine
|
|
122
122
|
<input x-mask:dynamic="creditCardMask">
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
---
|
|
2
|
+
order: 6
|
|
3
|
+
title: Sort
|
|
4
|
+
description: Easily re-order elements by dragging them with your mouse
|
|
5
|
+
graph_image: https://alpinejs.dev/social_sort.jpg
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Sort Plugin
|
|
9
|
+
|
|
10
|
+
Alpine's Sort plugin allows you to easily re-order elements by dragging them with your mouse.
|
|
11
|
+
|
|
12
|
+
This functionality is useful for things like Kanban boards, to-do lists, sortable table columns, etc.
|
|
13
|
+
|
|
14
|
+
The drag functionality used in this plugin is provided by the [SortableJS](https://github.com/SortableJS/Sortable) project.
|
|
15
|
+
|
|
16
|
+
<a name="installation"></a>
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
You can use this plugin by either including it from a `<script>` tag or installing it via NPM:
|
|
20
|
+
|
|
21
|
+
### Via CDN
|
|
22
|
+
|
|
23
|
+
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.
|
|
24
|
+
|
|
25
|
+
```alpine
|
|
26
|
+
<!-- Alpine Plugins -->
|
|
27
|
+
<script defer src="https://cdn.jsdelivr.net/npm/@alpinejs/sort@3.x.x/dist/cdn.min.js"></script>
|
|
28
|
+
|
|
29
|
+
<!-- Alpine Core -->
|
|
30
|
+
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Via NPM
|
|
34
|
+
|
|
35
|
+
You can install Anchor from NPM for use inside your bundle like so:
|
|
36
|
+
|
|
37
|
+
```shell
|
|
38
|
+
npm install @alpinejs/sort
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Then initialize it from your bundle:
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import Alpine from 'alpinejs'
|
|
45
|
+
import sort from '@alpinejs/sort'
|
|
46
|
+
|
|
47
|
+
Alpine.plugin(sort)
|
|
48
|
+
|
|
49
|
+
...
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
<a name="basic-usage"></a>
|
|
53
|
+
## Basic usage
|
|
54
|
+
|
|
55
|
+
The primary API for using this plugin is the `x-sort` directive. By adding `x-sort` to an element, its children become sortable—meaning you can drag them around with your mouse, and they will change positions.
|
|
56
|
+
|
|
57
|
+
```alpine
|
|
58
|
+
<ul x-sort>
|
|
59
|
+
<li>foo</li>
|
|
60
|
+
<li>bar</li>
|
|
61
|
+
<li>baz</li>
|
|
62
|
+
</ul>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
<!-- START_VERBATIM -->
|
|
66
|
+
<div x-data>
|
|
67
|
+
<ul x-sort>
|
|
68
|
+
<li>foo</li>
|
|
69
|
+
<li>bar</li>
|
|
70
|
+
<li>baz</li>
|
|
71
|
+
</ul>
|
|
72
|
+
</div>
|
|
73
|
+
<!-- END_VERBATIM -->
|
|
74
|
+
|
|
75
|
+
<a name="sort-handlers"></a>
|
|
76
|
+
## Sort handlers
|
|
77
|
+
|
|
78
|
+
You can react to sorting changes by passing a handler function to `x-sort` and adding keys to each item using `x-sort:key`. Here is an example of a simple handler function that shows an alert dialog with the changed item's key and its new position:
|
|
79
|
+
|
|
80
|
+
```alpine
|
|
81
|
+
<ul x-sort="alert($key + ' - ' + $position)">
|
|
82
|
+
<li x-sort:key="1">foo</li>
|
|
83
|
+
<li x-sort:key="2">bar</li>
|
|
84
|
+
<li x-sort:key="3">baz</li>
|
|
85
|
+
</ul>
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
<!-- START_VERBATIM -->
|
|
89
|
+
<div x-data>
|
|
90
|
+
<ul x-sort="alert($key + ' - ' + $position)">
|
|
91
|
+
<li x-sort:key="1">foo</li>
|
|
92
|
+
<li x-sort:key="2">bar</li>
|
|
93
|
+
<li x-sort:key="3">baz</li>
|
|
94
|
+
</ul>
|
|
95
|
+
</div>
|
|
96
|
+
<!-- END_VERBATIM -->
|
|
97
|
+
|
|
98
|
+
The `x-sort` handler will be called every time the sort order of the items change. The `$key` magic will contain the key of the sorted element (derived from `x-sort:key`), and `$position` will contain the new position of the item (staring at index `0`).
|
|
99
|
+
|
|
100
|
+
You can also pass a handler function to `x-sort` and that function will receive the `key` and `position` as the first and second parameter:
|
|
101
|
+
|
|
102
|
+
```alpine
|
|
103
|
+
<div x-data="{ handle: (key, position) => { ... } }">
|
|
104
|
+
<ul x-sort="handle">
|
|
105
|
+
<li x-sort:key="1">foo</li>
|
|
106
|
+
<li x-sort:key="2">bar</li>
|
|
107
|
+
<li x-sort:key="3">baz</li>
|
|
108
|
+
</ul>
|
|
109
|
+
</div>
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Handler functions are often used to persist the new order of items in the database so that the sorting order of a list is preserved between page refreshes.
|
|
113
|
+
|
|
114
|
+
<a name="sorting-groups"></a>
|
|
115
|
+
## Sorting groups
|
|
116
|
+
|
|
117
|
+
This plugin allows you to drag items from one `x-sort` sortable list into another one by adding a matching `.group` modifier to both lists:
|
|
118
|
+
|
|
119
|
+
```alpine
|
|
120
|
+
<div>
|
|
121
|
+
<ul x-sort.group.todos>
|
|
122
|
+
<li x-sort:key="1">foo</li>
|
|
123
|
+
<li x-sort:key="2">bar</li>
|
|
124
|
+
<li x-sort:key="3">baz</li>
|
|
125
|
+
</ul>
|
|
126
|
+
|
|
127
|
+
<ol x-sort.group.todos>
|
|
128
|
+
<li x-sort:key="1">foo</li>
|
|
129
|
+
<li x-sort:key="2">bar</li>
|
|
130
|
+
<li x-sort:key="3">baz</li>
|
|
131
|
+
</ol>
|
|
132
|
+
</div>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Because both sortable lists above use the same group name (`todos`), you can drag items from one list onto another.
|
|
136
|
+
|
|
137
|
+
> When using sort handlers like `x-sort="handle"` and dragging an item from one group to another, only the destination lists handler will be called with the key and new position.
|
|
138
|
+
|
|
139
|
+
<a name="drag-handles"></a>
|
|
140
|
+
## Drag handles
|
|
141
|
+
|
|
142
|
+
By default, each child element of `x-sort` is draggable by clicking and dragging anywhere within it. However, you may want to designate a smaller, more specific element as the "drag handle" so that the rest of the element can be interacted with like normal, and only the handle will respond to mouse dragging:
|
|
143
|
+
|
|
144
|
+
```alpine
|
|
145
|
+
<ul x-sort>
|
|
146
|
+
<li>
|
|
147
|
+
<span x-sort:handle> - </span>foo
|
|
148
|
+
</li>
|
|
149
|
+
|
|
150
|
+
<li>
|
|
151
|
+
<span x-sort:handle> - </span>bar
|
|
152
|
+
</li>
|
|
153
|
+
|
|
154
|
+
<li>
|
|
155
|
+
<span x-sort:handle> - </span>baz
|
|
156
|
+
</li>
|
|
157
|
+
</ul>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
<!-- START_VERBATIM -->
|
|
161
|
+
<div x-data>
|
|
162
|
+
<ul x-sort>
|
|
163
|
+
<li>
|
|
164
|
+
<span x-sort:handle> - </span>foo
|
|
165
|
+
</li>
|
|
166
|
+
<li>
|
|
167
|
+
<span x-sort:handle> - </span>bar
|
|
168
|
+
</li>
|
|
169
|
+
<li>
|
|
170
|
+
<span x-sort:handle> - </span>baz
|
|
171
|
+
</li>
|
|
172
|
+
</ul>
|
|
173
|
+
</div>
|
|
174
|
+
<!-- END_VERBATIM -->
|
|
175
|
+
|
|
176
|
+
As you can see in the above example, the hyphen "-" is draggable, but the item text ("foo") is not.
|
|
177
|
+
|
|
178
|
+
<a name="ghost-elements"></a>
|
|
179
|
+
## Ghost elements
|
|
180
|
+
|
|
181
|
+
When a user drags an item, the element will follow their mouse to appear as though they are physically dragging the element.
|
|
182
|
+
|
|
183
|
+
By default, a "hole" (empty space) will be left in the original element's place during the drag.
|
|
184
|
+
|
|
185
|
+
If you would like to show a "ghost" of the original element in its place instead of an empty space, you can add the `.ghost` modifier to `x-sort`:
|
|
186
|
+
|
|
187
|
+
```alpine
|
|
188
|
+
<ul x-sort.ghost>
|
|
189
|
+
<li>foo</li>
|
|
190
|
+
<li>bar</li>
|
|
191
|
+
<li>baz</li>
|
|
192
|
+
</ul>
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
<!-- START_VERBATIM -->
|
|
196
|
+
<div x-data>
|
|
197
|
+
<ul x-sort.ghost>
|
|
198
|
+
<li>foo</li>
|
|
199
|
+
<li>bar</li>
|
|
200
|
+
<li>baz</li>
|
|
201
|
+
</ul>
|
|
202
|
+
</div>
|
|
203
|
+
<!-- END_VERBATIM -->
|
|
204
|
+
|
|
205
|
+
<a name="ghost-styling"></a>
|
|
206
|
+
### Styling the ghost element
|
|
207
|
+
|
|
208
|
+
By default, the "ghost" element has a `.sortable-ghost` CSS class attached to it while the original element is being dragged.
|
|
209
|
+
|
|
210
|
+
This makes it easy to add any custom styling you would like:
|
|
211
|
+
|
|
212
|
+
```alpine
|
|
213
|
+
<style>
|
|
214
|
+
.sortable-ghost {
|
|
215
|
+
opacity: .5 !important;
|
|
216
|
+
}
|
|
217
|
+
</style>
|
|
218
|
+
|
|
219
|
+
<ul x-sort.ghost>
|
|
220
|
+
<li>foo</li>
|
|
221
|
+
<li>bar</li>
|
|
222
|
+
<li>baz</li>
|
|
223
|
+
</ul>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
<!-- START_VERBATIM -->
|
|
227
|
+
<div x-data>
|
|
228
|
+
<ul x-sort.ghost x-sort:config="{ ghostClass: 'opacity-50' }">
|
|
229
|
+
<li>foo</li>
|
|
230
|
+
<li>bar</li>
|
|
231
|
+
<li>baz</li>
|
|
232
|
+
</ul>
|
|
233
|
+
</div>
|
|
234
|
+
<!-- END_VERBATIM -->
|
|
235
|
+
|
|
236
|
+
<a name="custom-configuration"></a>
|
|
237
|
+
## Custom configuration
|
|
238
|
+
|
|
239
|
+
Alpine chooses sensible defaults for configuring [SortableJS](https://github.com/SortableJS/Sortable?tab=readme-ov-file#options) under the hood. However, you can add or override any of these options yourself using `x-sort:config`:
|
|
240
|
+
|
|
241
|
+
```alpine
|
|
242
|
+
<ul x-sort x-sort:config="{ filter: '.no-drag' }">
|
|
243
|
+
<li>foo</li>
|
|
244
|
+
<li class="no-drag">bar (not dragable)</li>
|
|
245
|
+
<li>baz</li>
|
|
246
|
+
</ul>
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
<!-- START_VERBATIM -->
|
|
250
|
+
<div x-data>
|
|
251
|
+
<ul x-sort x-sort:config="{ filter: '.no-drag' }">
|
|
252
|
+
<li>foo</li>
|
|
253
|
+
<li class="no-drag">bar (not dragable)</li>
|
|
254
|
+
<li>baz</li>
|
|
255
|
+
</ul>
|
|
256
|
+
</div>
|
|
257
|
+
<!-- END_VERBATIM -->
|
|
258
|
+
|
|
259
|
+
[View the full list of SortableJS configuration options here →](https://github.com/SortableJS/Sortable?tab=readme-ov-file#options)
|