generated from egoist/ts-lib-starter
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: drag the progress volume bar (#22)
- Loading branch information
Showing
4 changed files
with
221 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { useCallback, useRef, useState } from "react"; | ||
import { ReactComponent as IconVolumeUp } from "../assets/volume-up.svg"; | ||
import { ReactComponent as IconVolumeDown } from "../assets/volume-down.svg"; | ||
import { ReactComponent as IconVolumeOff } from "../assets/volume-off.svg"; | ||
import { computePercentageOfY } from "../utils/computePercentage"; | ||
import clsx from "clsx"; | ||
|
||
type VolumeProps = { | ||
themeColor: string; | ||
volume: number; | ||
muted: boolean; | ||
onToggleMuted: () => void; | ||
onChangeVolume: (volume: number) => void; | ||
}; | ||
|
||
export function Volume({ | ||
themeColor, | ||
volume, | ||
muted, | ||
onToggleMuted, | ||
onChangeVolume, | ||
}: VolumeProps) { | ||
const volumeBarRef = useRef<HTMLDivElement>(null); | ||
const [isDragging, setDragging] = useState(false); // ensure related element in :hover | ||
|
||
const handleMouseDown = useCallback( | ||
(e: React.MouseEvent) => { | ||
onChangeVolume(computePercentageOfY(e, volumeBarRef)); | ||
setDragging(true); | ||
|
||
const handleMouseMove = (e: MouseEvent) => { | ||
onChangeVolume(computePercentageOfY(e, volumeBarRef)); | ||
}; | ||
|
||
const handleMouseUp = (e: MouseEvent) => { | ||
document.removeEventListener("mouseup", handleMouseUp); | ||
document.removeEventListener("mousemove", handleMouseMove); | ||
|
||
setDragging(false); | ||
onChangeVolume(computePercentageOfY(e, volumeBarRef)); | ||
}; | ||
|
||
document.addEventListener("mousemove", handleMouseMove); | ||
document.addEventListener("mouseup", handleMouseUp); | ||
}, | ||
[onChangeVolume] | ||
); | ||
|
||
return ( | ||
<div className="aplayer-volume-wrap"> | ||
<button | ||
className="aplayer-icon aplayer-icon-volume-down" | ||
onClick={() => onToggleMuted()} | ||
> | ||
{muted || !volume ? ( | ||
<IconVolumeOff /> | ||
) : volume >= 1 ? ( | ||
<IconVolumeUp /> | ||
) : ( | ||
<IconVolumeDown /> | ||
)} | ||
</button> | ||
<div | ||
className={clsx("aplayer-volume-bar-wrap", { | ||
"aplayer-volume-bar-wrap-active": isDragging, | ||
})} | ||
ref={volumeBarRef} | ||
onMouseDown={handleMouseDown} | ||
> | ||
<div className="aplayer-volume-bar"> | ||
<div | ||
className="aplayer-volume" | ||
style={{ | ||
backgroundColor: themeColor, | ||
height: muted ? 0 : `${volume * 100}%`, | ||
}} | ||
></div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,123 @@ | ||
import { expect, test } from "vitest"; | ||
import { computePercentage } from "./computePercentage"; | ||
import { computePercentage, computePercentageOfY } from "./computePercentage"; | ||
import { describe } from "vitest"; | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mouseup", {}), { current: null }) | ||
).toBe(0); | ||
}); | ||
describe("computePercentage", () => { | ||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mouseup", {}), { current: null }) | ||
).toBe(0); | ||
}); | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mousemove"), { current: null }) | ||
).toBe(0); | ||
}); | ||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mousemove"), { current: null }) | ||
).toBe(0); | ||
}); | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mousedown"), { | ||
current: null, | ||
}) | ||
).toBe(0); | ||
}); | ||
|
||
describe("Given an valid percentage,when the mouse moves on the X axis", () => { | ||
/* MOCK DOM */ | ||
test("Return valid percentage,when input two valid Event Objet", () => { | ||
const container = document.createElement("div"); | ||
container.style.width = "200px"; | ||
container.style.height = "2px"; | ||
const mouseEvent = new MouseEvent("mousedown", { | ||
clientX: 50, | ||
clientY: 50, | ||
}); | ||
|
||
/* hack ! no value in the node environment , so overwrite they */ | ||
container.clientWidth = 200; | ||
container.getBoundingClientRect = () => ({ | ||
x: 10, | ||
y: 10, | ||
width: 300, | ||
height: 2, | ||
top: 10, | ||
right: 300, | ||
bottom: 10, | ||
left: 10, | ||
toJSON: function () { | ||
return ""; | ||
}, | ||
}); | ||
|
||
test("Return 0 if progressBarRef.current is undefined", () => { | ||
expect( | ||
computePercentage(new MouseEvent("mousedown"), { | ||
current: null, | ||
}) | ||
).toBe(0); | ||
container.addEventListener("mousedown", function (e) { | ||
const val = computePercentage(e, { current: container }); | ||
expect(val).toBe(0.2); | ||
}); | ||
|
||
container.dispatchEvent(mouseEvent); | ||
}); | ||
}); | ||
}); | ||
|
||
/* MOCK DOM */ | ||
test("Return percentage when mousedown event", () => { | ||
const container = document.createElement("div"); | ||
container.style.width = "200px"; | ||
container.style.height = "2px"; | ||
const mouseEvent = new MouseEvent("mousedown", { | ||
clientX: 50, | ||
clientY: 50, | ||
}); | ||
|
||
/* hack ! no value in the node environment , so overwrite they */ | ||
container.clientWidth = 200; | ||
container.getBoundingClientRect = () => ({ | ||
x: 10, | ||
y: 10, | ||
width: 300, | ||
height: 2, | ||
top: 10, | ||
right: 300, | ||
bottom: 10, | ||
left: 10, | ||
toJSON: function () { | ||
return ""; | ||
}, | ||
}); | ||
|
||
container.addEventListener("mousedown", function (e) { | ||
const val = computePercentage(e, { current: container }); | ||
expect(val).toBe(0.2); | ||
}); | ||
|
||
container.dispatchEvent(mouseEvent); | ||
describe("computePercentageOfY", () => { | ||
test("Return 0 if volumeBarRef.current is undefined", () => { | ||
expect( | ||
computePercentageOfY(new MouseEvent("mouseup"), { | ||
current: null, | ||
}) | ||
).toBe(0); | ||
}); | ||
|
||
test("Return 0 if volumeBarRef.current is undefined", () => { | ||
expect( | ||
computePercentageOfY(new MouseEvent("mousemove"), { | ||
current: null, | ||
}) | ||
).toBe(0); | ||
}); | ||
|
||
test("Return 0 if volumeBarRef.current is undefined", () => { | ||
expect( | ||
computePercentageOfY(new MouseEvent("mousedown"), { | ||
current: null, | ||
}) | ||
).toBe(0); | ||
}); | ||
|
||
describe("Given an valid percentage,when the mouse moves on the Y axis", () => { | ||
/* MOCK DOM */ | ||
test("Return valid percentage,when input two valid Event Objet", () => { | ||
const container = document.createElement("div"); | ||
container.style.width = "10px"; | ||
container.style.height = "300px"; | ||
const mouseEvent = new MouseEvent("mousedown", { | ||
clientX: 50, | ||
clientY: 100, | ||
}); | ||
|
||
/* hack ! no value in the node environment , so overwrite they */ | ||
container.clientHeight = 300; | ||
container.getBoundingClientRect = () => ({ | ||
x: 10, | ||
y: 10, | ||
width: 50, | ||
height: 300, | ||
top: 10, | ||
right: 300, | ||
bottom: 10, | ||
left: 10, | ||
toJSON: function () { | ||
return ""; | ||
}, | ||
}); | ||
|
||
container.addEventListener("mousedown", function (e) { | ||
const val = computePercentageOfY(e, { current: container }); | ||
expect(val).toBe(0.7); | ||
}); | ||
|
||
container.dispatchEvent(mouseEvent); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60fd80e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
aplayer-react – ./
aplayer-react-leishenghao.vercel.app
aplayer-react-git-master-leishenghao.vercel.app
aplayer-react.vercel.app
aplayer-react.js.org