SpecularSurface
Cursor-tracked specular highlight wrapper.
Live demo
01Card with default highlight
Move your pointer across the card. The highlight tracks the cursor. Click any button inside β clicks fall through the overlay to the children.
This card has a SpecularSurface wrapper. Buttons stay clickable.
Move your pointer across the cards below.Hero image β strong intensity
Larger size and strong intensity give a dramatic, almost cinematic feel. Useful for landing-page heroes where the surface should feel 'lit'.
Strong intensity, larger size. Try the entire stage.
Interactive playground
Drive the same SpecularSurface from sliders to see how intensity, size, and mode feel.
Move the pointer, change controls, watch the highlight respond.
Disabled β no overlay
When disabled is true, the wrapper stays in the DOM but renders no overlay. Useful for conditionally turning the effect off without unmounting the surface.
No highlight overlay. Static.
Implementation
02<script lang="ts">
import SpecularSurface from '$lib/components/SpecularSurface.svelte';
</script>
β
<SpecularSurface intensity="normal" mode="auto">
<div class="card">
<h2>Lit by the cursor</h2>
<p>Move your pointer across this card to see the highlight follow.</p>
</div>
</SpecularSurface>SpecularSurface renders an aria-hidden radial-gradient overlay that follows the pointer via reactive CSS custom properties. The overlay is pointer-events: none, so every click and selection falls through to the children β the wrapper is purely a visual effect, never an interaction barrier. prefers-reduced-transparency hides the overlay entirely; the wrapper stays accessible for users who prefer opaque surfaces.
Logic explainer
03What Does It Do? (Plain English)
A wrapper component that paints a soft, cursor-tracked specular highlight over its children. Drop any content inside β a card, an image, a hero β and a soft glow follows the pointer as it moves across the surface, giving the same "touched by light" feel as iOS 26 lock-screen controls or a magazine page turning under a lamp.
The highlight is purely decorative. Every click and selection falls through to the children underneath, so the surface stays fully interactive β your buttons, links, and form fields all work as if the wrapper weren't there.
Think of it as a thin sheet of glass laid over your content: it picks up the room lighting and bounces it back at the cursor's position, and as the cursor moves, the highlight moves with it. Take the cursor away, and the surface either fades to a static centre-glow (mode='always') or settles back to plain (mode='hover').
How It Works (Pseudo-Code)
state:
pointerX = 0.5 // 0..1, fraction across the wrapper
pointerY = 0.5 // 0..1, fraction down the wrapper
isInside = false // pointer is currently over the surface
resolvedMode = mode // 'auto' resolves to 'hover' or 'always' per device
wrapperEl = bound DOM ref
derive overlayVisible:
not disabled
and resolvedMode != 'never'
and (resolvedMode == 'always' or (resolvedMode == 'hover' and isInside))
derive intensityAlpha:
'subtle' β 0.6
'normal' β 1.0 (default)
'strong' β 1.4
derive overlayStyle:
"--mx: {pointerX * 100}%; --my: {pointerY * 100}%;"
"--ss-size: {size}px; --ss-angle: {angle}deg; --ss-alpha: {intensityAlpha};"
events:
on mount + matchMedia('(pointer: fine)').change:
resolvedMode = mql.matches ? 'hover' : 'always'
// (only when caller passed mode='auto')
on pointermove on wrapper:
rect = wrapper.getBoundingClientRect()
pointerX = (event.clientX - rect.left) / rect.width
pointerY = (event.clientY - rect.top) / rect.height
on pointerenter on wrapper:
isInside = true
on pointerleave on wrapper:
isInside = false
pointerX = 0.5
pointerY = 0.5 // snap back to centre so the highlight doesn't sit at the edgeMode Resolution: Why auto Does The Smart Thing
mode='auto' is the default and the most useful setting. It uses the pointer: fine media query to decide whether the user is on a precise pointer (mouse, pen, trackpad) or a coarse one (touch). On precise-pointer devices the highlight only shows when the cursor is inside the surface, because that's when "where the light falls" has meaning. On touch devices there's no cursor, so showing it only on hover would never trigger β the surface would look plain forever. Instead, auto resolves to always on touch, painting the highlight at the centre by default. Users still see "this surface is illuminated", they just can't move the highlight with their finger. (And on a phone, they don't expect to.)
If a caller pins mode='hover' or mode='always' directly, that wins. auto is the smart default for cases where the caller hasn't thought about it.
Reduced-Motion Is Boring On Purpose
The highlight follows the cursor frame-by-frame via reactive CSS custom properties. There is no transition on pointermove β the radial-gradient centre jumps to wherever the cursor is on every move event. That's the right choice: a transition would lag visibly behind the cursor, and that lag is motion sickness for some users. There are no other animations to gate.
The prefers-reduced-motion media query is therefore intentionally absent β the component is reduced-motion-friendly by construction.
prefers-reduced-transparency is different: it removes the overlay entirely (display: none). That setting is about the material, not the motion, and the right answer for "I want surfaces to look opaque, not glassy" is "don't paint the glass".
Distinct From
- CursorFollower / custom-cursor libraries replace the system cursor with a styled element. SpecularSurface keeps the system cursor and adds a decorative highlight; they coexist.
- Tilt3D rotates the surface based on cursor position. SpecularSurface keeps the surface flat and just shifts the light. The two compose β you can wrap a Tilt3D card in SpecularSurface for a layered "card lifts and light follows" feel.
- LiquidGlassCard / RefractiveCursorLens (Pair A's Batch 3 + Batch 4) refractions displace the backdrop. SpecularSurface just paints a static gradient β no SVG filters, no backdrop-filter, no WebGL fallback to think about. Cheaper, works everywhere, looks subtle.
State Flow Diagram
ββββββββββββββββ
β mounted β
β (mode=auto) β
ββββββββ¬ββββββββ
β
βββββββββββββββΌββββββββββββββ
βΌ βΌ βΌ
pointer:fine pointer:coarse pinned mode
β β β
βΌ βΌ βΌ
resolvedMode resolvedMode resolvedMode
= 'hover' = 'always' = mode
β β β
pointerenter β β β
βββββββββΌββββββββββββββΌββββββββββββββΌβββββββββββββββ
βΌ β β
isInside = true β β
β β β
βΌ βΌ βΌ
βββββββββββββββββββββββββββββββββββββββββ
β overlayVisible = true β
β (mode Γ inside Γ !disabled) β
βββββββββββββββββββββ¬ββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββ
β overlay rendered β
β radial-gradient β
β follows --mx/--myβ
βββββββββββββββββββββProps Reference
| Prop | Type | Default | Description |
|---|---|---|---|
intensity |
'subtle' | 'normal' | 'strong' |
'normal' |
Strength of the highlight gradient. Strong is dramatic; subtle is a glancing shimmer. |
angle |
number (deg) |
135 |
Hint for the highlight angle. 0 is topβbottom, 90 is leftβright. The radial gradient itself is symmetric; this is exposed for future anisotropic lighting. |
size |
number (px) |
400 |
Diameter of the radial gradient in pixels. Larger = softer and more spread out. |
tint |
string |
'rgba(255, 255, 255, 0.18)' |
Colour of the highlight. Defaults to a soft white that reads on both light and dark backgrounds. |
mode |
'auto' | 'always' | 'hover' | 'never' |
'auto' |
When the highlight shows. Auto resolves per device. |
disabled |
boolean |
false |
Disable without unmounting. |
children |
Snippet |
required | Content to wrap. |
class |
string |
'' |
Extra CSS class on the wrapper, appended after the layout class. |
Edge Cases
- Wrapper with no measurable size (
getBoundingClientRectreturns 0Γ0):pointermovebails out before dividing by zero; the highlight stays at the centre until the surface is laid out. - Pointer leaves via the surface edge (vs the children inside it): the
pointerleaveevent bubbles from the wrapper, not the children, so the highlight still snaps back to centre correctly. - Touch on mode='hover': the highlight never shows because
pointerenterfires on touch butpointermovedoesn't deliver fractional coordinates the way the component expects. Use mode='always' for touch-only surfaces. mode='auto'on a hybrid device (Surface Pro with touch + pen): the media query resolves to whichever pointer type was last active. The change listener re-runs the resolution when the user switches input methods.- Wrapper nested inside a transformed ancestor: the radial gradient is in the wrapper's own coordinate space, so transforms on the wrapper or its ancestors are honoured β the highlight moves with the surface.
Dependencies
Zero. Pure Svelte 5 runes + a CSS radial gradient.
File Structure
src/lib/components/
SpecularSurface.svelte # the component
SpecularSurface.test.ts # render, mode resolution, pointer events, source checks
SpecularSurfaceTestHarness.test.svelte # internal harness for the test
SpecularSurface.md # this fileThe catalog entry and the /specularsurface demo route live next to the other components in src/lib/componentCatalog.ts and src/routes/specularsurface/+page.svelte.
API
04| Prop | Type | Default | Description |
|---|---|---|---|
intensity | 'subtle' | 'normal' | 'strong' | 'normal' | Strength of the highlight gradient. |
angle | number (deg) | 135 | Hint angle for the highlight; 0 = topβbottom, 90 = leftβright. |
size | number (px) | 400 | Diameter of the radial gradient in pixels. |
tint | string | 'rgba(255, 255, 255, 0.18)' | Highlight tint colour. |
mode | 'auto' | 'always' | 'hover' | 'never' | 'auto' | When the highlight shows. Auto resolves per device. |
disabled | boolean | false | Disable the overlay without unmounting. |
children | Snippet | required | Content to wrap. |
class | string | '' | Extra CSS class on the wrapper. |