ol-toast.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import classNames from 'classnames'
  2. import { Toast as BS5Toast } from 'react-bootstrap'
  3. import {
  4. NotificationIcon,
  5. NotificationType,
  6. } from '../../../../shared/components/notification'
  7. import { useTranslation } from 'react-i18next'
  8. import MaterialIcon from '../../../../shared/components/material-icon'
  9. import { ReactNode, useCallback, useState } from 'react'
  10. export type OLToastProps = {
  11. type: NotificationType
  12. className?: string
  13. title?: string
  14. content: string | ReactNode
  15. isDismissible?: boolean
  16. onDismiss?: () => void
  17. autoHide?: boolean
  18. delay?: number
  19. }
  20. export const OLToast = ({
  21. type = 'info',
  22. className = '',
  23. content,
  24. title,
  25. isDismissible,
  26. onDismiss,
  27. autoHide,
  28. delay,
  29. }: OLToastProps) => {
  30. const { t } = useTranslation()
  31. const [show, setShow] = useState(true)
  32. const toastClassName = classNames(
  33. 'notification',
  34. `notification-type-${type}`,
  35. className,
  36. 'toast-content'
  37. )
  38. const handleClose = useCallback(() => {
  39. setShow(false)
  40. }, [])
  41. const handleOnHidden = useCallback(() => {
  42. if (onDismiss) onDismiss()
  43. }, [onDismiss])
  44. const toastElement = (
  45. <div className={toastClassName}>
  46. <NotificationIcon notificationType={type} />
  47. <div className="notification-content-and-cta">
  48. <div className="notification-content">
  49. {title && (
  50. <p>
  51. <b>{title}</b>
  52. </p>
  53. )}
  54. {content}
  55. </div>
  56. </div>
  57. {isDismissible && (
  58. <div className="notification-close-btn">
  59. <button
  60. aria-label={t('close')}
  61. data-bs-dismiss="toast"
  62. onClick={handleClose}
  63. >
  64. <MaterialIcon type="close" />
  65. </button>
  66. </div>
  67. )}
  68. </div>
  69. )
  70. return (
  71. <BS5Toast
  72. onClose={handleClose}
  73. autohide={autoHide}
  74. onExited={handleOnHidden}
  75. delay={delay}
  76. show={show}
  77. >
  78. {toastElement}
  79. </BS5Toast>
  80. )
  81. }