Skip to content

Commit 32c4e47

Browse files
fix: always set draggable through setAttribute to avoid weird behavior (#12649)
Closes #12643 Very weird behaviour from the draggable setter...if you set element.draggable="false" it will actually set draggable to true (the boolean).
1 parent e417d3a commit 32c4e47

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

.changeset/wild-poems-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: always set draggable through `setAttribute` to avoid weird behavior

packages/svelte/src/internal/client/dom/elements/attributes.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@ export function set_dynamic_element_attributes(node, prev, next, css_hash) {
341341
* because updating them through the property setter doesn't work reliably.
342342
* In the example of `width`/`height`, the problem is that the setter only
343343
* accepts numeric values, but the attribute can also be set to a string like `50%`.
344-
* If this list becomes too big, rethink this approach.
344+
* In case of draggable trying to set `element.draggable='false'` will actually set
345+
* draggable to `true`. If this list becomes too big, rethink this approach.
345346
*/
346-
var always_set_through_set_attribute = ['width', 'height'];
347+
var always_set_through_set_attribute = ['width', 'height', 'draggable'];
347348

348349
/** @type {Map<string, string[]>} */
349350
var setters_cache = new Map();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { ok, test } from '../../test';
2+
import { flushSync } from 'svelte';
3+
4+
export default test({
5+
html: `<div draggable="false"></div><div draggable="false"></div>`,
6+
7+
async test({ assert, target, logs }) {
8+
const [div, div2] = target.querySelectorAll('div');
9+
ok(div);
10+
ok(div2);
11+
12+
assert.deepEqual(div.getAttribute('draggable'), 'false');
13+
assert.deepEqual(div.draggable, false);
14+
assert.deepEqual(div2.getAttribute('draggable'), 'false');
15+
assert.deepEqual(div2.draggable, false);
16+
}
17+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
let attrs = $state({ draggable: 'false' });
3+
</script>
4+
5+
<svelte:element this={'div'} draggable="false"></svelte:element>
6+
7+
<div {...attrs}></div>

0 commit comments

Comments
 (0)