Shimmer button

A call-to-action button with a light that travels around its border and a sheen that sweeps across the face on hover or keyboard focus. Light colours default to the theme's chart colours and can be overridden, along with the speed and three sizes. All movement respects prefers-reduced-motion.

ButtonsComponentTailwind v4

Source

"use client";

import { forwardRef, type ButtonHTMLAttributes, type CSSProperties } from "react";
import { cn } from "@/lib/utils";

export type ShimmerButtonSize = "sm" | "md" | "lg";

export interface ShimmerButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
  size?: ShimmerButtonSize;
  /** Seconds for the light to travel once around the border. */
  duration?: number;
  /** The two colours of the travelling light, as any CSS colour. Defaults to the theme's chart-1 and chart-2. */
  colors?: [string, string];
}

const sizes: Record<ShimmerButtonSize, string> = {
  sm: "h-8 gap-1.5 px-3.5 text-[13px] [&_svg]:size-3.5",
  md: "h-10 gap-2 px-5 text-sm [&_svg]:size-4",
  lg: "h-12 gap-2 px-7 text-[15px] [&_svg]:size-[18px]",
};

/**
 * A call-to-action button with a light that travels around its border and a sheen that sweeps the face on hover.
 * All movement is limited to users without a reduced-motion preference; everyone else sees a still highlight.
 */
export const ShimmerButton = forwardRef<HTMLButtonElement, ShimmerButtonProps>(function ShimmerButton(
  { size = "md", duration = 3, colors, type = "button", className, style, children, ...props },
  ref,
) {
  const vars = {
    "--shimmer-duration": `${duration}s`,
    "--shimmer-a": colors?.[0] ?? "var(--chart-1)",
    "--shimmer-b": colors?.[1] ?? "var(--chart-2)",
  } as CSSProperties;

  return (
    <button
      ref={ref}
      type={type}
      style={{ ...vars, ...style }}
      className={cn(
        "group/shimmer relative isolate inline-flex shrink-0 select-none overflow-hidden rounded-full p-[1.5px] font-medium",
        "bg-[color-mix(in_oklab,var(--primary)_78%,var(--background))]",
        "shadow-[0_10px_28px_-14px_var(--shimmer-a)] outline-hidden transition-[box-shadow,scale] duration-200",
        "hover:shadow-[0_12px_32px_-12px_var(--shimmer-a)] focus-visible:ring-[3px] focus-visible:ring-ring/50",
        "disabled:pointer-events-none disabled:opacity-50 motion-safe:active:scale-[0.98]",
        className,
      )}
      {...props}
    >
      {/* The travelling light: a conic gradient spinning behind the 1.5px gap around the face. */}
      <span
        aria-hidden="true"
        className={cn(
          "pointer-events-none absolute left-1/2 top-1/2 -z-10 aspect-square w-[max(220%,14rem)] -translate-x-1/2 -translate-y-1/2",
          "bg-[conic-gradient(from_200deg,transparent_0deg,transparent_230deg,var(--shimmer-a)_290deg,var(--shimmer-b)_335deg,transparent_360deg)]",
          "motion-safe:animate-spin motion-safe:[animation-duration:var(--shimmer-duration)] group-disabled/shimmer:animate-none",
        )}
      />
      <span
        className={cn(
          "relative inline-flex w-full items-center justify-center overflow-hidden whitespace-nowrap rounded-full",
          "bg-primary text-primary-foreground inset-shadow-[0_1px_0_rgb(255_255_255/0.16)] dark:inset-shadow-[0_1px_0_rgb(255_255_255/0.6)]",
          "[&_svg]:shrink-0",
          sizes[size],
        )}
      >
        {/* The sheen: a soft band that sweeps across the face on hover and keyboard focus. */}
        <span
          aria-hidden="true"
          className={cn(
            "pointer-events-none absolute inset-y-0 left-0 hidden w-2/5 -translate-x-[120%] -skew-x-[20deg] motion-safe:block",
            "bg-[linear-gradient(90deg,transparent,color-mix(in_oklab,var(--primary-foreground)_30%,transparent),transparent)]",
            "transition-[translate] duration-0 group-hover/shimmer:translate-x-[300%] group-hover/shimmer:duration-[900ms]",
            "group-focus-visible/shimmer:translate-x-[300%] group-focus-visible/shimmer:duration-[900ms] ease-out",
          )}
        />
        <span className="relative inline-flex items-center gap-[inherit]">{children}</span>
      </span>
    </button>
  );
});