border-beam-react
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseborder-beam-react
border-beam-react
Skill by ara.so — Daily 2026 Skills collection.
border-beam@property由ara.so提供的Skill — 2026每日技能合集。
border-beam@propertyInstallation
安装
bash
npm install border-beamRequirements:
- React 18+
- Modern browser: Chrome 85+, Safari 15.4+, Firefox 128+
bash
npm install border-beam要求:
- React 18及以上版本
- 现代浏览器:Chrome 85+、Safari 15.4+、Firefox 128+
Basic Usage
基本用法
tsx
import { BorderBeam } from 'border-beam';
function App() {
return (
<BorderBeam>
<div style={{ padding: 32, borderRadius: 16, background: '#1d1d1d' }}>
Your content here
</div>
</BorderBeam>
);
}BorderBeamborder-radiuspointer-events: nonetsx
import { BorderBeam } from 'border-beam';
function App() {
return (
<BorderBeam>
<div style={{ padding: 32, borderRadius: 16, background: '#1d1d1d' }}>
你的内容放在这里
</div>
</BorderBeam>
);
}BorderBeamborder-radiuspointer-events: noneSize Presets
尺寸预设
tsx
// Full border glow (default) — for cards, panels
<BorderBeam size="md">
<Card />
</BorderBeam>
// Compact glow — for small elements like icon buttons
<BorderBeam size="sm">
<IconButton />
</BorderBeam>
// Bottom-only traveling glow — ideal for search bars, inputs
<BorderBeam size="line">
<SearchBar />
</BorderBeam>tsx
// 完整边框光晕(默认)——适用于卡片、面板
<BorderBeam size="md">
<Card />
</BorderBeam>
// 紧凑光晕——适用于图标按钮等小型元素
<BorderBeam size="sm">
<IconButton />
</BorderBeam>
// 仅底部移动光晕——非常适合搜索栏、输入框
<BorderBeam size="line">
<SearchBar />
</BorderBeam>Color Variants
颜色变体
tsx
<BorderBeam colorVariant="colorful" /> {/* Rainbow spectrum (default), animates hue */}
<BorderBeam colorVariant="mono" /> {/* Grayscale, no hue animation */}
<BorderBeam colorVariant="ocean" /> {/* Blue-purple tones */}
<BorderBeam colorVariant="sunset" /> {/* Orange-yellow-red tones */}All variants except animate through a hue-shift cycle by default.
monotsx
<BorderBeam colorVariant="colorful" /> {/* 彩虹光谱(默认),动态变换色相 */}
<BorderBeam colorVariant="mono" /> {/* 灰度模式,无色相动画 */}
<BorderBeam colorVariant="ocean" /> {/* 蓝紫色调 */}
<BorderBeam colorVariant="sunset" /> {/* 橙黄红色调 */}除外,所有变体默认都会进行色相偏移循环动画。
monoTheme (Dark / Light / Auto)
主题(深色/浅色/自动)
tsx
<BorderBeam theme="dark" /> {/* Default — for dark backgrounds */}
<BorderBeam theme="light" /> {/* For light backgrounds */}
<BorderBeam theme="auto" /> {/* Detects system prefers-color-scheme */}tsx
<BorderBeam theme="dark" /> {/* 默认——适用于深色背景 */}
<BorderBeam theme="light" /> {/* 适用于浅色背景 */}
<BorderBeam theme="auto" /> {/* 自动检测系统的prefers-color-scheme设置 */}Controlling Intensity
强度控制
tsx
// strength: 0 (invisible) to 1 (full, default) — only affects beam layers
<BorderBeam strength={0.6}>
<Card />
</BorderBeam>
// brightness and saturation multipliers
<BorderBeam brightness={1.5} saturation={1.4}>
<Card />
</BorderBeam>tsx
// strength: 0(不可见)至1(完全显示,默认值)——仅影响光束层
<BorderBeam strength={0.6}>
<Card />
</BorderBeam>
// 亮度和饱和度乘数
<BorderBeam brightness={1.5} saturation={1.4}>
<Card />
</BorderBeam>Play / Pause with Callbacks
通过回调控制播放/暂停
tsx
import { useState } from 'react';
import { BorderBeam } from 'border-beam';
function AnimatedCard() {
const [active, setActive] = useState(true);
return (
<>
<BorderBeam
active={active}
onActivate={() => console.log('Beam faded in')}
onDeactivate={() => console.log('Beam faded out')}
>
<div style={{ padding: 32, borderRadius: 16, background: '#111' }}>
Hover-activated beam
</div>
</BorderBeam>
<button onClick={() => setActive(a => !a)}>Toggle Beam</button>
</>
);
}tsx
import { useState } from 'react';
import { BorderBeam } from 'border-beam';
function AnimatedCard() {
const [active, setActive] = useState(true);
return (
<>
<BorderBeam
active={active}
onActivate={() => console.log('光束淡入完成')}
onDeactivate={() => console.log('光束淡出完成')}
>
<div style={{ padding: 32, borderRadius: 16, background: '#111' }}>
悬停触发光束
</div>
</BorderBeam>
<button onClick={() => setActive(a => !a)}>切换光束</button>
</>
);
}Custom Duration and Hue Range
自定义时长与色相范围
tsx
<BorderBeam
duration={3.5} // Animation cycle in seconds (default: 1.96 for md, 2.4 for line)
hueRange={60} // Hue rotation range in degrees (default: 30)
staticColors={true} // Disable hue-shift entirely
>
<Card />
</BorderBeam>tsx
<BorderBeam
duration={3.5} // 动画周期(秒),默认值:md尺寸为1.96,line尺寸为2.4
hueRange={60} // 色相旋转范围(度),默认值:30
staticColors={true} // 完全禁用色相偏移
>
<Card />
</BorderBeam>Custom Border Radius
自定义边框圆角
If auto-detection doesn't work (e.g., the radius is set via a CSS class), override it:
tsx
<BorderBeam borderRadius={24}>
<div className="my-card">Content</div>
</BorderBeam>如果自动检测失效(例如圆角通过CSS类设置),可以手动指定:
tsx
<BorderBeam borderRadius={24}>
<div className="my-card">内容</div>
</BorderBeam>Forwarded HTML Attributes
透传HTML属性
The wrapper forwards all standard attributes:
<div>HTMLDivElementtsx
<BorderBeam
className="my-wrapper"
style={{ display: 'inline-block' }}
data-testid="beam-card"
>
<Card />
</BorderBeam>包裹用的会透传所有标准属性:
<div>HTMLDivElementtsx
<BorderBeam
className="my-wrapper"
style={{ display: 'inline-block' }}
data-testid="beam-card"
>
<Card />
</BorderBeam>Common Patterns
常见用法示例
Hover-activated beam
悬停触发光束
tsx
import { useState } from 'react';
import { BorderBeam } from 'border-beam';
function HoverCard() {
const [hovered, setHovered] = useState(false);
return (
<BorderBeam active={hovered} strength={0.85} colorVariant="ocean">
<div
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
style={{ padding: 24, borderRadius: 12, background: '#0f0f0f' }}
>
Hover me
</div>
</BorderBeam>
);
}tsx
import { useState } from 'react';
import { BorderBeam } from 'border-beam';
function HoverCard() {
const [hovered, setHovered] = useState(false);
return (
<BorderBeam active={hovered} strength={0.85} colorVariant="ocean">
<div
onMouseEnter={() => setHovered(true)}
onMouseLeave={() => setHovered(false)}
style={{ padding: 24, borderRadius: 12, background: '#0f0f0f' }}
>
悬停我
</div>
</BorderBeam>
);
}Search bar with line beam
带线条光束的搜索栏
tsx
import { BorderBeam } from 'border-beam';
function SearchInput() {
return (
<BorderBeam size="line" colorVariant="sunset" theme="dark">
<input
type="text"
placeholder="Search..."
style={{
padding: '12px 16px',
borderRadius: 8,
background: '#1a1a1a',
border: '1px solid #333',
color: '#fff',
width: 320,
}}
/>
</BorderBeam>
);
}tsx
import { BorderBeam } from 'border-beam';
function SearchInput() {
return (
<BorderBeam size="line" colorVariant="sunset" theme="dark">
<input
type="text"
placeholder="搜索..."
style={{
padding: '12px 16px',
borderRadius: 8,
background: '#1a1a1a',
border: '1px solid #333',
color: '#fff',
width: 320,
}}
/>
</BorderBeam>
);
}Light theme card
浅色主题卡片
tsx
import { BorderBeam } from 'border-beam';
function LightCard() {
return (
<BorderBeam theme="light" colorVariant="colorful" strength={0.8}>
<div style={{ padding: 32, borderRadius: 16, background: '#f5f5f5' }}>
Light mode card
</div>
</BorderBeam>
);
}tsx
import { BorderBeam } from 'border-beam';
function LightCard() {
return (
<BorderBeam theme="light" colorVariant="colorful" strength={0.8}>
<div style={{ padding: 32, borderRadius: 16, background: '#f5f5f5' }}>
浅色模式卡片
</div>
</BorderBeam>
);
}Reduced motion accessibility
减少动画的无障碍适配
The component itself does not automatically respond to . Handle it in the consumer:
prefers-reduced-motiontsx
import { useReducedMotion } from 'framer-motion'; // or your own hook
import { BorderBeam } from 'border-beam';
function AccessibleCard() {
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
return (
<BorderBeam active={!reduceMotion}>
<Card />
</BorderBeam>
);
}组件本身不会自动响应,需要在业务代码中处理:
prefers-reduced-motiontsx
import { useReducedMotion } from 'framer-motion'; // 或自定义钩子
import { BorderBeam } from 'border-beam';
function AccessibleCard() {
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
return (
<BorderBeam active={!reduceMotion}>
<Card />
</BorderBeam>
);
}Full Props Reference
完整属性参考
| Prop | Type | Default | Description |
|---|---|---|---|
| | — | Content to wrap |
| | | Size/type preset |
| | | Color palette |
| | | Background adaptation |
| | | Beam opacity 0–1, does not affect children |
| | | Animation cycle in seconds |
| | | Play/pause with fade transition |
| | auto-detected | Override border radius in px |
| | | Glow brightness multiplier |
| | | Glow saturation multiplier |
| | | Hue rotation range in degrees |
| | | Disable hue-shift animation |
| | — | Class on wrapper div |
| | — | Inline styles on wrapper div |
| | — | Called when fade-in completes |
| | — | Called when fade-out completes |
| 属性 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| | — | 要包裹的内容 |
| | | 尺寸/类型预设 |
| | | 配色方案 |
| | | 背景适配模式 |
| | | 光束透明度(0–1),不影响子元素 |
| | | 动画周期(秒) |
| | | 控制播放/暂停,带淡入淡出过渡 |
| | 自动检测 | 手动指定边框圆角(像素) |
| | | 光晕亮度乘数 |
| | | 光晕饱和度乘数 |
| | | 色相旋转范围(度) |
| | | 禁用色相偏移动画 |
| | — | 包裹div的类名 |
| | — | 包裹div的内联样式 |
| | — | 淡入完成时触发的回调 |
| | — | 淡出完成时触发的回调 |
How It Works
工作原理
BorderBeam<div>pointer-events: none- — beam stroke (conic gradient masked to the border edge)
::after - — inner glow layer
::before - — outer bloom/glow child
[data-beam-bloom]<div>
Animations use CSS for smooth, GPU-accelerated hue cycling.
@propertyBorderBeam<div>pointer-events: none- — 光束描边(通过遮罩应用于边框边缘的圆锥渐变)
::after - — 内光晕层
::before - — 外光晕/发光子
[data-beam-bloom]<div>
动画使用CSS 实现流畅的GPU加速色相循环。
@propertyTroubleshooting
常见问题排查
Beam not visible:
- Ensure the child element has a visible and
backgroundborder-radius - Check is not
strength0 - Verify is
activetrue
Border radius not matching:
- Pass prop explicitly if the radius comes from a CSS class rather than inline style
borderRadius
Effect covers content / blocks clicks:
- All layers are by default — if this is happening, check for CSS conflicts in your wrapper
pointer-events: none
Animation not working in browser:
- Requires CSS support: Chrome 85+, Safari 15.4+, Firefox 128+. Check caniuse.com/@property
@property
SSR / Next.js:
- The component uses DOM APIs for border-radius detection; wrap in a client component () in Next.js App Router
'use client'
光束不可见:
- 确保子元素设置了可见的和
backgroundborder-radius - 检查值不为
strength0 - 确认值为
activetrue
边框圆角不匹配:
- 如果圆角通过CSS类而非内联样式设置,需手动传入属性
borderRadius
效果覆盖内容/阻挡点击:
- 所有层默认都设置,若出现此问题请检查包裹元素的CSS冲突
pointer-events: none
浏览器中动画不生效:
- 需要支持CSS :Chrome 85+、Safari 15.4+、Firefox 128+。可查看caniuse.com/@property
@property
SSR / Next.js:
- 组件使用DOM API检测边框圆角,在Next.js App Router中需包裹为客户端组件(添加)
'use client'