highlightText
Utility function to highlight matching text portions in search results
Installation
Overview
highlightText wraps matching portions of text in styled <mark> elements. Use it to show which parts of a search result match the user's query, in command menus, autocompletes, or any filtered list.
Usage
Example with Command
API Reference
Parameters
Prop
Type
Default
Returns
ReactNode - The original text with matched portions wrapped in styled <mark> elements.
Styling
Matched text is wrapped in <mark> elements with the following default styles:
Matches read as text-primary with bold weight and no background.
Implementation Details
- Case-insensitive matching: Query "hook" matches "Hook", "HOOK", etc.
- Safe regex handling: Special regex characters in the query are escaped
- Query length limit: Queries are limited to 100 characters for performance
- Empty query handling: Returns the original text unchanged if query is empty or whitespace-only
Used By
- Command - For highlighting search matches
- Autocomplete - For highlighting suggestion matches