@alpinejs/docs 3.13.8-revision.2 → 3.13.9-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
CHANGED
|
@@ -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://cdn.jsdelivr.net/npm/alpinejs@3.13.
|
|
36
|
+
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.13.9/dist/cdn.min.js"></script>
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
That's it! Alpine is now available for use inside your page.
|
|
@@ -66,7 +66,7 @@ Notice that, because of [event bubbling](https://en.wikipedia.org/wiki/Event_bub
|
|
|
66
66
|
</div>
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
-
> The first example won't work because when `
|
|
69
|
+
> The first example won't work because when `notify` is dispatched, it'll propagate to its common ancestor, the `div`, not its sibling, the `<span>`. The second example will work because the sibling is listening for `notify` at the `window` level, which the custom event will eventually bubble up to.
|
|
70
70
|
|
|
71
71
|
<a name="dispatching-to-components"></a>
|
|
72
72
|
## Dispatching to other components
|
package/src/en/plugins/sort.md
CHANGED
|
@@ -52,22 +52,22 @@ Alpine.plugin(sort)
|
|
|
52
52
|
<a name="basic-usage"></a>
|
|
53
53
|
## Basic usage
|
|
54
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.
|
|
55
|
+
The primary API for using this plugin is the `x-sort` directive. By adding `x-sort` to an element, its children containing `x-sort:item` become sortable—meaning you can drag them around with your mouse, and they will change positions.
|
|
56
56
|
|
|
57
57
|
```alpine
|
|
58
58
|
<ul x-sort>
|
|
59
|
-
<li>foo</li>
|
|
60
|
-
<li>bar</li>
|
|
61
|
-
<li>baz</li>
|
|
59
|
+
<li x-sort:item>foo</li>
|
|
60
|
+
<li x-sort:item>bar</li>
|
|
61
|
+
<li x-sort:item>baz</li>
|
|
62
62
|
</ul>
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
<!-- START_VERBATIM -->
|
|
66
66
|
<div x-data>
|
|
67
67
|
<ul x-sort>
|
|
68
|
-
<li>foo</li>
|
|
69
|
-
<li>bar</li>
|
|
70
|
-
<li>baz</li>
|
|
68
|
+
<li x-sort:item>foo</li>
|
|
69
|
+
<li x-sort:item>bar</li>
|
|
70
|
+
<li x-sort:item>baz</li>
|
|
71
71
|
</ul>
|
|
72
72
|
</div>
|
|
73
73
|
<!-- END_VERBATIM -->
|
|
@@ -75,73 +75,83 @@ The primary API for using this plugin is the `x-sort` directive. By adding `x-so
|
|
|
75
75
|
<a name="sort-handlers"></a>
|
|
76
76
|
## Sort handlers
|
|
77
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:
|
|
78
|
+
You can react to sorting changes by passing a handler function to `x-sort` and adding keys to each item using `x-sort:item`. 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
79
|
|
|
80
80
|
```alpine
|
|
81
|
-
<
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
</ul>
|
|
87
|
-
</div>
|
|
81
|
+
<ul x-sort="alert($item + ' - ' + $position)">
|
|
82
|
+
<li x-sort:item="1">foo</li>
|
|
83
|
+
<li x-sort:item="2">bar</li>
|
|
84
|
+
<li x-sort:item="3">baz</li>
|
|
85
|
+
</ul>
|
|
88
86
|
```
|
|
89
87
|
|
|
90
88
|
<!-- START_VERBATIM -->
|
|
91
|
-
<div x-data
|
|
92
|
-
<ul x-sort="
|
|
93
|
-
<li x-sort:
|
|
94
|
-
<li x-sort:
|
|
95
|
-
<li x-sort:
|
|
89
|
+
<div x-data>
|
|
90
|
+
<ul x-sort="alert($item + ' - ' + $position)">
|
|
91
|
+
<li x-sort:item="1">foo</li>
|
|
92
|
+
<li x-sort:item="2">bar</li>
|
|
93
|
+
<li x-sort:item="3">baz</li>
|
|
96
94
|
</ul>
|
|
97
95
|
</div>
|
|
98
96
|
<!-- END_VERBATIM -->
|
|
99
97
|
|
|
100
|
-
|
|
98
|
+
The `x-sort` handler will be called every time the sort order of the items change. The `$item` magic will contain the key of the sorted element (derived from `x-sort:item`), 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 `item` and `position` as the first and second parameter:
|
|
101
|
+
|
|
102
|
+
```alpine
|
|
103
|
+
<div x-data="{ handle: (item, position) => { ... } }">
|
|
104
|
+
<ul x-sort="handle">
|
|
105
|
+
<li x-sort:item="1">foo</li>
|
|
106
|
+
<li x-sort:item="2">bar</li>
|
|
107
|
+
<li x-sort:item="3">baz</li>
|
|
108
|
+
</ul>
|
|
109
|
+
</div>
|
|
110
|
+
```
|
|
101
111
|
|
|
102
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.
|
|
103
113
|
|
|
104
114
|
<a name="sorting-groups"></a>
|
|
105
115
|
## Sorting groups
|
|
106
116
|
|
|
107
|
-
This plugin allows you to drag items from one `x-sort` sortable list into another one by adding a matching
|
|
117
|
+
This plugin allows you to drag items from one `x-sort` sortable list into another one by adding a matching `x-sort:group` value to both lists:
|
|
108
118
|
|
|
109
119
|
```alpine
|
|
110
120
|
<div>
|
|
111
|
-
<ul x-sort
|
|
112
|
-
<li x-sort:
|
|
113
|
-
<li x-sort:
|
|
114
|
-
<li x-sort:
|
|
121
|
+
<ul x-sort x-sort:group="todos">
|
|
122
|
+
<li x-sort:item="1">foo</li>
|
|
123
|
+
<li x-sort:item="2">bar</li>
|
|
124
|
+
<li x-sort:item="3">baz</li>
|
|
115
125
|
</ul>
|
|
116
126
|
|
|
117
|
-
<ol x-sort
|
|
118
|
-
<li x-sort:
|
|
119
|
-
<li x-sort:
|
|
120
|
-
<li x-sort:
|
|
127
|
+
<ol x-sort x-sort:group="todos">
|
|
128
|
+
<li x-sort:item="4">foo</li>
|
|
129
|
+
<li x-sort:item="5">bar</li>
|
|
130
|
+
<li x-sort:item="6">baz</li>
|
|
121
131
|
</ol>
|
|
122
132
|
</div>
|
|
123
133
|
```
|
|
124
134
|
|
|
125
135
|
Because both sortable lists above use the same group name (`todos`), you can drag items from one list onto another.
|
|
126
136
|
|
|
127
|
-
> When using sort handlers like `x-sort="handle"` and dragging an item from one group to another, only the destination
|
|
137
|
+
> When using sort handlers like `x-sort="handle"` and dragging an item from one group to another, only the destination list's handler will be called with the key and new position.
|
|
128
138
|
|
|
129
139
|
<a name="drag-handles"></a>
|
|
130
140
|
## Drag handles
|
|
131
141
|
|
|
132
|
-
By default, each
|
|
142
|
+
By default, each `x-sort:item` element 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:
|
|
133
143
|
|
|
134
144
|
```alpine
|
|
135
145
|
<ul x-sort>
|
|
136
|
-
<li>
|
|
146
|
+
<li x-sort:item>
|
|
137
147
|
<span x-sort:handle> - </span>foo
|
|
138
148
|
</li>
|
|
139
149
|
|
|
140
|
-
<li>
|
|
150
|
+
<li x-sort:item>
|
|
141
151
|
<span x-sort:handle> - </span>bar
|
|
142
152
|
</li>
|
|
143
153
|
|
|
144
|
-
<li>
|
|
154
|
+
<li x-sort:item>
|
|
145
155
|
<span x-sort:handle> - </span>baz
|
|
146
156
|
</li>
|
|
147
157
|
</ul>
|
|
@@ -150,13 +160,13 @@ By default, each child element of `x-sort` is draggable by clicking and dragging
|
|
|
150
160
|
<!-- START_VERBATIM -->
|
|
151
161
|
<div x-data>
|
|
152
162
|
<ul x-sort>
|
|
153
|
-
<li>
|
|
163
|
+
<li x-sort:item>
|
|
154
164
|
<span x-sort:handle> - </span>foo
|
|
155
165
|
</li>
|
|
156
|
-
<li>
|
|
166
|
+
<li x-sort:item>
|
|
157
167
|
<span x-sort:handle> - </span>bar
|
|
158
168
|
</li>
|
|
159
|
-
<li>
|
|
169
|
+
<li x-sort:item>
|
|
160
170
|
<span x-sort:handle> - </span>baz
|
|
161
171
|
</li>
|
|
162
172
|
</ul>
|
|
@@ -176,18 +186,18 @@ If you would like to show a "ghost" of the original element in its place instead
|
|
|
176
186
|
|
|
177
187
|
```alpine
|
|
178
188
|
<ul x-sort.ghost>
|
|
179
|
-
<li>foo</li>
|
|
180
|
-
<li>bar</li>
|
|
181
|
-
<li>baz</li>
|
|
189
|
+
<li x-sort:item>foo</li>
|
|
190
|
+
<li x-sort:item>bar</li>
|
|
191
|
+
<li x-sort:item>baz</li>
|
|
182
192
|
</ul>
|
|
183
193
|
```
|
|
184
194
|
|
|
185
195
|
<!-- START_VERBATIM -->
|
|
186
196
|
<div x-data>
|
|
187
197
|
<ul x-sort.ghost>
|
|
188
|
-
<li>foo</li>
|
|
189
|
-
<li>bar</li>
|
|
190
|
-
<li>baz</li>
|
|
198
|
+
<li x-sort:item>foo</li>
|
|
199
|
+
<li x-sort:item>bar</li>
|
|
200
|
+
<li x-sort:item>baz</li>
|
|
191
201
|
</ul>
|
|
192
202
|
</div>
|
|
193
203
|
<!-- END_VERBATIM -->
|
|
@@ -207,18 +217,96 @@ This makes it easy to add any custom styling you would like:
|
|
|
207
217
|
</style>
|
|
208
218
|
|
|
209
219
|
<ul x-sort.ghost>
|
|
210
|
-
<li>foo</li>
|
|
211
|
-
<li>bar</li>
|
|
212
|
-
<li>baz</li>
|
|
220
|
+
<li x-sort:item>foo</li>
|
|
221
|
+
<li x-sort:item>bar</li>
|
|
222
|
+
<li x-sort:item>baz</li>
|
|
213
223
|
</ul>
|
|
214
224
|
```
|
|
215
225
|
|
|
216
226
|
<!-- START_VERBATIM -->
|
|
217
227
|
<div x-data>
|
|
218
228
|
<ul x-sort.ghost x-sort:config="{ ghostClass: 'opacity-50' }">
|
|
219
|
-
<li>foo</li>
|
|
220
|
-
<li>bar</li>
|
|
221
|
-
<li>baz</li>
|
|
229
|
+
<li x-sort:item>foo</li>
|
|
230
|
+
<li x-sort:item>bar</li>
|
|
231
|
+
<li x-sort:item>baz</li>
|
|
232
|
+
</ul>
|
|
233
|
+
</div>
|
|
234
|
+
<!-- END_VERBATIM -->
|
|
235
|
+
|
|
236
|
+
<a name="sorting-class"></a>
|
|
237
|
+
## Sorting class on body
|
|
238
|
+
|
|
239
|
+
While an element is being dragged around, Alpine will automatically add a `.sorting` class to the `<body>` element of the page.
|
|
240
|
+
|
|
241
|
+
This is useful for styling any element on the page conditionally using only CSS.
|
|
242
|
+
|
|
243
|
+
For example you could have a warning that only displays while a user is sorting items:
|
|
244
|
+
|
|
245
|
+
```html
|
|
246
|
+
<div id="sort-warning">
|
|
247
|
+
Page functionality is limited while sorting
|
|
248
|
+
</div>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
To show this only while sorting, you can use the `body.sorting` CSS selector:
|
|
252
|
+
|
|
253
|
+
```css
|
|
254
|
+
#sort-warning {
|
|
255
|
+
display: none;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
body.sorting #sort-warning {
|
|
259
|
+
display: block;
|
|
260
|
+
}
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
<a name="css-hover-bug"></a>
|
|
264
|
+
## CSS hover bug
|
|
265
|
+
|
|
266
|
+
Currently, there is a [bug in Chrome and Safari](https://issues.chromium.org/issues/41129937) (not Firefox) that causes issues with hover styles.
|
|
267
|
+
|
|
268
|
+
Consider HTML like the following, where each item in the list is styled differently based on a hover state (here we're using Tailwind's `.hover` class to conditionally add a border):
|
|
269
|
+
|
|
270
|
+
```html
|
|
271
|
+
<div x-sort>
|
|
272
|
+
<div x-sort:item class="hover:border">foo</div>
|
|
273
|
+
<div x-sort:item class="hover:border">bar</div>
|
|
274
|
+
<div x-sort:item class="hover:border">baz</div>
|
|
275
|
+
</div>
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
If you drag one of the elements in the list below you will see that the hover effect will be errantly applied to any element in the original element's place:
|
|
279
|
+
|
|
280
|
+
<!-- START_VERBATIM -->
|
|
281
|
+
<div x-data>
|
|
282
|
+
<ul x-sort class="flex flex-col items-start">
|
|
283
|
+
<li x-sort:item class="hover:border border-black">foo</li>
|
|
284
|
+
<li x-sort:item class="hover:border border-black">bar</li>
|
|
285
|
+
<li x-sort:item class="hover:border border-black">baz</li>
|
|
286
|
+
</ul>
|
|
287
|
+
</div>
|
|
288
|
+
<!-- END_VERBATIM -->
|
|
289
|
+
|
|
290
|
+
To fix this, you can leverage the `.sorting` class applied to the body while sorting to limit the hover effect to only be applied while `.sorting` does NOT exist on `body`.
|
|
291
|
+
|
|
292
|
+
Here is how you can do this directly inline using Tailwind arbitrary variants:
|
|
293
|
+
|
|
294
|
+
```html
|
|
295
|
+
<div x-sort>
|
|
296
|
+
<div x-sort:item class="[body:not(.sorting)_&]:hover:border">foo</div>
|
|
297
|
+
<div x-sort:item class="[body:not(.sorting)_&]:hover:border">bar</div>
|
|
298
|
+
<div x-sort:item class="[body:not(.sorting)_&]:hover:border">baz</div>
|
|
299
|
+
</div>
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
Now you can see below that the hover effect is only applied to the dragging element and not the others in the list.
|
|
303
|
+
|
|
304
|
+
<!-- START_VERBATIM -->
|
|
305
|
+
<div x-data>
|
|
306
|
+
<ul x-sort class="flex flex-col items-start">
|
|
307
|
+
<li x-sort:item class="[body:not(.sorting)_&]:hover:border border-black">foo</li>
|
|
308
|
+
<li x-sort:item class="[body:not(.sorting)_&]:hover:border border-black">bar</li>
|
|
309
|
+
<li x-sort:item class="[body:not(.sorting)_&]:hover:border border-black">baz</li>
|
|
222
310
|
</ul>
|
|
223
311
|
</div>
|
|
224
312
|
<!-- END_VERBATIM -->
|
|
@@ -229,21 +317,23 @@ This makes it easy to add any custom styling you would like:
|
|
|
229
317
|
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`:
|
|
230
318
|
|
|
231
319
|
```alpine
|
|
232
|
-
<ul x-sort x-sort:config="{
|
|
233
|
-
<li>foo</li>
|
|
234
|
-
<li
|
|
235
|
-
<li>baz</li>
|
|
320
|
+
<ul x-sort x-sort:config="{ animation: 0 }">
|
|
321
|
+
<li x-sort:item>foo</li>
|
|
322
|
+
<li x-sort:item>bar</li>
|
|
323
|
+
<li x-sort:item>baz</li>
|
|
236
324
|
</ul>
|
|
237
325
|
```
|
|
238
326
|
|
|
239
327
|
<!-- START_VERBATIM -->
|
|
240
328
|
<div x-data>
|
|
241
|
-
<ul x-sort x-sort:config="{
|
|
242
|
-
<li>foo</li>
|
|
243
|
-
<li
|
|
244
|
-
<li>baz</li>
|
|
329
|
+
<ul x-sort x-sort:config="{ animation: 0 }">
|
|
330
|
+
<li x-sort:item>foo</li>
|
|
331
|
+
<li x-sort:item>bar</li>
|
|
332
|
+
<li x-sort:item>baz</li>
|
|
245
333
|
</ul>
|
|
246
334
|
</div>
|
|
247
335
|
<!-- END_VERBATIM -->
|
|
248
336
|
|
|
337
|
+
> Any config options passed will overwrite Alpine defaults. In this case of `animation`, this is fine, however be aware that overwriting `handle`, `group`, `filter`, `onSort`, `onStart`, or `onEnd` may break functionality.
|
|
338
|
+
|
|
249
339
|
[View the full list of SortableJS configuration options here →](https://github.com/SortableJS/Sortable?tab=readme-ov-file#options)
|