124 lines
3.3 KiB
TypeScript
124 lines
3.3 KiB
TypeScript
"use client";
|
|
|
|
import { CSSProperties, ReactNode, useEffect, useRef, useState } from "react";
|
|
import styles from "@/css/ChargedButton.module.css";
|
|
import clsx from "clsx";
|
|
|
|
interface ChargedButtonProps {
|
|
children?: ReactNode;
|
|
style?: CSSProperties;
|
|
className?: string;
|
|
holdDuration?: number;
|
|
onAction?: () => void;
|
|
disabled?: boolean;
|
|
red?: boolean;
|
|
}
|
|
|
|
export default function ChargedButton({
|
|
children,
|
|
style,
|
|
className,
|
|
holdDuration = 500,
|
|
onAction,
|
|
disabled = false,
|
|
red = false,
|
|
}: ChargedButtonProps) {
|
|
const [progress, setProgress] = useState(0);
|
|
const [, setIsCharging] = useState(false);
|
|
const [, setIsDischarging] = useState(false);
|
|
const rafRef = useRef(0);
|
|
const startTimeRef = useRef(0);
|
|
const dischargingRef = useRef(false);
|
|
|
|
const cancelRaf = () => {
|
|
if (rafRef.current) {
|
|
cancelAnimationFrame(rafRef.current);
|
|
rafRef.current = 0;
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
return () => cancelRaf();
|
|
}, []);
|
|
|
|
const onPointerDown = () => {
|
|
if (disabled || dischargingRef.current) return;
|
|
cancelRaf();
|
|
startTimeRef.current = performance.now();
|
|
setIsCharging(true);
|
|
dischargingRef.current = false;
|
|
setIsDischarging(false);
|
|
rafRef.current = requestAnimationFrame(updateProgress);
|
|
};
|
|
|
|
const onPointerUp = () => {
|
|
if (disabled || 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 / Math.max(holdDuration * 0.05, 150));
|
|
setProgress(1.0 - fraction);
|
|
if (fraction >= 1) {
|
|
setProgress(0);
|
|
cancelRaf();
|
|
dischargingRef.current = false;
|
|
setIsDischarging(false);
|
|
return;
|
|
}
|
|
}
|
|
|
|
rafRef.current = requestAnimationFrame(updateProgress);
|
|
};
|
|
|
|
const clipPercent = (1 - progress) * 100;
|
|
|
|
return (
|
|
<button
|
|
type="button"
|
|
className={`${styles["charged-button"]} ${disabled ? styles["button-disabled"] : ""} ${className || ""}`.trim()}
|
|
style={style}
|
|
onPointerDown={onPointerDown}
|
|
onPointerUp={onPointerUp}
|
|
onPointerCancel={onPointerUp}
|
|
onContextMenu={(e) => {
|
|
e.preventDefault();
|
|
}}
|
|
>
|
|
<span
|
|
className={styles["charged-button-frame"]}
|
|
aria-hidden="true"
|
|
style={{ backgroundImage: red ? "url(/buttons/Button_rot_lang.webp)" : "url(/buttons/Button_blau_lang.webp)" }}
|
|
/>
|
|
|
|
<span
|
|
className={styles["charged-button-fill"]}
|
|
aria-hidden="true"
|
|
style={{
|
|
backgroundImage: red ? "url(/buttons/Button_rot_lang.webp)" : "url(/buttons/Button_blau_lang.webp)",
|
|
clipPath: `inset(0 ${clipPercent}% 0 0)`,
|
|
WebkitClipPath: `inset(0 ${clipPercent}% 0 0)`,
|
|
}}
|
|
/>
|
|
|
|
<span className={clsx(styles["charged-button-label"], "lancelot-regular")}>{children}</span>
|
|
</button>
|
|
);
|
|
} |