-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add focus context for managing focus on route changes
- Loading branch information
1 parent
da77adb
commit 0dbef43
Showing
10 changed files
with
66 additions
and
5 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/react-router/src/components/FocusContext/FocusContext.tsx
This file contains 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,26 @@ | ||
import React, {useRef, useEffect, ReactNode, createRef} from 'react'; | ||
import {FocusContext as Context} from '../../context'; | ||
import {useCurrentUrl} from '../../hooks'; | ||
import {Focusable} from '../../types'; | ||
|
||
export function FocusContext({children}: {children: ReactNode}) { | ||
const currentUrl = useCurrentUrl(); | ||
const focusRef = createRef<Focusable>(); | ||
const focus = () => { | ||
const target = focusRef.current ?? document.body; | ||
target.focus(); | ||
}; | ||
|
||
const firstRender = useRef(true); | ||
|
||
useEffect(() => { | ||
if (firstRender.current) { | ||
firstRender.current = false; | ||
} else { | ||
focus(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [currentUrl.pathname]); | ||
|
||
return <Context.Provider value={focusRef}>{children}</Context.Provider>; | ||
} |
This file contains 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 @@ | ||
export {FocusContext} from './FocusContext'; |
This file contains 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 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 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import {createContext} from 'react'; | ||
import {EnhancedURL} from './types'; | ||
import {createContext, RefObject} from 'react'; | ||
import {EnhancedURL, Focusable} from './types'; | ||
|
||
export const CurrentUrlContext = createContext<EnhancedURL | null>(null); | ||
export const RouterContext = createContext<import('./router').Router | null>( | ||
null, | ||
); | ||
export const SwitchContext = createContext<{matched(): void} | null>(null); | ||
|
||
export const FocusContext = createContext<RefObject<Focusable> | null>(null); |
This file contains 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,19 @@ | ||
import {useContext} from 'react'; | ||
|
||
import {FocusContext} from '../context'; | ||
|
||
function useFocusContext() { | ||
const context = useContext(FocusContext); | ||
|
||
if (context == null) { | ||
throw new Error( | ||
'You attempted to use the focus context, but none was found. Make sure your code is nested in a <Router />', | ||
); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
export function useRouteChangeFocusRef() { | ||
return useFocusContext(); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export {useCurrentUrl} from './url'; | ||
export {useRouter} from './router'; | ||
export {useNavigationBlock} from './navigation-block'; | ||
export {useRouteChangeFocusRef} from './focus'; |
This file contains 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 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 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