Skip to content
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
4 changes: 2 additions & 2 deletions .changeset/scope-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ The lightbox's keys now act only while focus is inside it, so text fields, selec
- Now the lightbox handles keys in an `onKeyDown` on its dialog, or on the carousel's root with `inline`. Keys pressed elsewhere on the page don't reach it.
- While the modal lightbox is open, it also takes keys pressed while nothing has focus, as if they were pressed in it. Focus falls to `<body>` when the focused control turns disabled or leaves the page, as a Next button rendered with `disabled={!canNext}` does on the last image. Such a key also moves focus back into the lightbox. Tab then goes to its first control and Shift+Tab to its last.
- ← and → leave text fields, selects, and other editable elements alone. Escape still closes the lightbox from a text field, unless the field calls `preventDefault()` in its own `onKeyDown`.
- ←, →, and Escape do nothing with Alt, Ctrl, or Cmd held, so browser and system shortcuts such as Alt+← keep working. Keys pressed while an input method composes text do nothing at all.
- ←, →, and Escape do nothing with Alt, Ctrl, or Cmd held, so browser and system shortcuts such as Alt+← keep working. Keys pressed while an input method composes text do nothing at all. That includes the Escape that cancels a composition in Safari, which fires `compositionend` first and marks that `keydown` only with `keyCode` 229. With 1.9.0, the lightbox didn't check for composition, so the Escape that cancels a Chinese or Japanese composition could close it.
- The keys act when they go down instead of when they come up. Holding an arrow key still pages once.
- While the modal lightbox is open, ↑, ↓, PageUp, PageDown, Home, and End pressed in it don't scroll the page behind it, with or without Alt, Ctrl, or Cmd, unless a text field has focus. Space doesn't scroll it either, unless Space presses a focused button. With 1.9.0, Space scrolled the page behind the lightbox. Keys pressed in content portaled out of the lightbox, such as a menu that a header button renders into `<body>`, are left to that content, so they scroll the page behind unless that content prevents them. An `inline` carousel never blocks these keys.
- Tested with the unit tests on React 19 and React 18.3.1, the Playwright tests in Chromium, Firefox, and mobile Chromium on React 19, and the Chromium Playwright tests on React 18.3.1. CI runs the WebKit projects.
- Tested with the unit tests on React 19 and React 18.3.1, the Playwright tests in Chromium, Firefox, and mobile Chromium on React 19, and the Chromium Playwright tests on React 18.3.1. CI runs the WebKit projects. A unit test covers the Safari input-method case with the `keydown` that Safari sends. It was not tested in Safari with an input method.

Upgrade notes:

Expand Down
10 changes: 10 additions & 0 deletions src/__tests__/opening-and-closing.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ describe('closing', () => {
expect(props.onClose).not.toHaveBeenCalled();
});

test('Esc that ends input method composition in Safari leaves it open', () => {
const props = lightboxProps({ renderFooter: () => <input aria-label="Caption" /> });
render(<Lightbox {...props} />);

// Safari fires compositionend first, so this keydown has isComposing false and keyCode 229
pressKey('Escape', screen.getByRole('textbox', { name: 'Caption' }), { isComposing: false, keyCode: 229 });

expect(props.onClose).not.toHaveBeenCalled();
});

test('ignores keys that reach it while it fades out', async () => {
const props = lightboxProps();
const { rerender } = render(<Lightbox {...props} />);
Expand Down
4 changes: 3 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ const Lightbox = ({
}: ILightboxProps) => {
const onKey = (event: KeyboardEvent) => {
const target = event.target as Element;
if (!isOpen || event.defaultPrevented || event.isComposing) {
// Safari fires compositionend before the keydown that ends a composition, and marks that key
// only with keyCode 229
if (!isOpen || event.defaultPrevented || event.isComposing || event.keyCode === 229) {
return;
}
const inTextField = target.closest(TEXT_FIELD) !== null;
Expand Down
Loading