XrayText

Mask-image punch-through text reveal with hover, click, and always modes.

Live demo

01

Hover reveal β€” dots pattern

Hover or focus the text. The mask punches through to reveal "HELLO" beneath the "CLASSIFIED" surface.

CLASSIFIED HELLO CLASSIFIED hidden, hover or focus to reveal
Hover or focus to reveal. The SR text reads: "CLASSIFIED hidden, hover or focus to reveal".

Click reveal β€” lines pattern

Click the text (or press Enter / Space when focused) to toggle the reveal. Useful for interactive labels.

Click to toggle. Real <button> with Enter / Space support.

Always-open decorative β€” cross pattern, dense

Static decorative use. No trigger needed; the pattern is always visible. Useful for masthead-style typographic effects.

TFE / SVELTE TFE / SVELTE

Interactive playground

Drive the same XrayText from controls to see how pattern, density, and reveal mode feel.

PLAYGROUND WELCOME PLAYGROUND hidden, hover or focus to reveal

Implementation

02
XrayText.svelte
<script lang="ts">
  import XrayText from '$lib/components/XrayText.svelte';
</script>
​
<XrayText
  text="CLASSIFIED"
  revealText="HELLO"
  pattern="dots"
  density="normal"
  revealOn="hover"
/>

XrayText renders a masked text layer (background-clip: text + -webkit-mask-image from an inline SVG data URI) and an optional reveal layer that becomes visible on hover, focus, click, or always. The visible text is aria-hidden with a sibling visually-hidden SR text span carrying the accessible label β€” keyboard users get the same triggers as pointer users, and click mode is a real <button> with Enter/Space toggling.

Logic explainer

03

What Does It Do? (Plain English)

Renders text with a punch-through mask pattern β€” dots, lines, cross-hatches, or diagonal stripes β€” that either shows a decorative pattern permanently (static mode) or reveals a second text layer behind it on hover, click, or always.

Think of it as text viewed through a stencil: the visible characters are made of the cut-out pattern, and what shows through the cuts is either the underlying surface (decorative mode) or a second piece of text (reveal mode). It's the same trick newspaper mastheads and sci-fi interfaces have used for decades β€” mask-image lets us do it on the web in pure CSS, GPU-accelerated, with no SVG filters or canvas.

The accessibility contract is solid: the visible text is aria-hidden="true", and a sibling visually-hidden span carries the full readable text for screen readers. When the reveal layer activates, the accessible label updates to announce the revealed text. Keyboard users get the same reveal triggers as pointer users β€” focus opens the reveal in hover mode; Enter / Space toggles it in click mode.

How It Works (Pseudo-Code)

state:
  isOpen     = false       // pointer/focus/click active
  manualOpen = bindable    // external control for 'manual' revealOn
  hostEl     = bound DOM ref

derive resolvedOpen:
  revealOn == 'always'  β†’ true
  revealOn == 'manual'   β†’ manualOpen
  else                   β†’ isOpen

derive maskUri:
  build SVG data URI for (pattern, density)
  tile size = sparse=24, normal=16, dense=10
  pattern geometry:
    dots:    filled circle at (tile/2, tile/2), r=tile*0.4
    lines:   horizontal stroke at mid-height
    cross:   horizontal + vertical strokes (grid)
    diagonal: two strokes β†˜ + β†—

events:
  on pointerenter / focus:
    if revealOn == 'hover': isOpen = true
  on pointerleave / blur:
    if revealOn == 'hover': isOpen = false
  on click:
    if revealOn == 'click': isOpen = !isOpen
  on keydown Enter / Space (click mode):
    isOpen = !isOpen

Mask Math: Why Tile Size Is The Density Knob

The mask image is a small SVG that tiles across the text. Tile size in pixels is the density knob β€” larger tiles = fewer repeats per character = sparser punch-through, smaller tiles = more repeats = denser. Going from 24px β†’ 16px β†’ 10px tiles makes the pattern noticeably tighter without changing the SVG geometry or the rendered glyph count.

For the dots pattern, the circle radius scales with tile size (r = tile * 0.4 for normal density, Β±0.05 for sparse/dense). For line patterns, stroke-width scales (2px β†’ 2.5px β†’ 3px from dense β†’ sparse). The geometry stays simple so the SVG stays small β€” even the largest tile (24px) renders an SVG under 200 bytes encoded.

Click Mode Is A Real Button

When revealOn="click", the host element is a real <button type="button">. The reveal toggle responds to mouse click AND to Enter / Space keys. The button has no default border or background β€” it inherits its parent's typography. The aria-label describes the current state ("CLICK ME hidden, hover or focus to reveal" vs "CLICK ME revealed: WORLD") so screen readers get the same context pointer users get.

For static decorative use (no revealText), the host is a non-interactive <span> with no role and no tabindex β€” just a typographic effect. If the consumer wants the masked text to be selectable / copyable, they wrap it in their own <p> / <h2> and use XrayText as a child.

Reduced-Motion Is Boring On Purpose

The reveal layer fades in via opacity transition (320ms cubic-bezier). Under prefers-reduced-motion: reduce, the transition is removed entirely β€” the reveal snaps in/out instantly. There's no animation choreography beyond the opacity fade; the pattern itself doesn't move, so there's nothing else to gate.

Distinct From

  • GlitchText (existing in catalog): RGB channel-split + clip-band animation. XrayText is a static punch-through, no animation by default.
  • ScrambledText: glyph-shuffle that resolves into readable copy. Different intent entirely.
  • ShinyText: shimmer sweep on the text surface. XrayText punches through; ShinyText paints over.
  • Backdrop-filter: backdrop-filter refracts the content behind the element. XrayText punches through the text glyphs themselves; the surface behind is irrelevant.

State Flow Diagram

                  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                  β”‚  mounted       β”‚
                  β”‚  isOpen=false  β”‚
                  β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό                 β–Ό                 β–Ό
   pointerenter      focus          click / Enter
   (hover mode)      (hover mode)   (click mode)
        β”‚                 β”‚                 β”‚
        β–Ό                 β–Ό                 β–Ό
   isOpen=true       isOpen=true       isOpen = !isOpen
        β”‚                 β”‚                 β”‚
        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚  resolvedOpen derived   β”‚
              β”‚  from (mode Γ— isOpen)   β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                           β”‚
                           β–Ό
                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                β”‚  reveal layer        β”‚
                β”‚  opacity = 1 if open β”‚
                β”‚  opacity = 0 if not  β”‚
                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Props Reference

Prop Type Default Description
text string required The visible (masked) text.
revealText string undefined Optional second text layer revealed on trigger.
pattern 'dots' | 'lines' | 'cross' | 'diagonal' 'dots' Mask pattern shape.
density 'sparse' | 'normal' | 'dense' 'normal' Mask density (smaller tile = denser).
revealOn 'hover' | 'click' | 'always' | 'manual' 'hover' Trigger mode.
manualOpen boolean (bindable) false When revealOn='manual', externally controls open state.
revealColor string 'rgba(15, 23, 42, 0.92)' Reveal layer colour.
ariaLabel string undefined Accessible name override. Default composes from text + revealText.
class string '' Extra CSS class on the host element.
children Snippet undefined Optional content rendered after the masked text (decorative mode only).

Edge Cases

  • Very long text (multi-line): the mask tiles across each line independently. For paragraph-length text, set display: block on the host via class and let the mask tile per line.
  • Right-to-left text: the mask is direction-agnostic; the punch-through geometry is the same regardless of glyph order.
  • Custom fonts: the masked layer inherits font from the host, so any web font applies. The mask tiles in user-coordinate pixels, so a custom font with different x-height will show a different punch-through density.
  • revealOn='always' + no revealText: renders just the masked base layer. The component degrades to a decorative text effect with no interaction.
  • revealText set + revealOn='manual': the bindable manualOpen controls the reveal layer; consumers drive it from any source (button click elsewhere, route load, etc.).

Dependencies

Zero. Pure Svelte 5 runes + CSS mask-image + inline SVG data URIs.

File Structure

src/lib/components/
  XrayText.svelte                  # the component
  XrayText.test.ts                 # render, modes, click toggling, SR text, source checks
  XrayTextTestHarness.test.svelte  # internal harness for the test
  XrayText.md                      # this file

The catalog entry and the /xraytext demo route live next to the other components in src/lib/componentCatalog.ts and src/routes/xraytext/+page.svelte.

API

04
PropTypeDefaultDescription
textstringrequiredThe visible (masked) text.
revealTextstringundefinedOptional second text layer revealed on trigger.
pattern'dots' | 'lines' | 'cross' | 'diagonal''dots'Mask pattern shape.
density'sparse' | 'normal' | 'dense''normal'Mask density (smaller tile = denser).
revealOn'hover' | 'click' | 'always' | 'manual''hover'Trigger mode.
manualOpenboolean (bindable)falseWhen revealOn='manual', externally controls open state.
revealColorstring'rgba(15, 23, 42, 0.92)'Reveal layer colour.
ariaLabelstringundefinedAccessible name override. Default composes from text + revealText.
classstring''Extra CSS class on the host element.