summaryrefslogtreecommitdiff
path: root/apps/web/src/hooks
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2025-08-18 10:54:28 +0200
committerMaze Winther <mazewinther@gmail.com>2025-08-18 10:54:28 +0200
commit756a7c7caaeada4fff9a51f2ec4fe2968ac3b01e (patch)
tree1194f58254f2450395df18fb8d03469cd3ac3cd9 /apps/web/src/hooks
parent13dff382c452be96e1adb05d26bc30fa6de38298 (diff)
downloadopencut-756a7c7caaeada4fff9a51f2ec4fe2968ac3b01e.tar.gz
feat: implement reveal in media feature
Diffstat (limited to 'apps/web/src/hooks')
-rw-r--r--apps/web/src/hooks/use-highlight-scroll.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/apps/web/src/hooks/use-highlight-scroll.ts b/apps/web/src/hooks/use-highlight-scroll.ts
new file mode 100644
index 00000000..241bffb2
--- /dev/null
+++ b/apps/web/src/hooks/use-highlight-scroll.ts
@@ -0,0 +1,36 @@
+import { useEffect, useState, useRef } from "react";
+
+export function useHighlightScroll(
+ highlightId: string | null,
+ onClearHighlight: () => void,
+ highlightDuration = 1000
+) {
+ const [highlightedId, setHighlightedId] = useState<string | null>(null);
+ const elementRefs = useRef<Map<string, HTMLElement>>(new Map());
+
+ const registerElement = (id: string, element: HTMLElement | null) => {
+ if (element) {
+ elementRefs.current.set(id, element);
+ } else {
+ elementRefs.current.delete(id);
+ }
+ };
+
+ useEffect(() => {
+ if (!highlightId) return;
+
+ setHighlightedId(highlightId);
+
+ const target = elementRefs.current.get(highlightId);
+ target?.scrollIntoView({ block: "center" });
+
+ const timeout = setTimeout(() => {
+ setHighlightedId(null);
+ onClearHighlight();
+ }, highlightDuration);
+
+ return () => clearTimeout(timeout);
+ }, [highlightId, onClearHighlight, highlightDuration]);
+
+ return { highlightedId, registerElement };
+}