Demos
How to import
<script lang="ts">
import { Highlight } from '@fibrae/semi-ui-svelte';
</script>
Basic usage
You can specify keywords to be highlighted with searchWords and source text with sourceString
从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中
Code
<script lang="ts">
import { Highlight } from '@fibrae/semi-ui-svelte';
const sourceString = '从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = ['设计系统', 'Semi Design'];
</script>
<h2>
<Highlight {searchWords} {sourceString} />
</h2>Specify highlight style
By default, the highlighted text will have its own text style, the text color is black, and the background color is --semi-yellow-4.
In dark mode, the text color is white, and the background color is --semi-yellow-2.
When you need to customize different highlight styles, you can specify them through highlightClassName, highlightStyle
highlightStyle and a search word's style are style objects whose keys may be camelCase or kebab-case. Numbers get no px: write lengths as strings such as '4px'. Unitless properties such as fontWeight: 600 still work.
从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中
从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中
Code
<script lang="ts">
import { Highlight } from '@fibrae/semi-ui-svelte';
const sourceString = '从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = ['设计系统', 'Semi Design'];
const baseStyle = {
borderRadius: '6px',
marginLeft: '4px',
marginRight: '4px',
paddingLeft: '4px',
paddingRight: '4px',
color: 'var(--semi-white)',
};
const tealStyle = { ...baseStyle, backgroundColor: 'var(--semi-teal-7)' };
const primaryStyle = { ...baseStyle, backgroundColor: 'var(--semi-color-primary)' };
</script>
<h2>
<Highlight highlightStyle={tealStyle} {searchWords} {sourceString} />
</h2>
<h2>
<Highlight highlightStyle={primaryStyle} {searchWords} {sourceString} />
</h2>Use Different Styles for Different Texts
It supports using different highlight styles for different highlighted texts. The searchWords is a string array by default. When an array of objects is passed in, the highlighted text can be specified through text, and the className and style can be specified separately at the same time.
The class field of an object item is class, as in Svelte templates.
从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中
Code
<script lang="ts">
import { Highlight } from '@fibrae/semi-ui-svelte';
const sourceString = '从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = [
{
text: 'Semi',
style: { backgroundColor: 'var(--semi-teal-7)', color: 'var(--semi-white)', padding: '4px' },
class: 'keyword1',
},
{
text: '设计系统',
style: { backgroundColor: 'var(--semi-color-primary)', color: 'var(--semi-white)', padding: '4px' },
class: 'keyword2',
},
{
text: '设计稿和代码',
style: { backgroundColor: 'var(--semi-violet-5)', color: 'var(--semi-white)', padding: '4px' },
class: 'keyword3',
},
];
</script>
<h2>
<Highlight component="span" highlightStyle={{ borderRadius: '4px' }} {searchWords} {sourceString} />
</h2>Specify the highlight tag
Semi will wrap the text matching searchWords in sourceString with mark tag by default, you can also re-specify the tag through component
从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中
Code
<script lang="ts">
import { Highlight } from '@fibrae/semi-ui-svelte';
const sourceString = '从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = ['设计系统', 'Semi Design'];
</script>
<h2>
<Highlight component="strong" {searchWords} {sourceString} />
</h2>API Reference
Highlight
Props
| Property | Description | Type | Default |
|---|---|---|---|
| autoEscape | Whether to escape regular expression characters in searchWords. | boolean | true |
| caseSensitive | Whether matching is case sensitive. | boolean | false |
| component | Tag name of the highlight element. | string | "mark" |
| highlightClassName | Class name of the highlight tag. | string | - |
| highlightStyle | Style object of the highlight tag; keys may be camelCase or kebab-case. Numbers get no px, so write lengths as strings. | HighlightStyle | - |
| searchWords | Text to highlight: a string, a string array, or an array of objects with text, class and style. | string | SearchWords | [] |
| sourceString | Source text. | string | "" |
Highlight has no root element and renders inline chunks, so undeclared attributes such as class, style and id have nowhere to land and are dropped, and bind:this cannot reach an element.