Skip to content

Commit 7747876

Browse files
committed
fix: restore input binding selection position
1 parent 8bee23e commit 7747876

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

.changeset/cyan-ducks-train.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: restore input binding selection position

packages/svelte/src/internal/client/dom/elements/bindings/input.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,17 @@ export function bind_value(input, get, set = get) {
3030
// In runes mode, respect any validation in accessors (doesn't apply in legacy mode,
3131
// because we use mutable state which ensures the render effect always runs)
3232
if (runes && value !== (value = get())) {
33+
var start = input.selectionStart;
34+
var end = input.selectionEnd;
35+
3336
// the value is coerced on assignment
3437
input.value = value ?? '';
38+
39+
// Restore selection
40+
if (end !== null) {
41+
input.selectionStart = start;
42+
input.selectionEnd = Math.min(end, input.value.length);
43+
}
3544
}
3645
});
3746

0 commit comments

Comments
 (0)