(
settings: FocusZoneHookSettings = {},
dependencies: React.DependencyList = [],
)
| 26 | } |
| 27 | |
| 28 | export function useFocusZone( |
| 29 | settings: FocusZoneHookSettings = {}, |
| 30 | dependencies: React.DependencyList = [], |
| 31 | ): {containerRef: React.RefObject<HTMLElement>; activeDescendantControlRef: React.RefObject<HTMLElement>} { |
| 32 | const containerRef = useProvidedRefOrCreate(settings.containerRef) |
| 33 | const useActiveDescendant = !!settings.activeDescendantFocus |
| 34 | const passedActiveDescendantRef = |
| 35 | typeof settings.activeDescendantFocus === 'boolean' || !settings.activeDescendantFocus |
| 36 | ? undefined |
| 37 | : settings.activeDescendantFocus |
| 38 | const activeDescendantControlRef = useProvidedRefOrCreate(passedActiveDescendantRef) |
| 39 | const disabled = settings.disabled |
| 40 | const abortController = React.useRef<AbortController>() |
| 41 | |
| 42 | useEffect( |
| 43 | () => { |
| 44 | if ( |
| 45 | containerRef.current instanceof HTMLElement && |
| 46 | (!useActiveDescendant || activeDescendantControlRef.current instanceof HTMLElement) |
| 47 | ) { |
| 48 | if (!disabled) { |
| 49 | const vanillaSettings: FocusZoneSettings = { |
| 50 | ...settings, |
| 51 | activeDescendantControl: activeDescendantControlRef.current ?? undefined, |
| 52 | } |
| 53 | abortController.current = focusZone(containerRef.current, vanillaSettings) |
| 54 | return () => { |
| 55 | abortController.current?.abort() |
| 56 | } |
| 57 | } else { |
| 58 | abortController.current?.abort() |
| 59 | } |
| 60 | } |
| 61 | }, |
| 62 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 63 | [disabled, ...dependencies], |
| 64 | ) |
| 65 | |
| 66 | return {containerRef, activeDescendantControlRef} |
| 67 | } |
no test coverage detected