하이라이트 (Highlight)
더 큰 본문 텍스트 안에서 특정 단어나 구절을 강조하는 데 사용하는 유틸리티예요. 일치하는 부분을 <mark> 태그로 감싸 렌더링합니다.
출처: 문서
본문
사용법 (Usage)
Highlight 컴포넌트는 전체 텍스트를 담는 text prop과 강조할 텍스트를 지정하는 query prop을 받습니다. 그러고 나서 강조된 부분을 <mark> 태그로 감싼 텍스트를 렌더링합니다.
import { Highlight } from '@ark-ui/react/highlight'
import styles from 'styles/highlight.module.css'
export const Basic = () => (
<p className={styles.Text}>
<Highlight
className={styles.Mark}
query="component"
text="Ark UI is a headless component library for building accessible web applications."
/>
</p>
)
동적 쿼리 (Dynamic Query)
query prop을 state로 제어하면 인터랙티브한 검색 하이라이트 경험을 만들 수 있어요.
import { Highlight } from '@ark-ui/react/highlight'
import { useState } from 'react'
import field from 'styles/field.module.css'
import styles from 'styles/highlight.module.css'
export const DynamicQuery = () => {
const [query, setQuery] = useState('component')
return (
<div className={styles.Root}>
<input
className={field.Input}
type="text"
placeholder="Search text..."
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
<p className={styles.Text}>
<Highlight
className={styles.Mark}
query={query}
text="With Ark UI, you can build accessible, custom components. Each component is fully typed and works seamlessly with React, Solid, Svelte, and Vue."
/>
</p>
</div>
)
}
여러 쿼리 (Multiple Queries)
query prop에 문자열 배열을 전달하면 여러 단어를 강조할 수 있어요.
import { Highlight } from '@ark-ui/react/highlight'
import styles from 'styles/highlight.module.css'
export const Multiple = () => (
<p className={styles.Text}>
<Highlight
className={styles.Mark}
query={['React', 'Vue']}
text="Ark UI provides React, Solid, Vue, and Svelte components that are accessible and customizable."
/>
</p>
)
대소문자 구분 (Case Sensitivity)
기본적으로 하이라이트는 대소문자를 구분합니다. ignoreCase prop을 사용하면 대소문자를 무시하도록 만들 수 있어요.
import { Highlight } from '@ark-ui/react/highlight'
import styles from 'styles/highlight.module.css'
export const IgnoreCase = () => (
<p className={styles.Text}>
<Highlight
className={styles.Mark}
text="TypeScript provides static type checking. Using typescript helps catch errors early in development."
query="typescript"
ignoreCase
/>
</p>
)
모두 일치 (Match All)
기본적으로 Highlight 컴포넌트는 쿼리의 첫 번째 일치 항목만 매칭합니다. 쿼리의 모든 일치 항목을 강조하려면 matchAll prop을 true로 설정하세요.
import { Highlight } from '@ark-ui/react/highlight'
import styles from 'styles/highlight.module.css'
export const MatchAll = () => (
<div className={styles.Root}>
<div className={styles.Section}>
<span className={styles.Label}>Match All</span>
<p className={styles.Text}>
<Highlight
className={styles.Mark}
text="Each component follows WAI-ARIA guidelines. Every component is rigorously tested to ensure accessibility."
query="component"
matchAll
/>
</p>
</div>
<div className={styles.Section}>
<span className={styles.Label}>Match First Only</span>
<p className={styles.Text}>
<Highlight
className={styles.Mark}
text="Each component follows WAI-ARIA guidelines. Every component is rigorously tested to ensure accessibility."
query="component"
matchAll={false}
/>
</p>
</div>
</div>
)
정확히 일치 (Exact Match)
기본적으로 Highlight 컴포넌트는 부분 단어도 매칭합니다. exactMatch prop을 사용하면 쿼리와 정확히 일치하는 전체 단어만 강조할 수 있어요.
import { Highlight } from '@ark-ui/react/highlight'
import styles from 'styles/highlight.module.css'
export const ExactMatch = () => (
<div className={styles.Root}>
<div className={styles.Section}>
<span className={styles.Label}>Partial Match</span>
<p className={styles.Text}>
<Highlight
className={styles.Mark}
query="box"
text="The checkbox component renders a box element. Use combobox for autocomplete."
matchAll
/>
</p>
</div>
<div className={styles.Section}>
<span className={styles.Label}>Exact Match</span>
<p className={styles.Text}>
<Highlight
className={styles.Mark}
query="box"
text="The checkbox component renders a box element. Use combobox for autocomplete."
exactMatch
matchAll
/>
</p>
</div>
</div>
)
API 레퍼런스 (API Reference)
Highlight
Props
query
Type: string | string[]
Required: true
Default Value: undefined
Description: 텍스트에서 강조할 쿼리
text
Type: string
Required: true
Default Value: undefined
Description: 강조할 텍스트
exactMatch
Type: boolean
Required: false
Default Value: undefined
Description: 전체 단어만 매칭할지 여부
ignoreCase
Type: boolean
Required: false
Default Value: undefined
Description: 매칭 시 대소문자를 무시할지 여부
matchAll
Type: boolean
Required: false
Default Value: undefined
Description: 쿼리의 여러 인스턴스를 매칭할지 여부
커스터마이징 (Customization)
Highlight 컴포넌트는 매칭된 텍스트를 <mark> 태그로 감쌉니다. className(Solid/Svelte/Vue에서는 class)을 전달해 강조된 부분을 스타일링할 수 있어요.
<Highlight
text="Ark UI is a headless component library for building accessible web applications."
query="component"
className="highlight-mark"
/>
CSS로 mark 태그를 스타일링해 강조된 텍스트의 모양을 커스터마이즈할 수 있어요.
.highlight-mark {
background-color: #ffe5e4;
color: #c9453b;
border-radius: 0.125rem;
}