Skip to content

Commit 010cc9c

Browse files
committed
feat: add duration animation to audio
1 parent 93ccd17 commit 010cc9c

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

pages/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "./style.css"
2+
import "./voice.css"
23
import { useCallback, useState } from "react"
34
import { voices } from "../constants/voices"
45
import Head from "next/head"
@@ -19,6 +20,7 @@ const sx = {
1920
margin: "6px 12px"
2021
},
2122
voice: {
23+
position: "relative",
2224
margin: "6px",
2325
flex: "1 1 auto",
2426
display: "inline-flex",
@@ -30,27 +32,37 @@ const sx = {
3032
backgroundColor: "#faa65f",
3133
borderLeft: "12px solid #FCDFA1",
3234
transition: "0.3s",
35+
overflow: "hidden",
3336
boxShadow: "0 3px 1px -2px rgba(0,0,0,.2), 0 2px 2px 0 rgba(0,0,0,.14), 0 1px 5px 0 rgba(0,0,0,.12)"
34-
},
35-
voiceActive: {
36-
backgroundColor: "hsla(27, 94%, 50%, 1)"
3737
}
3838
}
3939

4040
const Voice = ({ name }) => {
4141
const [el, setEl] = useState(null)
42+
const [duration, setDuration] = useState(0)
4243
const [active, setActive] = useState(false)
4344
const handleClick = useCallback(() => {
4445
if (el) {
46+
setDuration(el.duration)
4547
el.play()
4648
}
4749
}, [el])
4850

4951
return (
50-
<figure style={{ ...sx.voice, ...(active ? sx.voiceActive : {}) }} onClick={handleClick}>
52+
<figure className="voice-ui" onClick={handleClick}>
53+
<div
54+
className="progressbar"
55+
style={
56+
active
57+
? {
58+
animation: `decrement ${duration}s ease-in-out`
59+
}
60+
: undefined
61+
}
62+
></div>
5163
<figcaption>{name}</figcaption>
5264
<audio
53-
preload="none"
65+
preload="metadata"
5466
ref={node => {
5567
setEl(node)
5668
}}

pages/voice.css

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
.voice-ui{
2+
position: relative;
3+
display: inline-flex;
4+
flex: 1 1 auto;
5+
justify-content: space-between;
6+
padding: 12px 20px 12px 32px;
7+
margin: 6px;
8+
overflow: hidden;
9+
color: white;
10+
cursor: pointer;
11+
background-color: #faa65f;
12+
border-left: 12px solid #fcdfa1;
13+
border-radius: 10px;
14+
box-shadow: 0 3px 1px -2px rgba(0,0,0,.2), 0 2px 2px 0 rgba(0,0,0,.14), 0 1px 5px 0 rgba(0,0,0,.12);
15+
transition: 0.3s;
16+
}
17+
18+
@keyframes decrement{
19+
0%{
20+
width:100%;
21+
}
22+
100%{
23+
width: 0%;
24+
}
25+
}
26+
27+
.voice-ui .progressbar{
28+
position: absolute;
29+
top: 0px;
30+
left: 0px;
31+
height: 100%;
32+
background-color: black;
33+
mix-blend-mode: soft-light;
34+
}

0 commit comments

Comments
 (0)