1import { createContext, useCallback } 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. `onReveal` must be stable.
10 */
11export function useFindRevealRef(onReveal: () => void) {
12 return useCallback(
13 (element: HTMLElement | null) => {
14 if (!element) return;
15 element.addEventListener("beforematch", onReveal);
16 return () => element.removeEventListener("beforematch", onReveal);
17 },
18 [onReveal],
19 );
20}