{"version":3,"sources":["../../src/use-text-direction/index.ts"],"sourcesContent":["import { useMount } from '../use-mount'\nimport { useSafeState } from '../use-safe-state'\nimport { normalizeElement, useTargetElement } from '../use-target-element'\nimport { useUpdateEffect } from '../use-update-effect'\n\nimport type { ReactSetState } from '../use-safe-state'\nimport type { ElementTarget } from '../use-target-element'\n\nexport type UseTextDirectionValue = 'ltr' | 'rtl' | 'auto' | ''\n\nexport interface UseTextDirectionOptions {\n  /**\n   * The target element to set the text direction attribute.\n   *\n   * @defaultValue 'html'\n   */\n  target?: ElementTarget\n  /**\n   * The initial text direction value.\n   *\n   * @defaultValue 'ltr'\n   */\n  initialValue?: UseTextDirectionValue\n}\n\nexport type UseTextDirectionReturns = readonly [\n  /**\n   * The current text direction value.\n   */\n  dir: UseTextDirectionValue,\n  /**\n   * Sets the text direction value.\n   */\n  setDir: ReactSetState<UseTextDirectionValue>,\n]\n\n/**\n * A React Hook that helps to get/set the text direction of an element.\n */\nexport function useTextDirection(options: UseTextDirectionOptions = {}): UseTextDirectionReturns {\n  const { target = 'html', initialValue = 'ltr' } = options\n\n  const el = useTargetElement(target)\n  const [dir, setDir] = useSafeState<UseTextDirectionValue>(initialValue)\n\n  useMount(() => setDir(getDirAttribute(el.current, initialValue)))\n\n  useUpdateEffect(() => {\n    if (!el.current) return\n\n    if (dir) {\n      el.current.setAttribute('dir', dir)\n    } else {\n      el.current.removeAttribute('dir')\n    }\n  }, [dir, el.current])\n\n  return [dir, setDir] as const\n}\n\nfunction getDirAttribute(target: ElementTarget, defaultValue: UseTextDirectionValue = 'ltr'): UseTextDirectionValue {\n  const el = normalizeElement(target)\n\n  return (el?.getAttribute('dir') ?? defaultValue) as UseTextDirectionValue\n}\n"],"names":["useTextDirection","options","target","initialValue","el","useTargetElement","useSafeState","dir","setDir","useMount","getDirAttribute","current","useUpdateEffect","setAttribute","removeAttribute","defaultValue","normalizeElement","getAttribute"],"mappings":";;;;+BAuCgBA;;;eAAAA;;;wBAvCS;4BACI;gCACsB;+BACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCzB,SAASA;QAAiBC,UAAAA,iEAAmC,CAAC;IACnE,sBAAkDA,QAA1CC,QAAAA,sCAAS,kDAAiCD,QAAzBE,cAAAA,kDAAe;IAExC,IAAMC,KAAKC,IAAAA,kCAAgB,EAACH;IAC5B,IAAsBI,iCAAAA,IAAAA,0BAAY,EAAwBH,mBAAnDI,MAAeD,kBAAVE,SAAUF;IAEtBG,IAAAA,kBAAQ,EAAC;eAAMD,OAAOE,gBAAgBN,GAAGO,OAAO,EAAER;;IAElDS,IAAAA,gCAAe,EAAC;QACd,IAAI,CAACR,GAAGO,OAAO,EAAE;QAEjB,IAAIJ,KAAK;YACPH,GAAGO,OAAO,CAACE,YAAY,CAAC,OAAON;QACjC,OAAO;YACLH,GAAGO,OAAO,CAACG,eAAe,CAAC;QAC7B;IACF,GAAG;QAACP;QAAKH,GAAGO,OAAO;KAAC;IAEpB,OAAO;QAACJ;QAAKC;KAAO;AACtB;AAEA,SAASE,gBAAgBR,MAAqB;QAAEa,eAAAA,iEAAsC;IACpF,IAAMX,KAAKY,IAAAA,kCAAgB,EAACd;QAEpBE;IAAR,OAAQA,CAAAA,mBAAAA,eAAAA,yBAAAA,GAAIa,YAAY,CAAC,oBAAjBb,8BAAAA,mBAA2BW;AACrC"}