Skip to content

Commit a78acc6

Browse files
committed
feat(lab):css toast
1 parent 701981c commit a78acc6

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

src/data/labs/css-toast/page.tsx

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { useEffect, useState } from "react";
2+
import "./style.css";
3+
4+
interface Toast {
5+
id: number;
6+
title: string;
7+
content: string;
8+
}
9+
10+
const MAX_TOAST = 4;
11+
const INITIAL_TOASTS: Toast[] = Array.from({ length: 4 }, (_, i) => ({
12+
id: i,
13+
title: `toast - ${i}`,
14+
content: "here is the content"
15+
}));
16+
17+
18+
export default function CssToast() {
19+
const [toastList, setToastList] = useState<Toast[]>(INITIAL_TOASTS);
20+
const [index, setIndex] = useState(4);
21+
22+
const addToast = () => {
23+
setIndex(index => index + 1);
24+
const toast: Toast = {
25+
id: index,
26+
title: "toast - " + index,
27+
content: "here is the content"
28+
};
29+
setToastList(prev => [...prev, toast]);
30+
if (toastList.length > MAX_TOAST) {
31+
setToastList(prev => prev.slice(1));
32+
};
33+
};
34+
35+
return (
36+
<div className="css-toast-wrap">
37+
<ul className="toast-list">
38+
{
39+
toastList.map((toast, index) => (
40+
<Toast key={toast.id} index={toastList.length - index - 1} toast={toast} />
41+
))
42+
}
43+
</ul>
44+
<button onClick={addToast}>Add toast</button>
45+
</div>
46+
)
47+
}
48+
49+
const Toast = ({ index, toast }: { index: number, toast: Toast }) => {
50+
const [show, setShow] = useState(false);
51+
52+
const style = {
53+
["--index" as string]: index
54+
};
55+
56+
useEffect(() => {
57+
setShow(true);
58+
}, []);
59+
60+
return (
61+
<li style={style} className={`${show ? "show" : ""} ${index >= MAX_TOAST ? "hide" : ""}`}>
62+
<p>{toast.title}</p>
63+
<span>{toast.content}</span>
64+
</li>
65+
)
66+
}

src/data/labs/css-toast/style.css

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
.css-toast-wrap {
2+
--border-color: #d1d5db;
3+
--bg-color: #f3f4f6;
4+
--font-color: #000;
5+
width: 100%;
6+
height: 100%;
7+
min-height: 14rem;
8+
display: flex;
9+
flex-direction: column;
10+
justify-content: center;
11+
align-items: center;
12+
gap: 1rem;
13+
padding: 2rem 1rem;
14+
15+
button {
16+
background: var(--bg-color);
17+
border: 1px solid var(--border-color);
18+
border-radius: 0.5rem;
19+
padding: 0.5rem 1rem;
20+
cursor: pointer;
21+
color: var(--font-color);
22+
font-size: 0.875rem;
23+
border: 1px solid var(--border-color);
24+
transition: .3s ease transform;
25+
26+
&:active{
27+
transform: scale(0.98);
28+
}
29+
}
30+
31+
.toast-list {
32+
position: relative;
33+
display: flex;
34+
flex-direction: column;
35+
gap: 0.5rem;
36+
width: 80%;
37+
height: 100%;
38+
--gap: -80%;
39+
40+
&:hover {
41+
--gap: 0.5rem;
42+
43+
li.show {
44+
transform: translateY(calc(var(--index) * (100% + var(--gap)) * -1)) scale(1);
45+
}
46+
}
47+
48+
li {
49+
position: absolute;
50+
background: var(--bg-color);
51+
color: var(--font-color);
52+
width: 100%;
53+
padding: 0.75rem 1rem;
54+
bottom: 0;
55+
left: 0;
56+
gap: 1rem;
57+
border-radius: 0.5rem;
58+
border: 1px solid var(--border-color);
59+
box-shadow: 0 0 1rem rgba(0, 0, 0, 0.5);
60+
opacity: 0;
61+
transform: perspective(1000px) translateY(50%) scale(1.1) rotateX(45deg);
62+
transition: transform .5s ease, opacity .3s ease;
63+
will-change: transform;
64+
font-size: 0.75rem;
65+
66+
span {
67+
opacity: 0.5;
68+
}
69+
70+
&.show {
71+
opacity: 1;
72+
transform: translateY(calc(var(--index) * (100% + var(--gap)) * -1)) scale(calc(1 - var(--index) * 0.05));
73+
}
74+
75+
&.hide {
76+
opacity: 0;
77+
transform: scale(calc(1 - var(--index) * 0.05));
78+
transition: 0.3s ease-in;
79+
}
80+
}
81+
}
82+
}

src/data/labs/data.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@
4343
"TypeScript"
4444
]
4545
},
46+
"css-toast": {
47+
"name": "css-toast",
48+
"inDevelopment": false,
49+
"en_title": "CSS Toast 🚧",
50+
"origin_url": "",
51+
"title": "css toast 🚧",
52+
"tags": [
53+
"CSS"
54+
]
55+
},
4656
"dnd-stack": {
4757
"name": "dnd-stack",
4858
"inDevelopment": true,

0 commit comments

Comments
 (0)