Spotlight card
Card and list
On hover a soft light follows the pointer along the card border only, instead of a colored glow shadow around the whole card.
실제로 돌아가는 부품입니다. 눌러 보고 입력해 보세요.
내 앱에 넣기
아직 npm 패키지가 없어서 Taster 리포를 받아 그 안의 명령을 씁니다. 내 앱 폴더가 들어 있는 폴더에서 한 번만 받습니다.
git clone https://github.com/makim-414/taster.git && npm ci --prefix taster/packages/engine내 앱 폴더에서 이 부품을 넣습니다. 부품 파일과 토큰 파일이
src/components/taster에 들어갑니다.node ../taster/packages/engine/cli.mjs add blocks/spotlight-card --from https://taster-six.vercel.app --dir src/components/taster부품은 Tailwind 클래스로 그리고, Taster 프리셋이 그 클래스를 토큰에 연결합니다. Tailwind CSS 를 설치하고,
tokens.css를 앱 루트에서 한 번 불러온 뒤, 앱 루트에 이 설정을 둡니다. 프리셋이 없으면 빌드는 되어도 부품이 스타일 없이 나옵니다.// tailwind.config.cjs module.exports = { presets: [require('./src/components/taster/tailwind-preset.cjs')], content: ['./index.html', './src/**/*.{ts,tsx}'], };Tailwind v3 는 이 파일을 그대로 읽습니다. v4 는 Tailwind 를 불러오는
src/의 스타일시트 맨 위에 이 두 줄을 넣습니다.@import "tailwindcss"; @config "../tailwind.config.cjs";tokens.css는:root에--bg,--text,--border,--accent를 정의합니다. 내 앱의 스타일시트가 같은 이름을 뒤에서 다시 정의하면 그쪽 색이 이깁니다. 새 Vite 앱의 스타터 CSS 처럼 그런 변수가 있으면 지우거나 이름을 바꾸세요.
언제 쓰나요
- Spotlight border cardOn hover a soft light follows the pointer along the card border only, instead of a colored glow shadow around the whole card.
대신 없애 주는 AI 티
- Large colored glow shadow (COL-07)
정보
코드 보기
import * as React from 'react'
import { cn } from './_cn'
import { safeHref } from './_url'
import { useStyleOnce } from '../motion/_motion'
// Spotlight card: a hairline card whose 1px edge catches a light that follows the pointer. The inside stays flat, so
// text keeps its contrast. One shared pointer listener feeds every card, so the light also reaches a neighbor's edge
// when the pointer is near it. This is the component for patterns/spotlight-border-card.md.
// Written for this registry after a 21st.dev "Spotlight Card" the owner picked (jahed, EaseMize UI). Its listing says
// "License: unknown", so no code was taken; only the idea of one light across neighboring cards was kept. Left out on
// purpose: the filled radial glow, the outer blur, hue that shifts with position and touch-action none, which blocked
// scrolling when a drag started on a card.
export interface SpotlightCardProps extends React.HTMLAttributes<HTMLElement> {
/** Makes the whole card a link: http, https or mailto, or a relative path. Any other scheme leaves a plain card. */
href?: string
/** Light color, any CSS color or token. Default var(--text), a neutral sheen. */
color?: string
/** Light radius in px. Default 200. */
radius?: number
/** Light strength at its center, 0 to 1. Default 0.45. */
strength?: number
}
const CSS = `
.ds-spot{position:relative;display:block;background:var(--bg-elev);border:1px solid var(--border);border-radius:var(--r-card);color:inherit;text-decoration:none}
.ds-spot::before{content:"";position:absolute;inset:-1px;padding:1px;border-radius:inherit;pointer-events:none;opacity:0;
background:radial-gradient(var(--spot-r,200px) circle at var(--spot-x,50%) var(--spot-y,50%),color-mix(in oklch,var(--spot-c,var(--text)) calc(var(--spot-a,.45)*100%),transparent),transparent 70%);
-webkit-mask:linear-gradient(#000 0 0) content-box,linear-gradient(#000 0 0);-webkit-mask-composite:xor;mask:linear-gradient(#000 0 0) content-box exclude,linear-gradient(#000 0 0);
transition:opacity var(--dur-dropdown,200ms) var(--ease-out,ease-out)}
.ds-spot[data-lit]::before{opacity:1}
a.ds-spot:focus-visible{outline:2px solid var(--ring);outline-offset:2px}
@media (hover:none),(prefers-reduced-motion:reduce){.ds-spot::before{display:none}}
`
// One listener for all cards. Positions are read once per frame, and only for cards the light can reach.
const cards = new Map<HTMLElement, number>()
let frame = 0
let px = 0
let py = 0
let listening = false
function paint() {
frame = 0
cards.forEach((radius, el) => {
const r = el.getBoundingClientRect()
const dx = Math.max(r.left - px, 0, px - r.right)
const dy = Math.max(r.top - py, 0, py - r.bottom)
if (Math.hypot(dx, dy) > radius) {
el.removeAttribute('data-lit')
return
}
el.style.setProperty('--spot-x', `${(px - r.left).toFixed(1)}px`)
el.style.setProperty('--spot-y', `${(py - r.top).toFixed(1)}px`)
el.setAttribute('data-lit', '')
})
}
function onMove(e: PointerEvent) {
if (e.pointerType === 'touch') return
px = e.clientX
py = e.clientY
if (!frame) frame = requestAnimationFrame(paint)
}
function onLeave() {
cards.forEach((_, el) => el.removeAttribute('data-lit'))
}
function register(el: HTMLElement, radius: number) {
cards.set(el, radius)
if (!listening) {
listening = true
document.addEventListener('pointermove', onMove, { passive: true })
document.documentElement.addEventListener('pointerleave', onLeave)
}
return () => {
cards.delete(el)
if (cards.size) return
listening = false
document.removeEventListener('pointermove', onMove)
document.documentElement.removeEventListener('pointerleave', onLeave)
if (frame) cancelAnimationFrame(frame)
frame = 0
}
}
export function SpotlightCard({ href: rawHref, color, radius = 200, strength = 0.45, className, style, children, ...props }: SpotlightCardProps) {
useStyleOnce('ds-spotlight-card', CSS)
const ref = React.useRef<HTMLElement | null>(null)
React.useEffect(() => {
if (ref.current) return register(ref.current, radius)
}, [radius])
const vars = {
'--spot-r': `${radius}px`,
'--spot-a': Math.max(0, Math.min(1, strength)),
...(color ? { '--spot-c': color } : null),
...style,
} as React.CSSProperties
const setRef = (el: HTMLElement | null) => {
ref.current = el
}
const href = safeHref(rawHref)
return href ? (
<a ref={setRef} href={href} className={cn('ds-spot', className)} style={vars} {...(props as React.AnchorHTMLAttributes<HTMLAnchorElement>)}>
{children}
</a>
) : (
<div ref={setRef} className={cn('ds-spot', className)} style={vars} {...(props as React.HTMLAttributes<HTMLDivElement>)}>
{children}
</div>
)
}