before ai
This commit is contained in:
1 parent
85d0d0be57
commit
be50463157
11 files changed
+205
-76
No files matched your search
@@ -1,28 +1,32 @@
|
||||
"use client";
|
||||
|
||||
import { CSSProperties, ReactNode, useRef, useState } from "react";
|
||||
import styles from "@/css/ChargedButton.module.css";
|
||||
|
||||
interface ChargedButtonProps {
|
||||
children?: ReactNode;
|
||||
style?: CSSProperties;
|
||||
className?: string;
|
||||
/** 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;
|
||||
disabled?: boolean;
|
||||
}
|
||||
|
||||
export default function ChargedButton({
|
||||
children,
|
||||
style,
|
||||
className,
|
||||
holdDuration = 1000,
|
||||
onAction,
|
||||
disabled = false,
|
||||
}: 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 = () => {
|
||||
@@ -33,7 +37,7 @@ export default function ChargedButton({
|
||||
};
|
||||
|
||||
const onPointerDown = () => {
|
||||
if (dischargingRef.current) return;
|
||||
if (disabled || dischargingRef.current) return;
|
||||
cancelRaf();
|
||||
startTimeRef.current = performance.now();
|
||||
setIsCharging(true);
|
||||
@@ -43,7 +47,7 @@ export default function ChargedButton({
|
||||
};
|
||||
|
||||
const onPointerUp = () => {
|
||||
if (dischargingRef.current) return;
|
||||
if (disabled || dischargingRef.current) return;
|
||||
cancelRaf();
|
||||
setIsCharging(false);
|
||||
setProgress(0);
|
||||
@@ -65,7 +69,7 @@ export default function ChargedButton({
|
||||
onAction?.();
|
||||
}
|
||||
} else {
|
||||
const fraction = Math.min(1, elapsed / (holdDuration * 0.1));
|
||||
const fraction = Math.min(1, elapsed / Math.max(holdDuration * 0.05, 150));
|
||||
setProgress(1.0 - fraction);
|
||||
if (fraction >= 1) {
|
||||
setProgress(0);
|
||||
@@ -82,7 +86,7 @@ export default function ChargedButton({
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className="charged-button"
|
||||
className={styles["charged-button"] + ((disabled) ? " " + styles["button-disabled"] : "") + (className ? " " + className : "")}
|
||||
style={style}
|
||||
onPointerDown={onPointerDown}
|
||||
onTouchStart={onPointerDown}
|
||||
@@ -91,12 +95,14 @@ export default function ChargedButton({
|
||||
onPointerCancel={onPointerUp}
|
||||
onTouchCancel={onPointerUp}
|
||||
>
|
||||
<span className="charged-button__label">{children}</span>
|
||||
<span className={styles["charged-button-label"]}>{children}</span>
|
||||
<span
|
||||
className=" charged-button-fill"
|
||||
className={styles["charged-button-fill"]}
|
||||
aria-hidden="true"
|
||||
style={{ width: `${progress * 100}%` }}
|
||||
style={{
|
||||
clipPath: `inset(0 ${100 - progress * 100}% 0 0)`,
|
||||
}}
|
||||
/>
|
||||
</button>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user