1 import type { MutableRefObject } from 'react';
2 import { useEffect, useRef, useState } from 'react';
4 import { useHandler } from './useHandler';
6 export interface ContactFocusContext {
8 listRef: MutableRefObject<HTMLElement | null>;
11 export const useContactFocus = ({ elementIDs, listRef }: ContactFocusContext) => {
12 const [focusIndex, setFocusIndex] = useState<number>();
14 const getFocusedId = () => (focusIndex !== undefined ? elementIDs[focusIndex] : undefined);
16 const focusedIDRef = useRef(getFocusedId());
18 const handleFocus = useHandler((index) => {
22 const focusOnElementByIndex = (index: number) => {
23 const element = listRef.current?.querySelector(
24 `[data-shortcut-target="contact-container"][data-element-id="${elementIDs[index]}"]`
30 if (focusIndex !== undefined) {
31 focusOnElementByIndex(focusIndex);
34 focusedIDRef.current = getFocusedId();