-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34480 from shubham1206agra/fix-focus-behavior
Fixed focus behavior and optimize active element role implementation
- Loading branch information
Showing
8 changed files
with
86 additions
and
38 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import type {ActiveElementRoleContextValue, ActiveElementRoleProps} from './types'; | ||
|
||
const ActiveElementRoleContext = React.createContext<ActiveElementRoleContextValue>({ | ||
role: null, | ||
}); | ||
|
||
function ActiveElementRoleProvider({children}: ActiveElementRoleProps) { | ||
const value = React.useMemo( | ||
() => ({ | ||
role: null, | ||
}), | ||
[], | ||
); | ||
|
||
return <ActiveElementRoleContext.Provider value={value}>{children}</ActiveElementRoleContext.Provider>; | ||
} | ||
|
||
export default ActiveElementRoleProvider; | ||
export {ActiveElementRoleContext}; |
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,40 @@ | ||
import React, {useEffect, useState} from 'react'; | ||
import type {ActiveElementRoleContextValue, ActiveElementRoleProps} from './types'; | ||
|
||
const ActiveElementRoleContext = React.createContext<ActiveElementRoleContextValue>({ | ||
role: null, | ||
}); | ||
|
||
function ActiveElementRoleProvider({children}: ActiveElementRoleProps) { | ||
const [activeRoleRef, setRole] = useState<string | null>(document?.activeElement?.role ?? null); | ||
|
||
const handleFocusIn = () => { | ||
setRole(document?.activeElement?.role ?? null); | ||
}; | ||
|
||
const handleFocusOut = () => { | ||
setRole(null); | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener('focusin', handleFocusIn); | ||
document.addEventListener('focusout', handleFocusOut); | ||
|
||
return () => { | ||
document.removeEventListener('focusin', handleFocusIn); | ||
document.removeEventListener('focusout', handleFocusOut); | ||
}; | ||
}, []); | ||
|
||
const value = React.useMemo( | ||
() => ({ | ||
role: activeRoleRef, | ||
}), | ||
[activeRoleRef], | ||
); | ||
|
||
return <ActiveElementRoleContext.Provider value={value}>{children}</ActiveElementRoleContext.Provider>; | ||
} | ||
|
||
export default ActiveElementRoleProvider; | ||
export {ActiveElementRoleContext}; |
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,9 @@ | ||
type ActiveElementRoleContextValue = { | ||
role: string | null; | ||
}; | ||
|
||
type ActiveElementRoleProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export type {ActiveElementRoleContextValue, ActiveElementRoleProps}; |
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 was deleted.
Oops, something went wrong.
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,32 +1,15 @@ | ||
import {useEffect, useRef} from 'react'; | ||
import {useContext} from 'react'; | ||
import {ActiveElementRoleContext} from '@components/ActiveElementRoleProvider'; | ||
import type UseActiveElementRole from './types'; | ||
|
||
/** | ||
* Listens for the focusin and focusout events and sets the DOM activeElement to the state. | ||
* On native, we just return null. | ||
*/ | ||
const useActiveElementRole: UseActiveElementRole = () => { | ||
const activeRoleRef = useRef(document?.activeElement?.role); | ||
const {role} = useContext(ActiveElementRoleContext); | ||
|
||
const handleFocusIn = () => { | ||
activeRoleRef.current = document?.activeElement?.role; | ||
}; | ||
|
||
const handleFocusOut = () => { | ||
activeRoleRef.current = null; | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener('focusin', handleFocusIn); | ||
document.addEventListener('focusout', handleFocusOut); | ||
|
||
return () => { | ||
document.removeEventListener('focusin', handleFocusIn); | ||
document.removeEventListener('focusout', handleFocusOut); | ||
}; | ||
}, []); | ||
|
||
return activeRoleRef.current; | ||
return role; | ||
}; | ||
|
||
export default useActiveElementRole; |
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,3 @@ | ||
type UseActiveElementRole = () => string | null | undefined; | ||
type UseActiveElementRole = () => string | null; | ||
|
||
export default UseActiveElementRole; |