Skip to content

Commit b62a12c

Browse files
committed
Refactor Stimulus string editor
1 parent 42ce648 commit b62a12c

1 file changed

Lines changed: 6 additions & 20 deletions

File tree

src/panel/components/Stimulus/ValueEditor.svelte

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,19 @@
1212
const type = $derived(VALUE_TYPES.includes(valueType) ? valueType : "string")
1313
1414
$effect(() => {
15+
editValue // track editValue so this re-runs on input changes
1516
if (isEditing && inputElement && measureElement) {
1617
tick().then(() => updateWidth())
1718
}
1819
})
1920
20-
$effect(() => {
21-
if (editValue !== undefined && isEditing) {
22-
updateWidth()
23-
}
24-
})
25-
2621
function updateWidth() {
2722
if (!inputElement || !measureElement) return
2823
2924
const value = editValue?.toString() || inputElement.placeholder || "W"
3025
measureElement.textContent = value
31-
const width = measureElement.offsetWidth
32-
33-
const wrapper = inputElement.closest(".input-wrapper")
34-
const maxWidth = wrapper?.parentElement?.offsetWidth || Infinity
35-
36-
const finalWidth = Math.min(width, maxWidth - 40) // -40px for buttons
37-
inputElement.style.width = `${finalWidth}px`
38-
39-
if (wrapper) {
40-
wrapper.style.width = `${finalWidth}px`
41-
}
26+
const width = Math.ceil(measureElement.getBoundingClientRect().width) + 12
27+
inputElement.style.width = `${width}px`
4228
}
4329
4430
const handleSubmit = (e) => {
@@ -64,12 +50,12 @@
6450
}
6551
6652
const handleInput = (e) => {
67-
editValue = e.target.value
53+
editValue = type === "number" ? e.target.valueAsNumber : e.target.value
6854
}
6955
</script>
7056
7157
{#if isEditing}
72-
<form class="d-flex gap-2 w-100" onsubmit={handleSubmit}>
58+
<form class="d-flex gap-2" onsubmit={handleSubmit}>
7359
<div class="input-wrapper">
7460
<input bind:this={inputElement} class="auto-width-input" value={editValue} type={type === "null" || type === "undefined" ? "text" : type} oninput={handleInput} />
7561
<span bind:this={measureElement} class="size-measure"></span>
@@ -106,7 +92,7 @@
10692
min-width: 2.5rem;
10793
max-width: 100%;
10894
width: auto;
109-
transition: all 0.15s ease;
95+
transition: border-color 0.15s ease, box-shadow 0.15s ease;
11096
line-height: 1.4;
11197
height: 1.2rem;
11298
}

0 commit comments

Comments
 (0)