Open History to see the last commit
1import { createContext, useContext, useEffect, 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 * Whether panel animations are held off while `navigationKey`'s route paints. Never while they
13 * are off anyway: the flag is a context value, so each flip renders every reader again.
14 */
15export function usePanelNavigationSuppression(navigationKey: string, animated: boolean): boolean {
16 const [paintedNavigationKey, setPaintedNavigationKey] = useState<string | null>(null);
17 const suppressed = animated && paintedNavigationKey !== navigationKey;
18
19 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Wait two browser frames so the new route paints before panel animations are re-enabled; cancel the frames on change or unmount.
20 useEffect(() => {
21 if (!suppressed) return;
22 let releaseFrame = 0;
23 const paintFrame = window.requestAnimationFrame(() => {
24 releaseFrame = window.requestAnimationFrame(() => setPaintedNavigationKey(navigationKey));
25 });
26 return () => {
27 window.cancelAnimationFrame(paintFrame);
28 window.cancelAnimationFrame(releaseFrame);
29 };
30 }, [navigationKey, suppressed]);
31
32 return suppressed;
33}
34
35export function usePanelAnimationSettings(): {
36 active: boolean;
37 durationMs: PanelAnimationDurationMs;
38} {
39 const durationMs = useClientSettings((settings) => settings.panelAnimationDurationMs);
40 const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
41 const suppressed = useContext(PanelAnimationSuppressionContext);
42 return { active: durationMs > 0 && !prefersReducedMotion && !suppressed, durationMs };
43}
44
45export function usePanelPresence<T>(
46 open: boolean,
47 value: T | null,
48 animated: boolean,
49 scopeKey: string | null,
50 durationMs: PanelAnimationDurationMs,
51): { present: boolean; value: T | null } {
52 const [present, setPresent] = useState(open);
53 const [retained, setRetained] = useState<{ scopeKey: string | null; value: T | null } | null>(
54 open ? { scopeKey, value } : null,
55 );
56 if (open && (retained?.scopeKey !== scopeKey || retained.value !== value)) {
57 setRetained({ scopeKey, value });
58 }
59
60 if (open && !present) setPresent(true);
61 if (!open && !animated && present) setPresent(false);
62
63 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Keep the closing panel mounted for its CSS exit transition, then unmount it; the timer is cleared on reopen or unmount.
64 useEffect(() => {
65 if (open || !animated) return;
66 const timeout = window.setTimeout(() => setPresent(false), durationMs);
67 return () => window.clearTimeout(timeout);
68 }, [animated, durationMs, open]);
69
70 const retainedValue = retained?.scopeKey === scopeKey ? retained.value : null;
71 const visible = open || (animated && present && retained?.scopeKey === scopeKey);
72 return { present: visible, value: open ? value : visible ? retainedValue : null };
73}