With the Highlight component, you can spotlight words.
import { Highlight } from "@chakra-ui/react"
const Demo = () => {
return (
<Highlight
query="spotlight"
styles={{ px: "0.5", bg: "orange.subtle", color: "orange.fg" }}
>
With the Highlight component, you can spotlight words.
</Highlight>
)
}
Usage
import { Highlight } from "@chakra-ui/react"
<Highlight query="Hello">Hello World</Highlight>
Examples
Multiple
Pass an array of strings to the query
prop to highlight multiple substrings.
With the Highlight component, you can spotlight, emphasize and accentuate words.
import { Heading, Highlight } from "@chakra-ui/react"
const Demo = () => {
return (
<Heading lineHeight="tall">
<Highlight
query={["spotlight", "emphasize", "Accentuate"]}
styles={{ px: "0.5", bg: "teal.muted" }}
>
With the Highlight component, you can spotlight, emphasize and
accentuate words.
</Highlight>
</Heading>
)
}
Custom Style
Use the styles
prop to customize the style of the highlighted text.
With the Highlight component, you can spotlight words.
import { Highlight } from "@chakra-ui/react"
const Demo = () => {
return (
<Highlight query="component" styles={{ fontWeight: "semibold" }}>
With the Highlight component, you can spotlight words.
</Highlight>
)
}
Search Query
Use the highlight component to highlight search query results.
Search result for: spot
Spotlight bulb
Spot cleaner
Spot ceiling
import { Highlight, Stack, Text } from "@chakra-ui/react"
const query = "spot"
const results = ["Spotlight bulb", "Spot cleaner", "Spot ceiling"]
const Demo = () => {
return (
<Stack gap="6">
<Text>Search result for: spot</Text>
<Stack gap="1">
{results.map((item) => (
<p key={item}>
<Highlight
ignoreCase
query={query}
styles={{ fontWeight: "semibold" }}
>
{item}
</Highlight>
</p>
))}
</Stack>
</Stack>
)
}
With Squiggle
Here's an example of how to render a custom squiggle image around the highlighted text. Useful for a more fancy effect.
Endless scale, powered by real humans.
"use client"
import { Heading, Mark, useHighlight } from "@chakra-ui/react"
import { Fragment } from "react"
const Demo = () => {
const chunks = useHighlight({
text: "Endless scale, powered by real humans.",
query: ["endless", "real humans."],
})
return (
<Heading size="2xl" maxW="20ch">
{chunks.map((chunk, index) => {
return chunk.match ? (
<Mark
key={index}
css={{
fontStyle: "italic",
color: "red.500",
position: "relative",
}}
>
{chunk.text}
<img
style={{ position: "absolute", left: 0 }}
src="https://uploads-ssl.webflow.com/5fac11c3554384e2baf6481c/61c4dc7572d22f05ba26fd34_hero-underline.svg"
loading="lazy"
alt=""
/>
</Mark>
) : (
<Fragment key={index}>{chunk.text}</Fragment>
)
})}
</Heading>
)
}
Props
Prop | Default | Type |
---|---|---|
query * | string | string[] The query to highlight in the text | |
text * | string The text to highlight | |
ignoreCase | boolean Whether to ignore case while matching | |
matchAll | boolean Whether to match multiple instances of the query | |
styles | SystemStyleObject |