import * as React from 'react'; import Button from '@mui/joy/Button'; import FormControl from '@mui/joy/FormControl'; import FormLabel from '@mui/joy/FormLabel'; import Input from '@mui/joy/Input'; import Stack from '@mui/joy/Stack'; import Snackbar from '@mui/joy/Snackbar'; export default function SnackbarHideDuration() { const [open, setOpen] = React.useState(false); const [duration, setDuration] = React.useState(); const [left, setLeft] = React.useState(); const timer = React.useRef(undefined); const countdown = () => { timer.current = setInterval(() => { setLeft((prev) => (prev === undefined ? prev : Math.max(0, prev - 100))); }, 100); }; React.useEffect(() => { if (open && duration !== undefined && duration > 0) { setLeft(duration); countdown(); } else { clearInterval(timer.current); } }, [open, duration]); const handlePause = () => { clearInterval(timer.current); }; const handleResume = () => { countdown(); }; return (
Auto Hide Duration (ms) { setDuration(event.target.valueAsNumber || undefined); }} /> setLeft(undefined)} open={open} onClose={() => { setOpen(false); }} > This snackbar will{' '} {left !== undefined ? `disappear in ${left}ms` : `not disappear until you click away`} .
); }