Split Text Effect
bucharitesh/split-text-effectFreeComponent
A split text effect component for Next.js apps with next-themes and Tailwind CSS, supporting system, light, and dark modes.
Install it
npx shadcn@latest add https://bucharitesh.in/r/split-text-effect.jsonSource
1'use client';23import { cn } from '@/lib/utils';4import { motion, useSpring, useTransform } from 'motion/react';5import * as React from 'react';67interface CrossProps extends React.HTMLAttributes<HTMLDivElement> {8 position: 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right';9 color?: string;10}1112const Cross = React.forwardRef<HTMLDivElement, CrossProps>(13 ({ position, className, color, ...props }, ref) => {14 const positionClasses = {15 'top-left': '-top-px -left-px rotate-0',16 'top-right': '-top-px -right-px rotate-90',17 'bottom-left': 'bottom-[-2px] -left-px -rotate-90',18 'bottom-right': 'bottom-[-2px] -right-px -rotate-180',19 };2021 return (22 <div23 ref={ref}24 aria-hidden="true"25 className={cn(26 'absolute h-[15px] w-[15px] cursor-pointer',27 positionClasses[position],28 className29 )}30 data-position={position}31 {...props}32 >33 <div34 className="absolute top-0 left-0 h-px w-[15px]"35 style={{ backgroundColor: color }}36 />37 <div38 className="absolute bottom-0 left-0 h-[15px] w-px"39 style={{ backgroundColor: color }}40 />41 </div>42 );43 }44);45Cross.displayName = 'Cross';4647interface SplitTextEffectProps extends React.HTMLAttributes<HTMLDivElement> {48 text: string | React.ReactNode;49 fill?: number;50 accent?: string;51}5253const SplitTextEffect = React.forwardRef<HTMLDivElement, SplitTextEffectProps>(54 ({ text, fill = 0.5, accent = '#006efe', className, ...props }, ref) => {55 const containerRef = React.useRef<HTMLDivElement>(null);56 const lineRef = React.useRef<HTMLDivElement>(null);57 const [hasMounted, setHasMounted] = React.useState(false);5859 React.useEffect(() => {60 setHasMounted(true);61 }, []);6263 const smoothY = useSpring(0, {64 stiffness: 100,65 damping: 20,66 });6768 React.useEffect(() => {69 if (!hasMounted || !containerRef.current) return;7071 const container = containerRef.current;72 const height = container.offsetHeight;73 const initialY = Math.min(74 Math.max(height * (1 - fill), height * 0.1),75 height * 0.976 );7778 smoothY.set(initialY);79 }, [hasMounted, fill]);8081 const handleMouseMove = (e: React.MouseEvent) => {82 if (!containerRef.current) return;83 const rect = containerRef.current.getBoundingClientRect();84 const height = rect.height;8586// … truncated
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 | |
| Text Highlighttext-highlight | bucharitesh | Components | Free | 1 dep | |
| View Magnifierview-magnifier | bucharitesh | Components | Free | 1 dep |
