-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix: Pressing Esc closes fullscreen video instead of 3 dot menu #92167
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
Open
TaduJR
wants to merge
7
commits into
Expensify:main
Choose a base branch
from
TaduJR:refactor-decompose-popover-menu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c4c289
fix: claim Escape in PopoverMenu (v1) + v2 — prevent parent modal dis…
TaduJR e3db2ea
fix: route v1 Escape through handleClose so submenu state resets
TaduJR 9eec7cd
fix: singleton suppressNextEscapeKeyUp to prevent listener leak
TaduJR 39f3ec1
fix: move suppressNextEscapeKeyUp to window capture so PopoverProvide…
TaduJR 25fb38f
fix: claim Escape keydown at window capture to bypass shortcut-stack …
TaduJR 31b6538
refactor: scope useCloseOnEscape to web
TaduJR af27572
refactor: reuse v2 useCloseOnEscape from v1
TaduJR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import {useEffect} from 'react'; | ||
| import claimEscapeKeyDown from '@libs/claimEscapeKeyDown'; | ||
| import suppressNextEscapeKeyUp from '@libs/suppressNextEscapeKeyUp'; | ||
|
|
||
| /** | ||
| * Closes the popover on Escape. Web-only via `claimEscapeKeyDown` (no-op on native); | ||
| * the parent-modal Escape race we're defending against is web-specific. | ||
| */ | ||
| function useCloseOnEscape(isVisible: boolean, close: () => void): void { | ||
| useEffect(() => { | ||
| if (!isVisible) { | ||
| return undefined; | ||
| } | ||
| return claimEscapeKeyDown(() => { | ||
| suppressNextEscapeKeyUp(); | ||
| close(); | ||
| }); | ||
| }, [isVisible, close]); | ||
| } | ||
|
|
||
| export default useCloseOnEscape; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| function claimEscapeKeyDown(): () => void { | ||
| return () => {}; | ||
| } | ||
|
|
||
| export default claimEscapeKeyDown; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * `window` capture keydown for Escape — runs before `react-native-key-command`'s document-capture | ||
| * listener, so the shortcut stack never walks. Sidesteps the "parent re-subscribes and jumps ahead" | ||
| * race that the stack can't defend against. | ||
| */ | ||
| function claimEscapeKeyDown(handler: () => void): () => void { | ||
| const onKeyDown = (event: KeyboardEvent) => { | ||
| if (event.key !== 'Escape') { | ||
| return; | ||
| } | ||
| event.stopImmediatePropagation(); | ||
| handler(); | ||
| }; | ||
| window.addEventListener('keydown', onKeyDown, true); | ||
| return () => window.removeEventListener('keydown', onKeyDown, true); | ||
| } | ||
|
|
||
| export default claimEscapeKeyDown; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| function suppressNextEscapeKeyUp(): () => void { | ||
| return () => {}; | ||
| } | ||
|
|
||
| export default suppressNextEscapeKeyUp; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /** | ||
| * Swallows the next Escape `keyup`. | ||
| * - `window` (not `document`) capture: runs before `PopoverProvider`'s keyup which would otherwise preempt and leak us. | ||
| * - Singleton: each install evicts any pending listener. | ||
| */ | ||
| let pending: ((event: KeyboardEvent) => void) | null = null; | ||
|
|
||
| function suppressNextEscapeKeyUp(): () => void { | ||
| if (pending) { | ||
| window.removeEventListener('keyup', pending, true); | ||
| pending = null; | ||
| } | ||
|
|
||
| const onKeyUp = (event: KeyboardEvent) => { | ||
| if (event.key !== 'Escape') { | ||
| return; | ||
| } | ||
| event.stopImmediatePropagation(); | ||
| window.removeEventListener('keyup', onKeyUp, true); | ||
| if (pending === onKeyUp) { | ||
| pending = null; | ||
| } | ||
| }; | ||
|
|
||
| pending = onKeyUp; | ||
| window.addEventListener('keyup', onKeyUp, true); | ||
|
|
||
| return () => { | ||
| window.removeEventListener('keyup', onKeyUp, true); | ||
| if (pending === onKeyUp) { | ||
| pending = null; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| export default suppressNextEscapeKeyUp; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Bypass haste: react-native preset defaults to iOS → would resolve to the no-op index.native.ts. | ||
| // eslint-disable-next-line import/extensions | ||
| const claimEscapeKeyDown = (require('../../src/libs/claimEscapeKeyDown/index.ts') as {default: (handler: () => void) => () => void}).default; | ||
|
|
||
| function dispatchKeyDown(key: string): boolean { | ||
| let reached = false; | ||
| const sentinel = () => { | ||
| reached = true; | ||
| }; | ||
| document.addEventListener('keydown', sentinel, true); | ||
| document.body.dispatchEvent(new KeyboardEvent('keydown', {key, bubbles: true})); | ||
| document.removeEventListener('keydown', sentinel, true); | ||
| return reached; | ||
| } | ||
|
|
||
| describe('claimEscapeKeyDown', () => { | ||
| it('calls the handler on Escape and stops document-level propagation', () => { | ||
| const handler = jest.fn(); | ||
| const cleanup = claimEscapeKeyDown(handler); | ||
| try { | ||
| expect(dispatchKeyDown('Escape')).toBe(false); | ||
| expect(handler).toHaveBeenCalledTimes(1); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| it('ignores non-Escape keys', () => { | ||
| const handler = jest.fn(); | ||
| const cleanup = claimEscapeKeyDown(handler); | ||
| try { | ||
| expect(dispatchKeyDown('Enter')).toBe(true); | ||
| expect(handler).not.toHaveBeenCalled(); | ||
| } finally { | ||
| cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| it('cleanup detaches the listener', () => { | ||
| const handler = jest.fn(); | ||
| const cleanup = claimEscapeKeyDown(handler); | ||
| cleanup(); | ||
| expect(dispatchKeyDown('Escape')).toBe(true); | ||
| expect(handler).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('runs ahead of a document-capture handler — preempts the shortcut-stack analog', () => { | ||
| // Simulate react-native-key-command's document-capture keydown listener (which would walk the shortcut stack). | ||
| const stackWalker = jest.fn(); | ||
| document.addEventListener('keydown', stackWalker, true); | ||
| const handler = jest.fn(); | ||
| const cleanup = claimEscapeKeyDown(handler); | ||
| try { | ||
| dispatchKeyDown('Escape'); | ||
| expect(handler).toHaveBeenCalledTimes(1); | ||
| expect(stackWalker).not.toHaveBeenCalled(); | ||
| } finally { | ||
| cleanup(); | ||
| document.removeEventListener('keydown', stackWalker, true); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Bypass haste: react-native preset defaults to iOS → would resolve to the no-op index.native.ts. | ||
| // eslint-disable-next-line import/extensions | ||
| const suppressNextEscapeKeyUp = (require('../../src/libs/suppressNextEscapeKeyUp/index.ts') as {default: () => () => void}).default; | ||
|
|
||
| function dispatchAndCheckPropagation(key: string): boolean { | ||
| let reached = false; | ||
| const sentinel = () => { | ||
| reached = true; | ||
| }; | ||
| document.addEventListener('keyup', sentinel, true); | ||
| // body+bubbles instead of dispatching on document: jsdom skips at-target capture listeners. | ||
| document.body.dispatchEvent(new KeyboardEvent('keyup', {key, bubbles: true})); | ||
| document.removeEventListener('keyup', sentinel, true); | ||
| return reached; | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| // Drain any pending suppressor — the helper has module-scope state. | ||
| dispatchAndCheckPropagation('Escape'); | ||
| }); | ||
|
|
||
| describe('suppressNextEscapeKeyUp', () => { | ||
| it('stops propagation of the next Escape keyup', () => { | ||
| suppressNextEscapeKeyUp(); | ||
| expect(dispatchAndCheckPropagation('Escape')).toBe(false); | ||
| }); | ||
|
|
||
| it('ignores non-Escape keys', () => { | ||
| suppressNextEscapeKeyUp(); | ||
| expect(dispatchAndCheckPropagation('Enter')).toBe(true); | ||
| }); | ||
|
|
||
| it('removes itself after firing — the next Escape is not suppressed', () => { | ||
| suppressNextEscapeKeyUp(); | ||
| dispatchAndCheckPropagation('Escape'); | ||
| expect(dispatchAndCheckPropagation('Escape')).toBe(true); | ||
| }); | ||
|
|
||
| it('singleton: a second install evicts the first; only one Escape is suppressed across two installs', () => { | ||
| suppressNextEscapeKeyUp(); | ||
| suppressNextEscapeKeyUp(); | ||
| expect(dispatchAndCheckPropagation('Escape')).toBe(false); | ||
| expect(dispatchAndCheckPropagation('Escape')).toBe(true); | ||
| }); | ||
|
|
||
| it('cleanup return defuses a pending suppressor', () => { | ||
| const cleanup = suppressNextEscapeKeyUp(); | ||
| cleanup(); | ||
| expect(dispatchAndCheckPropagation('Escape')).toBe(true); | ||
| }); | ||
|
|
||
| it('survives a document-capture preempt — no leak when an earlier document listener stops propagation', () => { | ||
| // PopoverProvider analog: stops Escape keyup on document capture, registered before our suppressor. | ||
| let preemptActive = true; | ||
| const preempt = (e: KeyboardEvent) => { | ||
| if (!preemptActive || e.key !== 'Escape') { | ||
| return; | ||
| } | ||
| e.stopImmediatePropagation(); | ||
| }; | ||
| document.addEventListener('keyup', preempt, true); | ||
| try { | ||
| suppressNextEscapeKeyUp(); | ||
| dispatchAndCheckPropagation('Escape'); | ||
| preemptActive = false; | ||
| expect(dispatchAndCheckPropagation('Escape')).toBe(true); | ||
| } finally { | ||
| document.removeEventListener('keyup', preempt, true); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.