labs

notification deck

2026-09-04

measuring the peek

the deck's bottom edge is fixed and the cards are not the same height, so a card's collapsed offset is not a constant times its depth. it is the difference between its height and the one in front of it, plus the peek, which is what actually puts its top edge the same distance above every time.

the two-line test

the constant version reduces to the same numbers when every card is the same size, which is why it survives review: it looks correct until one notification wraps to two lines and the card behind it disappears inside it. the second card here runs to two lines on purpose, and the third still peeks out above it.

take it with you

three steps, and the stage above runs in your own project.

  1. install

    nothing to add. it runs on react alone, 18 or newer.

  2. copy

    3 files, kept together in one folder, notifications/. the styles travel with them.

    "use client";
    
    import { useEffect, useRef, useState, type CSSProperties } from "react";
    import { stackOffsets, type Pose } from "./stack";
    import "./notifications.css";
    
    export interface Note {
      id: string;
      app: string;
      title: string;
      body: string;
    }
    
    export interface DeckProps {
      notes: Note[];
      // px of each card left showing below the one in front
      peek?: number;
      // how much smaller each card behind gets
      scaleStep?: number;
    }
    
    const GAP = 8;
    
    export default function NotificationDeck({ notes, peek = 10, scaleStep = 0.04 }: DeckProps) {
      const [expanded, setExpanded] = useState(false);
      const [heights, setHeights] = useState<number[]>([]);
      const cardsRef = useRef<(HTMLElement | null)[]>([]);
    
      useEffect(() => {
        const nodes = cardsRef.current.slice(0, notes.length).filter(Boolean) as HTMLElement[];
        if (nodes.length === 0) return;
        const read = () => setHeights(nodes.map((n) => Math.max(n.scrollHeight, n.offsetHeight)));
        read();
        const ro = new ResizeObserver(read);
        for (const n of nodes) ro.observe(n);
        return () => ro.disconnect();
      }, [notes.length]);
    
      const poses: Pose[] = stackOffsets(
        heights.length === notes.length ? heights : notes.map(() => 0),
        { peek, scaleStep, expanded, gap: GAP },
      );
    
      const frontHeight = heights[0];
    
      return (
        <button
          type="button"
          className="deck"
          data-expanded={String(expanded)}
          aria-expanded={expanded}
          aria-label={expanded ? "collapse notifications" : "expand notifications"}
          onClick={() => setExpanded((e) => !e)}
        >
          {notes.map((note, i) => {
            const isBackground = !expanded && i > 0;
            return (
              <article
                key={note.id}
                className="deck-card"
                data-background={String(isBackground)}
                ref={(el) => {
                  cardsRef.current[i] = el;
                }}
                style={
                  {
                    zIndex: notes.length - i,
                    transform: `translateY(${poses[i]?.y ?? 0}px) scale(${poses[i]?.scale ?? 1})`,
                    ...(isBackground && frontHeight ? { maxHeight: `${frontHeight}px` } : {}),
                  } as CSSProperties
                }
              >
                <span className="deck-app">{note.app}</span>
                <strong>{note.title}</strong>
                <p>{note.body}</p>
              </article>
            );
          })}
        </button>
      );
    }
    
  3. use

    the stage above, exactly as it runs here. the comments mark what to change.

    example.tsx
    "use client";
    
    import NotificationDeck, { type Note } from "./notifications/NotificationDeck";
    
    // the second note wraps, so the stack has uneven heights to fit
    const NOTES: Note[] = [
      { id: "a", app: "messages", title: "sam", body: "did the reel land?" },
      {
        id: "b",
        app: "calendar",
        title: "in 15 minutes",
        body: "design review: bring the latest screens and your notes",
      },
      { id: "c", app: "mail", title: "vercel", body: "your deployment is ready" },
    ];
    
    export default function NotificationsExample() {
      return (
        <div className="deck-lab">
          <p className="deck-lab-time" aria-hidden="true">
            Sunday 31 August
          </p>
          {/* tap the stack to fan it out, tap again to fold it */}
          <NotificationDeck notes={NOTES} peek={10} scaleStep={0.04} />
        </div>
      );
    }