Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

panelAnimations.ts

116 lines4.0 KB

1import { createContext, useContext, useEffect, useRef, useState } from "react";
2import { type PanelAnimationDurationMs } from "@t3tools/contracts/settings";
3​
4import { useMediaQuery } from "./hooks/useMediaQuery";
5import { useClientSettings } from "./hooks/useSettings";
6​
7const PanelAnimationSuppressionContext = createContext(false);
8​
9export const PanelAnimationSuppressionProvider = PanelAnimationSuppressionContext.Provider;
10​
11/**
12 * Suppresses panel motion for the first painted frame of an initial route or navigation.
13 * State restored by a route must be visible immediately; later user actions can animate.
14 */
15export function usePanelNavigationSuppression(navigationKey: string): boolean {
16 const [paintedNavigationKey, setPaintedNavigationKey] = useState<string | null>(null);
17 const suppressed = paintedNavigationKey !== navigationKey;
18​
19 useEffect(() => {
20 if (!suppressed) return;
21 let releaseFrame = 0;
22 const paintFrame = window.requestAnimationFrame(() => {
23 releaseFrame = window.requestAnimationFrame(() => setPaintedNavigationKey(navigationKey));
24 });
25 return () => {
26 window.cancelAnimationFrame(paintFrame);
27 window.cancelAnimationFrame(releaseFrame);
28 };
29 }, [navigationKey, suppressed]);
30​
31 return suppressed;
33​
34export function observeResponsiveBreakpointFade(options: {
35 target: HTMLElement;
36 container: HTMLElement;
37 active: boolean;
38 durationMs: PanelAnimationDurationMs;
39 breakpoint: { value: number; unit: "px" | "rem" };
40}): () => void {
41 const { target, container, active, durationMs, breakpoint } = options;
42 if (!active || typeof ResizeObserver === "undefined") return () => {};
43​
44 const rootFontSize = Number.parseFloat(getComputedStyle(document.documentElement).fontSize);
45 const breakpointPx =
46 breakpoint.unit === "px"
47 ? breakpoint.value
48 : breakpoint.value * (Number.isFinite(rootFontSize) ? rootFontSize : 16);
49 let expanded = container.getBoundingClientRect().width >= breakpointPx;
50 let animation: Animation | null = null;
51​
52 const observer = new ResizeObserver(([entry]) => {
53 if (!entry) return;
54 const nextExpanded = entry.contentRect.width >= breakpointPx;
55 if (nextExpanded === expanded) return;
56 expanded = nextExpanded;
57 animation?.cancel();
58 animation = target.animate([{ opacity: 0 }, { opacity: 1 }], {
59 duration: Math.min(100, durationMs),
60 easing: "ease-out",
61 });
62 });
63​
64 observer.observe(container);
65 return () => {
66 observer.disconnect();
67 animation?.cancel();
68 };
70​
71export function usePanelAnimationSettings(): {
72 active: boolean;
73 durationMs: PanelAnimationDurationMs;
74} {
75 const durationMs = useClientSettings((settings) => settings.panelAnimationDurationMs);
76 const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
77 const suppressed = useContext(PanelAnimationSuppressionContext);
78 return { active: durationMs > 0 && !prefersReducedMotion && !suppressed, durationMs };
80​
81/** Keeps closing panel content mounted until its opt-in transition ends. */
82export function usePanelPresence<T>(
83 open: boolean,
84 value: T | null,
85 animated: boolean,
86 scopeKey: string | null,
87 durationMs: PanelAnimationDurationMs,
88): { present: boolean; value: T | null } {
89 const [present, setPresent] = useState(open);
90 const retainedRef = useRef<{ scopeKey: string | null; value: T | null } | null>(
91 open ? { scopeKey, value } : null,
92 );
93​
94 useEffect(() => {
95 if (open) retainedRef.current = { scopeKey, value };
96 }, [open, scopeKey, value]);
97​
98 useEffect(() => {
99 if (open) {
100 setPresent(true);
101 return;
102 }
103 if (!animated) {
104 setPresent(false);
105 return;
106 }
107​
108 const timeout = window.setTimeout(() => setPresent(false), durationMs);
109 return () => window.clearTimeout(timeout);
110 }, [animated, durationMs, open]);
111​
112 const retainedValue =
113 retainedRef.current?.scopeKey === scopeKey ? retainedRef.current.value : null;
114 const visible = open || (animated && present && retainedRef.current?.scopeKey === scopeKey);
115 return { present: visible, value: open ? value : visible ? retainedValue : null };