Skip to content

Commit 8acaabd

Browse files
committed
feat(svelte): add highlight
1 parent 9779ebd commit 8acaabd

File tree

8 files changed

+101
-0
lines changed

8 files changed

+101
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script lang="ts">
2+
import { Highlight } from '@ark-ui/svelte/highlight'
3+
</script>
4+
5+
<Highlight
6+
query=" ipsum"
7+
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt"
8+
/>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script lang="ts">
2+
import { Highlight } from '@ark-ui/svelte/highlight'
3+
</script>
4+
5+
<Highlight
6+
text="The quick brown Fox jumps over the lazy Dog."
7+
query={['fox', 'dog']}
8+
ignoreCase
9+
/>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script lang="ts">
2+
import { Highlight } from '@ark-ui/svelte/highlight'
3+
</script>
4+
5+
<div>
6+
<h3>Match All</h3>
7+
<Highlight text="The quick brown fox jumps over the lazy fox." query="fox" matchAll={true} />
8+
9+
<h3>Match First Occurrence Only</h3>
10+
<Highlight text="The quick brown fox jumps over the lazy fox." query="fox" matchAll={false} />
11+
</div>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Meta } from '@storybook/svelte'
2+
import BasicExample from './examples/basic.svelte'
3+
import IgnoreCaseExample from './examples/ignore-case.svelte'
4+
import MatchAllExample from './examples/match-all.svelte'
5+
6+
const meta = {
7+
title: 'Components / Highlight',
8+
} as Meta
9+
10+
export default meta
11+
12+
export const Basic = {
13+
render: () => ({
14+
Component: BasicExample,
15+
}),
16+
}
17+
18+
export const IgnoreCase = {
19+
render: () => ({
20+
Component: IgnoreCaseExample,
21+
}),
22+
}
23+
24+
export const MatchAll = {
25+
render: () => ({
26+
Component: MatchAllExample,
27+
}),
28+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script lang="ts">
2+
import { createSplitProps } from '$lib/utils/create-split-props'
3+
import type { HTMLProps, Assign } from '$lib/types'
4+
import { useHighlight, type UseHighlightProps } from './use-highlight.svelte'
5+
6+
export interface HighlightBaseProps extends UseHighlightProps {}
7+
export interface HighlightProps extends Assign<HTMLProps<'mark'>, HighlightBaseProps> {}
8+
9+
const props: HighlightProps = $props()
10+
11+
const [highlightProps, localProps] = createSplitProps<HighlightBaseProps>()(props, [
12+
'query',
13+
'text',
14+
'ignoreCase',
15+
'matchAll',
16+
])
17+
18+
if (typeof highlightProps.text !== 'string') {
19+
throw new Error('[ark-ui/highlight] text must be a string')
20+
}
21+
22+
const chunks = useHighlight(highlightProps)
23+
</script>
24+
25+
{#each chunks as { text, match }, i}
26+
{#if match}
27+
<mark {...localProps}>{text}</mark>
28+
{:else}
29+
{text}
30+
{/if}
31+
{/each}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as Highlight } from './highlight.svelte'
2+
export type { HighlightProps } from './highlight.svelte'
3+
export { useHighlight, type UseHighlightProps, type HighlightChunk } from './use-highlight.svelte'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { type HighlightChunk, type HighlightWordProps, highlightWord } from '@zag-js/highlight-word'
2+
3+
export interface UseHighlightProps extends HighlightWordProps {}
4+
5+
export const useHighlight = (props: UseHighlightProps): HighlightChunk[] => {
6+
const chunks = $derived(highlightWord(props))
7+
return chunks
8+
}
9+
10+
export type { HighlightChunk }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './avatar'
22
export * from './factory'
33
export * from './timer'
4+
export * from './highlight'

0 commit comments

Comments
 (0)