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

markdownFindContext.ts

19 lines785 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. `onReveal` must be stable.
10 */
11export function useFindRevealRef(onReveal: () => void) {
12 // The compiler skips hookless functions; a new ref callback each render would re-add the listener.
13 "use memo";
14 return (element: HTMLElement | null) => {
15 if (!element) return;
16 element.addEventListener("beforematch", onReveal);
17 return () => element.removeEventListener("beforematch", onReveal);
18 };