Skip to content
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

Add generic type parameter for useFocusZone container ref #5468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {FocusKeys} from '@primer/behaviors'
import {FocusKeys, type Direction} from '@primer/behaviors'
import {isFocusable} from '@primer/behaviors/utils'
import {omit} from '@styled-system/props'
import type {FocusEventHandler, KeyboardEventHandler, MouseEventHandler, RefObject} from 'react'
Expand Down Expand Up @@ -107,14 +107,14 @@ function TextInputWithTokensInnerComponent<TokenComponentType extends AnyReactCo
useRefObjectAsForwardedRef(forwardedRef, ref)
const [selectedTokenIndex, setSelectedTokenIndex] = useState<number | undefined>()
const [tokensAreTruncated, setTokensAreTruncated] = useState<boolean>(Boolean(visibleTokenCount))
const {containerRef} = useFocusZone(
const {containerRef} = useFocusZone<HTMLDivElement>(
{
focusOutBehavior: 'wrap',
bindKeys: FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd,
focusableElementFilter: element => {
return !element.getAttributeNames().includes('aria-hidden')
},
getNextFocusable: direction => {
getNextFocusable: (direction: Direction, from?: Element, event?: KeyboardEvent): HTMLElement | undefined => {
if (!selectedTokenIndex && selectedTokenIndex !== 0) {
return undefined
}
Expand Down
13 changes: 7 additions & 6 deletions packages/react/src/hooks/useFocusZone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import {useProvidedRefOrCreate} from './useProvidedRefOrCreate'
export {FocusKeys} from '@primer/behaviors'
export type {Direction} from '@primer/behaviors'

export interface FocusZoneHookSettings extends Omit<FocusZoneSettings, 'activeDescendantControl'> {
export interface FocusZoneHookSettings<T extends HTMLElement = HTMLElement>
extends Omit<FocusZoneSettings, 'activeDescendantControl'> {
/**
* Optional ref for the container that holds all elements participating in arrow key focus.
* If one is not passed, we will create one for you and return it from the hook.
*/
containerRef?: React.RefObject<HTMLElement>
containerRef?: React.RefObject<T>

/**
* If using the "active descendant" focus pattern, pass `true` or a ref to the controlling
Expand All @@ -25,11 +26,11 @@ export interface FocusZoneHookSettings extends Omit<FocusZoneSettings, 'activeDe
disabled?: boolean
}

export function useFocusZone(
settings: FocusZoneHookSettings = {},
export function useFocusZone<T extends HTMLElement>(
settings: FocusZoneHookSettings<T> = {},
dependencies: React.DependencyList = [],
): {containerRef: React.RefObject<HTMLElement>; activeDescendantControlRef: React.RefObject<HTMLElement>} {
const containerRef = useProvidedRefOrCreate(settings.containerRef)
): {containerRef: React.RefObject<T>; activeDescendantControlRef: React.RefObject<HTMLElement>} {
const containerRef = useProvidedRefOrCreate<T>(settings.containerRef)
const useActiveDescendant = !!settings.activeDescendantFocus
const passedActiveDescendantRef =
typeof settings.activeDescendantFocus === 'boolean' || !settings.activeDescendantFocus
Expand Down
Loading