Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2import { type PanelAnimationDurationMs } from "@t3tools/contracts/settings";
3
4import { useMediaQuery } from "./hooks/useMediaQuery";
5import { useClientSettings } from "./hooks/useSettings";
6
7export function observeResponsiveBreakpointFade(options: {
8 target: HTMLElement;
9 container: HTMLElement;
10 active: boolean;
11 durationMs: PanelAnimationDurationMs;
12 breakpoint: { value: number; unit: "px" | "rem" };
13}): () => void {
14 const { target, container, active, durationMs, breakpoint } = options;
15 if (!active || typeof ResizeObserver === "undefined") return () => {};
16
17 const rootFontSize = Number.parseFloat(getComputedStyle(document.documentElement).fontSize);
18 const breakpointPx =
19 breakpoint.unit === "px"
20 ? breakpoint.value
21 : breakpoint.value * (Number.isFinite(rootFontSize) ? rootFontSize : 16);
22 let expanded = container.getBoundingClientRect().width >= breakpointPx;
23 let animation: Animation | null = null;
24
25 const observer = new ResizeObserver(([entry]) => {
26 if (!entry) return;
27 const nextExpanded = entry.contentRect.width >= breakpointPx;
28 if (nextExpanded === expanded) return;
29 expanded = nextExpanded;
30 animation?.cancel();
31 animation = target.animate([{ opacity: 0 }, { opacity: 1 }], {
32 duration: Math.min(100, durationMs),
33 easing: "ease-out",
34 });
35 });
36
37 observer.observe(container);
38 return () => {
39 observer.disconnect();
40 animation?.cancel();
41 };
42}
43
44export function usePanelAnimationSettings(): {
45 active: boolean;
46 durationMs: PanelAnimationDurationMs;
47} {
48 const durationMs = useClientSettings((settings) => settings.panelAnimationDurationMs);
49 const prefersReducedMotion = useMediaQuery("(prefers-reduced-motion: reduce)");
50 return { active: durationMs > 0 && !prefersReducedMotion, durationMs };
51}
52
53/** Keeps closing panel content mounted until its opt-in transition ends. */
54export function usePanelPresence<T>(
55 open: boolean,
56 value: T | null,
57 animated: boolean,
58 scopeKey: string | null,
59 durationMs: PanelAnimationDurationMs,
60): { present: boolean; value: T | null } {
61 const [present, setPresent] = useState(open);
62 const retainedRef = useRef<{ scopeKey: string | null; value: T | null } | null>(
63 open ? { scopeKey, value } : null,
64 );
65
66 useEffect(() => {
67 if (open) retainedRef.current = { scopeKey, value };
68 }, [open, scopeKey, value]);
69
70 useEffect(() => {
71 if (open) {
72 setPresent(true);
73 return;
74 }
75 if (!animated) {
76 setPresent(false);
77 return;
78 }
79
80 const timeout = window.setTimeout(() => setPresent(false), durationMs);
81 return () => window.clearTimeout(timeout);
82 }, [animated, durationMs, open]);
83
84 const retainedValue =
85 retainedRef.current?.scopeKey === scopeKey ? retainedRef.current.value : null;
86 const visible = open || (animated && present && retainedRef.current?.scopeKey === scopeKey);
87 return { present: visible, value: open ? value : visible ? retainedValue : null };
88}