Skip to content

Commit

Permalink
implement fallback to composedPath in the unlikely case that it doesn…
Browse files Browse the repository at this point in the history
…'t exist or returns an empty array
  • Loading branch information
stropitek committed Sep 19, 2023
1 parent 4be9ce5 commit 16968f0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions code/lib/preview-api/src/modules/preview-web/PreviewWeb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3511,6 +3511,17 @@ describe('PreviewWeb', () => {
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);

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

it('does not emit PREVIEW_KEYDOWN for input elements', async () => {
document.location.search = '?id=component-one--docs&viewMode=docs';
const preview = await createAndRenderPreview();
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.composedPath()[0] 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

0 comments on commit 16968f0

Please sign in to comment.