Skip to content
goodtabs UI
BrowseBrowse components

Glass dock

A floating glass pill of icon buttons with always visible labels and arrow key movement.

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

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

Four labelled buttons, Home pressed.

Usage

import { GlassDock } from "@/components/ui/glass-dock"

<GlassDock items={[{ label: "Home", icon: <HomeIcon />, active: true }, { label: "Files", icon: <FolderIcon /> }]} />

Props

PropTypeDefaultDescription
items{ label: string; icon: ReactNode; onClick?: () => void; active?: boolean }[]Buttons in order. active sets aria-pressed.
labelstring"Dock"Accessible name of the toolbar.

Source

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

import * as React from "react";
import { GlassSurface } from "@/components/ui/glass-surface";
import { cn } from "@/lib/utils";

export type GlassDockItem = {
  label: string;
  icon: React.ReactNode;
  onClick?: () => void;
  active?: boolean;
};

type GlassDockProps = Omit<React.ComponentProps<typeof GlassSurface>, "children"> & {
  items: GlassDockItem[];
  /** Accessible name of the toolbar. */
  label?: string;
};

/**
 * A floating pill of glass with icon buttons. Unlike the plain dock, the label is under the icon and always
 * visible, so nothing waits on a tooltip. Arrow keys move between buttons.
 */
export function GlassDock({ items, label = "Dock", className, ...props }: GlassDockProps) {
  const refs = React.useRef<(HTMLButtonElement | null)[]>([]);
  const onKeyDown = (event: React.KeyboardEvent) => {
    const index = refs.current.findIndex((el) => el === document.activeElement);
    if (index < 0 || !["ArrowRight", "ArrowLeft", "Home", "End"].includes(event.key)) return;
    event.preventDefault();
    const last = items.length - 1;
    const next =
      event.key === "Home"
        ? 0
        : event.key === "End"
          ? last
          : event.key === "ArrowRight"
            ? Math.min(index + 1, last)
            : Math.max(index - 1, 0);
    refs.current[next]?.focus();
  };

  return (
    <GlassSurface
      data-slot="glass-dock"
      radius={28}
      role="toolbar"
      aria-label={label}
      onKeyDown={onKeyDown}
      className={cn("inline-flex items-center gap-1 rounded-full bg-background/75 p-1.5 text-foreground", className)}
      {...props}
    >
      {items.map((item, i) => (
        <button
          key={item.label}
          ref={(el) => {
            refs.current[i] = el;
          }}
          type="button"
          onClick={item.onClick}
          aria-pressed={item.active ?? undefined}
          className="group/item flex h-14 min-w-14 flex-col items-center justify-center gap-0.5 rounded-full px-2 text-foreground/80 transition-[background-color,color] duration-100 ease-out outline-none hover:bg-foreground/10 hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50 aria-pressed:bg-foreground/15 aria-pressed:text-foreground [&_svg]:size-5"
        >
          {item.icon}
          <span className="text-[11px] leading-none">{item.label}</span>
        </button>
      ))}
    </GlassSurface>
  );
}
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-dock/prompt.md, or the Copy AI prompt button above.

When to use

  • Three to six actions over imagery, such as a player or a viewer
  • When the label must be visible without hover

When not to use

  • Page navigation with links: use the dock, which renders anchors and tooltips
  • On touch only layouts where the labels are too small

Agent notes

  • Unlike dock, the label sits under the icon and is always visible, and there are no tooltips or links
  • A role=toolbar with arrow keys, Home and End; the active button is aria-pressed
  • It takes functions, so a demo that passes onClick must be a client component
  • Glass needs something behind it: it bends and frosts what sits under it, so on a flat background it reads as a grey box