Skip to content

Commit

Permalink
perf: 优化Highlight组件
Browse files Browse the repository at this point in the history
  • Loading branch information
huangmingfu committed Dec 11, 2024
1 parent f0ccc3f commit 579f461
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/components/high-light/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.highlight {
font-weight: bold;
background-color: unset;
}
18 changes: 14 additions & 4 deletions src/components/high-light/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import React from 'react';
import React, { memo } from 'react';

export default function Highlight({
import classnames from 'classnames';

import './index.scss';

function Highlight({
children,
keys
keys,
color = '#FFDA00',
className
}: {
children: React.ReactNode;
keys: string[];
color?: string;
className?: string;
}) {
const string = React.Children.toArray(children).join('');
const reg = new RegExp(keys.join('|'), 'g');
Expand All @@ -14,11 +22,13 @@ export default function Highlight({
index % 2 === 0 ? (
x
) : (
<mark key={index} className="highlight">
<mark key={index} className={classnames('highlight', className)} style={{ color }}>
{x[0] === '@' ? x.slice(1) : x}
</mark>
)
);

return <>{elements}</>;
}

export default memo(Highlight);

0 comments on commit 579f461

Please sign in to comment.