AI-native design editor. Open-source Figma alternative.
fix(ui): preserve focus for composed icon triggers
Danila Poyarkov7f80296c3b4c
diff --git a/src/components/ui/button/IconButton.vue b/src/components/ui/button/IconButton.vueindex 828f038cc..b05fc107e 100644--- a/src/components/ui/button/IconButton.vue+++ b/src/components/ui/button/IconButton.vue@@ -37,7 +37,7 @@ const cls = computed(() =></script><template>- <Tip :label="label" :side="side" :disabled="disabled || !label">+ <Tip as-child :label="label" :side="side" :disabled="disabled || !label"><buttonv-bind="buttonAttrs"data-slot="icon-button"diff --git a/src/components/ui/overlay/Tip.vue b/src/components/ui/overlay/Tip.vueindex 6b9fb9847..e8dd660bf 100644--- a/src/components/ui/overlay/Tip.vue+++ b/src/components/ui/overlay/Tip.vue@@ -1,5 +1,7 @@<script setup lang="ts">-import { useEventListener, useTimeoutFn } from '@vueuse/core'+import { unrefElement, useEventListener, useTimeoutFn } from '@vueuse/core'+import { Primitive } from 'reka-ui'+import type { ComponentPublicInstance } from 'vue'import { computed, nextTick, ref, watch } from 'vue'import { useTooltipUI } from '@/components/ui/overlay/tooltip'@@ -14,16 +16,22 @@ type TooltipSide = 'top' | 'bottom' | 'left' | 'right'const cls = useTooltipUI({ content: 'animate-in zoom-in-95 fade-in' })const {+ asChild = false,side = 'top',disabled = false,label} = defineProps<{+ asChild?: booleanlabel?: stringside?: TooltipSidedisabled?: boolean}>()const triggerRef = ref<HTMLElement>()+function setTrigger(value: Element | ComponentPublicInstance | null) {+ const element = value instanceof Element ? value : unrefElement(value)+ triggerRef.value = element instanceof HTMLElement ? element : undefined+}const contentRef = ref<HTMLElement>()const open = ref(false)const position = ref({ x: 0, y: 0 })@@ -45,6 +53,7 @@ const { start: startOpenTimer, stop: stopOpenTimer } = useTimeoutFn(function anchorElement() {const root = triggerRef.value+ if (asChild) return rootconst child = root?.firstElementChildreturn child instanceof HTMLElement ? child : root}@@ -167,10 +176,13 @@ watch(canOpen, (value) => {</script><template>- <span- ref="triggerRef"+ <Primitive+ :ref="setTrigger"+ as="span"+ :as-child="asChild"data-tooltip-trigger- class="contents"+ :data-as-child="asChild"+ class="data-[as-child=false]:contents"@focusin="onFocusIn"@focusout="onFocusOut"@pointerover="onPointerOver"@@ -179,7 +191,7 @@ watch(canOpen, (value) => {@click="hide"><slot />- </span>+ </Primitive><Teleport to="body"><divv-if="open && label"diff --git a/tests/e2e/properties/select-trigger.spec.ts b/tests/e2e/properties/select-trigger.spec.tsnew file mode 100644index 000000000..7633debd0--- /dev/null+++ b/tests/e2e/properties/select-trigger.spec.ts@@ -0,0 +1,69 @@+import { expect, test } from '@playwright/test'++import { CanvasHelper } from '#tests/helpers/canvas'++for (const custom of [false, true]) {+ test(`Select ${custom ? 'custom icon' : 'default value'} trigger preserves keyboard selection`, async ({+ page+ }) => {+ await page.goto('/?test')+ await new CanvasHelper(page).waitForInit()+ const fixture = await page.evaluateHandle(async (custom) => {+ const vuePath = '/node_modules/.vite/deps/vue.js'+ const selectPath = '/src/components/ui/select/AppSelect.vue'+ const buttonPath = '/src/components/ui/button/IconButton.vue'+ const { createApp, h, ref } = await import(vuePath)+ const { default: AppSelect } = await import(selectPath)+ const { default: IconButton } = await import(buttonPath)+ const value = ref('first')+ const host = document.createElement('div')+ host.dataset.slot = 'select-contract-fixture'+ document.body.append(host)+ const app = createApp(() =>+ h(+ AppSelect,+ {+ modelValue: value.value,+ 'onUpdate:modelValue': (next: string) => {+ value.value = next+ },+ label: 'Choose style',+ options: [+ { value: 'first', label: 'First' },+ { value: 'second', label: 'Second' }+ ]+ },+ custom ? { trigger: () => h(IconButton, { label: 'Choose style' }) } : undefined+ )+ )+ app.mount(host)+ return {+ value: () => value.value,+ dispose() {+ app.unmount()+ host.remove()+ }+ }+ }, custom)+ try {+ const host = page.locator('[data-slot="select-contract-fixture"]')+ const trigger = host.getByRole('combobox', { name: 'Choose style' })+ await expect(trigger).toHaveCount(1)+ await expect(host.locator('button button')).toHaveCount(0)+ await trigger.focus()+ await trigger.press('Enter')+ await expect(page.getByRole('option', { name: 'Second', exact: true })).toBeVisible()+ await page.getByRole('option', { name: 'Second', exact: true }).focus()+ await page.keyboard.press('Enter')+ await expect.poll(() => fixture.evaluate((state) => state.value())).toBe('second')+ await expect(trigger).toBeFocused()+ await trigger.press('Enter')+ await page.keyboard.press('Escape')+ await expect(trigger).toBeFocused()+ if (!custom) await expect(trigger).toHaveText('Second')+ } finally {+ await fixture.evaluate((state) => state.dispose())+ await fixture.dispose()+ }+ })+}