( props: FocusableOptions<T>, domRef: RefObject<FocusableElement | null> )
| 94 | * Used to make an element focusable and capable of auto focus. |
| 95 | */ |
| 96 | export function useFocusable<T extends FocusableElement = FocusableElement>( |
| 97 | props: FocusableOptions<T>, |
| 98 | domRef: RefObject<FocusableElement | null> |
| 99 | ): FocusableAria { |
| 100 | let {focusProps} = useFocus(props); |
| 101 | let {keyboardProps} = useKeyboard(props); |
| 102 | let interactions = mergeProps(focusProps, keyboardProps); |
| 103 | let domProps = useFocusableContext(domRef); |
| 104 | let interactionProps = props.isDisabled ? {} : domProps; |
| 105 | let autoFocusRef = useRef(props.autoFocus); |
| 106 | |
| 107 | useEffect(() => { |
| 108 | if (autoFocusRef.current && domRef.current) { |
| 109 | focusSafely(domRef.current); |
| 110 | } |
| 111 | autoFocusRef.current = false; |
| 112 | }, [domRef]); |
| 113 | |
| 114 | // Always set a tabIndex so that Safari allows focusing native buttons and inputs. |
| 115 | let tabIndex: number | undefined = props.excludeFromTabOrder ? -1 : 0; |
| 116 | if (props.isDisabled) { |
| 117 | tabIndex = undefined; |
| 118 | } |
| 119 | |
| 120 | return { |
| 121 | focusableProps: mergeProps( |
| 122 | { |
| 123 | ...interactions, |
| 124 | tabIndex |
| 125 | }, |
| 126 | interactionProps |
| 127 | ) |
| 128 | }; |
| 129 | } |
| 130 | |
| 131 | export interface FocusableComponentProps extends FocusableOptions { |
| 132 | children: ReactElement<DOMAttributes, string>; |
no test coverage detected