Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.tsx 4.9KB

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