Skip to content

Commit

Permalink
Minor improvements on Toaster
Browse files Browse the repository at this point in the history
  • Loading branch information
vczb committed Nov 11, 2023
1 parent a49db42 commit 6e0dca7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/packages/Toaster/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,41 @@ export type ToasterProps = {
duration?: number | null
closable?: boolean
initialVisible?: boolean
showProgress?: boolean
} & Omit<AlertProps, 'header'> // Omit the 'header' prop from AlertProps

const Toaster = ({
duration,
closable = true,
initialVisible = true,
showProgress = true,
severity = 'success',
onClose,
...props
}: ToasterProps) => {
const [isVisible, setIsVisible] = useState(initialVisible)

const handleClose = useCallback(() => {
setIsVisible(false)
onClose && onClose()
}, [onClose])

useEffect(() => {
if (typeof duration === 'number') {
const timer = setTimeout(() => {
setIsVisible(false)
handleClose()
}, duration)

return () => {
clearTimeout(timer)
}
}
}, [duration])
}, [duration, handleClose])

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const theme = useContext(ThemeContext)

const handleClose = useCallback(() => {
setIsVisible(false)
onClose && onClose()
}, [onClose])

if (!isVisible) {
return null
}
Expand All @@ -55,7 +57,9 @@ const Toaster = ({
severity={severity}
onClose={() => handleClose()}
/>
<ProgressBar indeterminate={true} color={theme.colors.base[severity]} />
{showProgress && (
<ProgressBar indeterminate={true} color={theme.colors.base[severity]} />
)}
</S.Toaster>
)
}
Expand Down
3 changes: 2 additions & 1 deletion src/packages/Toaster/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default {
children: 'This is a toaster message',
severity: 'info',
showIcon: true,
closable: true
closable: true,
onClose: () => console.log('closed')
}
} as Meta

Expand Down

0 comments on commit 6e0dca7

Please sign in to comment.