Text Highlight
bucharitesh/text-highlightFreeComponent
A highlighted text effect component using CSS conic and linear gradients with multiple color presets.
Install it
npx shadcn@latest add https://bucharitesh.in/r/text-highlight.jsonSource
1import { cn } from '@/lib/utils';2import { type VariantProps, cva } from 'class-variance-authority';3import * as React from 'react';45function buildHighlightBackground(rgb: string, angle: string) {6 return [7 `conic-gradient(at 0 100%, rgb(${rgb} / 100%) 1%, #fff0 3%) no-repeat 0 0 / auto 120%`,8 `conic-gradient(from 180deg at 100% 0, #fff0, rgb(${rgb} / 100%) 1%, #fff0 4%) no-repeat 100% 100% / auto 120%`,9 `linear-gradient(${angle}, rgb(${rgb} / 60%), rgb(${rgb} / 20%) 75%, rgb(${rgb} / 55%)) no-repeat center / auto`,10 ].join(', ');11}1213const textHighlightVariants = cva(14 'relative inline-block w-fit select-none whitespace-nowrap text-center font-semibold',15 {16 variants: {17 size: {18 sm: 'px-2 py-1 text-lg',19 default: 'px-3 py-1.5 text-2xl',20 lg: 'px-4 py-2 text-4xl',21 },22 },23 defaultVariants: {24 size: 'default',25 },26 }27);2829export interface TextHighlightProps30 extends React.HTMLAttributes<HTMLSpanElement>,31 VariantProps<typeof textHighlightVariants> {32 /** RGB values (space-separated, e.g. "255 232 62") */33 color: string;34 /** Gradient angle (e.g. "50deg") */35 angle?: string;36 /** Rotation applied to the highlight (e.g. "1deg") */37 rotate?: string;38 /** Scale applied to the highlight (e.g. "1.1") */39 scale?: string;40 /** Skew applied to the highlight (e.g. "-5deg") */41 skew?: string;42}4344const TextHighlight = React.forwardRef<HTMLSpanElement, TextHighlightProps>(45 (46 {47 children,48 className,49 size,50 color,51 angle = '50deg',52 rotate = '0deg',53 scale = '1',54 skew = '0deg',55 ...props56 },57 ref58 ) => {59 return (60 <span61 ref={ref}62 className={cn(textHighlightVariants({ size }), className)}63 {...props}64 >65 <span66 aria-hidden="true"67 className="absolute inset-0"68 style={{69 zIndex: -1,70 borderRadius: '3px 5px 3px 5px',71 background: buildHighlightBackground(color, angle),72 rotate,73 scale,74 transform: `skew(${skew})`,75 }}76 />77 {children}78 </span>79 );80 }81);82TextHighlight.displayName = 'TextHighlight';8384export { TextHighlight, textHighlightVariants };
What it pulls in
npm packages
Files it writes
More from bucharitesh
All 26 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Bookbook | bucharitesh | Components | Free | 1 dep · 2 files | |
| Game of Lifegame-of-life | bucharitesh | Components | Free | no deps | |
| Magical Mousemagical-mouse | bucharitesh | Components | Free | 1 dep | |
| Metallic Buttonmetallic-button | bucharitesh | Components | Free | 2 deps | |
| Modern Progressmodern-progress | bucharitesh | Components | Free | 1 dep | |
| Pixel Iconpixel-icon | bucharitesh | Components | Free | no deps | |
| Split Text Effectsplit-text-effect | bucharitesh | Components | Free | 1 dep | |
| View Magnifierview-magnifier | bucharitesh | Components | Free | 1 dep |
