---
title: "Shimmer · Base UI — StyleX Blocks"
description: "A GPU-accelerated highlight sweep over inline text for streaming and busy labels."
url: "https://stylexblocks.com/components/base-ui/shimmer/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Shimmer

A GPU-accelerated highlight sweep over inline text for streaming and busy labels.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/shimmer.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/shimmer.md)
- Category: Feedback
- HTML page: https://stylexblocks.com/components/base-ui/shimmer/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style base-ui
```

Then add the item as source you own:

```sh
npx stylexblocks add shimmer
```

Equivalent: `npx shadcn add @stylexblocks/shimmer`. Registry item JSON: https://stylexblocks.com/r/base-ui/shimmer.json

## Usage

```tsx
import { Shimmer } from "#components/ui/base-ui/shimmer.tsx";

export function Example() {
  return (
    // The highlight is masked to the glyphs and only animates a transform,
    // so the sweep runs on the GPU in Chrome/Safari; Firefox falls back to
    // -moz-element() masking. Reduced motion gets the plain muted text.
    <>
      <Shimmer>Generating response…</Shimmer>
      <Shimmer duration="1.2s" spread="2ch">Searching the docs…</Shimmer>
    </>
  );
}

// the highlight color is a custom property, overridable per instance:
// <Shimmer style={styles.accent}> with { "--shimmer-color": "..." }
```

## Source

`components/ui/base-ui/shimmer.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { useId } from "react";
import type { StyleOverrideProps } from "#lib/recipe-props.ts";
import { semanticColors } from "@stylexblocks/ui/semantic-tokens/colors.stylex.ts";

const shimmerSweep = stylex.keyframes({
  from: { translate: "-100% 0" },
  to: { translate: "100% 0" },
});

const styles = stylex.create({
  root: {
    "--shimmer-color": semanticColors.fg,
    "--shimmer-duration": "2.5s",
    "--shimmer-spread": "4ch",
    color: semanticColors.fgMuted,
    display: "inline-block",
    position: "relative",
  },
  text: {
    display: "inline-block",
  },
  // Inert clone stacked over the original text; `-webkit-mask-clip: text`
  // clips the ::after sweep to the clone's glyphs (Chrome/Safari — the whole
  // overlay stays GPU-composited since the sweep only animates `translate`).
  // Masking a clone instead of the original keeps the visible text's
  // sub-pixel antialiasing intact. Firefox lacks the text mask and instead
  // masks with `-moz-element()` pointed at the visible text span (the
  // `--shimmer-moz-mask` var set per instance) — visually identical, though
  // element masking is not GPU accelerated. Under prefers-reduced-motion, or
  // without either mask, the overlay is hidden and the text renders plainly.
  overlay: {
    color: "transparent",
    contain: "strict",
    display: {
      default: "none",
      "@supports ((-webkit-mask-clip: text) or (mask-image: -moz-element(#t)))": {
        default: "block",
        "@media (prefers-reduced-motion: reduce)": "none",
      },
    },
    inset: 0,
    maskImage: {
      default: "linear-gradient(#fff, #fff)",
      "@supports (mask-image: -moz-element(#t)) and (not (-webkit-mask-clip: text))":
        "var(--shimmer-moz-mask)",
    },
    overflow: "clip",
    pointerEvents: "none",
    position: "absolute",
    userSelect: "none",
    WebkitMaskClip: "text",
    "::after": {
      animationDuration: "var(--shimmer-duration)",
      animationIterationCount: "infinite",
      animationName: shimmerSweep,
      backgroundImage:
        "linear-gradient(90deg, transparent calc(50% - var(--shimmer-spread)), var(--shimmer-color) 50%, transparent calc(50% + var(--shimmer-spread)))",
      content: "''",
      inset: 0,
      position: "absolute",
      willChange: "transform",
    },
  },
});

const dynamic = stylex.create({
  mozMask: (mask: string) => ({ "--shimmer-moz-mask": mask }),
  duration: (duration: string) => ({ "--shimmer-duration": duration }),
  spread: (spread: string) => ({ "--shimmer-spread": spread }),
});

export interface ShimmerProps
  extends Omit<React.ComponentProps<"span">, "style">,
    StyleOverrideProps {
  /** One full sweep's pace as a CSS time, e.g. `"1.2s"`. Default `"2.5s"`. */
  duration?: string;
  /** Half-width of the highlight band, e.g. `"2ch"`. Default `"4ch"`. */
  spread?: string;
}

/**
 * Shimmer — a GPU-accelerated highlight sweep over inline text, for
 * streaming/busy labels ("Generating response…"). Children render twice: the
 * visible text plus an aria-hidden clone that masks the animated gradient to
 * its glyphs, so only a cheap transform animates each frame in Chrome and
 * Safari; Firefox falls back to `-moz-element()` masking (same look, CPU
 * composited). Pace and band width are the `duration` / `spread` props; the
 * highlight color overrides through the `--shimmer-color` custom property.
 * Reduced motion gets the plain text.
 */
export function Shimmer(allProps: ShimmerProps) {
  const { palette, style, className, children, duration, spread, ...rest } =
    allProps;
  // -moz-element() needs an id selector; strip useId's wrapper punctuation
  const textId = `shimmer-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`;
  const sx = stylex.props(
    palette,
    styles.root,
    dynamic.mozMask(`-moz-element(#${textId})`),
    duration != null && dynamic.duration(duration),
    spread != null && dynamic.spread(spread),
    style,
  );
  return (
    <span
      {...rest}
      className={
        typeof className === "string" && className.length > 0
          ? [sx.className, className].filter(Boolean).join(" ")
          : sx.className
      }
      style={sx.style}
    >
      <span id={textId} {...stylex.props(styles.text)}>
        {children}
      </span>
      <span aria-hidden="true" {...stylex.props(styles.overlay)}>
        {children}
      </span>
    </span>
  );
}
```
