Todae

Todae

useHighlight

Marks text matches, such as search results, with the browser's CSS Custom Highlight API. The hook finds each match as a Range and adds it to a Highlight in CSS.highlights; your CSS paints it with ::highlight(name). No <mark> elements are added, so the DOM, the selection and focus stay as they are.

On this page

Put ref on the element to search and pass query. The hook searches again when text or elements under ref change. Matching ignores letter case unless caseSensitive is set, and the query is plain text, not a pattern. A list matches any of its terms.

const found = useHighlight({ name: 'search', query });

<article ref={found.ref}>{children}</article>
<output>{found.matches.length} results</output>
::highlight(search) {
  background-color: var(--todae-color-status-warning-bg);
  color: var(--todae-color-fg-default);
}

::highlight(search-current) {
  background-color: var(--todae-color-status-warning-border);
  color: var(--todae-color-fg-default);
}
import { Button, Field, Label, Stack, useHighlight } from '@tounsoo/todae';
import { useId, useState } from 'react';

export default function HighlightSearch() {
  const inputId = useId();
  const [query, setQuery] = useState('token');
  const [index, setIndex] = useState(0);
  const found = useHighlight({ name: 'search', query: query.trim() });
  const total = found.matches.length;
  // The matches can shrink under a kept index, so wrap it on read.
  const at = total === 0 ? 0 : index % total;
  const current = found.matches[at];
  useHighlight({ name: 'search-current', ranges: current ? [current] : undefined, priority: 1 });
  const step = (by: number) => setIndex((at + by + total) % total);
  return (
    <Stack.Root>
      <Field.Group>
        <Label.Root htmlFor={inputId}>Find</Label.Root>
        <Field.Root id={inputId}>
          <Field.TextInput
No matches

Todae ships design tokens in the DTCG format. A company adds its own tokens on top, and every component reads them, so a token change reaches buttons, fields and dialogs at once.

matches holds a live Range per match, in document order. Text hidden by CSS is still matched. A match must sit inside one text node, so a word split by markup, such as fo<b>o</b>, is not found.

To mark the current result, pass it to a second hook as ranges under another name, with a higher priority so it paints on top. The demo above does this. Hooks that share a name paint together into one Highlight, so one rule styles them all, such as every combobox on a page; the one mounted last sets its priority and type. Give a hook its own name when its marks need their own look, as the current result does; to style several names the same way, list them: ::highlight(a), ::highlight(b). type sets spelling-error or grammar-error for spelling and grammar marks.

const current = found.matches[index];
useHighlight({ name: 'search-current', ranges: current ? [current] : undefined, priority: 1 });

The hook does not announce anything. Show the count in an <output> or other live region when a screen reader user needs it.

Accessible composition

Todae handles: The hook changes no DOM: no <mark>, no role, no attribute. The searched content keeps its roles, names, focus and selection. Matches are painted by CSS alone; matches gives them to your code as Ranges in document order.

You supply: The same information in text: the match count in a live region, as described above, and the position of the current match, such as "3 of 12". A ::highlight() style with enough contrast against both the text and its background, in light and dark themes. A current-match style that differs from the others by more than hue. Scrolling and focus: the hook moves neither, so bring the current match into view yourself.

Watch for: The highlight as the only sign of the current result. Two hooks with the same name and different priority or type: a name has one of each, so the one mounted last sets them for both.

When to use

When: Marking text without changing the DOM: find in page, search results in a list or document, matched letters in a filter.

When not: Highlighting that must be in the accessibility tree → a real <mark> element. A single selected item in a list → aria-selected and a style, as OptionList does.

Props

useHighlight props
NameTypeDescription
name Requiredstring

Name to style with ::highlight(name). Hooks that share a name paint together under it.

querystring | readonly string[]

Text to find inside the element on ref. A list matches any of its terms; empty strings are skipped.

caseSensitiveboolean

Match letter case exactly.

Default: false

rangesreadonly AbstractRange[]

More ranges to paint, such as the current match. They need no ref and are not added to matches.

prioritynumber

Which highlight paints on top where two overlap. Higher wins.

Default: 0

type'highlight'| 'spelling-error'| 'grammar-error'

Highlight type, for spelling and grammar marks.

Default: 'highlight'