Some checks failed
No response / noResponse (push) Has been cancelled
CI / Continuous releases (push) Has been cancelled
CI / test-dev (macos-latest) (push) Has been cancelled
CI / test-dev (ubuntu-latest) (push) Has been cancelled
CI / test-dev (windows-latest) (push) Has been cancelled
Maintenance / main (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
CodeQL / Analyze (push) Has been cancelled
77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import * as React from 'react';
|
|
import Box from '@mui/material/Box';
|
|
import Fade from '@mui/material/Fade';
|
|
import Button from '@mui/material/Button';
|
|
import CircularProgress from '@mui/material/CircularProgress';
|
|
import Typography from '@mui/material/Typography';
|
|
|
|
export default function DelayingAppearance() {
|
|
const [loading, setLoading] = React.useState(false);
|
|
const [query, setQuery] = React.useState('idle');
|
|
const timerRef = React.useRef<ReturnType<typeof setTimeout>>(undefined);
|
|
|
|
React.useEffect(
|
|
() => () => {
|
|
clearTimeout(timerRef.current);
|
|
},
|
|
[],
|
|
);
|
|
|
|
const handleClickLoading = () => {
|
|
setLoading((prevLoading) => !prevLoading);
|
|
};
|
|
|
|
const handleClickQuery = () => {
|
|
if (timerRef.current) {
|
|
clearTimeout(timerRef.current);
|
|
}
|
|
|
|
if (query !== 'idle') {
|
|
setQuery('idle');
|
|
return;
|
|
}
|
|
|
|
setQuery('progress');
|
|
timerRef.current = setTimeout(() => {
|
|
setQuery('success');
|
|
}, 2000);
|
|
};
|
|
|
|
return (
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
|
|
<Box sx={{ height: 40 }}>
|
|
<Fade
|
|
in={loading}
|
|
style={{
|
|
transitionDelay: loading ? '800ms' : '0ms',
|
|
}}
|
|
unmountOnExit
|
|
>
|
|
<CircularProgress />
|
|
</Fade>
|
|
</Box>
|
|
<Button onClick={handleClickLoading} sx={{ m: 2 }}>
|
|
{loading ? 'Stop loading' : 'Loading'}
|
|
</Button>
|
|
<Box sx={{ height: 40 }}>
|
|
{query === 'success' ? (
|
|
<Typography>Success!</Typography>
|
|
) : (
|
|
<Fade
|
|
in={query === 'progress'}
|
|
style={{
|
|
transitionDelay: query === 'progress' ? '800ms' : '0ms',
|
|
}}
|
|
unmountOnExit
|
|
>
|
|
<CircularProgress />
|
|
</Fade>
|
|
)}
|
|
</Box>
|
|
<Button onClick={handleClickQuery} sx={{ m: 2 }}>
|
|
{query !== 'idle' ? 'Reset' : 'Simulate a load'}
|
|
</Button>
|
|
</Box>
|
|
);
|
|
}
|