Sparkles Title
A title component with a sparkling effect, adding dynamic and eye-catching sparkles to your title
A title component with a sparkling effect, adding dynamic and eye-catching sparkles to your title
npm install @tsparticles/react @tsparticles/slim
1// @ts-nocheck2'use client';34import { useEffect, useId, useState } from 'react';5import Particles, { initParticlesEngine } from '@tsparticles/react';6import { loadSlim } from '@tsparticles/slim';78interface SparklesProps {9className?: string;10size?: number;11minSize?: number | null;12density?: number;13speed?: number;14minSpeed?: number | null;15opacity?: number;16direction?: string;17opacitySpeed?: number;18minOpacity?: number | null;19color?: string;20mousemove?: boolean;21hover?: boolean;22background?: string;23options?: Record<string, any>; // Adjust type as needed based on `options` structure24}2526export function Sparkles({27className,28size = 1.2,29minSize = null,30density = 800,31speed = 1.5,32minSpeed = null,33opacity = 1,34direction = '',35opacitySpeed = 3,36minOpacity = null,37color = '#ffffff',38mousemove = false,39hover = false,40background = 'transparent',41options = {},42}: SparklesProps) {43const [isReady, setIsReady] = useState(false);4445useEffect(() => {46initParticlesEngine(async (engine) => {47await loadSlim(engine);48}).then(() => {49setIsReady(true);50});51}, []);5253const id = useId();54const defaultOptions = {55background: {56color: {57value: background,58},59},60fullScreen: {61enable: false,62zIndex: 1,63},64fpsLimit: 300,6566interactivity: {67events: {68onClick: {69enable: true,70mode: 'push',71},72onHover: {73enable: hover,74mode: 'grab',75parallax: {76enable: mousemove,77force: 60,78smooth: 10,79},80},81resize: true as any,82},83modes: {84push: {85quantity: 4,86},87repulse: {88distance: 200,89duration: 0.4,90},91},92},93particles: {94color: {95value: color,96},97move: {98enable: true,99direction,100speed: {101min: minSpeed || speed / 130,102max: speed,103},104straight: true,105},106collisions: {107absorb: {108speed: 2,109},110bounce: {111horizontal: {112value: 1,113},114vertical: {115value: 1,116},117},118enable: false,119maxSpeed: 50,120mode: 'bounce',121overlap: {122enable: true,123retries: 0,124},125},126number: {127value: density,128},129opacity: {130value: {131min: minOpacity || opacity / 10,132max: opacity,133},134animation: {135enable: true,136sync: false,137speed: opacitySpeed,138},139},140size: {141value: {142min: minSize || size / 1.5,143max: size,144},145},146},147detectRetina: true,148};149return (150isReady && (151<Particles id={id} options={defaultOptions} className={className} />152)153);154}
Prop | Type | Default | Description |
---|---|---|---|
className | string | Optional CSS class for styling the sparkles component. | |
size | number | 1.2 | Size of the sparkles. |
minSize | number | null | null | Minimum size of the sparkles, or null if no minimum size is set. |
density | number | 800 | Density of the sparkles. |
speed | number | 1.5 | Speed of the sparkles' animation. |
minSpeed | number | null | null | Minimum speed of the sparkles, or null if no minimum speed is set. |
opacity | number | 1 | Opacity of the sparkles. |
direction | string | '' | Direction of the sparkles' movement. |
opacitySpeed | number | 3 | Speed at which the opacity of the sparkles changes. |
minOpacity | number | null | null | Minimum opacity of the sparkles, or null if no minimum opacity is set. |
color | string | '#ffffff' | Color of the sparkles. |
mousemove | boolean | false | Whether the sparkles should follow the mouse movement. |
hover | boolean | false | Whether the sparkles should be active on hover. |
background | string | 'transparent' | Background color of the component. |
options | Record<string, any> | {} | Additional options for configuring the sparkles. |