|
| 1 | +import { Pause, PlayArrow, Stop } from "@mui/icons-material"; |
| 2 | +import { Box, FormControlLabel, IconButton, Stack, Switch } from "@mui/material"; |
| 3 | +import * as React from "react"; |
| 4 | +import Lottie from "react-lottie"; |
| 5 | + |
| 6 | +type PlayState = "playing" | "paused" | "stopped"; |
| 7 | + |
| 8 | +type LottiePlayerStateType = { |
| 9 | + loop: boolean; |
| 10 | + isStopped: boolean; |
| 11 | + isPaused: boolean; |
| 12 | +}; |
| 13 | + |
| 14 | +export const LottieDebugPanel: React.FC<{ animationData: unknown }> = ({ animationData }) => { |
| 15 | + const [playerState, setPlayerState] = React.useState<LottiePlayerStateType>({ |
| 16 | + loop: true, |
| 17 | + isStopped: false, |
| 18 | + isPaused: false, |
| 19 | + }); |
| 20 | + |
| 21 | + const toggleLoop = () => setPlayerState((ps) => ({ ...ps, loop: !ps.loop })); |
| 22 | + const setPlayState = (playState: PlayState) => { |
| 23 | + if (playState === "playing") setPlayerState((ps) => ({ ...ps, isStopped: false, isPaused: false })); |
| 24 | + if (playState === "paused") setPlayerState((ps) => ({ ...ps, isStopped: false, isPaused: true })); |
| 25 | + if (playState === "stopped") setPlayerState((ps) => ({ ...ps, isStopped: true, isPaused: true })); |
| 26 | + }; |
| 27 | + |
| 28 | + const stop = () => setPlayState("stopped"); |
| 29 | + const togglePause = () => setPlayState(playerState.isPaused ? "playing" : "paused"); |
| 30 | + |
| 31 | + return ( |
| 32 | + <Stack direction="column"> |
| 33 | + <Box> |
| 34 | + <Lottie |
| 35 | + isStopped={playerState.isStopped} |
| 36 | + isPaused={playerState.isPaused} |
| 37 | + options={{ |
| 38 | + animationData, |
| 39 | + loop: playerState.loop, |
| 40 | + autoplay: true, |
| 41 | + rendererSettings: { preserveAspectRatio: "xMidYMid slice" }, |
| 42 | + }} |
| 43 | + /> |
| 44 | + </Box> |
| 45 | + <Stack direction="row" spacing={2}> |
| 46 | + <IconButton onClick={togglePause} children={playerState.isPaused ? <PlayArrow /> : <Pause />} /> |
| 47 | + <IconButton onClick={stop} children={<Stop />} /> |
| 48 | + <FormControlLabel control={<Switch checked={playerState.loop} onChange={toggleLoop} />} label="반복 재생" /> |
| 49 | + </Stack> |
| 50 | + </Stack> |
| 51 | + ); |
| 52 | +}; |
0 commit comments