Port of helium-prism's GradientShimmer to React on top of ogl, with intro and emphasize handles and reduced-motion support. Adds the ogl dependency and the shimmer styles. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
276 lines
7 KiB
TypeScript
276 lines
7 KiB
TypeScript
"use client";
|
|
|
|
// React port of GradientShimmer.svelte from helium-prism (https://github.com/imputnet/helium-prism),
|
|
// licensed under GPL-3.0. This file is part of the FRG web app and is licensed under GPL-3.0 (see LICENSE).
|
|
|
|
import { useEffect, useImperativeHandle, useRef, useState, type Ref } from "react";
|
|
import * as Shimmer from "@/lib/gradient-shimmer";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
export type GradientShimmerHandle = {
|
|
intro: () => void;
|
|
emphasize: () => void;
|
|
};
|
|
|
|
type GradientShimmerProps = {
|
|
intro?: boolean;
|
|
background?: boolean;
|
|
className?: string;
|
|
ref?: Ref<GradientShimmerHandle>;
|
|
};
|
|
|
|
export function GradientShimmer({
|
|
intro: shouldIntro,
|
|
background,
|
|
className,
|
|
ref,
|
|
}: GradientShimmerProps) {
|
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
const controllerRef = useRef<Shimmer.GradientShimmerControls | null>(null);
|
|
const [hasStaticFallback, setHasStaticFallback] = useState(false);
|
|
const [visible, setVisible] = useState(false);
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
intro: () => controllerRef.current?.intro(),
|
|
emphasize: () => controllerRef.current?.emphasize(),
|
|
}), []);
|
|
|
|
useEffect(() => {
|
|
const canvas = canvasRef.current;
|
|
|
|
if (!canvas) {
|
|
return;
|
|
}
|
|
|
|
const renderer = Shimmer.createGradientShimmerRenderer(canvas);
|
|
|
|
if (!renderer) {
|
|
// One-off sync from a failed WebGL probe; there is no later render to wait for.
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect
|
|
setHasStaticFallback(true);
|
|
setVisible(true);
|
|
return;
|
|
}
|
|
|
|
const colorSchemeQuery = window.matchMedia(
|
|
"(prefers-color-scheme: dark)",
|
|
);
|
|
const reducedMotionQuery = window.matchMedia(
|
|
"(prefers-reduced-motion: reduce)",
|
|
);
|
|
|
|
let animationFrame: number | null = null;
|
|
let resizeFrame: number | null = null;
|
|
let introAnimation: Shimmer.IntroAnimation | null = null;
|
|
let speedUpAnimation: Shimmer.WaveSpeedUpAnimation | null = null;
|
|
let wavePhase = Shimmer.getRandomWavePhase();
|
|
let secondaryWavePhase = -wavePhase * 0.7;
|
|
let lastFrameTime: number | null = null;
|
|
let onScreen = true;
|
|
|
|
const cancelResizeFrame = () => {
|
|
if (resizeFrame === null) {
|
|
return;
|
|
}
|
|
|
|
cancelAnimationFrame(resizeFrame);
|
|
resizeFrame = null;
|
|
};
|
|
|
|
const cancelAnimation = () => {
|
|
if (animationFrame === null) {
|
|
return;
|
|
}
|
|
|
|
cancelAnimationFrame(animationFrame);
|
|
animationFrame = null;
|
|
};
|
|
|
|
const drawFrame = (time: number) => {
|
|
const reducedMotion = reducedMotionQuery.matches;
|
|
const deltaSeconds = reducedMotion || lastFrameTime === null
|
|
? 0
|
|
: Math.min((time - lastFrameTime) / 1000, 0.064);
|
|
const speedUpState = Shimmer.updateSpeedUpAnimation(
|
|
time,
|
|
speedUpAnimation,
|
|
);
|
|
const activeIntroAnimation = reducedMotion ? null : introAnimation;
|
|
|
|
lastFrameTime = reducedMotion ? null : time;
|
|
speedUpAnimation = reducedMotion ? null : speedUpState.animation;
|
|
|
|
if (!reducedMotion) {
|
|
wavePhase += Shimmer.IDLE_WAVE.speed
|
|
* speedUpState.multiplier
|
|
* deltaSeconds;
|
|
secondaryWavePhase += Shimmer.IDLE_WAVE.secondarySpeed
|
|
* speedUpState.multiplier
|
|
* deltaSeconds;
|
|
}
|
|
|
|
renderer.render({
|
|
time,
|
|
wavePhase,
|
|
secondaryWavePhase,
|
|
introStartedAt: activeIntroAnimation?.startedAt ?? null,
|
|
shineProgress: reducedMotion ? 0 : speedUpState.shineProgress,
|
|
});
|
|
|
|
if (
|
|
Shimmer.isIntroComplete(
|
|
time,
|
|
introAnimation,
|
|
renderer.getStripeCount(),
|
|
)
|
|
) {
|
|
introAnimation = null;
|
|
}
|
|
};
|
|
|
|
const startIntro = () => {
|
|
if (reducedMotionQuery.matches) {
|
|
return;
|
|
}
|
|
|
|
introAnimation = { startedAt: performance.now() };
|
|
};
|
|
|
|
const startWaveSpeedUp = () => {
|
|
if (reducedMotionQuery.matches) {
|
|
return;
|
|
}
|
|
|
|
const time = performance.now();
|
|
|
|
speedUpAnimation = Shimmer.triggerSpeedUpAnimation(
|
|
time,
|
|
speedUpAnimation,
|
|
);
|
|
};
|
|
|
|
const controller: Shimmer.GradientShimmerControls = {
|
|
intro: startIntro,
|
|
emphasize: startWaveSpeedUp,
|
|
};
|
|
|
|
const applyResize = () => {
|
|
renderer.resize();
|
|
renderer.readColors();
|
|
};
|
|
|
|
const scheduleResize = () => {
|
|
cancelResizeFrame();
|
|
resizeFrame = requestAnimationFrame((time) => {
|
|
applyResize();
|
|
drawFrame(time);
|
|
});
|
|
};
|
|
|
|
const draw = (time: number) => {
|
|
animationFrame = null;
|
|
drawFrame(time);
|
|
requestAnimation();
|
|
};
|
|
|
|
const requestAnimation = () => {
|
|
if (animationFrame !== null || reducedMotionQuery.matches ||
|
|
!onScreen) {
|
|
return;
|
|
}
|
|
|
|
animationFrame = requestAnimationFrame(draw);
|
|
};
|
|
|
|
const resumeAnimation = () => {
|
|
lastFrameTime = null;
|
|
requestAnimation();
|
|
};
|
|
|
|
applyResize();
|
|
|
|
if (shouldIntro !== false) {
|
|
startIntro();
|
|
}
|
|
|
|
// Delay the reveal until after the first WebGL frame has been painted.
|
|
drawFrame(performance.now());
|
|
requestAnimationFrame(() => {
|
|
setVisible(true);
|
|
});
|
|
controllerRef.current = controller;
|
|
|
|
const resizeObserver = new ResizeObserver(scheduleResize);
|
|
resizeObserver.observe(canvas);
|
|
|
|
const scheduleColorRead = () => {
|
|
cancelResizeFrame();
|
|
resizeFrame = requestAnimationFrame((time) => {
|
|
renderer.readColors();
|
|
drawFrame(time);
|
|
});
|
|
};
|
|
|
|
const intersectionObserver = new IntersectionObserver(([entry]) => {
|
|
onScreen = entry?.isIntersecting ?? true;
|
|
|
|
if (!onScreen) {
|
|
cancelAnimation();
|
|
return;
|
|
}
|
|
|
|
resumeAnimation();
|
|
});
|
|
intersectionObserver.observe(canvas);
|
|
|
|
const themeObserver = new MutationObserver(scheduleColorRead);
|
|
themeObserver.observe(document.documentElement, { attributes: true });
|
|
|
|
const updateMotionPreference = () => {
|
|
if (reducedMotionQuery.matches) {
|
|
introAnimation = null;
|
|
speedUpAnimation = null;
|
|
cancelAnimation();
|
|
drawFrame(performance.now());
|
|
return;
|
|
}
|
|
|
|
resumeAnimation();
|
|
};
|
|
|
|
colorSchemeQuery.addEventListener("change", scheduleColorRead);
|
|
reducedMotionQuery.addEventListener("change", updateMotionPreference);
|
|
|
|
updateMotionPreference();
|
|
|
|
return () => {
|
|
if (controllerRef.current === controller) {
|
|
controllerRef.current = null;
|
|
}
|
|
|
|
setVisible(false);
|
|
cancelAnimation();
|
|
cancelResizeFrame();
|
|
resizeObserver.disconnect();
|
|
intersectionObserver.disconnect();
|
|
themeObserver.disconnect();
|
|
colorSchemeQuery.removeEventListener("change", scheduleColorRead);
|
|
reducedMotionQuery.removeEventListener("change", updateMotionPreference);
|
|
renderer.dispose();
|
|
};
|
|
}, [shouldIntro]);
|
|
|
|
return (
|
|
<canvas
|
|
ref={canvasRef}
|
|
aria-hidden="true"
|
|
className={cn(
|
|
"gradient-shimmer",
|
|
background && "background",
|
|
hasStaticFallback && "fallback",
|
|
visible && "visible",
|
|
className,
|
|
)}
|
|
/>
|
|
);
|
|
}
|