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

markdownFindContext.ts

18 lines759 Bytes

1import { createContext } from "react";
2​
3// While find is active, folded content stays mounted (hidden) so its text can be
4// matched. Nothing opens until the selected match lies inside it.
5export const MarkdownFindContext = createContext(false);
6​
7/**
8 * Find dispatches `beforematch` on hidden or clipped ancestors of the selected
9 * match, like the browser's own find-in-page. Callers create the ref during render
10 * from a stable `onReveal`, so the compiler caches it and React attaches it once.
11 */
12export function findRevealRef(onReveal: () => void) {
13 return (element: HTMLElement | null) => {
14 if (!element) return;
15 element.addEventListener("beforematch", onReveal);
16 return () => element.removeEventListener("beforematch", onReveal);
17 };