{"version":3,"file":"useInertWhenHidden.mjs","names":[],"sources":["../../../../../src/components/Accessibility/hooks/useInertWhenHidden.ts"],"sourcesContent":["import React, { useMemo } from 'react';\n\n/**\n * Props returned by {@link useInertWhenHidden}, to spread onto the element that stays mounted.\n *\n * `inert` is typed `boolean | undefined` so the object spreads onto a host element on every React\n * version, although its runtime value is the string `'true'` on React 17/18.\n */\nexport type InertWhenHiddenProps = {\n  'aria-hidden': true | undefined;\n  hidden: boolean | undefined;\n  inert: boolean | undefined;\n  tabIndex: number | undefined;\n};\n\nconst REACT_MAJOR = parseInt(React.version, 10);\n// React 19 takes `inert` as a boolean; React 17/18 only forward it as a string and warn or drop the\n// attribute for a boolean. The cast keeps the public type `boolean`.\nconst INERT_PRESENT = (REACT_MAJOR >= 19 ? true : 'true') as unknown as boolean;\n\nconst SHOWN_PROPS: InertWhenHiddenProps = {\n  'aria-hidden': undefined,\n  hidden: undefined,\n  inert: undefined,\n  tabIndex: undefined,\n};\n\nconst HIDDEN_PROPS: InertWhenHiddenProps = {\n  'aria-hidden': true,\n  hidden: true,\n  inert: INERT_PRESENT,\n  tabIndex: -1,\n};\n\nconst HIDDEN_PROPS_WITHOUT_HIDDEN_ATTRIBUTE: InertWhenHiddenProps = {\n  'aria-hidden': true,\n  hidden: undefined,\n  inert: INERT_PRESENT,\n  tabIndex: -1,\n};\n\n/** Options for {@link useInertWhenHidden}. */\nexport type UseInertWhenHiddenOptions = {\n  /**\n   * Whether to set the `hidden` attribute (`display: none`) while hidden. Set `false` for elements\n   * hidden by CSS, such as a transition that `display: none` would cancel: `aria-hidden`, `inert`\n   * and `tabIndex={-1}` still apply and the element stays laid out. Defaults to `true`.\n   */\n  setHiddenAttribute?: boolean;\n};\n\n/**\n * Hides a control from assistive technology and the tab order while it stays mounted.\n *\n * @remarks\n * Prefer not rendering the control at all; use this only when it must stay mounted, e.g. to keep\n * state, layout measurements or enter/exit transitions. While `hidden` is `true` the returned\n * props set `aria-hidden`, `tabIndex={-1}`, `inert` and, unless `setHiddenAttribute` is `false`,\n * `hidden`; `aria-hidden` and `tabIndex` cover engines without `inert` support. Otherwise every\n * prop is `undefined`, so spreading them is a no-op and `aria-hidden` is never set to `\"false\"`.\n *\n * @example\n * ```tsx\n * const Toolbar = ({ collapsed }: { collapsed: boolean }) => {\n *   const inertProps = useInertWhenHidden(collapsed);\n *   return <button {...inertProps}>Add attachment</button>;\n * };\n * ```\n *\n * @param hidden - Whether the control is currently hidden.\n * @param options - Behavior options, see {@link UseInertWhenHiddenOptions}.\n * @returns A memoized props object to spread onto the element.\n */\nexport const useInertWhenHidden = (\n  hidden: boolean,\n  options?: UseInertWhenHiddenOptions,\n): InertWhenHiddenProps => {\n  const setHiddenAttribute = options?.setHiddenAttribute ?? true;\n  return useMemo(() => {\n    if (!hidden) return SHOWN_PROPS;\n    return setHiddenAttribute ? HIDDEN_PROPS : HIDDEN_PROPS_WITHOUT_HIDDEN_ATTRIBUTE;\n  }, [hidden, setHiddenAttribute]);\n};\n"],"mappings":";;AAkBA,IAAM,gBAHc,SAAS,MAAM,SAAS,EAGrB,KAAe,KAAK,OAAO;AAElD,IAAM,cAAoC;CACxC,eAAe,KAAA;CACf,QAAQ,KAAA;CACR,OAAO,KAAA;CACP,UAAU,KAAA;AACZ;AAEA,IAAM,eAAqC;CACzC,eAAe;CACf,QAAQ;CACR,OAAO;CACP,UAAU;AACZ;AAEA,IAAM,wCAA8D;CAClE,eAAe;CACf,QAAQ,KAAA;CACR,OAAO;CACP,UAAU;AACZ;;;;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,sBACX,QACA,YACyB;CACzB,MAAM,qBAAqB,SAAS,sBAAsB;CAC1D,OAAO,cAAc;EACnB,IAAI,CAAC,QAAQ,OAAO;EACpB,OAAO,qBAAqB,eAAe;CAC7C,GAAG,CAAC,QAAQ,kBAAkB,CAAC;AACjC"}