-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make SubNav SubMenus accessible using keyboard (#679)
* add useIsChildFocused hook * allow submenus to be opened from keyboard * remove jsx-a11y rule disable * move useIsChildFocused hook to SubNav folder * rename hook and move ref responsibility * forward refs * fix incorrect spacing * add changeset
- Loading branch information
1 parent
443243b
commit 30f717d
Showing
6 changed files
with
217 additions
and
47 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/react-brand': patch | ||
--- | ||
|
||
Fixed an issue where `SubNav` submenus were not accessible through keyboard navigation |
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
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,53 @@ | ||
import {useCallback, useEffect, useState, type RefObject} from 'react' | ||
|
||
/** | ||
* Determine if a child element of the provided ref is currently focussed. | ||
* @param containerRef The ref to the container element. | ||
* @param onFocusChange The callback to be called when the focus state changes. | ||
* @type T The type of the container element. | ||
* @returns The ref to be applied to the container element. | ||
*/ | ||
export const useContainsFocus = <T extends HTMLElement>( | ||
containerRef?: RefObject<T>, | ||
onFocusChange?: (isFocussed: boolean) => void, | ||
) => { | ||
const [isChildFocused, setIsChildFocused] = useState(false) | ||
|
||
const updateState = useCallback( | ||
(isFocused: boolean) => { | ||
if (isFocused !== isChildFocused) { | ||
setIsChildFocused(isFocused) | ||
onFocusChange?.(isFocused) | ||
} | ||
}, | ||
[isChildFocused, onFocusChange], | ||
) | ||
|
||
useEffect(() => { | ||
if (!containerRef) { | ||
return | ||
} | ||
|
||
const handleFocusIn = () => { | ||
updateState(true) | ||
} | ||
|
||
const handleFocusOut = (event: FocusEvent) => { | ||
if (containerRef.current && !containerRef.current.contains(event.relatedTarget as Node)) { | ||
updateState(false) | ||
} | ||
} | ||
|
||
const container = containerRef.current | ||
|
||
if (container) { | ||
container.addEventListener('focusin', handleFocusIn, true) | ||
container.addEventListener('focusout', handleFocusOut, true) | ||
|
||
return () => { | ||
container.removeEventListener('focusin', handleFocusIn, true) | ||
container.removeEventListener('focusout', handleFocusOut, true) | ||
} | ||
} | ||
}, [updateState, containerRef]) | ||
} |