You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 5.1KB

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