Skip to content

Commit ff131da

Browse files
committed
added animation upon closing
1 parent a38d588 commit ff131da

2 files changed

Lines changed: 53 additions & 28 deletions

File tree

src/components/LevelComponent.tsx

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,57 @@
1-
import React, { useState, useEffect, useRef } from 'react'
1+
import React, { useState } from 'react'
22
import OutputComponent from './OutputComponent'
33
import InfoComponent from './InfoComponent'
44

5-
export default function LevelComponent() {
5+
export default function LevelComponent({ onClose }: { onClose: () => void }) {
6+
const [isClosing, setIsClosing] = useState(false);
67

7-
return (
8-
<div className="animate-[zoomIn_0.3s_ease-out_forwards] origin-center w-screen h-screen flex gap-10 p-4 font-mono overflow-hidden">
9-
<div className="flex gap-4 p-4 w-6/7 mx-auto">
8+
const handleClose = () => setIsClosing(true);
109

11-
<div className="flex-1 bg-[#2a2a2a] rounded-2xl p-3 flex flex-col gap-3">
12-
<InfoComponent />
13-
</div>
14-
<div className="flex-1 bg-[#2a2a2a] rounded-2xl p-3 flex flex-col gap-3">
15-
<OutputComponent />
16-
</div>
10+
const handleAnimationEnd = () => {
11+
if (isClosing) onClose();
12+
};
1713

18-
</div>
19-
14+
return (
15+
<>
2016
<style>{`
21-
@keyframes zoomIn {
17+
@keyframes expandDown {
2218
from { transform: scaleY(0); }
2319
to { transform: scaleY(1); }
2420
}
25-
.zoom-in {
26-
animation: zoomIn 0.3s ease-out forwards;
21+
@keyframes collapseUp {
22+
from { transform: scaleY(1); }
23+
to { transform: scaleY(0); }
24+
}
25+
.expand-down {
26+
animation: expandDown 0.2s ease-out forwards;
27+
transform-origin: center;
28+
}
29+
.collapse-up {
30+
animation: collapseUp 0.2s ease-in forwards;
2731
transform-origin: center;
2832
}
2933
`}</style>
30-
<div className="zoom-in">
31-
{/* your content */}
32-
</div>
33-
34+
<div
35+
className={`${isClosing ? 'collapse-up' : 'expand-down'} w-screen h-screen flex gap-10 p-4 font-mono overflow-hidden`}
36+
onAnimationEnd={handleAnimationEnd}
37+
>
38+
{/* X button */}
39+
<button
40+
onClick={handleClose}
41+
className="absolute top-9 right-38 text-white text-xl leading-none"
42+
>
43+
44+
</button>
3445

35-
</div>
36-
)
46+
<div className="flex gap-4 p-4 w-6/7 mx-auto">
47+
<div className="flex-1 bg-[#2a2a2a] rounded-2xl p-3 flex flex-col gap-3">
48+
<InfoComponent />
49+
</div>
50+
<div className="flex-1 bg-[#2a2a2a] rounded-2xl p-3 flex flex-col gap-3">
51+
<OutputComponent />
52+
</div>
53+
</div>
54+
</div>
55+
</>
56+
);
3757
}

src/pages/home/Home.tsx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,23 @@ export default function Home() {
88
const [isVisible, setIsVisible] = useState(false);
99
const [mountKey, setMountKey] = useState(0);
1010

11-
const handleToggle = () => {
12-
if (!isVisible) setMountKey(k => k + 1);
13-
setIsVisible(v => !v);
11+
const handleOpen = () => {
12+
setMountKey(k => k + 1);
13+
setIsVisible(true);
1414
};
1515

1616
return (
1717
<div>
18-
<button onClick={handleToggle}>
19-
{isVisible ? 'Hide' : 'Show'} Component
18+
<button className="ml-100 mt-80 absolute border-4 rounded-3xl p-5" onClick={handleOpen} disabled={isVisible}>
19+
Glorp
2020
</button>
2121

22-
{isVisible && <LevelComponent key={mountKey} />}
22+
{isVisible && (
23+
<LevelComponent
24+
key={mountKey}
25+
onClose={() => setIsVisible(false)}
26+
/>
27+
)}
2328
</div>
2429
);
2530
}

0 commit comments

Comments
 (0)