From 4ba5d741b970105d7e9e037cccc5e6cb243e2a55 Mon Sep 17 00:00:00 2001 From: Tim Ellenberger Date: Tue, 10 Nov 2026 15:57:55 -0700 Subject: [PATCH 1/4] test(keys): expect the Escape that ends a composition in Safari to leave it open Safari fires compositionend before the keydown that commits or cancels a composition, so that keydown has isComposing false and keyCode 229. The lightbox closed on it. Co-Authored-By: Claude Opus 5.5 --- src/__tests__/opening-and-closing.test.tsx | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/__tests__/opening-and-closing.test.tsx b/src/__tests__/opening-and-closing.test.tsx index 61ab63b..407e393 100644 --- a/src/__tests__/opening-and-closing.test.tsx +++ b/src/__tests__/opening-and-closing.test.tsx @@ -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: () => }); + render(); + + // 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(); From 00c32773faa4d8f6854d1367279d73be95dacb2a Mon Sep 17 00:00:00 2001 From: Tim Ellenberger Date: Tue, 10 Nov 2026 15:57:55 -0700 Subject: [PATCH 2/4] fix(keys): ignore the keydown that ends a composition in Safari The composition guard checked only isComposing, which Safari has already cleared for the keydown that ends a composition. keyCode 229 marks that key, so the guard checks it too. Co-Authored-By: Claude Opus 5.5 --- src/index.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.tsx b/src/index.tsx index 78bae13..d8e3b8d 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -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; From 503e42a0fef3a2bb944ce25624f15eb016128ea4 Mon Sep 17 00:00:00 2001 From: Tim Ellenberger Date: Tue, 10 Nov 2026 15:57:55 -0700 Subject: [PATCH 3/4] docs(changeset): say keys that end a composition in Safari do nothing Co-Authored-By: Claude Opus 5.5 --- .changeset/scope-keys.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/scope-keys.md b/.changeset/scope-keys.md index f095533..3a8f969 100644 --- a/.changeset/scope-keys.md +++ b/.changeset/scope-keys.md @@ -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 `` 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 or Enter that ends a composition in Safari, which fires `compositionend` first and marks that `keydown` only with `keyCode` 229. - 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 ``, 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: From af28f92cf28e5d1f7fad87f240cb7b2dcbbf908b Mon Sep 17 00:00:00 2001 From: Tim Ellenberger Date: Tue, 10 Nov 2026 16:21:02 -0700 Subject: [PATCH 4/4] docs(changeset): say what 1.9.0 did with the Escape that cancels a composition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.9.0 acted on keyup for ←, →, and Escape with no composition check, so the Escape that cancels a composition could close the lightbox. The narrower guard this branch fixes came with #81 and was never released, so the release notes compare against 1.9.0. Co-Authored-By: Claude Opus 5.5 --- .changeset/scope-keys.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/scope-keys.md b/.changeset/scope-keys.md index 3a8f969..859b89f 100644 --- a/.changeset/scope-keys.md +++ b/.changeset/scope-keys.md @@ -8,7 +8,7 @@ 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 `` 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. That includes the Escape or Enter that ends a composition in Safari, which fires `compositionend` first and marks that `keydown` only with `keyCode` 229. +- ←, →, 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 ``, 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. 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.