Glow Card for Astro
An Astro container that paints a pointer glow on each child.
Installation
bunx --bun shadcn@latest add @gnostic/glow-cardnpx shadcn@latest add @gnostic/glow-cardpnpm dlx shadcn@latest add @gnostic/glow-cardComplete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/glow-card/glow-card.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import { mergeStyles } from "../../../lib/merge-styles.ts"
import { GLOW_CARD_DEFAULTS, finiteNumber, glowGradient } from "./glow-card.ts"
import type { GlowCardProps } from "./types.ts"
/**
* Container that paints a pointer glow on each direct child.
* Layout classes belong here. The script reads each child's box.
*/
type Props = HTMLAttributes<"div"> & GlowCardProps
const {
colors,
proximity = GLOW_CARD_DEFAULTS.proximity,
spread = GLOW_CARD_DEFAULTS.spread,
blur = GLOW_CARD_DEFAULTS.blur,
opacity = GLOW_CARD_DEFAULTS.opacity,
class: className,
style: styleProp,
...props
} = Astro.props
/** Props become inherited custom properties. Callers can override them with `style`. */
const style = mergeStyles(
{
"--glow": glowGradient(colors),
"--spread": String(finiteNumber(spread, GLOW_CARD_DEFAULTS.spread)),
"--blur": String(finiteNumber(blur, GLOW_CARD_DEFAULTS.blur)),
"--glow-proximity": String(
finiteNumber(proximity, GLOW_CARD_DEFAULTS.proximity),
),
"--glow-opacity": String(finiteNumber(opacity, GLOW_CARD_DEFAULTS.opacity)),
"--glow-border": "2px",
},
styleProp,
)
---
<div
data-slot="glow-card"
class={cn("relative isolate *:data-[slot=card]:overflow-visible", className)}
style={style}
{...props}
>
<!--
Cloned into each child. The child's own ::before and ::after stay free.
-->
<template data-slot="glow-card-template">
<span
aria-hidden="true"
data-slot="glow-card-glow"
class="pointer-events-none absolute inset-0 z-10 rounded-[inherit] before:pointer-events-none before:absolute before:inset-0 before:rounded-[inherit] before:bg-origin-border before:bg-fixed before:p-[calc(var(--glow-border)*2)] before:opacity-(--active) before:transition-opacity before:duration-1000 before:blur-[calc(var(--blur)*1px)] before:content-[''] before:[background-image:var(--glow)] after:pointer-events-none after:absolute after:inset-0 after:rounded-[inherit] after:bg-origin-border after:bg-fixed after:p-(--glow-border) after:opacity-(--active) after:brightness-150 after:transition-opacity after:duration-1000 after:content-[''] after:[background-image:var(--glow)] motion-reduce:before:transition-none motion-reduce:after:transition-none"
></span>
</template>
<slot />
</div>
<style is:global>
@property --start {
syntax: "<number>";
inherits: true;
initial-value: 0;
}
@property --spread {
syntax: "<number>";
inherits: true;
initial-value: 60;
}
@property --blur {
syntax: "<number>";
inherits: true;
initial-value: 20;
}
/* Zero specificity so a child's position or z-index utility still wins. */
:where([data-slot="glow-card"] > :not(template)) {
position: relative;
--active: var(--glow-opacity, 0);
--start: 0;
}
:where([data-slot="glow-card"] > :not(template)):is(:hover, :focus-within) {
z-index: 1;
}
/*
The conic mask is the arc. Excluding the content box leaves the border.
Intersect keeps that arc on the border. The soft arc is wider.
*/
:where([data-slot="glow-card-glow"])::before,
:where([data-slot="glow-card-glow"])::after {
content: "";
background-clip: border-box;
mask-repeat: no-repeat;
-webkit-mask-repeat: no-repeat;
}
:where([data-slot="glow-card-glow"])::before {
/* Wide arc. The bright point sits on --start. */
--glow-arc: calc(var(--spread) * 1deg);
mask-image:
conic-gradient(
from calc((var(--start) * 1deg) - var(--glow-arc)),
transparent 0deg,
#fff var(--glow-arc),
transparent calc(var(--glow-arc) * 2)
),
linear-gradient(#fff 0 0), linear-gradient(#fff 0 0);
mask-clip: border-box, content-box, border-box;
mask-composite: intersect, exclude;
-webkit-mask-image:
conic-gradient(
from calc((var(--start) * 1deg) - var(--glow-arc)),
transparent 0deg,
#fff var(--glow-arc),
transparent calc(var(--glow-arc) * 2)
),
linear-gradient(#fff 0 0), linear-gradient(#fff 0 0);
-webkit-mask-clip: border-box, content-box, border-box;
-webkit-mask-composite: source-in, xor;
}
:where([data-slot="glow-card-glow"])::after {
/* Narrow arc, centered on the same angle. */
--glow-arc: calc(var(--spread) * 0.25deg);
mask-image:
conic-gradient(
from calc((var(--start) * 1deg) - var(--glow-arc)),
transparent 0deg,
#fff var(--glow-arc),
transparent calc(var(--glow-arc) * 2)
),
linear-gradient(#fff 0 0), linear-gradient(#fff 0 0);
mask-clip: border-box, content-box, border-box;
mask-composite: intersect, exclude;
-webkit-mask-image:
conic-gradient(
from calc((var(--start) * 1deg) - var(--glow-arc)),
transparent 0deg,
#fff var(--glow-arc),
transparent calc(var(--glow-arc) * 2)
),
linear-gradient(#fff 0 0), linear-gradient(#fff 0 0);
-webkit-mask-clip: border-box, content-box, border-box;
-webkit-mask-composite: source-in, xor;
}
</style>
<script>
import { isNear, pointerAngle } from "./glow-card.ts"
/** Containers already bound on this page. A later page load skips them. */
const bound = new WeakSet<HTMLElement>()
/** Removes listeners before the next page replaces them. */
const stops: Array<() => void> = []
/** Elements that cannot contain the glow layer. */
const voidElements = new Set([
"AREA",
"BASE",
"BR",
"COL",
"EMBED",
"HR",
"IMG",
"INPUT",
"LINK",
"META",
"PARAM",
"SOURCE",
"TRACK",
"WBR",
])
/** Read one inline custom property. Blank text keeps the fallback. */
function readNumber(element: HTMLElement, name: string, fallback: number) {
const value = Number.parseFloat(element.style.getPropertyValue(name))
return Number.isFinite(value) ? value : fallback
}
/** Direct children that can show a glow. The template is not one of them. */
function glowChildren(container: HTMLElement) {
return [...container.children].filter(
(node): node is HTMLElement =>
node instanceof HTMLElement &&
node.tagName !== "TEMPLATE" &&
!voidElements.has(node.tagName),
)
}
/** Copy the glow layer into a child once. */
function ensureGlow(container: HTMLElement, child: HTMLElement) {
if (child.querySelector(":scope > [data-slot='glow-card-glow']")) return
const template = container.querySelector<HTMLTemplateElement>(
":scope > template[data-slot='glow-card-template']",
)
const layer = template?.content.firstElementChild
if (!layer) return
child.append(layer.cloneNode(true))
}
/** Aim every child from the pointer, and fade children outside proximity. */
function update(container: HTMLElement, event: PointerEvent) {
const proximity = readNumber(container, "--glow-proximity", 40)
const rest = readNumber(container, "--glow-opacity", 0)
for (const child of glowChildren(container)) {
ensureGlow(container, child)
const bounds = child.getBoundingClientRect()
const near = isNear(event.clientX, event.clientY, bounds, proximity)
child.style.setProperty("--active", near ? "1" : String(rest))
child.style.setProperty(
"--start",
String(pointerAngle(event.clientX, event.clientY, bounds)),
)
}
}
/** Return every child to the resting opacity. Keep the last angle. */
function rest(container: HTMLElement) {
const opacity = readNumber(container, "--glow-opacity", 0)
for (const child of glowChildren(container)) {
child.style.setProperty("--active", String(opacity))
}
}
/** Insert glow layers and follow the pointer for one container. */
function bind(container: HTMLElement) {
if (bound.has(container)) return
bound.add(container)
for (const child of glowChildren(container)) ensureGlow(container, child)
// The glow position follows the pointer. Reduced motion keeps the rest state.
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return
const onMove = (event: PointerEvent) => update(container, event)
const onLeave = () => rest(container)
container.addEventListener("pointermove", onMove)
container.addEventListener("pointerleave", onLeave)
stops.push(() => {
container.removeEventListener("pointermove", onMove)
container.removeEventListener("pointerleave", onLeave)
bound.delete(container)
})
}
/** Bind every glow container currently in the document. */
const start = () => {
document
.querySelectorAll<HTMLElement>("[data-slot='glow-card']")
.forEach(bind)
}
start()
document.addEventListener("astro:page-load", start)
document.addEventListener("astro:before-swap", () => {
for (const stop of stops) stop()
stops.length = 0
})
</script>
src/components/ui/glow-card/glow-card.ts
/** Box used to aim the glow. Values match `DOMRect` fields the script reads. */
export type GlowRect = {
left: number
top: number
width: number
height: number
}
/** Palette used when a caller does not pass colors. */
export const DEFAULT_GLOW_CARD_COLORS: readonly string[] = [
'var(--foreground)',
'var(--primary)',
]
/** Numeric defaults. The container writes these onto its style. */
export const GLOW_CARD_DEFAULTS = {
proximity: 40,
spread: 60,
blur: 20,
opacity: 0,
} as const
/**
* Keep a finite number. Any other value uses the fallback.
*/
export function finiteNumber(value: number | undefined, fallback: number) {
return typeof value === 'number' && Number.isFinite(value)
? value
: fallback
}
/**
* Build the conic gradient shared by every child.
* Fixed attachment lets each child show a different slice of this image.
*/
export function glowGradient(colors?: readonly string[]) {
const given = colors
?.map((color) => color.trim())
.filter((color) => color.length > 0)
const list = given && given.length > 0 ? given : DEFAULT_GLOW_CARD_COLORS
const stops = list.map((color, index) => {
const deg = (index / list.length) * 360
return `${color} ${deg}deg`
})
stops.push(`${list[0]} 360deg`)
return `conic-gradient(from 180deg at 50% 70%, ${stops.join(', ')})`
}
/**
* True when the pointer is inside the child box, plus `proximity` on each edge.
*/
export function isNear(
x: number,
y: number,
rect: GlowRect,
proximity: number,
) {
return (
x > rect.left - proximity &&
x < rect.left + rect.width + proximity &&
y > rect.top - proximity &&
y < rect.top + rect.height + proximity
)
}
/**
* Degrees for `--start`.
* The value follows the pointer around the child center.
*/
export function pointerAngle(x: number, y: number, rect: GlowRect) {
const centerX = rect.left + rect.width * 0.5
const centerY = rect.top + rect.height * 0.5
let angle = (Math.atan2(y - centerY, x - centerX) * 180) / Math.PI
if (angle < 0) angle += 360
return angle + 90
}
src/components/ui/glow-card/index.ts
export { default as GlowCard } from './glow-card.astro'
export type { GlowCardProps } from './types.ts'
src/components/ui/glow-card/types.ts
/**
* Props for a container that paints a pointer glow on its children.
*
* Div attributes such as `class` and `style` pass through.
* Put layout classes on this element. The glow reads each child's box.
*/
export interface GlowCardProps {
/**
* Colors in the shared conic glow.
* The first color also closes the loop.
* The default uses the theme foreground and primary.
*
* @default ["var(--foreground)", "var(--primary)"]
*/
colors?: string[]
/**
* Distance from a child edge, in pixels, that turns the glow on.
*
* @default 40
*/
proximity?: number
/**
* Width of the bright arc, in degrees.
*
* @default 60
*/
spread?: number
/**
* Blur of the soft arc, in pixels.
*
* @default 20
*/
blur?: number
/**
* Opacity of the glow when the pointer is outside `proximity`.
* `0` hides the glow until the pointer is near. `1` keeps it on.
*
* @default 0
*/
opacity?: number
}
Usage
---
import {
Card,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card"
import { GlowCard } from "@/components/ui/glow-card"
---
<GlowCard class="flex flex-wrap gap-8 p-8">
<Card>
<CardHeader>
<CardTitle>Atlas</CardTitle>
<CardDescription>Records the work in one place.</CardDescription>
</CardHeader>
</Card>
</GlowCard>
GlowCard is one container. Put each Card in the
default slot. Any other element works the same way. The component does not
set gap, direction, or alignment. Use your own layout classes. A row, a
column, and a grid all work.
The script reads each child’s position. When the pointer is within
proximity of that box, the arc on that child fades in and turns toward
the pointer.
class merges onto the container. colors, proximity, spread, blur,
and opacity set custom properties on the container style.
The root uses data-slot="glow-card". Each child receives a decorative
layer with data-slot="glow-card-glow".
Examples
Stack
The same glow follows children in a column. Give the container padding so the arc can turn on in the space around a child.
Custom colors
Pass CSS colors to replace the theme default. Use at least two colors.
Props
GlowCardProps
colors?string[]
Colors in the shared conic glow. The first color also closes the loop. The default uses the theme foreground and primary.
string[]["var(--foreground)", "var(--primary)"]proximity?number
Distance from a child edge, in pixels, that turns the glow on.
number40spread?number
Width of the bright arc, in degrees.
number60blur?number
Blur of the soft arc, in pixels.
number20opacity?number
Opacity of the glow when the pointer is outside `proximity`. `0` hides the glow until the pointer is near. `1` keeps it on.
number0Read the installed source for the complete prop types.
Slots
| Slot | Content |
|---|---|
| Default | The elements that receive the glow. |
Keyboard and behavior
The glow is decorative and is hidden from assistive technology. It follows the pointer. It does not move for keyboard focus.
prefers-reduced-motion: reduce skips pointer tracking and keeps the resting
opacity.
Theme and layout
Give each child a background, a radius, and padding. The arc uses that radius.
The soft arc can paint outside the child. A direct Card child gets
overflow-visible from the container, so the card recipe does not clip that
blur.
The child’s own ::before and ::after stay available. The glow uses a layer
inside the child.