Skip to content
Primitives

Gradient Border

A masked gradient hairline around any content, with an optional rotating sweep.

Motionconic sweep rotates on a linear loop; falls back to a static ring under reduced motion

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { GradientBorder } from "kineto/gradient-border";

Preview

Animated

A sweep travels the hairline.

Neutral

A hairline that fades as it falls.

tsx
"use client";

import * as React from "react";
import { motion, useReducedMotion } from "motion/react";

import { cn } from "@/lib/cn";

/* A gradient hairline around arbitrary content — the thing `border-color` can't express.
 *
 * The ring is a masked overlay, not a border: the layer is padded by `width`, filled with a
 * gradient, then masked so only that padding survives (`content-box` XOR full box). That keeps
 * the border on top of any background the child paints, and lets the corner radius come from
 * `rounded-*` on the wrapper via `rounded-[inherit]`.
 *
 * `animated` rotates a conic sweep behind the same mask. Rotation is a transform on an oversized
 * child rather than an animated `--angle`, because interpolating a custom property needs
 * `@property` registration and that can't ride along in a copy-pasted component file.
 *
 * Needs kineto's stylesheet for the `bg-gradient-*` utilities (`@import "kineto/theme.css"`). */

export interface GradientBorderProps extends React.HTMLAttributes<HTMLDivElement> {
  /** `brand` uses the accent ramp; `neutral` uses the hairline that fades as it falls. */
  tone?: "brand" | "neutral";
  /** Ring thickness in px. */
  width?: number;
  /** Rotate a conic sweep around the ring. Ignored under `prefers-reduced-motion`. */
  animated?: boolean;
  /** Seconds for one full revolution. */
  duration?: number;
}

/* Cut the middle out of the padded layer so only the ring paints. Both spellings ship: Safari
   still needs the -webkit- pair. */
const RING_MASK = {
  WebkitMask: "linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)",
  WebkitMaskComposite: "xor",
  mask: "linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)",
  maskComposite: "exclude",
} satisfies React.CSSProperties;

const GradientBorder = React.forwardRef<HTMLDivElement, GradientBorderProps>(
  (
    {
      className,
      tone = "brand",
      width = 1,
      animated = false,
      duration = 6,
      children,
      ...props
    },
    ref
  ) => {
    const reduced = useReducedMotion();
    const spinning = animated && !reduced;

    return (
      <div
        ref={ref}
        className={cn("relative isolate rounded-xl", className)}
        {...props}
      >
        <span
          aria-hidden
          className={cn(
            "pointer-events-none absolute inset-0 overflow-hidden rounded-[inherit]",
            // While spinning, the base drops to the neutral hairline so the conic reads as a
            // highlight travelling around a real border, not as a wheel of color.
            spinning || tone === "neutral"
              ? "bg-gradient-hairline"
              : "bg-gradient-brand"
          )}
          style={{ padding: width, ...RING_MASK }}
        >
          {spinning ? (
            <motion.span
              // 200% on both axes so the rotating box still covers the ring at 45° for any
              // reasonable aspect ratio.
              className="absolute left-1/2 top-1/2 block h-[200%] w-[200%]"
              style={{
                // Transparent for most of the sweep so the ring reads as a travelling
                // highlight rather than a spinning wheel of color.
                backgroundImage:
                  "conic-gradient(from 0deg, transparent 0deg, hsl(var(--gradient-from, var(--brand))) 90deg, hsl(var(--gradient-to, var(--chart-3))) 160deg, transparent 260deg)",
                x: "-50%",
                y: "-50%",
              }}
              animate={{ rotate: 360 }}
              transition={{ duration, ease: "linear", repeat: Infinity }}
            />
          ) : null}
        </span>
        {children}
      </div>
    );
  }
);
GradientBorder.displayName = "GradientBorder";

export { GradientBorder };

src/components/ui/gradient-border.tsx

Props

PropTypeDefaultDescription
tone"brand" | "neutral""brand"Accent ramp or the fading neutral hairline.
widthnumber1Ring thickness in px.
animatedbooleanfalseRotate a sweep around the ring.
durationnumber6Seconds per revolution.