index.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use client'
  2. import type { ReactNode } from 'react'
  3. import React, { useEffect, useState } from 'react'
  4. import { createRoot } from 'react-dom/client'
  5. import {
  6. RiAlertFill,
  7. RiCheckboxCircleFill,
  8. RiCloseLine,
  9. RiErrorWarningFill,
  10. RiInformation2Fill,
  11. } from '@remixicon/react'
  12. import { createContext, useContext } from 'use-context-selector'
  13. import ActionButton from '@/app/components/base/action-button'
  14. import classNames from '@/utils/classnames'
  15. export type IToastProps = {
  16. type?: 'success' | 'error' | 'warning' | 'info'
  17. size?: 'md' | 'sm'
  18. duration?: number
  19. message: string
  20. children?: ReactNode
  21. onClose?: () => void
  22. className?: string
  23. customComponent?: ReactNode
  24. }
  25. type IToastContext = {
  26. notify: (props: IToastProps) => void
  27. close: () => void
  28. }
  29. export const ToastContext = createContext<IToastContext>({} as IToastContext)
  30. export const useToastContext = () => useContext(ToastContext)
  31. const Toast = ({
  32. type = 'info',
  33. size = 'md',
  34. message,
  35. children,
  36. className,
  37. customComponent,
  38. }: IToastProps) => {
  39. const { close } = useToastContext()
  40. // sometimes message is react node array. Not handle it.
  41. if (typeof message !== 'string')
  42. return null
  43. return <div className={classNames(
  44. className,
  45. 'fixed w-[360px] rounded-xl my-4 mx-8 flex-grow z-[9999] overflow-hidden',
  46. size === 'md' ? 'p-3' : 'p-2',
  47. 'border border-components-panel-border-subtle bg-components-panel-bg-blur shadow-sm',
  48. 'top-0',
  49. 'right-0',
  50. )}>
  51. <div className={`absolute inset-0 -z-10 opacity-40 ${
  52. (type === 'success' && 'bg-toast-success-bg')
  53. || (type === 'warning' && 'bg-toast-warning-bg')
  54. || (type === 'error' && 'bg-toast-error-bg')
  55. || (type === 'info' && 'bg-toast-info-bg')
  56. }`}
  57. />
  58. <div className={`flex ${size === 'md' ? 'gap-1' : 'gap-0.5'}`}>
  59. <div className={`flex items-center justify-center ${size === 'md' ? 'p-0.5' : 'p-1'}`}>
  60. {type === 'success' && <RiCheckboxCircleFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-success`} aria-hidden="true" />}
  61. {type === 'error' && <RiErrorWarningFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-destructive`} aria-hidden="true" />}
  62. {type === 'warning' && <RiAlertFill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-warning-secondary`} aria-hidden="true" />}
  63. {type === 'info' && <RiInformation2Fill className={`${size === 'md' ? 'h-5 w-5' : 'h-4 w-4'} text-text-accent`} aria-hidden="true" />}
  64. </div>
  65. <div className={`flex py-1 ${size === 'md' ? 'px-1' : 'px-0.5'} grow flex-col items-start gap-1`}>
  66. <div className='flex items-center gap-1'>
  67. <div className='system-sm-semibold text-text-primary'>{message}</div>
  68. {customComponent}
  69. </div>
  70. {children && <div className='system-xs-regular text-text-secondary'>
  71. {children}
  72. </div>
  73. }
  74. </div>
  75. {close
  76. && (<ActionButton className='z-[1000]' onClick={close}>
  77. <RiCloseLine className='h-4 w-4 shrink-0 text-text-tertiary' />
  78. </ActionButton>)
  79. }
  80. </div>
  81. </div>
  82. }
  83. export const ToastProvider = ({
  84. children,
  85. }: {
  86. children: ReactNode
  87. }) => {
  88. const placeholder: IToastProps = {
  89. type: 'info',
  90. message: 'Toast message',
  91. duration: 6000,
  92. }
  93. const [params, setParams] = React.useState<IToastProps>(placeholder)
  94. const defaultDuring = (params.type === 'success' || params.type === 'info') ? 3000 : 6000
  95. const [mounted, setMounted] = useState(false)
  96. useEffect(() => {
  97. if (mounted) {
  98. setTimeout(() => {
  99. setMounted(false)
  100. }, params.duration || defaultDuring)
  101. }
  102. }, [defaultDuring, mounted, params.duration])
  103. return <ToastContext.Provider value={{
  104. notify: (props) => {
  105. setMounted(true)
  106. setParams(props)
  107. },
  108. close: () => setMounted(false),
  109. }}>
  110. {mounted && <Toast {...params} />}
  111. {children}
  112. </ToastContext.Provider>
  113. }
  114. Toast.notify = ({
  115. type,
  116. size = 'md',
  117. message,
  118. duration,
  119. className,
  120. customComponent,
  121. onClose,
  122. }: Pick<IToastProps, 'type' | 'size' | 'message' | 'duration' | 'className' | 'customComponent' | 'onClose'>) => {
  123. const defaultDuring = (type === 'success' || type === 'info') ? 3000 : 6000
  124. if (typeof window === 'object') {
  125. const holder = document.createElement('div')
  126. const root = createRoot(holder)
  127. root.render(
  128. <ToastContext.Provider value={{
  129. notify: () => { },
  130. close: () => {
  131. if (holder) {
  132. root.unmount()
  133. holder.remove()
  134. }
  135. onClose?.()
  136. },
  137. }}>
  138. <Toast type={type} size={size} message={message} duration={duration} className={className} customComponent={customComponent} />
  139. </ToastContext.Provider>,
  140. )
  141. document.body.appendChild(holder)
  142. setTimeout(() => {
  143. if (holder) {
  144. root.unmount()
  145. holder.remove()
  146. }
  147. onClose?.()
  148. }, duration || defaultDuring)
  149. }
  150. }
  151. export default Toast