-
Notifications
You must be signed in to change notification settings - Fork 51
feat(react-native): add opt-in back handler API #310
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
heecheolman
wants to merge
8
commits into
main
Choose a base branch
from
back-press
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
8 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@granite-js/react-native': patch | ||
| --- | ||
|
|
||
| add opt-in back handler API |
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
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 |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ | |
| * The name of the app. | ||
| */ | ||
| appName: string; | ||
| /** | ||
|
Check warning on line 17 in packages/react-native/src/app/Granite.tsx
|
||
| * @description | ||
| * The context of the app. | ||
| * | ||
|
|
@@ -39,6 +39,12 @@ | |
| */ | ||
| setIosSwipeGestureEnabled?: ({ isEnabled }: { isEnabled: boolean }) => void; | ||
|
|
||
| /** | ||
| * @description | ||
| * The function to register a handler that runs when the iOS swipe back gesture is detected. | ||
| */ | ||
| setiOSBackPressHandler?: ({ handler }: { handler: () => void }) => Promise<void> | void; | ||
|
|
||
| /** | ||
| * @description | ||
| * The function to provide the initial URL to router. | ||
|
|
@@ -65,7 +71,7 @@ | |
| return { | ||
| registerApp( | ||
| AppContainer: ComponentType<PropsWithChildren<InitialProps>>, | ||
| { appName, context, router, initialScheme, setIosSwipeGestureEnabled, getInitialUrl }: GraniteProps | ||
| { appName, context, router, initialScheme, setIosSwipeGestureEnabled, setiOSBackPressHandler, getInitialUrl }: GraniteProps | ||
| ): (initialProps: InitialProps) => JSX.Element { | ||
| if (appName === ENTRY_BUNDLE_NAME) { | ||
| throw new Error(`Reserved app name 'shared' cannot be used`); | ||
|
|
@@ -80,6 +86,7 @@ | |
| initialProps={initialProps} | ||
| initialScheme={initialSchemeValue} | ||
| setIosSwipeGestureEnabled={setIosSwipeGestureEnabled} | ||
| setiOSBackPressHandler={setiOSBackPressHandler} | ||
| getInitialUrl={getInitialUrl} | ||
| appName={appName} | ||
| context={context} | ||
|
|
||
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
85 changes: 85 additions & 0 deletions
85
packages/react-native/src/router/components/CanGoBackGuard.spec.tsx
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,85 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import { BackHandler } from 'react-native'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { CanGoBackGuard } from './CanGoBackGuard'; | ||
|
|
||
| describe('CanGoBackGuard', () => { | ||
| beforeEach(() => { | ||
| vi.mocked(BackHandler.addEventListener).mockClear(); | ||
| }); | ||
|
|
||
| it('passes Android hardware back events to onBack and consumes the native event', () => { | ||
| const onBack = vi.fn(); | ||
|
|
||
| render( | ||
| <CanGoBackGuard canGoBack={false} hasBackEvent={true} isInitialScreen={true} onBack={onBack}> | ||
| <div /> | ||
| </CanGoBackGuard> | ||
| ); | ||
|
|
||
| const handler = vi.mocked(BackHandler.addEventListener).mock.calls[0]?.[1]; | ||
|
|
||
| expect(BackHandler.addEventListener).toHaveBeenCalledWith('hardwareBackPress', expect.any(Function)); | ||
| expect(handler?.()).toBe(true); | ||
| expect(onBack).toHaveBeenCalledWith({ source: 'androidHardwareBackPress' }); | ||
| }); | ||
|
|
||
| it('does not register Android hardware back when no back event exists', () => { | ||
| const onBack = vi.fn(); | ||
|
|
||
| render( | ||
| <CanGoBackGuard canGoBack={true} hasBackEvent={false} isInitialScreen={true} onBack={onBack}> | ||
| <div /> | ||
| </CanGoBackGuard> | ||
| ); | ||
|
|
||
| expect(BackHandler.addEventListener).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('registers the iOS swipe back handler while back events exist', () => { | ||
| const onBack = vi.fn(); | ||
| const setiOSBackPressHandler = vi.fn(); | ||
|
|
||
| const { unmount } = render( | ||
| <CanGoBackGuard | ||
| canGoBack={true} | ||
| hasBackEvent={true} | ||
| isInitialScreen={true} | ||
| onBack={onBack} | ||
| setiOSBackPressHandler={setiOSBackPressHandler} | ||
| > | ||
| <div /> | ||
| </CanGoBackGuard> | ||
| ); | ||
|
|
||
| setiOSBackPressHandler.mock.calls[0]?.[0].handler(); | ||
|
|
||
| expect(setiOSBackPressHandler).toHaveBeenCalledWith({ handler: expect.any(Function) }); | ||
| expect(onBack).toHaveBeenCalledWith({ source: 'iosSwipeGesture' }); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(setiOSBackPressHandler).toHaveBeenLastCalledWith({ handler: expect.any(Function) }); | ||
| }); | ||
|
|
||
| it('disables iOS swipe when the current state should block default back navigation', () => { | ||
| const setIosSwipeGestureEnabled = vi.fn(); | ||
|
|
||
| const { unmount } = render( | ||
| <CanGoBackGuard | ||
| canGoBack={false} | ||
| hasBackEvent={true} | ||
| isInitialScreen={true} | ||
| setIosSwipeGestureEnabled={setIosSwipeGestureEnabled} | ||
| > | ||
| <div /> | ||
| </CanGoBackGuard> | ||
| ); | ||
|
|
||
| expect(setIosSwipeGestureEnabled).toHaveBeenCalledWith({ isEnabled: false }); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect(setIosSwipeGestureEnabled).toHaveBeenLastCalledWith({ isEnabled: 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about
oniOSBackPressHandler?