diff --git a/e2e/storage/context-menu.spec.ts b/e2e/storage/context-menu.spec.ts index 979e05d5..b63895b3 100644 --- a/e2e/storage/context-menu.spec.ts +++ b/e2e/storage/context-menu.spec.ts @@ -99,6 +99,39 @@ test.describe('Storage S3 — Context Menu', () => { } }); + test('a row actions button opens the menu next to it from the keyboard', async ({ + page + }, testInfo) => { + const credentials = requireGarageCredentials(); + const client = createS3Client(credentials); + const prefix = uniquePrefix(testInfo, 'ctx-keyboard'); + const cleanupKeys = [`${prefix}keyboard.txt`]; + + try { + await putTextObject(client, credentials.bucket, cleanupKeys[0], 'x'); + + await connectAndOpenPrefix(page, credentials, prefix); + + const button = page.getByRole('button', { name: 'Actions for keyboard.txt', exact: true }); + await button.focus(); + await button.press('Enter'); + + await expect(page.getByRole('menu')).toBeFocused(); + // Right below the button, not in the top-left corner. It opens at the button's bottom-right + // corner, on whichever side of it fits into the viewport. + const buttonBox = (await button.boundingBox())!; + const panel = page.locator(':popover-open').filter({ has: page.getByRole('menu') }); + const menuBox = (await panel.boundingBox())!; + const buttonRight = buttonBox.x + buttonBox.width; + expect(menuBox.y).toBeGreaterThanOrEqual(buttonBox.y + buttonBox.height - 1); + expect(menuBox.y - (buttonBox.y + buttonBox.height)).toBeLessThan(24); + expect(menuBox.x).toBeLessThanOrEqual(buttonRight + 1); + expect(menuBox.x + menuBox.width).toBeGreaterThanOrEqual(buttonRight - 1); + } finally { + await deleteKnownKeys(client, credentials.bucket, cleanupKeys); + } + }); + test('unpins a pinned location from its sidebar menu', async ({ page }, testInfo) => { const credentials = requireGarageCredentials(); const client = createS3Client(credentials); diff --git a/src/lib/components/storage/explorer/FileRow.svelte b/src/lib/components/storage/explorer/FileRow.svelte index 97f33eb5..a3706d83 100644 --- a/src/lib/components/storage/explorer/FileRow.svelte +++ b/src/lib/components/storage/explorer/FileRow.svelte @@ -69,7 +69,7 @@ diff --git a/src/lib/components/storage/explorer/FolderRow.svelte b/src/lib/components/storage/explorer/FolderRow.svelte index c84592e3..cf987add 100644 --- a/src/lib/components/storage/explorer/FolderRow.svelte +++ b/src/lib/components/storage/explorer/FolderRow.svelte @@ -122,7 +122,7 @@ diff --git a/src/lib/components/storage/explorer/__tests__/FileRow.svelte.spec.ts b/src/lib/components/storage/explorer/__tests__/FileRow.svelte.spec.ts index 2ed4d30f..aeb1661b 100644 --- a/src/lib/components/storage/explorer/__tests__/FileRow.svelte.spec.ts +++ b/src/lib/components/storage/explorer/__tests__/FileRow.svelte.spec.ts @@ -1,4 +1,4 @@ -import { page } from 'vitest/browser'; +import { page, userEvent } from 'vitest/browser'; import { describe, expect, it, vi } from 'vitest'; import { render } from 'vitest-browser-svelte'; import { faker } from '@faker-js/faker'; @@ -257,17 +257,6 @@ describe('FileRow', () => { expect(spy).toHaveBeenCalledWith('check.txt', true); }); - it('should call openContextMenu on actions button click', async () => { - const file = makeFile({ key: 'actions.txt', contentType: 'text/plain' }); - const state = createState([file]); - const spy = vi.spyOn(state, 'openContextMenu'); - render(FileRowWrapper, { state, file }); - - const btn = page.getByRole('button', { name: 'Actions for actions.txt' }); - await btn.click(); - expect(spy).toHaveBeenCalled(); - }); - it('should not highlight as selected when context menu is active', async () => { const file = makeFile({ key: 'both.txt', contentType: 'text/plain' }); const state = createState([file], { selectedKeys: ['both.txt'] }); @@ -332,4 +321,31 @@ describe('FileRow', () => { await expect.element(page.getByText('résumé.txt')).toBeInTheDocument(); }); }); + + describe('actions button', () => { + const actionsButton = () => page.getByRole('button', { name: 'Actions for menu.txt' }); + + it('opens the menu below the button when pressed from the keyboard', async () => { + const file = makeFile({ key: 'menu.txt', contentType: 'text/plain' }); + const state = createState([file]); + render(FileRowWrapper, { state, file }); + + (actionsButton().element() as HTMLElement).focus(); + await userEvent.keyboard('{Enter}'); + + const rect = actionsButton().element().getBoundingClientRect(); + expect(state.contextMenu).toEqual({ x: rect.right, y: rect.bottom, key: 'menu.txt' }); + }); + + it('opens the menu below the button when clicked, wherever the pointer is', async () => { + const file = makeFile({ key: 'menu.txt', contentType: 'text/plain' }); + const state = createState([file]); + render(FileRowWrapper, { state, file }); + + await actionsButton().click({ position: { x: 2, y: 2 } }); + + const rect = actionsButton().element().getBoundingClientRect(); + expect(state.contextMenu).toEqual({ x: rect.right, y: rect.bottom, key: 'menu.txt' }); + }); + }); }); diff --git a/src/lib/components/storage/explorer/__tests__/FolderRow.svelte.spec.ts b/src/lib/components/storage/explorer/__tests__/FolderRow.svelte.spec.ts index 61771ad6..227d5c5e 100644 --- a/src/lib/components/storage/explorer/__tests__/FolderRow.svelte.spec.ts +++ b/src/lib/components/storage/explorer/__tests__/FolderRow.svelte.spec.ts @@ -170,7 +170,7 @@ describe('FolderRow', () => { it('should open context menu when actions button is clicked', async () => { const folder = makeFolder('my-folder/'); const state = createState([folder]); - const spy = vi.spyOn(state, 'openContextMenu'); + const spy = vi.spyOn(state, 'openActionsMenu'); render(FolderRowWrapper, { state, folder }); await page.getByRole('button', { name: 'Actions for my-folder' }).click(); diff --git a/src/lib/storage/state.svelte.ts b/src/lib/storage/state.svelte.ts index 7f412be1..8bc8bb0f 100644 --- a/src/lib/storage/state.svelte.ts +++ b/src/lib/storage/state.svelte.ts @@ -468,7 +468,21 @@ export class StorageState { // Context menu // ──────────────────────────────────────────────────────────────────────────── + /** Opens the menu of an item at the pointer, e.g. on a right-click on its row. */ openContextMenu = (e: MouseEvent, key: string): void => { + this.openItemMenu(e, key, e.clientX, e.clientY); + }; + + /** + * Opens the menu of an item below its actions button, also when the button is pressed from the + * keyboard, which has no pointer position. + */ + openActionsMenu = (e: MouseEvent & { currentTarget: Element }, key: string): void => { + const rect = e.currentTarget.getBoundingClientRect(); + this.openItemMenu(e, key, rect.right, rect.bottom); + }; + + private openItemMenu(e: MouseEvent, key: string, x: number, y: number): void { e.preventDefault(); e.stopPropagation(); @@ -476,8 +490,8 @@ export class StorageState { this.selectedKeys = new SvelteSet([key]); } - this.contextMenu = { x: e.clientX, y: e.clientY, key }; - }; + this.contextMenu = { x, y, key }; + } /** Open the context menu for the empty space (no specific item). */ openEmptyContextMenu = (e: MouseEvent): void => {