Skip to content

Commit 4733eb5

Browse files
committed
Add x-sort:ignore
1 parent 9e67245 commit 4733eb5

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/docs/src/en/plugins/sort.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,37 @@ By default, each `x-sort:item` element is draggable by clicking and dragging any
175175

176176
As you can see in the above example, the hyphen "-" is draggable, but the item text ("foo") is not.
177177

178+
<a name="ignoring-elements"></a>
179+
## Ignoring elements
180+
181+
Sometimes you want to prevent certain elements within a sortable item from initiating a drag operation. This is especially useful when you have interactive elements like buttons, dropdowns, or links that users should be able to click without accidentally dragging the sortable item.
182+
183+
You can use the `x-sort:ignore` directive to mark elements that should not trigger dragging:
184+
185+
```alpine
186+
<ul x-sort>
187+
<li x-sort:item>
188+
<!-- ... -->
189+
190+
<button x-sort:ignore>Edit</button>
191+
</li>
192+
193+
<li x-sort:item>
194+
<!-- ... -->
195+
196+
<button x-sort:ignore>Edit</button>
197+
</li>
198+
199+
<li x-sort:item>
200+
<!-- ... -->
201+
202+
<button x-sort:ignore>Edit</button>
203+
</li>
204+
</ul>
205+
```
206+
207+
In the above example, users can click and drag the item itself, but clicking on the "Edit" button will not initiate a drag operation.
208+
178209
<a name="ghost-elements"></a>
179210
## Ghost elements
180211

packages/sort/src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ function initSortable(el, config, preferences, handle) {
9898
scrollSensitivity: 50,
9999

100100
filter(e) {
101+
if (e.target.hasAttribute('x-sort:ignore') || e.target.hasAttribute('wire:sort:ignore')) return true
102+
if (e.target.closest('[x-sort\\:ignore]') || e.target.closest('[wire\\:sort\\:ignore]')) return true
103+
101104
// Normally, we would just filter out any elements without `[x-sort:item]`
102105
// on them, however for backwards compatibility (when we didn't require
103106
// `[x-sort:item]`) we will check for x-sort\\:item being used at all

0 commit comments

Comments
 (0)