{"version":3,"file":"DirectionProvider.cjs","names":[],"sources":["../../../src/core/DirectionProvider/DirectionProvider.tsx"],"sourcesContent":["import { createContext, use, useCallback, useState } from 'react';\nimport { useIsomorphicEffect, useMutationObserverTarget } from '@mantine/hooks';\n\nexport type Direction = 'ltr' | 'rtl';\n\nexport interface DirectionContextValue {\n  dir: Direction;\n  toggleDirection: () => void;\n  setDirection: (dir: Direction) => void;\n}\n\nexport const DirectionContext = createContext<DirectionContextValue>({\n  dir: 'ltr',\n  toggleDirection: () => {},\n  setDirection: () => {},\n});\n\nexport function useDirection() {\n  return use(DirectionContext);\n}\n\nexport interface DirectionProviderProps {\n  /** Your application */\n  children: React.ReactNode;\n\n  /** Direction set as a default value, `ltr` by default */\n  initialDirection?: Direction;\n\n  /** Determines whether direction should be updated on mount based on `dir` attribute set on root element (usually html element) @default true  */\n  detectDirection?: boolean;\n}\n\nexport function DirectionProvider({\n  children,\n  initialDirection = 'ltr',\n  detectDirection = true,\n}: DirectionProviderProps) {\n  const [dir, setDir] = useState<Direction>(initialDirection);\n\n  const setDirection = useCallback((direction: Direction) => {\n    setDir(direction);\n    if (document.documentElement.getAttribute('dir') !== direction) {\n      document.documentElement.setAttribute('dir', direction);\n    }\n  }, []);\n\n  const toggleDirection = () => setDirection(dir === 'ltr' ? 'rtl' : 'ltr');\n\n  useIsomorphicEffect(() => {\n    if (detectDirection) {\n      const direction = document.documentElement.getAttribute('dir');\n      if (direction === 'rtl' || direction === 'ltr') {\n        setDir(direction);\n      }\n    }\n  }, []);\n\n  const mutationCallback = useCallback<MutationCallback>(() => {\n    if (typeof document === 'undefined') {\n      return;\n    }\n    const direction = document.documentElement.getAttribute('dir');\n    if (direction === 'rtl' || direction === 'ltr') {\n      setDir((prev) => (prev !== direction ? (direction as Direction) : prev));\n    }\n  }, []);\n\n  useMutationObserverTarget(\n    mutationCallback,\n    detectDirection ? { attributes: true, attributeFilter: ['dir'] } : {},\n    typeof document !== 'undefined' && detectDirection ? document.documentElement : null\n  );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