nomadro/src/modules/social/lib/useRevealMotion.ts
Eric 5271aabc98 Ship social selfie face-check, avatar UX by device, blog/admin, and notify polish.
Add InsightFace/OpenCV challenge upload, PC vs mobile capture branching, and fix avatar persistence into roster/profile pages.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 14:38:20 +08:00

42 lines
1.4 KiB
TypeScript

"use client";
import { useEffect } from "react";
/** Observe `.reveal` inside a root selector and mark `.visible` once. */
export function useRevealMotion(rootSelector: string, deps: unknown[] = []) {
useEffect(() => {
if (!rootSelector) return;
const root = document.querySelector(rootSelector);
if (!root) return;
const media = matchMedia("(prefers-reduced-motion: reduce)");
const observer = new IntersectionObserver(
(entries) =>
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add("visible");
observer.unobserve(entry.target);
}),
{ threshold: 0.08, rootMargin: "0px 0px -20px 0px" },
);
const scan = () => {
root
.querySelectorAll(".reveal:not(.visible)")
.forEach((el) => {
if (media.matches) el.classList.add("visible");
else observer.observe(el);
});
};
scan();
// Searches, remote catalogs and empty states mount after the initial render.
const mutations = new MutationObserver(scan);
mutations.observe(root, { childList: true, subtree: true });
media.addEventListener("change", scan);
return () => {
mutations.disconnect();
media.removeEventListener("change", scan);
observer.disconnect();
};
// eslint-disable-next-line react-hooks/exhaustive-deps -- caller owns deps
}, deps);
}