import { useContext, useMemo } from 'react' import StdinContext from '../components/StdinContext.js' import type { DOMElement } from '../dom.js' import instances from '../instances.js' import type { MatchPosition } from '../render-to-screen.js' /** * Set the search highlight query on the Ink instance. */ export function useSearchHighlight(): { setQuery: (query: string) => void scanElement: (el: DOMElement) => MatchPosition[] setPositions: ( state: { positions: MatchPosition[] rowOffset: number currentIdx: number } | null, ) => void } { useContext(StdinContext) // anchor to App subtree for hook rules const ink = instances.get(process.stdout) return useMemo(() => { if (!ink) { return { setQuery: () => {}, scanElement: () => [], setPositions: () => {}, } } return { setQuery: (query: string) => ink.setSearchHighlight(query), scanElement: (el: DOMElement) => ink.scanElementSubtree(el), setPositions: (state: Parameters[0]) => ink.setSearchPositions(state), } }, [ink]) }