Skip to content

Commit

Permalink
feat(svelte): add highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Nov 29, 2024
1 parent 9779ebd commit 8acaabd
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script lang="ts">
import { Highlight } from '@ark-ui/svelte/highlight'
</script>

<Highlight
query=" ipsum"
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script lang="ts">
import { Highlight } from '@ark-ui/svelte/highlight'
</script>

<Highlight
text="The quick brown Fox jumps over the lazy Dog."
query={['fox', 'dog']}
ignoreCase
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script lang="ts">
import { Highlight } from '@ark-ui/svelte/highlight'
</script>

<div>
<h3>Match All</h3>
<Highlight text="The quick brown fox jumps over the lazy fox." query="fox" matchAll={true} />

<h3>Match First Occurrence Only</h3>
<Highlight text="The quick brown fox jumps over the lazy fox." query="fox" matchAll={false} />
</div>
28 changes: 28 additions & 0 deletions packages/svelte/src/lib/components/highlight/highlight.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { Meta } from '@storybook/svelte'
import BasicExample from './examples/basic.svelte'
import IgnoreCaseExample from './examples/ignore-case.svelte'
import MatchAllExample from './examples/match-all.svelte'

const meta = {
title: 'Components / Highlight',
} as Meta

export default meta

export const Basic = {
render: () => ({
Component: BasicExample,
}),
}

export const IgnoreCase = {
render: () => ({
Component: IgnoreCaseExample,
}),
}

export const MatchAll = {
render: () => ({
Component: MatchAllExample,
}),
}
31 changes: 31 additions & 0 deletions packages/svelte/src/lib/components/highlight/highlight.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script lang="ts">
import { createSplitProps } from '$lib/utils/create-split-props'
import type { HTMLProps, Assign } from '$lib/types'
import { useHighlight, type UseHighlightProps } from './use-highlight.svelte'
export interface HighlightBaseProps extends UseHighlightProps {}
export interface HighlightProps extends Assign<HTMLProps<'mark'>, HighlightBaseProps> {}
const props: HighlightProps = $props()
const [highlightProps, localProps] = createSplitProps<HighlightBaseProps>()(props, [
'query',
'text',
'ignoreCase',
'matchAll',
])
if (typeof highlightProps.text !== 'string') {
throw new Error('[ark-ui/highlight] text must be a string')
}
const chunks = useHighlight(highlightProps)
</script>

{#each chunks as { text, match }, i}
{#if match}
<mark {...localProps}>{text}</mark>
{:else}
{text}
{/if}
{/each}
3 changes: 3 additions & 0 deletions packages/svelte/src/lib/components/highlight/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as Highlight } from './highlight.svelte'
export type { HighlightProps } from './highlight.svelte'
export { useHighlight, type UseHighlightProps, type HighlightChunk } from './use-highlight.svelte'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { type HighlightChunk, type HighlightWordProps, highlightWord } from '@zag-js/highlight-word'

export interface UseHighlightProps extends HighlightWordProps {}

export const useHighlight = (props: UseHighlightProps): HighlightChunk[] => {
const chunks = $derived(highlightWord(props))
return chunks
}

export type { HighlightChunk }
1 change: 1 addition & 0 deletions packages/svelte/src/lib/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './avatar'
export * from './factory'
export * from './timer'
export * from './highlight'

0 comments on commit 8acaabd

Please sign in to comment.