{"version":3,"file":"useReleaseAnnouncement.cjs","names":[],"sources":["../../../src/components/ReleaseAnnouncement/useReleaseAnnouncement.tsx"],"sourcesContent":["/*\nCopyright 2024 New Vector Ltd.\n\nSPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial\nPlease see LICENSE files in the repository root for full details.\n*/\n\nimport {\n  arrow,\n  autoUpdate,\n  limitShift,\n  offset,\n  type Placement,\n  shift,\n  useFloating,\n  useId,\n  useInteractions,\n  useRole,\n} from \"@floating-ui/react\";\nimport { type MouseEventHandler, useMemo, useRef } from \"react\";\n\ninterface UseReleaseAnnouncementProps {\n  /**\n   * Whether the release announcement is open.\n   */\n  open: boolean;\n  /**\n   * The header of the release announcement.\n   */\n  header: string;\n  /**\n   * The description of the release announcement.\n   */\n  description: string;\n  /**\n   * The label of the close button.\n   */\n  closeLabel: string;\n  /**\n   * The placement of the release announcement.\n   */\n  placement: Placement;\n  /**\n   * The event handler for the close button.\n   */\n  onClick: MouseEventHandler<HTMLButtonElement>;\n  /**\n   * Whether to display an arrow.\n   */\n  displayArrow?: boolean;\n}\n\n/**\n * This hook provides the necessary props to create a release announcement.\n */\nexport function useReleaseAnnouncement({\n  open,\n  header,\n  description,\n  closeLabel,\n  placement,\n  onClick,\n  displayArrow,\n}: UseReleaseAnnouncementProps) {\n  // Set on `aria-labelledby` attribute\n  const labelId = useId();\n  // Set on `aria-describedby` attribute\n  const descriptionId = useId();\n  const arrowRef = useRef(null);\n\n  const data = useFloating({\n    placement,\n    open,\n    whileElementsMounted: autoUpdate,\n    middleware: [\n      // arrow height 12px + 4px padding\n      offset(16),\n      shift({ limiter: limitShift({ offset: 50 }) }),\n      // add the little arrow along with the floating content\n      displayArrow &&\n        arrow({\n          element: arrowRef,\n        }),\n    ],\n  });\n\n  // The role is used to determine the correct aria role for the floating content.\n  const role = useRole(data.context);\n  const interactions = useInteractions([role]);\n\n  // Put in the context all the useful data and interactions\n  return useMemo(\n    () => ({\n      open,\n      ...data,\n      ...interactions,\n      labelId,\n      descriptionId,\n      header,\n      description,\n      closeLabel,\n      onClick,\n      displayArrow,\n      arrowRef,\n    }),\n    [\n      open,\n      role,\n      interactions,\n      data,\n      labelId,\n      descriptionId,\n      header,\n      description,\n      closeLabel,\n      onClick,\n      displayArrow,\n      arrowRef,\n    ],\n  );\n}\n"],"mappings":";;;;;;;AAuDA,SAAgB,uBAAuB,EACrC,MACA,QACA,aACA,YACA,WACA,SACA,gBAC8B;CAE9B,MAAM,WAAA,GAAA,mBAAA,QAAiB;CAEvB,MAAM,iBAAA,GAAA,mBAAA,QAAuB;CAC7B,MAAM,YAAA,GAAA,MAAA,QAAkB,KAAK;CAE7B,MAAM,QAAA,GAAA,mBAAA,aAAmB;EACvB;EACA;EACA,sBAAsB,mBAAA;EACtB,YAAY;kCAEH,GAAG;iCACJ,EAAE,UAAA,GAAA,mBAAA,YAAoB,EAAE,QAAQ,IAAI,CAAC,EAAE,CAAC;GAE9C,iBAAA,GAAA,mBAAA,OACQ,EACJ,SAAS,UACV,CAAC;GACL;EACF,CAAC;CAGF,MAAM,QAAA,GAAA,mBAAA,SAAe,KAAK,QAAQ;CAClC,MAAM,gBAAA,GAAA,mBAAA,iBAA+B,CAAC,KAAK,CAAC;AAG5C,SAAA,GAAA,MAAA,gBACS;EACL;EACA,GAAG;EACH,GAAG;EACH;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD,GACD;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACD,CACF"}