{"version":3,"file":"Toast.mjs","names":["SonnerToaster","sonnerToast"],"sources":["../../../src/toast/Toast.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport { type ReactNode } from 'react';\nimport {\n  type ExternalToast as SonnerExternalToast,\n  toast as sonnerToast,\n  Toaster as SonnerToaster,\n  type ToasterProps as SonnerToasterProps,\n  type ToastT as SonnerToastT,\n} from 'sonner';\n\nimport { type CommonProps } from '../_common/types';\nimport { CleanIconButton } from '../clean-icon-button';\nimport { CheckCircleIconSolid } from '../icons/generated/heroIcons/CheckCircleIconSolid';\nimport { ExclamationCircleIconSolid } from '../icons/generated/heroIcons/ExclamationCircleIconSolid';\nimport { XMarkIconOutline } from '../icons/generated/heroIcons/XMarkIconOutline';\nimport { OutlinedButton } from '../outlined-button';\nimport { ProgressBar } from '../progress-bar';\n\ntype ToastVariants = 'neutral' | 'success' | 'danger' | 'progress-bar';\n\ntype ToastCommandVariants = 'neutral' | 'success' | 'danger' | 'progressBar';\n\ntype ToastId = string | number;\n\ntype BaseToastProps = CommonProps<\n  'div',\n  {\n    /** If `true` a close button will be displayed in the toast. */\n    isCloseable?: boolean;\n    /** If `true` the toast will close when the action button is triggered. */\n    shouldCloseOnAction?: boolean;\n    /** Function that will be called when the toast is closed. */\n    onClose?: (id: ToastId) => void;\n    /** If true the toast will close automatically after a certain time. */\n    shouldAutoClose?: boolean;\n    /** Function that will be called when the toast is automatically closed. */\n    onAutoClose?: (id: ToastId) => void;\n    /** If set an action button will be displayed. The value here will be displayed\n     * in the action button. If this is set the toast property `shouldAutoClose` will\n     * automatically be set to `false` unless overridden. */\n    actionLabel?: string;\n    /** Function that will be called when the action button is clicked. */\n    onAction?: (id: ToastId) => void;\n    /** If a dev wishes to set the underlying framework props (Sonner Toast).\n     *  https://sonner.emilkowal.ski/toast#api-reference */\n    sonnerToastProps?: SonnerExternalToast;\n    /** If true the toast is controlled by the parent. */\n    isControlled?: boolean;\n  }\n>;\n\ninterface ToastNeutralProps extends BaseToastProps {\n  icon?: ReactNode;\n}\n// oxlint-disable-next-line @typescript-eslint/no-empty-object-type\ninterface ToastSuccessProps extends BaseToastProps {}\n// oxlint-disable-next-line @typescript-eslint/no-empty-object-type\ninterface ToastDangerProps extends BaseToastProps {}\n\ninterface ToastProgressBarProps extends BaseToastProps {\n  icon?: ReactNode;\n  initialProgressBarValue?: number;\n}\n\ntype ToastVariantsProps =\n  | ToastNeutralProps\n  | ToastSuccessProps\n  | ToastDangerProps\n  | ToastProgressBarProps;\n\ninterface ToasterProps extends Pick<\n  SonnerToasterProps,\n  'position' | 'visibleToasts' | 'className'\n> {\n  sonnerToasterProps?: SonnerToasterProps;\n}\n\nconst Toaster = (props: ToasterProps) => {\n  const {\n    position = 'bottom-left',\n    visibleToasts = 1,\n    className,\n    sonnerToasterProps,\n  } = props;\n\n  const classes = classNames('ndl-toast-container', className);\n\n  return (\n    <SonnerToaster\n      className={classes}\n      position={position}\n      visibleToasts={visibleToasts}\n      {...sonnerToasterProps}\n    />\n  );\n};\n\ntype SonnerToast = typeof sonnerToast;\ninterface CustomSonnerToast extends SonnerToast {\n  needle: Record<string, { onClose?: (id: ToastId) => void }>;\n}\n\nfunction onCloseCallback(\n  callback: ((id: ToastId) => void) | undefined,\n  id: ToastId,\n) {\n  if (callback) {\n    callback(id);\n    // Cleanup\n    delete (sonnerToast as CustomSonnerToast).needle[id];\n  }\n}\n\ntype ToastElement = ToastVariantsProps & {\n  icon: ReactNode;\n  id: ToastId;\n  variant: ToastVariants;\n  text: ReactNode;\n  initialProgressBarValue?: number;\n  isControlled?: boolean;\n};\n\nconst Toast = ({\n  icon,\n  id,\n  text,\n  variant,\n  isCloseable,\n  onClose,\n  actionLabel,\n  onAction,\n  shouldCloseOnAction,\n  initialProgressBarValue,\n  isControlled,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: ToastElement) => {\n  const isProgressBar = variant === 'progress-bar';\n\n  const handleOnClose = () => {\n    if (isControlled === true) {\n      onCloseCallback(onClose, id);\n      return;\n    }\n    sonnerToast.dismiss(id);\n    onCloseCallback(onClose, id);\n  };\n\n  const handleOnAction = () => {\n    if (onAction) {\n      onAction(id);\n    }\n    if (shouldCloseOnAction === true) {\n      sonnerToast.dismiss(id);\n      onCloseCallback(onClose, id);\n    }\n  };\n\n  return (\n    <div\n      id={String(id)}\n      className=\"ndl-toast\"\n      tabIndex={-1}\n      ref={ref}\n      data-variant={variant}\n      data-testid=\"ndl-toast\"\n      {...restProps}\n      {...htmlAttributes}\n    >\n      <div className=\"ndl-toast-content\" tabIndex={-1}>\n        {icon && (\n          <div className={`ndl-toast-icon ndl-toast-${variant}`}>{icon}</div>\n        )}\n        <div className=\"ndl-toast-text\">{text}</div>\n        {actionLabel && (\n          <OutlinedButton\n            className=\"ndl-toast-action\"\n            size=\"small\"\n            onClick={handleOnAction}\n          >\n            {actionLabel}\n          </OutlinedButton>\n        )}\n        {(isCloseable || actionLabel || isProgressBar) && (\n          <CleanIconButton\n            className=\"ndl-toast-close-button\"\n            onClick={handleOnClose}\n            description={null}\n            size=\"small\"\n            htmlAttributes={{\n              'aria-label': 'Close',\n            }}\n          >\n            <XMarkIconOutline />\n          </CleanIconButton>\n        )}\n      </div>\n      {isProgressBar && (\n        <ProgressBar size=\"small\" value={initialProgressBarValue ?? 0} />\n      )}\n    </div>\n  );\n};\n\nfunction getProps(\n  onClose: ((id: ToastId) => void) | undefined,\n  shouldAutoClose: boolean | undefined,\n  onAutoClose: ((id: ToastId) => void) | undefined,\n  onAction: ((id: ToastId) => void) | undefined,\n  sonnerToastProps: SonnerExternalToast | undefined,\n  variant: ToastVariants,\n) {\n  const isProgressBar = variant === 'progress-bar';\n  // https://sonner.emilkowal.ski/toast#api-reference\n  return {\n    duration:\n      !isProgressBar && shouldAutoClose ? (!onAction ? 5000 : 8000) : Infinity,\n    onAutoClose: (toast: SonnerToastT) => {\n      if (onAutoClose) {\n        return onAutoClose(toast.id);\n      }\n    },\n    onDismiss: (toast: SonnerToastT) => {\n      onCloseCallback(onClose, toast.id);\n    },\n    unstyled: true,\n    ...sonnerToastProps,\n  };\n}\n\nfunction createToast(\n  text: ReactNode,\n  props: ToastVariantsProps,\n  variant: ToastVariants,\n  icon: ReactNode,\n) {\n  const {\n    ref,\n    sonnerToastProps = {},\n    onClose,\n    shouldAutoClose,\n    onAutoClose,\n    onAction,\n    ...restProps\n  } = props;\n\n  const { id: sonnerToastPropsId } = sonnerToastProps;\n  sonnerToastProps.id = sonnerToastPropsId ?? `ndl-toast-${Date.now()}`;\n\n  // Unfortunately calling sonnerToast.dismiss does not trigger the onDismiss\n  // callback (weird). So we need to save the onClose callback and call it manually\n  // when calling dismiss manually as we want an onClose callback.\n  // https://sonner.emilkowal.ski/toast#on-close-callback\n  // https://github.com/emilkowalski/sonner/blob/main/src/index.tsx#L320\n  // https://github.com/emilkowalski/sonner/blob/main/src/index.tsx#L359\n  (sonnerToast as CustomSonnerToast).needle = {\n    [String(sonnerToastProps.id)]: {\n      onClose,\n    },\n  };\n\n  return sonnerToast.custom(\n    (id: ToastId) => (\n      <Toast\n        {...restProps}\n        ref={ref}\n        id={id}\n        text={text}\n        variant={variant}\n        icon={icon}\n        onClose={onClose}\n        onAction={onAction}\n      />\n    ),\n    {\n      ...getProps(\n        onClose,\n        shouldAutoClose,\n        onAutoClose,\n        onAction,\n        sonnerToastProps,\n        variant,\n      ),\n    },\n  );\n}\n\n// eslint-disable-next-line no-redeclare\ninterface Toast {\n  neutral: (text: ReactNode, props?: ToastNeutralProps) => ToastId;\n  success: (text: ReactNode, props?: ToastSuccessProps) => ToastId;\n  danger: (text: ReactNode, props?: ToastDangerProps) => ToastId;\n  progressBar: (text: ReactNode, props?: ToastProgressBarProps) => ToastId;\n  updateProgressBarValue: (id: ToastId, value: number) => void;\n  close: (id: ToastId) => void;\n  sonnerToast: typeof sonnerToast;\n}\n\nconst toast: Toast = {\n  close: (id) => {\n    sonnerToast.dismiss(id);\n    onCloseCallback((sonnerToast as CustomSonnerToast).needle[id]?.onClose, id);\n  },\n  danger: (text: ReactNode, props = {}) =>\n    createToast(text, props, 'danger', <ExclamationCircleIconSolid />),\n  neutral: (text, props = {}) => {\n    const { icon, ...restProps } = props;\n    return createToast(text, restProps, 'neutral', icon);\n  },\n  progressBar: (text: ReactNode, props = {}) => {\n    const { icon, ...restProps } = props;\n    return createToast(text, restProps, 'progress-bar', icon);\n  },\n  sonnerToast,\n  success: (text: ReactNode, props = {}) => {\n    const icon = <CheckCircleIconSolid />;\n    return createToast(text, props, 'success', icon);\n  },\n  updateProgressBarValue: (id, givenValue) => {\n    const value = givenValue > 100 ? 100 : givenValue < 0 ? 0 : givenValue;\n\n    // Unable to use the sonner toast.update on custom toasts.\n    const widthValue = `${value}%`;\n    const toastElement = document.getElementById(String(id)) as HTMLDivElement;\n\n    if (!toastElement) {\n      return;\n    }\n\n    const progressBarContainerElement = toastElement.querySelector(\n      '.ndl-progress-bar-container',\n    ) as HTMLDivElement;\n    progressBarContainerElement.ariaValueNow = String(value);\n    const progressBarElement = progressBarContainerElement.querySelector(\n      '.ndl-progress-bar',\n    ) as HTMLDivElement;\n\n    if (!progressBarContainerElement) {\n      return;\n    }\n\n    progressBarElement.style.setProperty('width', widthValue);\n  }, // If devs wish to directly access sonner toast.\n};\n\nexport { Toaster, toast };\nexport type {\n  ToasterProps,\n  ToastVariants,\n  ToastCommandVariants,\n  ToastNeutralProps,\n  ToastSuccessProps,\n  ToastDangerProps,\n  ToastProgressBarProps,\n  ToastVariantsProps,\n  ToastElement,\n  ToastId,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+IE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;;CAuGE;CACA;CACA;CACA;CACA;CACA;;AAgEQ,MAAA,aAAA,CAAA,MAAA;AAIA,MAAA,aAAA,CAAA,MAAA;AA1OZ,MAAM,WAAW,UAAwB;CACvC,MAAM,EACJ,WAAW,eACX,gBAAgB,GAChB,WACA,uBACE;CAEJ,MAAM,UAAU,WAAW,uBAAuB,SAAS;CAE3D,OACE,oBAACA,WAAAA,eAAAA;EACC,WAAW;EACD;EACK;CACX,GAAA,kBACL,CAAA;AAEL;AAOA,SAAS,gBACP,UACA,IACA;CACA,IAAI,UAAU;EACZ,SAAS,EAAE;EAEX,OAAQC,QAAkC,OAAO;CACnD;AACF;AAWA,MAAM,SAAS,SAeK;CAfL,IAAA,EACb,MACA,IACA,MACA,SACA,aACA,SACA,aACA,UACA,qBACA,yBACA,cACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,gBAAgB,YAAY;CAElC,MAAM,sBAAsB;EAC1B,IAAI,iBAAiB,MAAM;GACzB,gBAAgB,SAAS,EAAE;GAC3B;EACF;EACA,QAAY,QAAQ,EAAE;EACtB,gBAAgB,SAAS,EAAE;CAC7B;CAEA,MAAM,uBAAuB;EAC3B,IAAI,UACF,SAAS,EAAE;EAEb,IAAI,wBAAwB,MAAM;GAChC,QAAY,QAAQ,EAAE;GACtB,gBAAgB,SAAS,EAAE;EAC7B;CACF;CAEA,OACE,qBAAC,OAAA,eAAA,eAAA,eAAA;EACC,IAAI,OAAO,EAAE;EACb,WAAU;EACV,UAAU;EACL;EACL,gBAAc;EACd,eAAY;CACR,GAAA,SAAA,GACA,cAAA,GAAA,CAAA,GAAA,EAAA,UARN,CAUE,qB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