Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Fix keydown shortcut within shadow tree #24179

Merged
merged 2 commits into from
Sep 20, 2023
Merged
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
17 changes: 16 additions & 1 deletion code/lib/preview-api/src/modules/preview-web/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3502,6 +3502,19 @@ describe('PreviewWeb', () => {
document.location.search = '?id=component-one--docs&viewMode=docs';
const preview = await createAndRenderPreview();

preview.onKeydown({
composedPath: jest
.fn()
.mockReturnValue([{ tagName: 'div', getAttribute: jest.fn().mockReturnValue(null) }]),
} as any);

expect(mockChannel.emit).toHaveBeenCalledWith(PREVIEW_KEYDOWN, expect.objectContaining({}));
});

it('emits PREVIEW_KEYDOWN for regular elements, fallback to event.target', async () => {
document.location.search = '?id=component-one--docs&viewMode=docs';
const preview = await createAndRenderPreview();

preview.onKeydown({
target: { tagName: 'div', getAttribute: jest.fn().mockReturnValue(null) },
} as any);
Expand All @@ -3514,7 +3527,9 @@ describe('PreviewWeb', () => {
const preview = await createAndRenderPreview();

preview.onKeydown({
target: { tagName: 'input', getAttribute: jest.fn().mockReturnValue(null) },
composedPath: jest
.fn()
.mockReturnValue([{ tagName: 'input', getAttribute: jest.fn().mockReturnValue(null) }]),
} as any);

expect(mockChannel.emit).not.toHaveBeenCalledWith(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import type { StorySpecifier } from '../store/StoryIndexStore';
const globalWindow = globalThis;

function focusInInput(event: Event) {
const target = event.target as Element;
const target = ((event.composedPath && event.composedPath()[0]) || event.target) as Element;
return /input|textarea/i.test(target.tagName) || target.getAttribute('contenteditable') !== null;
}

Expand Down