Border beam
The working beam (border-beam) for a wait over three seconds on the element doing the work.
Turn it on for a wait of more than three seconds, on the element doing the work, and never beside a second effect on that element. It does not follow reduced motion by itself: gate active on useReducedMotion(). Pass theme from the app's theme; auto reads the system setting, not the .dark class.
On a working element
March report
Ready to build
"use client"
import { useEffect, useState, useSyncExternalStore } from "react"
import { useReducedMotion } from "motion/react"
import { BorderBeam } from "@/components/ui/border-beam"
import { Button } from "@/components/ui/button"
// The beam takes its theme as a prop: follow the `.dark` class the app sets.
function watchTheme(notify: () => void) {
const observer = new MutationObserver(notify)
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] })
return () => observer.disconnect()
}
const isDark = () => document.documentElement.classList.contains("dark")
export default function BorderBeamOnAWorkingElement() {
const [working, setWorking] = useState(false)
const reduce = useReducedMotion()
const dark = useSyncExternalStore(watchTheme, isDark, () => false)
useEffect(() => {
if (!working) return
const timer = setTimeout(() => setWorking(false), 5000)
return () => clearTimeout(timer)
}, [working])
return (
<div className="flex flex-col items-center gap-4">
<BorderBeam active={working && !reduce} theme={dark ? "dark" : "light"}>
<div className="w-64 rounded-xl border bg-card p-4">
<p className="font-medium">March report</p>
<p role="status" className="text-sm text-muted-foreground">
{working ? "Building the report" : "Ready to build"}
</p>
</div>
</BorderBeam>
<Button variant="outline" onClick={() => setWorking(!working)}>
{working ? "Cancel" : "Build the report"}
</Button>
</div>
)
}
Types
md
sm
line
pulse-inner
pulse-outside
"use client"
import { useEffect, useState, useSyncExternalStore } from "react"
import { useReducedMotion } from "motion/react"
import { BorderBeam } from "@/components/ui/border-beam"
import { Button } from "@/components/ui/button"
const sizes = ["md", "sm", "line", "pulse-inner", "pulse-outside"] as const
// The beam takes its theme as a prop: follow the `.dark` class the app sets.
function watchTheme(notify: () => void) {
const observer = new MutationObserver(notify)
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] })
return () => observer.disconnect()
}
const isDark = () => document.documentElement.classList.contains("dark")
export default function BorderBeamTypes() {
const [playing, setPlaying] = useState(false)
const reduce = useReducedMotion()
const dark = useSyncExternalStore(watchTheme, isDark, () => false)
useEffect(() => {
if (!playing) return
const timer = setTimeout(() => setPlaying(false), 5000)
return () => clearTimeout(timer)
}, [playing])
return (
<div className="flex w-full max-w-lg flex-col items-center gap-6">
<div className="grid w-full grid-cols-2 gap-4 sm:grid-cols-3">
{sizes.map((size) => (
<BorderBeam key={size} size={size} active={playing && !reduce} theme={dark ? "dark" : "light"}>
<div className="grid h-16 place-items-center rounded-xl border bg-card font-mono text-sm">{size}</div>
</BorderBeam>
))}
</div>
<Button variant="outline" onClick={() => setPlaying(!playing)}>
{!playing ? "Play for five seconds" : reduce ? "Off with reduced motion" : "Stop"}
</Button>
</div>
)
}
Colours
colorful
mono
ocean
sunset
forest
candy
ice
gold
"use client"
import { useEffect, useState, useSyncExternalStore } from "react"
import { useReducedMotion } from "motion/react"
import { BorderBeam } from "@/components/ui/border-beam"
import { Button } from "@/components/ui/button"
const variants = ["colorful", "mono", "ocean", "sunset", "forest", "candy", "ice", "gold"] as const
// The beam takes its theme as a prop: follow the `.dark` class the app sets.
function watchTheme(notify: () => void) {
const observer = new MutationObserver(notify)
observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"] })
return () => observer.disconnect()
}
const isDark = () => document.documentElement.classList.contains("dark")
export default function BorderBeamColours() {
const [playing, setPlaying] = useState(false)
const reduce = useReducedMotion()
const dark = useSyncExternalStore(watchTheme, isDark, () => false)
useEffect(() => {
if (!playing) return
const timer = setTimeout(() => setPlaying(false), 5000)
return () => clearTimeout(timer)
}, [playing])
return (
<div className="flex w-full max-w-lg flex-col items-center gap-6">
<div className="grid w-full grid-cols-2 gap-4 sm:grid-cols-4">
{variants.map((variant) => (
<BorderBeam key={variant} colorVariant={variant} active={playing && !reduce} theme={dark ? "dark" : "light"}>
<div className="grid h-16 place-items-center rounded-xl border bg-card font-mono text-sm">{variant}</div>
</BorderBeam>
))}
</div>
<Button variant="outline" onClick={() => setPlaying(!playing)}>
{!playing ? "Play for five seconds" : reduce ? "Off with reduced motion" : "Stop"}
</Button>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/border-beambunx --bun shadcn@latest add @khaeli/border-beamInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- npm packages
- border-beam ^1.4.1
Accessible name
- The beam is decoration: it adds no role and no name, and ignores the pointer. Say what is happening in text, in an element with
role="status".
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/border-beam.json