Init
This commit is contained in:
1 parent
dd596f16b9
commit
3c052a8b43
25 files changed
+2252
-74
No files matched your search
@@ -0,0 +1,102 @@
|
||||
"use client";
|
||||
|
||||
import { CSSProperties, ReactNode, useRef, useState } from "react";
|
||||
|
||||
interface ChargedButtonProps {
|
||||
children?: ReactNode;
|
||||
style?: CSSProperties;
|
||||
/** Time in milliseconds needed to fully charge by continuous hold. */
|
||||
holdDuration?: number;
|
||||
/** Runs exactly once, when the button is fully charged at 100%. */
|
||||
onAction?: () => void;
|
||||
}
|
||||
|
||||
export default function ChargedButton({
|
||||
children,
|
||||
style,
|
||||
holdDuration = 1000,
|
||||
onAction,
|
||||
}: ChargedButtonProps) {
|
||||
const [progress, setProgress] = useState(0);
|
||||
const [isCharging, setIsCharging] = useState(false);
|
||||
const [isDischarging, setIsDischarging] = useState(false);
|
||||
const rafRef = useRef(0);
|
||||
const startTimeRef = useRef(0);
|
||||
const stateRef = useRef({ charging: false, discharging: false });
|
||||
const dischargingRef = useRef(false);
|
||||
|
||||
const cancelRaf = () => {
|
||||
if (rafRef.current) {
|
||||
cancelAnimationFrame(rafRef.current);
|
||||
rafRef.current = 0;
|
||||
}
|
||||
};
|
||||
|
||||
const onPointerDown = () => {
|
||||
if (dischargingRef.current) return;
|
||||
cancelRaf();
|
||||
startTimeRef.current = performance.now();
|
||||
setIsCharging(true);
|
||||
dischargingRef.current = false;
|
||||
setIsDischarging(false);
|
||||
rafRef.current = requestAnimationFrame(updateProgress);
|
||||
};
|
||||
|
||||
const onPointerUp = () => {
|
||||
if (dischargingRef.current) return;
|
||||
cancelRaf();
|
||||
setIsCharging(false);
|
||||
setProgress(0);
|
||||
};
|
||||
|
||||
const updateProgress = () => {
|
||||
const elapsed = performance.now() - startTimeRef.current;
|
||||
const discharging = dischargingRef.current ?? false;
|
||||
|
||||
if (!discharging) {
|
||||
const fraction = Math.min(1, elapsed / holdDuration);
|
||||
setProgress(fraction);
|
||||
if (fraction >= 1) {
|
||||
setProgress(1);
|
||||
setIsCharging(false);
|
||||
dischargingRef.current = true;
|
||||
setIsDischarging(true);
|
||||
startTimeRef.current = performance.now();
|
||||
onAction?.();
|
||||
}
|
||||
} else {
|
||||
const fraction = Math.min(1, elapsed / (holdDuration * 0.1));
|
||||
setProgress(1.0 - fraction);
|
||||
if (fraction >= 1) {
|
||||
setProgress(0);
|
||||
cancelRaf();
|
||||
dischargingRef.current = false;
|
||||
setIsDischarging(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
rafRef.current = requestAnimationFrame(updateProgress);
|
||||
};
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className="charged-button"
|
||||
style={style}
|
||||
onPointerDown={onPointerDown}
|
||||
onTouchStart={onPointerDown}
|
||||
onPointerUp={onPointerUp}
|
||||
onTouchEnd={onPointerUp}
|
||||
onPointerCancel={onPointerUp}
|
||||
onTouchCancel={onPointerUp}
|
||||
>
|
||||
<span className="charged-button__label">{children}</span>
|
||||
<span
|
||||
className=" charged-button-fill"
|
||||
aria-hidden="true"
|
||||
style={{ width: `${progress * 100}%` }}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user