ComponentsChangelogPro

    Getting Started

    Animated Cards

    Components

    Backgrounds

    3d & Shaders

    Text Animations

    Buttons

    Docs
    Star button

    Star button

    The StarButton is a visually striking button that features a radial light animation with a starry background.

    Loading...

    Installation

    Install dependencies

    npm install

    npm install clsx tailwind-merge

    Add utils file

    lib/utils.ts

    import { ClassValue, clsx } from "clsx";
    import { twMerge } from "tailwind-merge";
     
    export function cn(...inputs: ClassValue[]) {
      return twMerge(clsx(inputs));
    }

    Add the following code in your tailwind.config.js file

    tailwind.config.js

    /** @type {import('tailwindcss').Config} */
    module.exports = {
      darkMode: ["class"],
      content: [
        "./pages/**/*.{js,ts,jsx,tsx,mdx}",
        "./components/**/*.{js,ts,jsx,tsx,mdx}",
        "./app/**/*.{js,ts,jsx,tsx,mdx}",
      ],
      theme: {
        extend: {
          animation: {
            "star-btn": "star-btn calc(var(--duration)*1s) linear infinite",
          },
          keyframes: {
            "star-btn": {
              "0%": { "offset-distance": "0%" },
              "100%": { "offset-distance": "100%" },
            },
          }
        },
      },
      plugins: [],
    };

    Copy the source code

    star-button.tsx

    "use client";
     
    import React, { useRef, useEffect, ReactNode, CSSProperties } from "react";
    import { cn } from "@/lib/utils";
     
    interface StarBackgroundProps {
      color?: string;
    }
     
    function StarBackground({ color }: StarBackgroundProps) {
      return (
        <svg
          width="100%"
          height="100%"
          preserveAspectRatio="none"
          viewBox="0 0 100 40"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
        >
          <path
            fillRule="evenodd"
            clipRule="evenodd"
            d="M138.6 0H0V46.2H138.6V0ZM56.1 3.96C56.4645 3.96 56.76 4.25519 56.76 4.62C56.76 4.98481 56.4645 5.28 56.1 5.28C55.9131 5.28 55.7443 5.20201 55.624 5.07762C55.5632 5.01446 55.5147 4.93904 55.4829 4.8559C55.4552 4.78243 55.44 4.70315 55.44 4.62C55.44 4.5549 55.4494 4.49174 55.4668 4.43244C55.4906 4.35188 55.5292 4.27775 55.5795 4.21329C55.7004 4.05926 55.8885 3.96 56.1 3.96ZM80.52 15.18C80.52 14.8152 80.2245 14.52 79.86 14.52C79.4956 14.52 79.2 14.8152 79.2 15.18C79.2 15.5448 79.4956 15.84 79.86 15.84C80.2245 15.84 80.52 15.5448 80.52 15.18ZM40.26 17.16C40.6245 17.16 40.92 17.4552 40.92 17.82C40.92 18.1848 40.6245 18.48 40.26 18.48C39.8955 18.48 39.6 18.1848 39.6 17.82C39.6 17.4552 39.8955 17.16 40.26 17.16ZM46.2 7.26C46.2 6.89519 45.9045 6.6 45.54 6.6C45.5174 6.6 45.4953 6.60129 45.4733 6.60387L45.453 6.60579L45.4124 6.61225L45.3857 6.61804C45.3683 6.62256 45.3508 6.62707 45.3341 6.63287C45.2522 6.65929 45.1774 6.70184 45.1134 6.75597C45.0627 6.79916 45.0186 6.84943 44.9828 6.90551C44.9178 7.00799 44.88 7.12981 44.88 7.26C44.88 7.62481 45.1755 7.92 45.54 7.92C45.7372 7.92 45.9141 7.83363 46.0353 7.69635C46.0808 7.64478 46.1182 7.58613 46.1459 7.52232C46.1807 7.4424 46.2 7.35346 46.2 7.26ZM74.58 5.28C74.7701 5.28 74.9413 5.36057 75.0618 5.48882C75.073 5.50043 75.0837 5.51268 75.094 5.52557C75.1088 5.54426 75.1231 5.56359 75.1359 5.58357L75.1479 5.60291L75.1595 5.62353C75.1711 5.64481 75.1814 5.66672 75.1906 5.68928C75.2226 5.76662 75.24 5.85106 75.24 5.94C75.24 6.1585 75.1336 6.3525 74.9699 6.47238C74.9158 6.51234 74.8555 6.54393 74.7908 6.56584C74.7247 6.58775 74.6538 6.6 74.58 6.6C74.2156 6.6 73.92 6.30481 73.92 5.94C73.92 5.87684 73.929 5.8156 73.9455 5.7576C73.9596 5.70862 73.979 5.66221 74.0032 5.61903C74.0657 5.50688 74.1595 5.41471 74.2728 5.35541C74.3647 5.30707 74.4691 5.28 74.58 5.28Z"
            fill={color || "currentColor"}
          />
        </svg>
      );
    }
     
    interface StarButtonProps {
      children: ReactNode;
      lightWidth?: number;
      duration?: number;
      lightColor?: string;
      backgroundColor?: string;
      className?: string;
    }
     
    export function StarButton({
      children,
      lightWidth = 110,
      duration = 5,
      lightColor = "#FAFAFA",
      backgroundColor = "currentColor",
      className,
      ...props
    }: StarButtonProps) {
      const pathRef = useRef<HTMLButtonElement>(null);
     
      useEffect(() => {
        if (pathRef.current) {
          const div = pathRef.current;
          div.style.setProperty(
            "--path",
            `path('M 0 0 H ${div.offsetWidth} V ${div.offsetHeight} H 0 V 0')`
          );
        }
      }, []);
     
      return (
        <button
          style={
            {
              "--duration": duration,
              "--light-width": `${lightWidth}px`,
              "--light-color": lightColor,
              isolation: "isolate",
            } as CSSProperties
          }
          ref={pathRef}
          className={cn(
            "relative z-[3] overflow-hidden h-10 px-4 py-2 inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-3xl text-sm font-medium transition-colors disabled:pointer-events-none disabled:opacity-50 group/star-button",
            className
          )}
          {...props}
        >
          <div
            className="absolute aspect-square inset-0 animate-star-btn bg-[radial-gradient(ellipse_at_center,var(--light-color),transparent,transparent)]"
            style={
              {
                offsetPath: "var(--path)",
                offsetDistance: "0%",
                width: "var(--light-width)",
              } as CSSProperties
            }
          />
          <div
            className="absolute inset-0 border-2 dark:border-white/15 border-black/10 z-[4] overflow-hidden rounded-[inherit] dark:text-black text-white after:content-[''] after:absolute after:inset-0 after:bg-gradient-to-b after:from-transparent after:to-neutral-100/50 dark:after:to-neutral-700/50 after:opacity-0 group-hover/star-button:after:opacity-100 after:transition-opacity after:duration-700"
            aria-hidden="true"
          >
            <StarBackground color={backgroundColor} />
          </div>
          <span className="z-10 relative bg-gradient-to-t dark:from-white dark:to-neutral-500 from-black to-neutral-400 inline-block text-transparent bg-clip-text">
            {children}
          </span>
        </button>
      );
    }

    Props

    PropTypeDescriptionDefault
    classNameStringThe class name for the component, allowing for custom styling.-
    lightWidthNumberThe diameter of the light halo (in px).120
    durationNumberThe duration of the effect (in s).5
    lightColorStringThe color of the light halo."#FAFAFA"
    childrenReact.ReactNodeThe content inside the button, typically text or other elements.-
    backgroundColorStringThe color for the background of the StarBackground SVG."currentColor"

    Credits

  1. This component is inspired by Noon