{"version":3,"file":"use-toolbar-navigation.mjs","names":[],"sources":["../../../src/components/Toolbar/use-toolbar-navigation.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\ninterface UseToolbarNavigationOptions {\n  orientation: 'horizontal' | 'vertical';\n  loop: boolean;\n  dir: 'ltr' | 'rtl';\n}\n\nexport function useToolbarNavigation({ orientation, loop, dir }: UseToolbarNavigationOptions) {\n  const toolbarRef = useRef<HTMLDivElement>(null);\n  const activeItemRef = useRef<HTMLElement | null>(null);\n\n  const getFocusableItems = useCallback((): HTMLElement[] => {\n    if (!toolbarRef.current) {\n      return [];\n    }\n    return Array.from(\n      toolbarRef.current.querySelectorAll<HTMLElement>('[data-toolbar-toggle]:not([data-disabled])')\n    );\n  }, []);\n\n  const syncTabIndex = useCallback(() => {\n    const items = getFocusableItems();\n    const tabStop =\n      activeItemRef.current && items.includes(activeItemRef.current)\n        ? activeItemRef.current\n        : items[0];\n\n    items.forEach((item) => {\n      item.setAttribute('tabindex', item === tabStop ? '0' : '-1');\n    });\n  }, [getFocusableItems]);\n\n  useEffect(() => {\n    syncTabIndex();\n\n    const node = toolbarRef.current;\n    if (!node) {\n      return undefined;\n    }\n\n    const observer = new MutationObserver(syncTabIndex);\n    observer.observe(node, { childList: true, subtree: true, attributeFilter: ['data-disabled'] });\n\n    return () => observer.disconnect();\n  }, [syncTabIndex]);\n\n  const handleKeyDown = useCallback(\n    (event: React.KeyboardEvent<HTMLDivElement>) => {\n      const items = getFocusableItems();\n      if (items.length === 0) {\n        return;\n      }\n\n      const currentIndex = items.findIndex((item) => item === document.activeElement);\n      if (currentIndex === -1) {\n        return;\n      }\n\n      let nextIndex: number | null = null;\n\n      const forwardKey = dir === 'rtl' ? 'ArrowLeft' : 'ArrowRight';\n      const backwardKey = dir === 'rtl' ? 'ArrowRight' : 'ArrowLeft';\n\n      const isForward =\n        (orientation === 'horizontal' && event.key === forwardKey) ||\n        (orientation === 'vertical' && event.key === 'ArrowDown');\n\n      const isBackward =\n        (orientation === 'horizontal' && event.key === backwardKey) ||\n        (orientation === 'vertical' && event.key === 'ArrowUp');\n\n      if (isForward) {\n        event.preventDefault();\n        if (currentIndex < items.length - 1) {\n          nextIndex = currentIndex + 1;\n        } else if (loop) {\n          nextIndex = 0;\n        }\n      } else if (isBackward) {\n        event.preventDefault();\n        if (currentIndex > 0) {\n          nextIndex = currentIndex - 1;\n        } else if (loop) {\n          nextIndex = items.length - 1;\n        }\n      } else if (event.key === 'Home') {\n        event.preventDefault();\n        nextIndex = 0;\n      } else if (event.key === 'End') {\n        event.preventDefault();\n        nextIndex = items.length - 1;\n      }\n\n      if (nextIndex !== null) {\n        items[nextIndex].focus();\n      }\n    },\n    [orientation, loop, dir, getFocusableItems]\n  );\n\n  const handleItemFocus = useCallback(\n    (event: React.FocusEvent<HTMLDivElement>) => {\n      const target = event.target as HTMLElement;\n      if (getFocusableItems().includes(target)) {\n        activeItemRef.current = target;\n        syncTabIndex();\n      }\n    },\n    [getFocusableItems, syncTabIndex]\n  );\n\n  return { toolbarRef, handleKeyDown, handleItemFocus };\n}\n"],"mappings":";;;AAQA,SAAgB,qBAAqB,EAAE,aAAa,MAAM,OAAoC;CAC5F,MAAM,aAAa,OAAuB,IAAI;CAC9C,MAAM,gBAAgB,OAA2B,IAAI;CAErD,MAAM,oBAAoB,kBAAiC;EACzD,IAAI,CAAC,WAAW,SACd,OAAO,CAAC;EAEV,OAAO,MAAM,KACX,WAAW,QAAQ,iBAA8B,4CAA4C,CAC/F;CACF,GAAG,CAAC,CAAC;CAEL,MAAM,eAAe,kBAAkB;EACrC,MAAM,QAAQ,kBAAkB;EAChC,MAAM,UACJ,cAAc,WAAW,MAAM,SAAS,cAAc,OAAO,IACzD,cAAc,UACd,MAAM;EAEZ,MAAM,SAAS,SAAS;GACtB,KAAK,aAAa,YAAY,SAAS,UAAU,MAAM,IAAI;EAC7D,CAAC;CACH,GAAG,CAAC,iBAAiB,CAAC;CAEtB,gBAAgB;EACd,aAAa;EAEb,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