index.tsx 4.6 KB

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