Highlight Text
Highlights specific parts of a string or content, commonly used in search results to show matched keywords.
Usage
Basic usage example to quickly see how the Highlight
works.
Pass the main string as children to Highlight component and string part that should be highlighted to highlight prop.
Properties
Interactive configurator to explore customization options for the Highlight
component.
Pass the main string as children to Highlight component and string part that should be highlighted to highlight prop.
If the main string does not contain the highlighted part, it is left alone. The highlighted part is only the string of characters that have a matching sequence, whitespace is not included.
You can change styles of highlighted part if you do not like default styles.
API References
Styles API
type T = "root" | "highlight";
Styles API | Type | Default | Annotation |
---|---|---|---|
className? | string | undefined | pass to root component <p> |
classNames? | Partial<Record<T, string>> | undefined | |
style? | CSSProperties | undefined | pass to root component <p> |
styles? | Partial<Record<T, CSSProperties>> | undefined | |
color | Property.Color | yellow | Key of CSS color, passed to mark component |
Props API
Props API | Type | Default | Annotation |
---|---|---|---|
text? | string | - | Define text to process the entire string |
children? | string | - | Define text to process the entire string |
highlight? | string | string[] | Substring or an array of substrings to highlight in children or text="" | |
el? | ElementType | <p> | element that will wrap the entire text |
Source Codes
Full working code example, including necessary markup and styles. You can copy and paste this code directly to start using the component immediately.