Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions e2e/storage/context-menu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/storage/explorer/FileRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<button
class="btn btn-ghost btn-xs opacity-0 group-hover:opacity-100"
aria-label={m.storage_action_actions_for({ name: keyToName(file.key) })}
onclick={(e) => storage.openContextMenu(e, file.key)}
onclick={(e) => storage.openActionsMenu(e, file.key)}
>
<IconMoreHoriz class="size-4" aria-hidden="true" />
</button>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/storage/explorer/FolderRow.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<button
class="btn btn-ghost btn-xs opacity-0 group-hover:opacity-100"
aria-label={m.storage_action_actions_for({ name: keyToName(folder.key) })}
onclick={(e) => storage.openContextMenu(e, folder.key)}
onclick={(e) => storage.openActionsMenu(e, folder.key)}
>
<IconMoreHoriz class="size-4" aria-hidden="true" />
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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'] });
Expand Down Expand Up @@ -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' });
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 16 additions & 2 deletions src/lib/storage/state.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,30 @@ 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();

if (!this.selectedKeys.has(key)) {
this.selectedKeys = new SvelteSet<string>([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 => {
Expand Down
Loading