Skip to content
goodtabs UI
BrowseBrowse components

Glass toolbar

A pill of icon actions on glass with roving focus and arrow key movement.

npx shadcn@latest add https://ui.goodtabs.co/r/glass-toolbar.json

With the @goodtabs namespace in components.json: npx shadcn@latest add @goodtabs/glass-toolbar

Five icon actions in two groups.

Labelled

Text and icon actions mixed in one row.

Usage

import { GlassToolbar, GlassToolbarButton, GlassToolbarSeparator } from "@/components/ui/glass-toolbar"

<GlassToolbar aria-label="Editing">
  <GlassToolbarButton aria-label="Undo"><Undo2Icon /></GlassToolbarButton>
  <GlassToolbarSeparator />
  <GlassToolbarButton aria-label="Share"><ShareIcon /></GlassToolbarButton>
</GlassToolbar>

Props

PropTypeDefaultDescription
aria-labelstringRequired name on GlassToolbar, and on every GlassToolbarButton.
size"icon-sm" | "sm""icon-sm"On GlassToolbarButton. icon-sm is 28px square, sm is 28px tall with a label.

Source

glass-toolbar/glass-toolbar.tsx
"use client";

import * as React from "react";
import { Toolbar as ToolbarPrimitive } from "radix-ui";

import { Button } from "@/components/ui/button";
import { GlassSurface } from "@/components/ui/glass-surface";
import { cn } from "@/lib/utils";

/**
 * A row of actions on a pane of glass. Radix toolbar underneath: role toolbar, one tab stop for the
 * whole row, Left and Right move between actions, Home and End jump to the ends. Name it with aria-label.
 */
function GlassToolbar({
  className,
  "aria-label": label,
  ...props
}: Omit<React.ComponentProps<typeof ToolbarPrimitive.Root>, "aria-label"> & { "aria-label": string }) {
  return (
    <GlassSurface
      radius={20}
      rim={10}
      strength={24}
      data-slot="glass-toolbar"
      className={cn("inline-flex w-fit rounded-full bg-background/65 p-1", className)}
    >
      <ToolbarPrimitive.Root aria-label={label} className="flex items-center gap-0.5" {...props} />
    </GlassSurface>
  );
}

type GlassToolbarButtonProps = Omit<React.ComponentProps<typeof Button>, "variant" | "asChild"> & {
  /** Accessible name. Required, since an icon has no text. */
  "aria-label": string;
};

/** One action. A ghost icon button by default; pass size="sm" and a text child for a labelled one. */
function GlassToolbarButton({ className, size = "icon-sm", ...props }: GlassToolbarButtonProps) {
  return (
    <ToolbarPrimitive.Button asChild>
      <Button
        variant="ghost"
        size={size}
        data-slot="glass-toolbar-button"
        className={cn("text-foreground", className)}
        {...props}
      />
    </ToolbarPrimitive.Button>
  );
}

function GlassToolbarSeparator({ className, ...props }: React.ComponentProps<typeof ToolbarPrimitive.Separator>) {
  return (
    <ToolbarPrimitive.Separator
      data-slot="glass-toolbar-separator"
      className={cn("mx-1 h-4 w-px bg-foreground/25", className)}
      {...props}
    />
  );
}

export { GlassToolbar, GlassToolbarButton, GlassToolbarSeparator };
components/ui/glass-surface.tsx
"use client";

import * as React from "react";

import { cn } from "@/lib/utils";

type GlassSurfaceProps = React.ComponentProps<"div"> & {
  /** Corner radius in pixels. Keep it equal to the rounded class on the surface, so the rim follows the edge. */
  radius?: number;
  /** Width of the bent rim in pixels. */
  rim?: number;
  /** How far the rim pushes what is behind it, in pixels. */
  strength?: number;
  /** Frost behind the glass, in pixels. */
  blur?: number;
};

/** Distance from a point to the edge of a rounded rectangle centred on the origin. Negative is inside. */
function roundedBoxDistance(x: number, y: number, halfWidth: number, halfHeight: number, radius: number) {
  const qx = Math.abs(x) - halfWidth + radius;
  const qy = Math.abs(y) - halfHeight + radius;
  return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - radius;
}

/**
 * Paints a displacement map: red and green hold how far each pixel is pushed, 128 is no push.
 * Inside the rim the push points along the edge normal and falls off with a quarter circle, the
 * profile of a lens edge, so the centre of the glass stays flat.
 */
function paintRefractionMap(width: number, height: number, radius: number, rim: number) {
  const canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  const context = canvas.getContext("2d");
  if (!context) return null;
  const image = context.createImageData(width, height);
  const halfWidth = width / 2;
  const halfHeight = height / 2;
  for (let py = 0; py < height; py++) {
    for (let px = 0; px < width; px++) {
      const x = px + 0.5 - halfWidth;
      const y = py + 0.5 - halfHeight;
      const edge = roundedBoxDistance(x, y, halfWidth, halfHeight, radius);
      const depth = Math.min(Math.max(-edge / rim, 0), 1);
      const bend = 1 - Math.sqrt(1 - (1 - depth) * (1 - depth));
      // The edge normal, by central differences on the distance field.
      const nx = roundedBoxDistance(x + 1, y, halfWidth, halfHeight, radius) - roundedBoxDistance(x - 1, y, halfWidth, halfHeight, radius);
      const ny = roundedBoxDistance(x, y + 1, halfWidth, halfHeight, radius) - roundedBoxDistance(x, y - 1, halfWidth, halfHeight, radius);
      const length = Math.hypot(nx, ny) || 1;
      const at = (py * width + px) * 4;
      image.data[at] = 128 - (nx / length) * bend * 127;
      image.data[at + 1] = 128 - (ny / length) * bend * 127;
      image.data[at + 2] = 128;
      image.data[at + 3] = 255;
    }
  }
  context.putImageData(image, 0, 0);
  return canvas.toDataURL();
}

/**
 * A pane of glass. Chromium bends what is behind it with an SVG filter; every other browser gets the
 * frosted fallback, which is the same surface without the bent rim. Either way the pane keeps enough
 * fill to hold text, and it turns solid for people who ask for less transparency.
 */
function GlassSurface({
  radius = 12,
  rim = 14,
  strength = 40,
  blur = 6,
  className,
  style,
  children,
  ...props
}: GlassSurfaceProps) {
  const id = React.useId().replace(/[^a-zA-Z0-9]/g, "");
  const surface = React.useRef<HTMLDivElement>(null);
  const [map, setMap] = React.useState<string | null>(null);

  React.useEffect(() => {
    const el = surface.current;
    if (!el) return;
    // backdrop-filter with a url() only renders in Chromium, and other browsers can parse it and draw nothing.
    if (!("userAgentData" in navigator) || !CSS.supports("backdrop-filter", "url(#a)")) return;
    const paint = () => {
      const width = Math.round(el.clientWidth);
      const height = Math.round(el.clientHeight);
      if (width > 0 && height > 0) setMap(paintRefractionMap(width, height, radius, rim));
    };
    const observer = new ResizeObserver(paint);
    observer.observe(el);
    return () => observer.disconnect();
  }, [radius, rim]);

  const refracting = map !== null;
  return (
    <div
      ref={surface}
      data-slot="glass-surface"
      data-refracting={refracting || undefined}
      className={cn(
        "relative border bg-background/50 shadow-[inset_0_1px_0_0_color-mix(in_oklab,var(--foreground)_8%,transparent)] backdrop-blur-md contrast-more:bg-background [@media(prefers-reduced-transparency:reduce)]:bg-background [@media(prefers-reduced-transparency:reduce)]:backdrop-blur-none",
        className,
      )}
      style={refracting ? { ...style, backdropFilter: `url(#${id})` } : style}
      {...props}
    >
      {refracting ? (
        <svg aria-hidden="true" width="0" height="0" className="absolute">
          <filter id={id} x="0" y="0" width="100%" height="100%" colorInterpolationFilters="sRGB">
            <feImage href={map ?? undefined} preserveAspectRatio="none" result="map" />
            <feGaussianBlur in="SourceGraphic" stdDeviation={blur} result="frost" />
            <feDisplacementMap in="frost" in2="map" scale={strength} xChannelSelector="R" yChannelSelector="G" />
          </filter>
        </svg>
      ) : null}
      {children}
    </div>
  );
}

export { GlassSurface };

For AI agents

Hand your agent this page, the markdown at /components/glass-toolbar/prompt.md, or the Copy AI prompt button above.

When to use

  • Floating actions over an image, a canvas or a video
  • Three to eight related actions that belong together

When not to use

  • Navigation between pages: use links
  • Dense app chrome on a flat background, where a plain toolbar is calmer

Agent notes

  • It is the radix toolbar: role toolbar, one tab stop, Left and Right move, Home and End jump. Icon-only buttons must have an aria-label
  • Pass size sm with a text child for a labelled action
  • The pane is inline-flex and as wide as its content, so centre it with the parent