Skip to content

Commit

Permalink
Adjust handlestep
Browse files Browse the repository at this point in the history
  • Loading branch information
yoonieaj committed Dec 14, 2024
1 parent 8d3bc39 commit 9497035
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions webstepper/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export default function App() {
const codeText = window.codeText;
const limit = window.svgArray.length;

const handleStep = (newStep: number) => {
setStep(Math.min(Math.max(newStep, 0), limit - 1));
const handleStep = (offset: number) => {
setStep((step) => Math.min(Math.max(step + offset, 0), limit - 1));
};

useEffect(() => {
const handleKeyDown = (event) => {
if (event.key == "ArrowLeft") {
handleStep(step - 1);
handleStep(-1);
}
if (event.key == "ArrowRight") {
handleStep(step + 1);
handleStep(1);
}
};

Expand All @@ -35,7 +35,7 @@ export default function App() {
return () => {
document.removeEventListener("keydown", handleKeyDown);
};
}, [step]);
}, []);

return (
<main className="container">
Expand All @@ -55,13 +55,13 @@ export default function App() {
<Box className="button-container">
<Button
disabled={step === 0}
onClick={() => handleStep(step - 1)}
onClick={() => handleStep(-1)}
>
Back
</Button>
<Button
disabled={step === limit - 1}
onClick={() => handleStep(step + 1)}
onClick={() => handleStep(1)}
>
Next
</Button>
Expand Down

0 comments on commit 9497035

Please sign in to comment.