Star Border

Button whose border is swept by two orbiting glowing stars.

pnpm dlx shadcn@latest add @leo/star-border

Props

NameTypeDefaultDescription
asElementType<any, keyof IntrinsicElements> | undefined-
backgroundColorstring | undefined#000000
borderColorstring | undefined#222222
childrenReactNodeStar Border
classNamestring | undefined-
colorstring | undefinedwhite
speedAnimationDuration<string & {}> | undefined6s
textColorstring | undefined#ffffff
thicknessnumber | undefined1

Source

"use client";

import type React from "react";
import { cn } from "@/lib/utils";

export type StarBorderProps<T extends React.ElementType> = React.ComponentPropsWithoutRef<T> & {
  as?: T;
  className?: string;
  children?: React.ReactNode;
  color?: string;
  speed?: React.CSSProperties["animationDuration"];
  thickness?: number;
  backgroundColor?: string;
  textColor?: string;
  borderColor?: string;
};

const KEYFRAMES =
  "@keyframes leo-star-border-bottom{0%{transform:translate(0%,0%);opacity:1}100%{transform:translate(-100%,0%);opacity:0}}" +
  "@keyframes leo-star-border-top{0%{transform:translate(0%,0%);opacity:1}100%{transform:translate(100%,0%);opacity:0}}";

const StarBorder = <T extends React.ElementType = "button">({
  as,
  className,
  color = "white",
  speed = "6s",
  thickness = 1,
  backgroundColor = "#000000",
  textColor = "#ffffff",
  borderColor = "#222222",
  children = "Star Border",
  ...rest
}: StarBorderProps<T>) => {
  const Component = as || "button";
  // biome-ignore lint/suspicious/noExplicitAny: polymorphic props
  const { style, ...elementProps } = rest as any;

  return (
    <Component
      className={cn("relative inline-block overflow-hidden rounded-[20px]", className)}
      {...elementProps}
      style={{
        padding: `${thickness}px 0`,
        ...style,
      }}
    >
      <style href="leo-star-border" precedence="default">
        {KEYFRAMES}
      </style>
      <div
        className="absolute w-[300%] h-[50%] opacity-70 bottom-[-11px] right-[-250%] rounded-full animate-[leo-star-border-bottom_linear_infinite_alternate] z-0"
        style={{
          background: `radial-gradient(circle, ${color}, transparent 10%)`,
          animationDuration: speed,
        }}
      />
      <div
        className="absolute w-[300%] h-[50%] opacity-70 top-[-10px] left-[-250%] rounded-full animate-[leo-star-border-top_linear_infinite_alternate] z-0"
        style={{
          background: `radial-gradient(circle, ${color}, transparent 10%)`,
          animationDuration: speed,
        }}
      />
      <div
        className="relative z-1 border text-center text-[16px] py-[16px] px-[26px] rounded-[20px]"
        style={{ background: backgroundColor, color: textColor, borderColor }}
      >
        {children}
      </div>
    </Component>
  );
};

export { StarBorder };
export default StarBorder;