index.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 opacity-40 -z-10 ${(type === 'success' && 'bg-[linear-gradient(92deg,rgba(23,178,106,0.25)_0%,rgba(255,255,255,0.00)_100%)]')
  52. || (type === 'warning' && 'bg-[linear-gradient(92deg,rgba(247,144,9,0.25)_0%,rgba(255,255,255,0.00)_100%)]')
  53. || (type === 'error' && 'bg-[linear-gradient(92deg,rgba(240,68,56,0.25)_0%,rgba(255,255,255,0.00)_100%)]')
  54. || (type === 'info' && 'bg-[linear-gradient(92deg,rgba(11,165,236,0.25)_0%,rgba(255,255,255,0.00)_100%)]')
  55. }`}
  56. />
  57. <div className={`flex ${size === 'md' ? 'gap-1' : 'gap-0.5'}`}>
  58. <div className={`flex justify-center items-center ${size === 'md' ? 'p-0.5' : 'p-1'}`}>
  59. {type === 'success' && <RiCheckboxCircleFill className={`${size === 'md' ? 'w-5 h-5' : 'w-4 h-4'} text-text-success`} aria-hidden="true" />}
  60. {type === 'error' && <RiErrorWarningFill className={`${size === 'md' ? 'w-5 h-5' : 'w-4 h-4'} text-text-destructive`} aria-hidden="true" />}
  61. {type === 'warning' && <RiAlertFill className={`${size === 'md' ? 'w-5 h-5' : 'w-4 h-4'} text-text-warning-secondary`} aria-hidden="true" />}
  62. {type === 'info' && <RiInformation2Fill className={`${size === 'md' ? 'w-5 h-5' : 'w-4 h-4'} text-text-accent`} aria-hidden="true" />}
  63. </div>
  64. <div className={`flex py-1 ${size === 'md' ? 'px-1' : 'px-0.5'} flex-col items-start gap-1 flex-grow z-10`}>
  65. <div className='flex items-center gap-1'>
  66. <div className='text-text-primary system-sm-semibold'>{message}</div>
  67. {customComponent}
  68. </div>
  69. {children && <div className='text-text-secondary system-xs-regular'>
  70. {children}
  71. </div>
  72. }
  73. </div>
  74. <ActionButton onClick={close}>
  75. <RiCloseLine className='w-4 h-4 flex-shrink-0 text-text-tertiary' />
  76. </ActionButton>
  77. </div>
  78. </div>
  79. }
  80. export const ToastProvider = ({
  81. children,
  82. }: {
  83. children: ReactNode
  84. }) => {
  85. const placeholder: IToastProps = {
  86. type: 'info',
  87. message: 'Toast message',
  88. duration: 6000,
  89. }
  90. const [params, setParams] = React.useState<IToastProps>(placeholder)
  91. const defaultDuring = (params.type === 'success' || params.type === 'info') ? 3000 : 6000
  92. const [mounted, setMounted] = useState(false)
  93. useEffect(() => {
  94. if (mounted) {
  95. setTimeout(() => {
  96. setMounted(false)
  97. }, params.duration || defaultDuring)
  98. }
  99. }, [defaultDuring, mounted, params.duration])
  100. return <ToastContext.Provider value={{
  101. notify: (props) => {
  102. setMounted(true)
  103. setParams(props)
  104. },
  105. close: () => setMounted(false),
  106. }}>
  107. {mounted && <Toast {...params} />}
  108. {children}
  109. </ToastContext.Provider>
  110. }
  111. Toast.notify = ({
  112. type,
  113. size = 'md',
  114. message,
  115. duration,
  116. className,
  117. customComponent,
  118. }: Pick<IToastProps, 'type' | 'size' | 'message' | 'duration' | 'className' | 'customComponent'>) => {
  119. const defaultDuring = (type === 'success' || type === 'info') ? 3000 : 6000
  120. if (typeof window === 'object') {
  121. const holder = document.createElement('div')
  122. const root = createRoot(holder)
  123. root.render(
  124. <ToastContext.Provider value={{
  125. notify: () => {},
  126. close: () => {
  127. if (holder) {
  128. root.unmount()
  129. holder.remove()
  130. }
  131. },
  132. }}>
  133. <Toast type={type} size={size} message={message} duration={duration} className={className} customComponent={customComponent} />
  134. </ToastContext.Provider>,
  135. )
  136. document.body.appendChild(holder)
  137. setTimeout(() => {
  138. if (holder) {
  139. root.unmount()
  140. holder.remove()
  141. }
  142. }, duration || defaultDuring)
  143. }
  144. }
  145. export default Toast