Skip to content

Commit 776f66d

Browse files
committed
feat: add StrikeHighlight component for colorful strikethrough-style text highlights
1 parent 2c9824d commit 776f66d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { cn } from '@/lib/utils';
2+
3+
interface StrikeHighlightProps {
4+
text: string;
5+
color?:
6+
| 'cyan'
7+
| 'pink'
8+
| 'violet-web'
9+
| 'slate-blue'
10+
| 'red'
11+
| 'yellow'
12+
| 'lemon'
13+
| 'laser-lemon';
14+
}
15+
16+
/**
17+
* Highlights the given text with the highlighter of specified color with a strikethrough effect.
18+
*
19+
* @param text - the text to be highlighted
20+
* @param color - the color of the highlighter [cyan,pink,violet-web,slate-blue,red,yellow,lemon,laser-lemon]
21+
* @returns Returns a `span` element with the highlighted text of specified color with strikethrough effect.
22+
*
23+
*/
24+
export default function StrikeHighlight({ text, color = 'yellow' }: StrikeHighlightProps) {
25+
const colors: Record<string, string> = {
26+
cyan: 'bg-cyan-200 dark:bg-cyan-500/45',
27+
pink: 'bg-[#ffa7ee] dark:bg-[#f73ed2]/50',
28+
'slate-blue': 'bg-[#a0a8ff] dark:bg-[#675bf9]/50',
29+
red: 'bg-[#ffa0a0] dark:bg-[#f83b3b]/45',
30+
yellow: 'bg-[#ffff77] dark:bg-[#fce913]/35',
31+
};
32+
33+
if (color === 'violet-web') {
34+
color = 'pink';
35+
} else if (color === 'lemon' || color === 'laser-lemon') {
36+
color = 'yellow';
37+
}
38+
39+
const colorClass = colors[color];
40+
41+
return (
42+
<span className="relative inline-block">
43+
<span
44+
className={cn(
45+
'absolute left-0 right-0 top-[60%] h-[0.6em] -translate-y-1/2 px-[0.1em]',
46+
colorClass,
47+
)}
48+
></span>
49+
<span className="z-2 relative">{text}</span>
50+
</span>
51+
);
52+
}

0 commit comments

Comments
 (0)