Skip to content

AI-native design editor. Open-source Figma alternative.

refactor: eliminate HTMLInputElement template casts

Danila Poyarkova6da185e5481
diff --git a/packages/vue/src/editor/inline-rename/use.ts b/packages/vue/src/editor/inline-rename/use.ts
index 50a23afe4..f31cb2912 100644
--- a/packages/vue/src/editor/inline-rename/use.ts
+++ b/packages/vue/src/editor/inline-rename/use.ts
@@ -37,10 +37,12 @@ export function useInlineRename<T extends string>(
function commit(id: T, eventOrInput: Event | HTMLInputElement) {
if (editingId.value !== id) return
- const input =
- eventOrInput instanceof HTMLInputElement
- ? eventOrInput
- : (eventOrInput.target as HTMLInputElement | null)
+ let input: HTMLInputElement | null
+ if (eventOrInput instanceof HTMLInputElement) {
+ input = eventOrInput
+ } else {
+ input = eventOrInput.target instanceof HTMLInputElement ? eventOrInput.target : null
+ }
if (!input) return
const value = input.value.trim()
if (value && value !== originalName) {
@@ -61,7 +63,7 @@ export function useInlineRename<T extends string>(
function onKeydown(e: KeyboardEvent) {
if (e.code === 'Enter') {
- ;(e.target as HTMLInputElement).blur()
+ if (e.target instanceof HTMLElement) e.target.blur()
return
}
diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts
index f5dc7cd83..a09e0f627 100644
--- a/packages/vue/src/index.ts
+++ b/packages/vue/src/index.ts
@@ -130,6 +130,9 @@ export { TypographyControlsRoot } from '#vue/primitives/TypographyControls'
export { ToolbarRoot, ToolbarItem, useToolbar } from '#vue/primitives/Toolbar'
export type { ToolbarContext } from '#vue/primitives/Toolbar'
+/** DOM event helpers for cast-free template bindings. */
+export { blurTarget, inputNumberValue, inputValue, selectTarget } from '#vue/shared/dom-events'
+
/** Internationalization. */
export { useI18n } from '#vue/i18n'
export { locale, localeSetting, setLocale, AVAILABLE_LOCALES, LOCALE_LABELS } from '#vue/i18n'
diff --git a/packages/vue/src/primitives/ScrubInput/ScrubInputRoot.vue b/packages/vue/src/primitives/ScrubInput/ScrubInputRoot.vue
index 45352aeb1..8d620d54b 100644
--- a/packages/vue/src/primitives/ScrubInput/ScrubInputRoot.vue
+++ b/packages/vue/src/primitives/ScrubInput/ScrubInputRoot.vue
@@ -3,6 +3,7 @@ import { ref, computed, toRef, watch } from 'vue'
import { useEventListener } from '@vueuse/core'
import { provideScrubInput } from '#vue/primitives/ScrubInput/context'
+import { inputNumberValue } from '#vue/shared/dom-events'
const {
modelValue,
@@ -88,7 +89,7 @@ function startEdit() {
function commitEdit(e: Event) {
if (!editing.value) return
- const val = +(e.target as HTMLInputElement).value
+ const val = inputNumberValue(e)
const previous = numericValue.value
editing.value = false
if (!Number.isNaN(val)) {
@@ -99,7 +100,7 @@ function commitEdit(e: Event) {
}
function liveUpdate(e: Event) {
- const val = +(e.target as HTMLInputElement).value
+ const val = inputNumberValue(e)
if (!Number.isNaN(val)) {
const clamped = Math.min(max, Math.max(min, val))
emit('update:modelValue', clamped)
diff --git a/packages/vue/src/shared/dom-events.ts b/packages/vue/src/shared/dom-events.ts
new file mode 100644
index 000000000..3ccfdd0c0
--- /dev/null
+++ b/packages/vue/src/shared/dom-events.ts
@@ -0,0 +1,15 @@
+export function inputValue(e: Event): string {
+ return e.target instanceof HTMLInputElement ? e.target.value : ''
+}
+
+export function inputNumberValue(e: Event): number {
+ return +inputValue(e)
+}
+
+export function blurTarget(e: Event) {
+ if (e.target instanceof HTMLElement) e.target.blur()
+}
+
+export function selectTarget(e: Event) {
+ if (e.target instanceof HTMLInputElement) e.target.select()
+}
diff --git a/src/app/shell/menu/document-name.ts b/src/app/shell/menu/document-name.ts
index 9fe8beb94..9cd73239f 100644
--- a/src/app/shell/menu/document-name.ts
+++ b/src/app/shell/menu/document-name.ts
@@ -16,8 +16,8 @@ export function useDocumentNameRename(store: EditorStore) {
rename.start(DOCUMENT_NAME_ID, store.state.documentName)
}
- function commitRename(input: HTMLInputElement) {
- rename.commit(DOCUMENT_NAME_ID, input)
+ function commitRename(e: Event) {
+ rename.commit(DOCUMENT_NAME_ID, e)
}
return { rename, editingName, startRename, commitRename }
diff --git a/src/components/AppMenu.vue b/src/components/AppMenu.vue
index 3683c55b2..07b2d3d63 100644
--- a/src/components/AppMenu.vue
+++ b/src/components/AppMenu.vue
@@ -67,8 +67,7 @@ const subMenuCls = useMenuUI({ content: 'min-w-44' })
data-test-id="app-document-name-input"
class="min-w-0 flex-1 rounded border border-accent bg-input px-1 py-0.5 text-xs text-surface outline-none"
:value="store.state.documentName"
- @blur="commitRename($event.target as HTMLInputElement)"
- @keydown.enter="($event.target as HTMLInputElement).blur()"
+ @blur="commitRename($event)"
@keydown="rename.onKeydown"
/>
<span
diff --git a/src/components/CollabPanel/ConnectedRoom.vue b/src/components/CollabPanel/ConnectedRoom.vue
index ada2d76b1..4d181bdd2 100644
--- a/src/components/CollabPanel/ConnectedRoom.vue
+++ b/src/components/CollabPanel/ConnectedRoom.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { selectTarget } from '@open-pencil/vue'
import { useCollabPanelContext } from '@/components/CollabPanel/context'
const collab = useCollabPanelContext()
@@ -12,7 +13,7 @@ const collab = useCollabPanelContext()
readonly
data-test-id="collab-room-link"
class="min-w-0 flex-1 rounded border border-border bg-input px-2 py-1 text-xs text-surface"
- @focus="($event.target as HTMLInputElement).select()"
+ @focus="selectTarget($event)"
/>
<button
data-test-id="collab-copy-link"
diff --git a/src/components/ColorPicker/ColorInput.vue b/src/components/ColorPicker/ColorInput.vue
index bf5f67e0e..1ca46acf3 100644
--- a/src/components/ColorPicker/ColorInput.vue
+++ b/src/components/ColorPicker/ColorInput.vue
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { useAttrs } from 'vue'
-import { ColorInputRoot } from '@open-pencil/vue'
+import { ColorInputRoot, inputValue } from '@open-pencil/vue'
import ColorPicker from '@/components/ColorPicker/ColorPicker.vue'
@@ -40,7 +40,7 @@ const emit = defineEmits<{ update: [color: Color] }>()
class="min-w-0 flex-1 border-none bg-transparent font-mono text-xs text-surface outline-none"
:value="hex"
maxlength="6"
- @change="actions.updateFromHex(($event.target as HTMLInputElement).value)"
+ @change="actions.updateFromHex(inputValue($event))"
/>
<span v-else class="min-w-0 flex-1 truncate font-mono text-xs text-muted">
{{ hex }}
diff --git a/src/components/ColorPickerPanel/HsbFields.vue b/src/components/ColorPickerPanel/HsbFields.vue
index be2e3984f..5f8212c4d 100644
--- a/src/components/ColorPickerPanel/HsbFields.vue
+++ b/src/components/ColorPickerPanel/HsbFields.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { inputNumberValue } from '@open-pencil/vue'
import { colorToCSS } from '@open-pencil/core/color'
import PickerSlider from '@/components/PickerSlider.vue'
@@ -17,7 +18,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.hsbColor.h)"
min="0"
max="360"
- @change="ctx.updateHSBChannelValue('h', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateHSBChannelValue('h', inputNumberValue($event))"
/>
<input
type="number"
@@ -25,7 +26,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.hsbColor.s)"
min="0"
max="100"
- @change="ctx.updateHSBChannelValue('s', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateHSBChannelValue('s', inputNumberValue($event))"
/>
<input
type="number"
@@ -33,7 +34,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.hsbColor.b)"
min="0"
max="100"
- @change="ctx.updateHSBChannelValue('b', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateHSBChannelValue('b', inputNumberValue($event))"
/>
</div>
diff --git a/src/components/ColorPickerPanel/HslFields.vue b/src/components/ColorPickerPanel/HslFields.vue
index 7a0dec651..ecf672589 100644
--- a/src/components/ColorPickerPanel/HslFields.vue
+++ b/src/components/ColorPickerPanel/HslFields.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { inputNumberValue } from '@open-pencil/vue'
import { colorToCSS } from '@open-pencil/core/color'
import PickerSlider from '@/components/PickerSlider.vue'
@@ -17,7 +18,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.hslColor.h ?? 0)"
min="0"
max="360"
- @change="ctx.updateHSLChannelValue('h', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateHSLChannelValue('h', inputNumberValue($event))"
/>
<input
type="number"
@@ -25,7 +26,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.hslColor.s ?? 0)"
min="0"
max="100"
- @change="ctx.updateHSLChannelValue('s', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateHSLChannelValue('s', inputNumberValue($event))"
/>
<input
type="number"
@@ -33,7 +34,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.hslColor.l ?? 0)"
min="0"
max="100"
- @change="ctx.updateHSLChannelValue('l', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateHSLChannelValue('l', inputNumberValue($event))"
/>
</div>
diff --git a/src/components/ColorPickerPanel/RgbFields.vue b/src/components/ColorPickerPanel/RgbFields.vue
index ca5d44c66..e97c1d5a3 100644
--- a/src/components/ColorPickerPanel/RgbFields.vue
+++ b/src/components/ColorPickerPanel/RgbFields.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { inputNumberValue } from '@open-pencil/vue'
import { useColorPickerPanelContext } from '@/components/ColorPickerPanel/context'
const ctx = useColorPickerPanelContext()
@@ -14,7 +15,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.rgbColor.r)"
min="0"
max="255"
- @change="ctx.updateRGBChannelValue('r', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateRGBChannelValue('r', inputNumberValue($event))"
/>
<input
type="number"
@@ -22,7 +23,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.rgbColor.g)"
min="0"
max="255"
- @change="ctx.updateRGBChannelValue('g', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateRGBChannelValue('g', inputNumberValue($event))"
/>
<input
type="number"
@@ -30,7 +31,7 @@ const ctx = useColorPickerPanelContext()
:value="Math.round(ctx.rgbColor.b)"
min="0"
max="255"
- @change="ctx.updateRGBChannelValue('b', +($event.target as HTMLInputElement).value)"
+ @change="ctx.updateRGBChannelValue('b', inputNumberValue($event))"
/>
</div>
</template>
diff --git a/src/components/GradientEditor.vue b/src/components/GradientEditor.vue
index 034c79415..b84f5c3fc 100644
--- a/src/components/GradientEditor.vue
+++ b/src/components/GradientEditor.vue
@@ -8,6 +8,7 @@ import {
GradientEditorRoot,
GradientEditorBar,
GradientEditorStop,
+ inputValue,
useI18n
} from '@open-pencil/vue'
@@ -98,7 +99,7 @@ const { panels } = useI18n()
class="min-w-0 flex-1 rounded border border-border bg-input px-1 py-0.5 font-mono text-[11px] text-surface"
:value="s.hex"
maxlength="6"
- @change="s.actions.updateColor(($event.target as HTMLInputElement).value)"
+ @change="s.actions.updateColor(inputValue($event))"
@click.stop
/>
<ScrubInput
diff --git a/src/components/PickerSlider.vue b/src/components/PickerSlider.vue
index 43d4857dd..6d1fa82bc 100644
--- a/src/components/PickerSlider.vue
+++ b/src/components/PickerSlider.vue
@@ -1,4 +1,5 @@
<script setup lang="ts">
+import { inputNumberValue } from '@open-pencil/vue'
import { usePickerSliderUI } from './ui/picker-slider'
const {
@@ -74,7 +75,7 @@ function thumbLeft(): string {
:max="max"
:step="step"
:value="modelValue"
- @input="emit('update:modelValue', +($event.target as HTMLInputElement).value)"
+ @input="emit('update:modelValue', inputNumberValue($event))"
/>
<div :class="cls.thumb" :style="{ left: thumbLeft(), background: thumbFill }" />
</div>
@@ -85,7 +86,7 @@ function thumbLeft(): string {
:max="displayMax ?? max"
:step="displayStep ?? step"
:value="numberValue()"
- @change="handleNumberChange(+($event.target as HTMLInputElement).value)"
+ @change="handleNumberChange(inputNumberValue($event))"
/>
</div>
</template>