GooseUI

Components

Highlight Text

New

Wraps the matches of a search query in <mark>. The query is split into words, RegExp special characters are escaped, case is ignored, and optionally diacritics too, so cafe finds café. It is a server component with no client JavaScript. With animate the marker paints itself left to right as each match scrolls into view, driven by a CSS view() timeline.

Browser Support

Scroll-driven animations

With animate, the marker paints itself as a match scrolls into view.

Your browser:

Preview

Type a query. Several words are highlighted independently.

  • Café menu: crème brûlée with fresh berries
  • Jalapeño nachos and a lime soda
  • Green tea, mint tea and lemonade
  • Smoothie of the day (ask the barista)

Installation

npx shadcn@latest add @gooseui/highlight-text
pnpm dlx shadcn@latest add @gooseui/highlight-text
yarn dlx shadcn@latest add @gooseui/highlight-text
bunx --bun shadcn@latest add @gooseui/highlight-text

Usage

import { HighlightText } from "@/components/ui/highlight-text"

export function SearchResult({ title, query }: { title: string; query: string }) {
  return <HighlightText text={title} query={query} ignoreDiacritics />
}

Examples

Animated marker

The background fills in from the left as each match scrolls up from the bottom edge of the window. The progress follows the scroll, so scrolling back erases it.

Scroll down ↓

A clear text starts before the first word is typed. Decide who will read it, what they already know and what you want them to do after the last line. If you cannot name the goal in a single line, the reader will not find it either.

Put the main point at the top. People skim, and most of them stop after a few lines, so the conclusion belongs where the eye lands first. Details, background and caveats can follow for those who want to dig deeper.

Prefer short sentences. One sentence carries one idea, and when a line runs past two commas it usually hides two thoughts that want to be separated. Short sentences are easier to read on a phone and easier to translate.

Choose the active voice. Compare "the report was approved by the team" with "the team approved the report": the second version names the actor and saves three words. The passive voice has its place, but it should be a decision, not a habit.

Cut filler. Words such as very, really, basically and in order to rarely add meaning. Read each sentence again without them, and if nothing is lost, leave them out. Replace abstract nouns with verbs: say decide, not make a decision.

Use the words your readers use. Jargon is fine inside a team and tiring outside of it. When a term is unavoidable, explain it once, in plain language, the first time it appears, and keep the same word for the same thing afterwards.

Finally, edit with fresh eyes. Leave the draft for an hour, then read it as a stranger would. Mark every place where you stumbled, fix it, and once more check that the active voice and short sentences survived the edit.

<HighlightText
  text={text}
  query="short sentences active voice"
  animate
/>

Server rendering

The component has no client code, so it can render search results straight from a server component.

A naïve café owner served crème brûlée

// app/search/page.tsx (server component)
export default async function SearchPage({ searchParams }) {
  const { q = "" } = await searchParams
  const items = await getItems(q)

  return items.map((item) => (
    <HighlightText
      key={item.id}
      text={item.title}
      query={q}
      ignoreDiacritics
      markClassName="bg-accent underline decoration-primary"
    />
  ))
}

Chunks without React

splitHighlight returns plain chunks, handy for custom markup, a canvas or tests.

"A ""naïve"" ""café"" owner served ""crème"" brûlée"
import { splitHighlight } from "@/components/ui/highlight-text"

splitHighlight("A naïve café owner", "naive cafe", { ignoreDiacritics: true })
// [
//   { text: "A ", match: false },
//   { text: "naïve", match: true },
//   { text: " ", match: false },
//   { text: "café", match: true },
//   { text: " owner", match: false },
// ]

Props

PropTypeDefaultDescription
textstring-Source text
querystring-Search query, split into words by whitespace
ignoreDiacriticsbooleanfalseMatch accented letters by their base letter, é as e
animatebooleanfalsePaint the marker as each match scrolls into view
markClassNamestring-Classes for every <mark>
classNamestring-Classes for the wrapping span

splitHighlight(text, query, { ignoreDiacritics }) Returns an array of { text, match } chunks.