diff --git a/knip.jsonc b/knip.jsonc index 2c2045fd..a9ee5d41 100644 --- a/knip.jsonc +++ b/knip.jsonc @@ -35,7 +35,8 @@ // `skills/devframe/templates/*.ts` are the devframe-authoring skill's // copy-and-run reference snippets (`skills/devframe/SKILL.md`) - each // one a standalone, runnable example, not imported by anything else. - "entry": ["design/*.ts", "skills/devframe/templates/*.ts", "scripts/smoke-bun.ts"], + // The floating dock fixture is loaded by its HTML script tag. + "entry": ["design/*.ts", "skills/devframe/templates/*.ts", "scripts/smoke-bun.ts", "tests/e2e/_support/floating-dock/main.ts"], // `@x18n/cli` is run by hand (`pnpm x18n`) and no script calls it yet. "ignoreDependencies": ["@x18n/cli"] }, diff --git a/packages/hub-ui/src/client/components/dock/Dock.vue b/packages/hub-ui/src/client/components/dock/Dock.vue index 6c263535..aa1a86aa 100644 --- a/packages/hub-ui/src/client/components/dock/Dock.vue +++ b/packages/hub-ui/src/client/components/dock/Dock.vue @@ -2,7 +2,7 @@ import type { DocksContext } from '@devframes/hub/client' import type { CSSProperties } from 'vue' import type { DockLayout } from './dock-layout' -import { useEventListener, whenever } from '@vueuse/core' +import { useEventListener, useFocusWithin, whenever } from '@vueuse/core' import { computed, onMounted, reactive, ref, useTemplateRef, watch } from 'vue' import { BUILTIN_ENTRY_CLIENT_AUTH_NOTICE } from '../../constants' import { t } from '../../i18n' @@ -49,6 +49,7 @@ const safeArea = useSafeArea(anchorEl) const panelMargins = computed(() => resolveViewportMargins(safeArea.value, layout.value)) const dockEl = useTemplateRef('dockEl') +const { focused: isFocused } = useFocusWithin(dockEl) const recalculateCounter = ref(0) const isHovering = ref(false) @@ -219,6 +220,8 @@ const docksGroupPanel = useDocksGroupPanel() const isMinimized = computed(() => { if (context.panel.store.inactiveTimeout < 0) return false + if (isFocused.value) + return false if (docksGroupPanel.value) return false // Keep an active tool's button in sight while it hides the panel. @@ -307,7 +310,8 @@ onMounted(() => { @pointerdown="onPointerDown" >
diff --git a/tests/e2e/_support/floating-dock/index.html b/tests/e2e/_support/floating-dock/index.html new file mode 100644 index 00000000..0b0a4212 --- /dev/null +++ b/tests/e2e/_support/floating-dock/index.html @@ -0,0 +1,11 @@ + + + + + Floating dock interaction fixture + + + + + + diff --git a/tests/e2e/_support/floating-dock/main.ts b/tests/e2e/_support/floating-dock/main.ts new file mode 100644 index 00000000..a86e86e8 --- /dev/null +++ b/tests/e2e/_support/floating-dock/main.ts @@ -0,0 +1,37 @@ +import { setDevframeClientContext } from '@devframes/hub/client' +import { watchEffect } from 'vue' +import { DockEmbedded } from '../../../../packages/hub-ui/src/client/components/DockEmbedded' +import { createMockDocksContext } from '../../../../packages/hub-ui/src/client/stories/mock-context' + +async function main(): Promise { + const params = new URLSearchParams(location.search) + const context = await createMockDocksContext({ + isTrusted: params.has('trusted'), + panel: { + position: params.has('vertical') + ? 'left' + : 'bottom', + left: params.has('vertical') + ? 0 + : 50, + top: params.has('vertical') + ? 50 + : 100, + inactiveTimeout: 30_000, + }, + }) + setDevframeClientContext(context) + document.body.appendChild(new DockEmbedded({ context })) + + watchEffect(() => { + document.body.dataset.selected = context.docks.selected?.id ?? '' + document.body.dataset.open = String(context.panel.session.open) + }) + + const after = document.createElement('button') + after.id = 'after' + after.textContent = 'After dock' + document.body.appendChild(after) +} + +void main() diff --git a/tests/e2e/floating-dock.spec.ts b/tests/e2e/floating-dock.spec.ts new file mode 100644 index 00000000..4da6a9a7 --- /dev/null +++ b/tests/e2e/floating-dock.spec.ts @@ -0,0 +1,117 @@ +import type { Locator } from '@playwright/test' +import type { ViteDevServer } from 'vite' +import { fileURLToPath } from 'node:url' +import { expect, test } from '@playwright/test' +import { createServer } from 'vite' +import { alias } from '../../alias' + +let server: ViteDevServer +let origin: string + +test.beforeAll(async ({ browserName }, { workerIndex }) => { + server = await createServer({ + configFile: fileURLToPath(new URL('../../packages/hub-ui/vite.embedded.config.ts', import.meta.url)), + root: fileURLToPath(new URL('./_support/floating-dock', import.meta.url)), + cacheDir: fileURLToPath(new URL(`../../node_modules/.vite/floating-dock/${browserName}-${workerIndex}`, import.meta.url)), + resolve: { alias }, + server: { host: '127.0.0.1', port: 0 }, + }) + await server.listen() + origin = server.resolvedUrls!.local[0] +}) + +test.afterAll(async () => { + await server?.close() +}) + +async function center(locator: Locator): Promise<{ x: number, y: number }> { + const box = await locator.boundingBox() + expect(box).not.toBeNull() + return { x: box!.x + box!.width / 2, y: box!.y + box!.height / 2 } +} + +for (const trusted of [false, true]) { + test.describe(trusted ? 'trusted floating dock' : 'unauthorized floating dock', () => { + const name = trusted ? 'Settings' : 'Unauthorized' + + test.beforeEach(async ({ page }) => { + await page.clock.install() + await page.goto(`${origin}?${trusted ? 'trusted' : ''}`) + await expect(page.locator('#devframes-dock')).toBeVisible() + }) + + for (const vertical of [false, true]) { + test(`center pointer click activates the visible control (${vertical ? 'vertical' : 'horizontal'})`, async ({ page }) => { + if (vertical) + await page.goto(`${origin}?${trusted ? 'trusted&' : ''}vertical`) + const dock = page.locator('#devframes-dock') + await dock.hover() + await expect(page.locator('#devframes-anchor')).not.toHaveClass(/devframes-minimized/) + const button = dock.getByRole('button', { name, exact: true }) + // Wait for expansion so the click lands beneath the centered logo. + await expect(button).toBeInViewport() + const logo = dock.locator(':scope > div').first() + await expect(logo).toHaveCSS('opacity', '0') + await expect(dock.locator(':scope > div').last()).toHaveCSS('opacity', '1') + expect(await logo.evaluate((el) => { + const box = el.getBoundingClientRect() + const root = el.getRootNode() as ShadowRoot + return root.elementsFromPoint(box.x + box.width / 2, box.y + box.height / 2) + .some(target => el.contains(target)) + })).toBe(false) + const point = await center(button) + await page.mouse.click(point.x, point.y) + await expect(page.locator('body')).toHaveAttribute('data-open', 'true') + await expect(page.locator('body')).toHaveAttribute('data-selected', trusted ? '~settings' : '~client-auth-notice') + }) + } + + test('minimized controls leave the accessibility tree and tab order', async ({ page }) => { + const dock = page.locator('#devframes-dock') + await dock.hover() + await expect(dock.getByRole('button', { name, exact: true })).toHaveCount(1) + await page.mouse.move(0, 0) + await page.clock.fastForward(31_000) + await expect(page.locator('#devframes-anchor')).toHaveClass(/devframes-minimized/) + await expect(dock.getByRole('button')).toHaveCount(0) + const hidden = dock.locator('button').first() + await hidden.evaluate(el => el.focus()) + await expect(hidden).not.toBeFocused() + await page.locator('#before').focus() + await page.keyboard.press('Tab') + await expect(page.locator('#after')).toBeFocused() + + await dock.hover() + await expect(dock.getByRole('button', { name, exact: true })).toHaveCount(1) + await dock.getByRole('button', { name, exact: true }).focus() + await expect(dock.getByRole('button', { name, exact: true })).toBeFocused() + }) + + test('keeps focused controls available past the inactivity timeout', async ({ page }) => { + const dock = page.locator('#devframes-dock') + await dock.hover() + const button = dock.getByRole('button', { name, exact: true }) + await button.focus() + await page.mouse.move(0, 0) + await page.clock.fastForward(31_000) + await expect(button).toBeFocused() + await expect(page.locator('#devframes-anchor')).not.toHaveClass(/devframes-minimized/) + + await page.locator('#before').focus() + await expect(page.locator('#devframes-anchor')).toHaveClass(/devframes-minimized/) + await expect(dock.getByRole('button')).toHaveCount(0) + }) + + test('minimized dock remains draggable', async ({ page }) => { + const anchor = page.locator('#devframes-anchor') + await page.clock.fastForward(31_000) + await expect(anchor).toHaveClass(/devframes-minimized/) + const point = await center(page.locator('#devframes-dock')) + await page.mouse.move(point.x, point.y) + await page.mouse.down() + await page.mouse.move(20, 300, { steps: 10 }) + await page.mouse.up() + await expect(anchor).toHaveClass(/devframes-vertical/) + }) + }) +}