{"version":3,"file":"portal-atom.mjs","names":[],"sources":["../../../../@mongez/react-atom/src/portal-atom.ts"],"sourcesContent":["import { atom } from \"./react-atom\";\nimport { type ReactAtom } from \"./types\";\n\nexport type PortalActions<T = any> = {\n  /**\n   * Opens the portal with optional data.\n   * @param data - Optional data to be passed when opening the portal.\n   */\n  open: (data?: T) => void;\n\n  /**\n   * Closes the portal.\n   */\n  close: () => void;\n\n  /**\n   * Toggles the portal's open state. If the portal is closed, it opens with optional data.\n   * If the portal is open, it closes.\n   * @param data - Optional data to be passed when opening the portal.\n   */\n  toggle: (data?: T) => void;\n\n  /**\n   * Hook to determine if the portal is currently open.\n   * @returns A boolean indicating if the portal is open.\n   */\n  useOpened: () => boolean;\n\n  /**\n   * Hook to retrieve the current data associated with the portal.\n   * @returns The data associated with the portal.\n   */\n  useData: () => T;\n};\n\ntype PortalData<T = any> = {\n  opened: boolean;\n  data: T;\n};\n\nexport type AtomPortal<T = any> = ReactAtom<PortalData<T>, PortalActions<T>>;\n\n/**\n * Create a portal atom\n * This atom is used to create a portal (a modal, a tooltip, a dropdown, etc.)\n */\nexport function portalAtom<T = any>(name: string, opened: boolean = false) {\n  return atom<PortalData<T>, PortalActions<T>>({\n    key: `${name}-portal`,\n    default: {\n      opened: opened,\n      data: {} as T,\n    },\n    actions: {\n      open(data?: T) {\n        this.merge({\n          opened: true,\n          data,\n        });\n      },\n      close() {\n        this.change(\"opened\", false);\n      },\n      toggle(data?: T) {\n        const opened = this.get(\"opened\");\n\n        if (opened) {\n          return this.change(\"opened\", false);\n        }\n\n        this.merge({\n          opened: true,\n          data,\n        });\n      },\n      useOpened() {\n        return (this as ReactAtom).use(\"opened\");\n      },\n      useData() {\n        return (this as ReactAtom).use(\"data\");\n      },\n    },\n  }) as AtomPortal<T>;\n}\n"],"mappings":";;;;;;;AA8CA,SAAgB,WAAoB,MAAc,SAAkB,OAAO;CACzE,OAAO,KAAsC;EAC3C,KAAK,GAAG,KAAK;EACb,SAAS;GACC;GACR,MAAM,CAAC;EACT;EACA,SAAS;GACP,KAAK,MAAU;IACb,KAAK,MAAM;KACT,QAAQ;KACR;IACF,CAAC;GACH;GACA,QAAQ;IACN,KAAK,OAAO,UAAU,KAAK;GAC7B;GACA,OAAO,MAAU;IAGf,IAFe,KAAK,IAAI,QAEf,GACP,OAAO,KAAK,OAAO,UAAU,KAAK;IAGpC,KAAK,MAAM;KACT,QAAQ;KACR;IACF,CAAC;GACH;GACA,YAAY;IACV,OAAQ,KAAmB,IAAI,QAAQ;GACzC;GACA,UAAU;IACR,OAAQ,KAAmB,IAAI,MAAM;GACvC;EACF;CACF,CAAC;AACH"}