Show · Highlight

Highlight

highlight specific content

Demos

How to import

import { Highlight } from '@fibrous/semi-ui-solid';

Basic usage

You can specify keywords to be highlighted with searchWords and source text with sourceString

从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中

Code
import { Highlight } from '@fibrous/semi-ui-solid';

const sourceString = '从 Semi Design 到 Any Design  快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = ['设计系统', 'Semi Design'];

export default function HighlightDemo() {
    return (
        <h2>
            <Highlight sourceString={sourceString} searchWords={searchWords} />
        </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

Numbers get no px: write lengths in highlightStyle and in a search word's style as strings such as '4px'. Unitless properties such as 'font-weight': 600 still work. Keys must be CSS property names; camelCase keys such as backgroundColor have no effect.

从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中

从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中

Code
import { Highlight } from '@fibrous/semi-ui-solid';

const sourceString = '从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = ['设计系统', 'Semi Design'];

const tealStyle = {
    'border-radius': '6px',
    'margin-left': '4px',
    'margin-right': '4px',
    'padding-left': '4px',
    'padding-right': '4px',
    'background-color': 'var(--semi-teal-7)',
    color: 'var(--semi-white)',
};

const primaryStyle = {
    'border-radius': '6px',
    'margin-left': '4px',
    'margin-right': '4px',
    'padding-left': '4px',
    'padding-right': '4px',
    'background-color': 'var(--semi-color-primary)',
    color: 'var(--semi-white)',
};

export default function HighlightDemo() {
    return (
        <>
            <h2>
                <Highlight sourceString={sourceString} searchWords={searchWords} highlightStyle={tealStyle} />
            </h2>
            <h2>
                <Highlight sourceString={sourceString} searchWords={searchWords} highlightStyle={primaryStyle} />
            </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 JSX.

从 Semi Design 到 Any Design 快速定义你的设计系统,并应用在设计稿和代码中

Code
import { Highlight } from '@fibrous/semi-ui-solid';

const sourceString = '从 Semi Design 到 Any Design  快速定义你的设计系统,并应用在设计稿和代码中';

const searchWords = [
    {
        text: 'Semi',
        style: { 'background-color': 'var(--semi-teal-7)', color: 'var(--semi-white)', padding: '4px' },
        class: 'keyword1',
    },
    {
        text: '设计系统',
        style: { 'background-color': 'var(--semi-color-primary)', color: 'var(--semi-white)', padding: '4px' },
        class: 'keyword2',
    },
    {
        text: '设计稿和代码',
        style: { 'background-color': 'var(--semi-violet-5)', color: 'var(--semi-white)', padding: '4px' },
        class: 'keyword3',
    },
];

const highlightStyle = { 'border-radius': '4px' };

export default function HighlightDemo() {
    return (
        <h2>
            <Highlight component="span" sourceString={sourceString} searchWords={searchWords} highlightStyle={highlightStyle} />
        </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
import { Highlight } from '@fibrous/semi-ui-solid';

const sourceString = '从 Semi Design 到 Any Design  快速定义你的设计系统,并应用在设计稿和代码中';
const searchWords = ['设计系统', 'Semi Design'];

export default function HighlightDemo() {
    return (
        <h2>
            <Highlight sourceString={sourceString} searchWords={searchWords} component="strong" />
        </h2>
    );
}

API Reference

Highlight

Props

PropertyDescriptionTypeDefault
autoEscapeWhether to escape regular expression characters in searchWords.booleantrue
caseSensitiveWhether matching is case sensitive.booleanfalse
componentTag name of the highlight element.string"mark"
highlightClassNameClass name of the highlight tag.string-
highlightStyleInline style of the highlight tag. Keys are CSS property names such as 'border-radius'; numbers get no px, so write lengths as strings.JSX.CSSProperties-
searchWordsText to highlight: a string, a string array, or an array of objects with text, class and style.string | SearchWords-
sourceStringSource text.string""

Highlight has no root element and renders inline chunks, so class, style, ref and native attributes such as id have nowhere to land and are dropped.