Skip to content

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

Fix tab order and keyboard handling in inspector panel (#191)

Anton Soldatov029c3637a17b
diff --git a/packages/vue/src/ScrubInput/ScrubInputField.vue b/packages/vue/src/ScrubInput/ScrubInputField.vue
index 3cb8951e1..621cdfea1 100644
--- a/packages/vue/src/ScrubInput/ScrubInputField.vue
+++ b/packages/vue/src/ScrubInput/ScrubInputField.vue
@@ -17,6 +17,7 @@ const ctx = useScrubInput()
v-bind="$attrs"
@blur="ctx.commitEdit"
@keydown="ctx.onKeydown"
+ @input="ctx.liveUpdate"
/>
</template>
diff --git a/packages/vue/src/ScrubInput/ScrubInputRoot.vue b/packages/vue/src/ScrubInput/ScrubInputRoot.vue
index 7e59f4cae..53b2cca5d 100644
--- a/packages/vue/src/ScrubInput/ScrubInputRoot.vue
+++ b/packages/vue/src/ScrubInput/ScrubInputRoot.vue
@@ -1,5 +1,5 @@
<script setup lang="ts">
-import { ref, computed, toRef } from 'vue'
+import { ref, computed, toRef, watch } from 'vue'
import { useEventListener } from '@vueuse/core'
import { provideScrubInput } from './context'
@@ -25,6 +25,7 @@ const props = withDefaults(
const emit = defineEmits<{
'update:modelValue': [value: number]
commit: [value: number, previous: number]
+ 'editing-change': [editing: boolean]
}>()
const editing = ref(false)
@@ -78,7 +79,13 @@ function startScrub(e: PointerEvent) {
function startEdit() {
editing.value = true
- requestAnimationFrame(() => inputRef.value?.select())
+ requestAnimationFrame(() => {
+ const input = inputRef.value
+ if (input) {
+ input.focus()
+ input.select()
+ }
+ })
}
function commitEdit(e: Event) {
@@ -93,6 +100,14 @@ function commitEdit(e: Event) {
}
}
+function liveUpdate(e: Event) {
+ const val = +(e.target as HTMLInputElement).value
+ if (!Number.isNaN(val)) {
+ const clamped = Math.min(props.max, Math.max(props.min, val))
+ emit('update:modelValue', clamped)
+ }
+}
+
function onKeydown(e: KeyboardEvent) {
if (e.code === 'Enter') commitEdit(e)
else if (e.code === 'Escape') editing.value = false
@@ -107,11 +122,14 @@ const ctx = {
inputRef,
startScrub,
startEdit,
+ liveUpdate,
commitEdit,
onKeydown
}
provideScrubInput(ctx)
+
+watch(editing, (v) => emit('editing-change', v))
</script>
<template>
diff --git a/packages/vue/src/ScrubInput/context.ts b/packages/vue/src/ScrubInput/context.ts
index 89d83c56b..d93b8a943 100644
--- a/packages/vue/src/ScrubInput/context.ts
+++ b/packages/vue/src/ScrubInput/context.ts
@@ -11,6 +11,7 @@ export interface ScrubInputContext {
inputRef: Ref<HTMLInputElement | null>
startScrub: (e: PointerEvent) => void
startEdit: () => void
+ liveUpdate: (e: Event) => void
commitEdit: (e: Event) => void
onKeydown: (e: KeyboardEvent) => void
}
diff --git a/src/components/ScrubInput.vue b/src/components/ScrubInput.vue
index 708359642..c51890a5c 100644
--- a/src/components/ScrubInput.vue
+++ b/src/components/ScrubInput.vue
@@ -1,5 +1,8 @@
<script setup lang="ts">
import { ScrubInputRoot, ScrubInputField, ScrubInputDisplay } from '@open-pencil/vue'
+import { useEditorStore } from '@/stores/editor'
+
+const store = useEditorStore()
const { modelValue, min, max, step, icon, label, suffix, sensitivity, placeholder } = defineProps<{
modelValue: number | symbol
@@ -21,7 +24,7 @@ const emit = defineEmits<{
<template>
<ScrubInputRoot
- v-slot="{ editing, startScrub, placeholder: ph }"
+ v-slot="{ editing, startScrub, startEdit, placeholder: ph }"
:model-value="modelValue"
:min="min"
:max="max"
@@ -30,12 +33,15 @@ const emit = defineEmits<{
:placeholder="placeholder"
@update:model-value="emit('update:modelValue', $event)"
@commit="(val: number, prev: number) => emit('commit', val, prev)"
+ @editing-change="store.state.scrubInputFocused = $event"
>
<div
data-test-id="scrub-input"
- class="group flex h-[26px] min-w-0 flex-1 items-center rounded border border-border bg-input focus-within:border-accent"
+ :tabindex="editing ? undefined : 0"
+ class="group flex h-[26px] min-w-0 flex-1 items-center rounded border border-border bg-input focus-within:border-accent focus:border-accent"
:style="{ cursor: editing ? 'auto' : 'ew-resize' }"
@pointerdown="!editing && startScrub($event)"
+ @focus="!editing && startEdit()"
>
<span
class="flex shrink-0 items-center justify-center self-stretch px-[5px] text-muted select-none [&>*]:pointer-events-none"
diff --git a/src/composables/use-keyboard.ts b/src/composables/use-keyboard.ts
index 1b09c9604..9145f765d 100644
--- a/src/composables/use-keyboard.ts
+++ b/src/composables/use-keyboard.ts
@@ -213,7 +213,8 @@ export function useKeyboard() {
!keys['control'].value &&
!keys['shift'].value &&
(allowAlt || !keys['alt'].value) &&
- !store.state.editingTextId
+ !store.state.editingTextId &&
+ !store.state.scrubInputFocused
)
}
diff --git a/src/stores/editor.ts b/src/stores/editor.ts
index d657a9a20..2aaf392c0 100644
--- a/src/stores/editor.ts
+++ b/src/stores/editor.ts
@@ -93,6 +93,7 @@ export function createEditorStore(initialGraph?: SceneGraph) {
tangentField: 'tangentStart' | 'tangentEnd'
} | null
} | null
+ scrubInputFocused: boolean
}
>({
...createDefaultEditorState(graph.getPages()[0].id),
@@ -107,7 +108,8 @@ export function createEditorStore(initialGraph?: SceneGraph) {
autosaveEnabled: false,
cursorCanvasX: null,
cursorCanvasY: null,
- nodeEditState: null
+ nodeEditState: null,
+ scrubInputFocused: false
})
const editor = createEditor({ graph, state, loadFont, skipInitialGraphSetup: !!initialGraph })