Peek List
Point at a row and its picture follows your pointer; a click opens the row like a pop up. Use it for 3 to 8 products, services or features.
npx shadcn@latest add https://impeccable-ui.com/r/peek-list.jsonRun it in a project set up with shadcn. No other packages needed. Or open and copy the file into components/ui/peek-list.tsx.
Prompts
Paste one into Claude Code, Cursor or any AI coding tool. Install adds this exact list and asks only where it goes and which rows it shows. Blend asks three short questions (where and which rows, the picture size and its shape), then fits it to your brand without changing how it moves.
# Peek List
A 1-file React component from Impeccable. Add it to this project so it works and looks exactly like the source below, in the one place I choose, and change nothing else.
A list of rows, one for each product, service or feature. When I point at a row, its picture appears just above my pointer and follows it, but only inside that row, and the other rows soften; leaving the row fades it away and entering another row shows that row's picture. When I click a row, it grows in place like a pop up and pushes the rows below down, while the picture glides from my pointer to the left of the story: the heading, a short description and one button. The + turns into an X in the top right corner of the open row. The X, Esc or a click anywhere outside closes it. On a phone there is no floating picture: one tap opens the row.
## Before you build: one question
Look through this project first: pages that list products, services, features or projects (a services section, a "what we do" list, a features list, a work index), the pictures they already have, and where their words and links come from. Then ask me this in one message and wait for my answer:
- **Where should it go, and which rows does it show?** Suggest the 1–3 spots it fits best from my code. For each one, name the 3–8 rows you would use, with the title, a one to three sentence description, the picture and the button (words and link) for each, taken from my real content. I can pick one or name another.
*Why: each row opens a short story with one button, so it belongs where people compare a few things and open one, and it needs real words and pictures to show.*
Ask nothing else. If I say *skip*, use the best suggestion.
## Keep it as it is
The picture that follows the pointer inside its row, the row growing like a pop up while the picture glides to its seat, the + turning into the X in the corner, the other rows softening, the line-by-line drop, the pop up close and all their timing are the design. Keep them exactly as in the source. If something doesn't fit my project, tell me instead of changing it.
- Add only the component file and the one place I picked. If it replaces an existing list there, keep that spot's data and links and remove only the old markup. Don't add packages, change other files, or restyle anything.
- Don't add the demo parts from the Impeccable website: its five item pictures and their stories, and the home page's TV.
## Fastest path
If this project has a `components.json` (shadcn), run `npx shadcn@latest add https://impeccable-ui.com/r/peek-list.json`.
If the command succeeds, only fix import paths to match this project and skip ahead. If it fails or there is no `components.json`, build it by hand as below.
## Build it
- Stack: React 18 or 19 with TypeScript. No other packages: the picture follows the pointer with one `requestAnimationFrame` loop that runs only while a picture shows, the fades and the turning X are CSS transitions, and the row growing and the picture's glide use the browser's own Web Animations API. Its styles sit in a `<style>` tag inside the file.
- JavaScript project? Name the file `peek-list.jsx` and remove the type annotations; nothing else changes.
- Create this file, in this project's usual place for components:
- `components/ui/peek-list.tsx`
- Keep these exports and their names: `PeekList` (also the default export), `PeekListProps`, `PeekListItem`.
- Keep `"use client"` at the top in Next.js App Router projects. In other setups it is harmless.
- Keep the tuning values at the top exactly as written: `PEEK_IN_MS`, `PEEK_OUT_MS`, `PEEK_SCALE`, `PEEK_EASE`, `PEEK_GAP`, `FOLLOW`, `OPEN_MS`, `CLOSE_MS`, `GLIDE_EASE`, `HOVER_DIM`, `DIM`, `TEXT_DELAY`, `TEXT_MS`, `TEXT_DROP`, `TEXT_BLUR`, `TEXT_STAGGER`, `TEXT_OUT_MS`, `X_SIZE`, `X_INSET`, `STACK_AT` and the `CSS` block. They are the design and its timing.
- Keep the fallbacks: the floating picture shows only where a real mouse can hover (`(hover: hover) and (pointer: fine)`), never on phones or tablets, where one tap opens the row. Narrow lists (under 560px) put the picture above the words. Tab moves row by row, Enter opens a row and moves focus to the X, Esc closes it and gives focus back. With `prefers-reduced-motion: reduce` nothing glides or grows, things only fade.
## Use it
```tsx
import { PeekList } from "@/components/ui/peek-list";
const ITEMS = [
{
title: "Design systems",
description: "Tokens, components and docs your team can build on for years.",
image: { src: "/work/design-systems.webp", alt: "A design system's color tokens and buttons" },
cta: { label: "See the work", href: "/work/design-systems" },
},
// one entry per row, 3 to 8 reads best
];
export function Services() {
return <PeekList items={ITEMS} aria-label="What we do" />;
}
```
- It fills the width it is given; about 640–720px wide reads best. The floating picture rises about 100px above the first row, so leave that much room above the list, and make sure no parent cuts it off with `overflow: hidden`.
- Pictures are shown at 3:2 and cropped to fill. Keep each file small (about 1000px wide, WebP), with real `alt` text. Pictures from another domain must be allowed by the project's Content-Security-Policy (`img-src`).
- `cta` with `href` is a link; use the project's own link component only if plain `href` breaks its routing, then pass `onClick` and navigate there instead.
## How the list works
Read this before you touch anything, so you know what the code does and why it must stay as it is.
1. **The rows.** Each row is a soft gray box (the text color mixed 5% into the page) with 16px corners, the name on the left and a + on the right. Pointing at a row deepens its gray a little.
2. **Point at a row.** Its picture (240px wide by default, 3:2, 10px corners, a soft shadow) appears centered over the pointer with its bottom edge 12px above the pointer's tip, so it never covers the pointer, fading in while it grows from 90% in 420ms with a gentle start and a long soft landing. It trails the pointer softly (about a tenth of a second behind) and stays inside the row's sides, so it is never cut. Leave the row and it fades away in 300ms where it is; enter another row and that row's picture appears at the pointer. Only one row shows a picture at a time. While the pointer is on the list, every other row softens to 70% in the same 420ms, and the row you point at stays at full strength.
3. **Click it.** The row grows to fit its story in 720ms and pushes the rows below down, while the other rows dim to 40% at the same pace. The picture glides from where it floats to its seat on the left of the story in the same 720ms, growing as it goes. The title glides from its spot in the row to the top of the story. The + turns 45 degrees into an X on a small round button, moving in step to 16px inside the open row's top right corner (on narrow lists it sits on the picture's top right corner). Then the description and the button drop in from above, line by line, each falling 16px, fading in and losing a 4px blur over 650ms, 110ms apart, starting 380ms in.
4. **Close it.** Like a pop up: the X, Esc or a click anywhere outside the row. The story and the picture fade away together in 220ms, then the row folds back in 600ms while the title glides home and the X turns back into the +, and the other rows come back. A click on another row closes the open one and opens that one.
5. **Phones.** No hover, so no floating picture: one tap opens the row, and on narrow lists the picture sits above the words.
6. **Colors.** The text takes `--foreground` (else `--fg`) and the rows mix it into `--background` (else `--bg`), so it follows light and dark mode. The button is a quiet outlined button with 8px corners in those same colors.
## Check
- It type-checks and the page renders with no console errors.
- Pointing at a row shows its picture just above the pointer in about 0.4 seconds; it follows the pointer softly, never leaves the row's sides, fades when I leave the row and switches when I enter another, while the other rows soften to 70%. Clicking grows the row and glides the picture to the left in 0.72 seconds while the description and button drop in from above, and the + turns into an X in the top right corner of the open row. The X, Esc and a click outside each close it in under a second.
- It looks right in light and dark mode (check both): rows, text, button and X read well on each background.
- It works with a finger on a phone: no floating picture, one tap opens the row with the picture above the words, a tap outside closes it, and nothing scrolls sideways.
- With `prefers-reduced-motion: reduce`, nothing glides or grows; things only fade.
- Nothing else on the site looks or behaves differently.
- Finish with a short report: the files you added or changed, where it's placed, and every place where you had to do something differently from the source, and why.
## Source
The reference implementation. Copy it as-is, then apply only the changes listed above.
### `components/ui/peek-list.tsx`
```tsx
"use client";
/**
* Peek List, from Impeccable.
*
* A list of rows, one for each product, service or feature. Point at a row and
* its picture appears just above the pointer and follows it, but only inside
* that row, while the other rows soften to 70%: leave the row and the picture
* fades away, enter another row and that row's picture takes over. Click a row and it grows in place like a pop up,
* pushing the rows below it down, while the picture glides from the pointer to
* the left of the story: the heading, a short description and one button. The
* + turns into an X in the top right corner of the open row. Close it with
* the X, Esc or a click anywhere outside, and the row folds back.
*
* Phones have no hover, so they never show a floating picture: one tap opens
* the row straight away, with the picture above the words.
* Reduced motion: nothing glides or grows, things only fade.
*
* Colors come from your theme: the text from --foreground (else --fg), the page
* from --background (else --bg), so it follows light and dark mode.
* One file, React only, no packages.
*/
import {
useCallback,
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
type CSSProperties,
type MouseEvent as ReactMouseEvent,
type PointerEvent as ReactPointerEvent,
} from "react";
export type PeekListItem = {
/** The row's name, and the heading of its story. */
title: string;
/** A short description: one to three sentences reads best. */
description: string;
/** The picture: it floats by the pointer, then sits left of the story. alt is read by screen readers. */
image: { src: string; alt: string };
/** The one button under the description. With href it is a link, with only onClick a button. */
cta?: {
label: string;
href?: string;
/** Link target, like "_blank" (then rel="noopener noreferrer" is added). */
target?: string;
onClick?: (event: ReactMouseEvent<HTMLElement>) => void;
};
};
export type PeekListProps = {
/** The rows, in order. */
items: PeekListItem[];
/** Width of the floating picture, in px. */
previewWidth?: number;
/** Shape of the pictures, as width / height. 1.5 is 3:2. */
ratio?: number;
className?: string;
style?: CSSProperties;
"aria-label"?: string;
};
/* ---------- the design: keep as written ---------- */
const PEEK_IN_MS = 420; // the picture appears by the pointer, growing a little as it fades in
const PEEK_OUT_MS = 300; // and fades away when the pointer leaves the row
const PEEK_SCALE = 0.9; // it grows from 90%
const PEEK_EASE = "cubic-bezier(.22,1,.36,1)"; // gentle start, long soft landing
const PEEK_GAP = 12; // px between the pointer and the bottom of the picture: it floats just above the pointer, never covering it
const FOLLOW = 0.12; // s: the picture trails the pointer softly, never stuck to it
const OPEN_MS = 720; // the row grows and the picture glides to its place
const CLOSE_MS = 600; // and folds back
const GLIDE_EASE = "cubic-bezier(.4,0,.2,1)"; // eases in, travels, settles softly
const HOVER_DIM = 0.7; // pointing at a row softens the others to 70%, in step with the picture fading in
const DIM = 0.4; // the other rows drop to 40% while one is open, in step with the row growing
const TEXT_DELAY = 380; // the story starts to drop in as the row opens up
const TEXT_MS = 650; // each line drops from above and comes into focus
const TEXT_DROP = 16; // px each line falls
const TEXT_BLUR = 4; // px of soft blur it loses as it lands
const TEXT_STAGGER = 110; // line by line, top to bottom
const TEXT_OUT_MS = 220; // on close the story lifts away first, then the row folds
const X_SIZE = 36; // the close button
const X_INSET = 16; // px from the open row's top and right edges
const STACK_AT = 560; // px: narrower than this, the picture sits above the words
const CSS = `
.pkl { --pkl-fg: var(--foreground, var(--fg, CanvasText)); --pkl-bg: var(--background, var(--bg, Canvas));
--pkl-surface: color-mix(in oklab, var(--pkl-fg) 5%, var(--pkl-bg));
position: relative; display: grid; gap: 8px; width: 100%; margin: 0; padding: 0; list-style: none; box-sizing: border-box; color: var(--pkl-fg); }
.pkl *, .pkl *::before, .pkl *::after { box-sizing: border-box; }
.pkl-row { position: relative; margin: 0; padding: 0; transition: opacity ${PEEK_IN_MS}ms ${PEEK_EASE}; }
.pkl-row[data-peek="true"], .pkl-row[data-expanded="true"] { z-index: 2; }
/* Pointing at a row softens the others; opening one dims them further, at the pace of the row growing. */
.pkl[data-hovering="true"] .pkl-row:not([data-lit="true"]) { opacity: ${HOVER_DIM}; }
.pkl[data-active="true"] .pkl-row:not([data-expanded="true"]) { opacity: ${DIM}; transition: opacity ${OPEN_MS}ms ${GLIDE_EASE}; }
.pkl-panel { position: relative; overflow: hidden; border-radius: 16px; background: var(--pkl-surface); transition: background-color ${PEEK_IN_MS}ms ${PEEK_EASE}; }
.pkl-body { display: grid; grid-template-columns: minmax(0, 1fr); padding: 26px 64px 26px 24px; }
.pkl-row[data-expanded="true"] .pkl-body { grid-template-columns: minmax(0, 42fr) minmax(0, 58fr); align-items: start; gap: 32px; padding: 28px; }
.pkl-row[data-expanded="true"] .pkl-text { padding-right: ${X_SIZE + X_INSET - 28 + 12}px; }
.pkl[data-stack="true"] .pkl-body { padding: 20px 56px 20px 20px; }
.pkl[data-stack="true"] .pkl-row[data-expanded="true"] .pkl-body { grid-template-columns: minmax(0, 1fr); gap: 18px; padding: 16px 16px 20px; }
.pkl[data-stack="true"] .pkl-row[data-expanded="true"] .pkl-text { padding-right: 0; }
.pkl-slot { position: relative; width: 100%; aspect-ratio: var(--pkl-ratio); border-radius: 10px; }
.pkl-text { min-width: 0; }
.pkl-title { margin: 0; font-size: 22px; font-weight: 500; line-height: 1.25; letter-spacing: -0.015em; overflow-wrap: anywhere; }
.pkl[data-stack="true"] .pkl-title { font-size: 19px; }
.pkl-hit { display: inline; margin: 0; padding: 0; border: 0; background: none; font: inherit; color: inherit; letter-spacing: inherit; text-align: start;
cursor: pointer; outline: none; -webkit-tap-highlight-color: transparent; }
.pkl-hit::after { content: ""; position: absolute; inset: 0; border-radius: 16px; z-index: 1; }
.pkl-row[data-expanded="true"] .pkl-hit { cursor: default; }
.pkl-row[data-expanded="true"] .pkl-hit::after { display: none; }
.pkl-hit:focus-visible::after { box-shadow: inset 0 0 0 2px color-mix(in oklab, var(--pkl-fg) 55%, transparent); }
.pkl-desc { margin: 12px 0 0; font-size: 16px; line-height: 1.55; color: color-mix(in oklab, var(--pkl-fg) 70%, transparent); overflow-wrap: anywhere; }
.pkl[data-stack="true"] .pkl-desc { font-size: 15px; }
.pkl-cta { display: inline-flex; align-items: center; height: 36px; margin-top: 20px; padding: 0 14px; border-radius: 8px; font: inherit; font-size: 14px; font-weight: 500;
color: var(--pkl-fg); text-decoration: none; cursor: pointer; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis;
background: var(--pkl-bg); border: 1px solid color-mix(in oklab, var(--pkl-fg) 14%, transparent);
box-shadow: 0 1px 2px color-mix(in oklab, var(--pkl-fg) 8%, transparent);
transition: background-color 150ms ease, transform 150ms ease; outline: none; -webkit-tap-highlight-color: transparent; }
.pkl-cta:active { transform: scale(.97); }
.pkl-cta:focus-visible, .pkl-x:focus-visible { box-shadow: 0 0 0 2px var(--pkl-bg), 0 0 0 4px color-mix(in oklab, var(--pkl-fg) 55%, transparent); }
/* The + in the row; open, it turns into the X in the top right corner, moving with the row as it grows and folds. */
.pkl-x { position: absolute; top: 0; right: 0; z-index: 3; display: grid; place-items: center; width: ${X_SIZE}px; height: ${X_SIZE}px; margin: 0; padding: 0;
border-radius: 999px; border: 1px solid transparent; background: transparent; color: var(--pkl-fg); cursor: pointer; outline: none; pointer-events: none;
transform: translate(-16px, var(--pkl-xy)); -webkit-tap-highlight-color: transparent;
transition: transform ${CLOSE_MS}ms ${GLIDE_EASE}, background-color ${CLOSE_MS}ms ${GLIDE_EASE}, border-color ${CLOSE_MS}ms ${GLIDE_EASE}; }
.pkl[data-stack="true"] .pkl-x { transform: translate(-12px, var(--pkl-xy)); }
.pkl-x svg { transition: transform ${CLOSE_MS}ms ${GLIDE_EASE}; }
.pkl-row[data-expanded="true"] .pkl-x { transform: translate(-${X_INSET}px, ${X_INSET}px);
background: var(--pkl-bg); border-color: color-mix(in oklab, var(--pkl-fg) 10%, transparent);
transition-duration: ${OPEN_MS}ms; }
/* Narrow: the X sits on the picture's top right corner, inside the row. */
.pkl[data-stack="true"] .pkl-row[data-expanded="true"] .pkl-x { transform: translate(-24px, 24px); }
.pkl-row[data-expanded="true"] .pkl-x svg { transform: rotate(45deg); transition-duration: ${OPEN_MS}ms; }
.pkl-row[data-open="true"] .pkl-x { pointer-events: auto; }
/* The picture: floats by the pointer, then glides into the open row. */
.pkl-pic { position: absolute; left: 0; top: 0; z-index: 2; pointer-events: none; will-change: transform; }
.pkl-pic-in { position: relative; width: 100%; height: 100%; overflow: hidden; border-radius: 10px; opacity: 0; transform: scale(${PEEK_SCALE});
background: color-mix(in oklab, var(--pkl-fg) 6%, var(--pkl-bg));
box-shadow: 0 22px 44px -26px rgba(0,0,0,.5), 0 2px 6px -2px rgba(0,0,0,.12);
transition: opacity ${PEEK_OUT_MS}ms ease, transform ${PEEK_OUT_MS}ms ease; }
.pkl-pic-in::after { content: ""; position: absolute; inset: 0; border-radius: inherit; box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--pkl-fg) 9%, transparent); }
.pkl-row[data-peek="true"] .pkl-pic-in, .pkl-row[data-open="true"] .pkl-pic-in { opacity: 1; transform: none;
transition: opacity ${PEEK_IN_MS}ms ${PEEK_EASE}, transform ${PEEK_IN_MS}ms ${PEEK_EASE}, box-shadow ${OPEN_MS}ms ${GLIDE_EASE}; }
.pkl-row[data-open="true"] .pkl-pic-in { box-shadow: 0 1px 2px rgba(0,0,0,.06); }
.pkl-pic img { position: absolute; inset: 0; width: 100%; height: 100%; max-width: none; object-fit: cover; display: block; user-select: none; -webkit-user-drag: none; }
/* The story drops in from above, line by line, coming into focus as it lands. */
.pkl-line { opacity: 0; transform: translateY(-${TEXT_DROP}px); filter: blur(${TEXT_BLUR}px); }
.pkl-row[data-shown="true"] .pkl-line { opacity: 1; transform: none; filter: none;
transition: opacity ${TEXT_MS}ms ${PEEK_EASE}, transform ${TEXT_MS}ms ${PEEK_EASE}, filter ${TEXT_MS}ms ${PEEK_EASE};
transition-delay: calc(${TEXT_DELAY}ms + var(--i) * ${TEXT_STAGGER}ms); }
.pkl-row[data-closing="true"] .pkl-line { opacity: 0; transform: translateY(-${TEXT_DROP / 2}px); filter: none;
transition: opacity ${TEXT_OUT_MS}ms ease, transform ${TEXT_OUT_MS}ms ease; }
.pkl-row[data-closing="true"] .pkl-pic-in { opacity: 0; transform: scale(.96); transition: opacity ${TEXT_OUT_MS}ms ease, transform ${TEXT_OUT_MS}ms ease; }
@media (hover: hover) {
.pkl-row:not([data-expanded="true"]) .pkl-panel:hover { background: color-mix(in oklab, var(--pkl-fg) 6.5%, var(--pkl-bg)); }
.pkl[data-active="true"] .pkl-row:not([data-expanded="true"]):hover { opacity: ${HOVER_DIM}; }
.pkl-cta:hover { background: color-mix(in oklab, var(--pkl-fg) 4%, var(--pkl-bg)); }
.pkl-row[data-open="true"] .pkl-x:hover { background: color-mix(in oklab, var(--pkl-fg) 10%, var(--pkl-bg)); }
}
@media (prefers-reduced-motion: reduce) {
.pkl-row, .pkl-x, .pkl-x svg, .pkl-pic-in, .pkl-line { transition-property: opacity, background-color !important; }
.pkl-line, .pkl-pic-in { transform: none !important; filter: none !important; }
}`;
type Box = { x: number; y: number; w: number; h: number };
/** A picture that fails to load hides, so the soft box behind it shows instead of a broken icon. */
const hideBroken = (e: { currentTarget: HTMLImageElement }) => {
e.currentTarget.style.visibility = "hidden";
};
/** The same check for a picture that failed before the page came alive (its error event is already gone). */
const checkBroken = (img: HTMLImageElement | null) => {
if (img?.complete && !img.naturalWidth) img.style.visibility = "hidden";
};
const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v));
/** How much the row is scaled on screen (1 unless a parent scales it), so screen px turn back into the row's own px. */
const scaleOf = (row: HTMLElement) => row.getBoundingClientRect().width / (row.offsetWidth || 1) || 1;
/** Where an element sits inside its row, in the row's own px. */
function within(el: Element, row: HTMLElement) {
const a = el.getBoundingClientRect();
const b = row.getBoundingClientRect();
const k = scaleOf(row);
return { x: (a.left - b.left) / k, y: (a.top - b.top) / k, w: a.width / k, h: a.height / k };
}
export function PeekList({
items,
previewWidth = 240,
ratio = 1.5,
className,
style,
"aria-label": ariaLabel,
}: PeekListProps) {
const uid = useId();
const root = useRef<HTMLUListElement>(null);
const rows = useRef<(HTMLLIElement | null)[]>([]);
const panels = useRef<(HTMLDivElement | null)[]>([]);
const titles = useRef<(HTMLHeadingElement | null)[]>([]);
const hits = useRef<(HTMLButtonElement | null)[]>([]);
const pics = useRef<(HTMLDivElement | null)[]>([]);
const slots = useRef<(HTMLDivElement | null)[]>([]);
const xs = useRef<(HTMLButtonElement | null)[]>([]);
const [canHover, setCanHover] = useState(false);
const [still, setStill] = useState(false); // reduced motion
const [stack, setStack] = useState(false); // a narrow list: picture above the words
const [peek, setPeek] = useState<number | null>(null); // the row whose picture floats by the pointer
const [lit, setLit] = useState<number | null>(null); // the row the pointer was last in, kept bright while the others soften (null: pointer off the list)
const [open, setOpen] = useState<number | null>(null); // the open row
const [closing, setClosing] = useState<number | null>(null); // the row whose story is lifting away
const [shown, setShown] = useState<number | null>(null); // the open row's lines, a frame after it opens, so they can drop in
const n = items.length;
// Odd values fall back to something that still works: a picture 80 to 640px wide, between very tall (1:3) and very wide (4:1).
const shape = Number.isFinite(ratio) ? clamp(ratio, 1 / 3, 4) : 1.5;
const w = Math.round(Number.isFinite(previewWidth) ? clamp(previewWidth, 80, 640) : 240);
const h = Math.round(w / shape);
/* Watch hover support, reduced motion and the list's width. */
useLayoutEffect(() => {
const el = root.current;
if (!el) return;
const hoverQ = window.matchMedia("(hover: hover) and (pointer: fine)");
const motionQ = window.matchMedia("(prefers-reduced-motion: reduce)");
const sync = () => {
setCanHover(hoverQ.matches);
setStill(motionQ.matches);
};
sync();
const ro = new ResizeObserver(() => setStack(el.clientWidth < STACK_AT));
ro.observe(el);
setStack(el.clientWidth < STACK_AT);
hoverQ.addEventListener("change", sync);
motionQ.addEventListener("change", sync);
return () => {
ro.disconnect();
hoverQ.removeEventListener("change", sync);
motionQ.removeEventListener("change", sync);
};
}, []);
/* ---------- the floating picture ---------- */
// Where each picture is and where it is heading, in px inside its row. Kept in refs so following the pointer never re-renders.
const pos = useRef<{ x: number; y: number }[]>([]);
const goal = useRef<{ x: number; y: number }[]>([]);
const raf = useRef(0);
const last = useRef(0);
const peekRef = useRef<number | null>(null);
peekRef.current = peek;
const place = (i: number, x: number, y: number) => {
const p = pics.current[i];
if (p) p.style.transform = `translate3d(${x}px, ${y}px, 0)`;
};
const loop = useCallback(
(now: number) => {
const i = peekRef.current;
if (i === null) return (raf.current = 0);
const dt = Math.min(0.05, (now - (last.current || now)) / 1000);
last.current = now;
const p = pos.current[i];
const g = goal.current[i];
if (p && g) {
const k = still ? 1 : 1 - Math.exp(-dt / FOLLOW);
p.x += (g.x - p.x) * k;
p.y += (g.y - p.y) * k;
place(i, p.x, p.y);
}
raf.current = requestAnimationFrame(loop);
},
[still],
);
useEffect(() => () => cancelAnimationFrame(raf.current), []);
/** The spot for the picture: centered over the pointer, just above its tip, never past the row's sides. */
const aim = (i: number, cx: number, cy: number) => {
const row = rows.current[i];
if (!row) return null;
const r = row.getBoundingClientRect();
const k = scaleOf(row);
return { x: clamp((cx - r.left) / k - w / 2, 0, Math.max(0, row.offsetWidth - w)), y: (cy - r.top) / k - h - PEEK_GAP };
};
const peekable = canHover && open === null && closing === null;
const onMove = (i: number) => (e: ReactPointerEvent) => {
if (!peekable || e.pointerType !== "mouse") return;
setLit(i);
const g = aim(i, e.clientX, e.clientY);
if (!g) return;
goal.current[i] = g;
if (peekRef.current !== i) {
// A fresh row: the picture appears right at the pointer, then follows it.
pos.current[i] = { ...g };
const p = pics.current[i];
if (p) {
p.style.width = `${w}px`;
p.style.height = `${h}px`;
}
place(i, g.x, g.y);
peekRef.current = i;
setPeek(i);
}
if (!raf.current) {
last.current = 0;
raf.current = requestAnimationFrame(loop);
}
};
const onLeave = (i: number) => () => {
if (peekRef.current === i) {
peekRef.current = null;
setPeek(null);
}
};
/* ---------- open and close ---------- */
// Before the layout changes: how tall each row is and where each title sits, so the change can glide.
const snap = useRef<{ h: number[]; t: Box[] } | null>(null);
const from = useRef<{ box: Box | null; i: number; keys: boolean; real: boolean } | null>(null);
const anims = useRef<Map<Element, Animation>>(new Map());
const openedAt = useRef(0);
const timer = useRef(0);
const takeSnap = () => {
snap.current = {
h: panels.current.map((p) => p?.offsetHeight ?? 0),
t: titles.current.map((t, i) => (t && rows.current[i] ? within(t, rows.current[i]!) : { x: 0, y: 0, w: 0, h: 0 })),
};
};
const run = (el: Element, frames: Keyframe[], ms: number) => {
anims.current.get(el)?.cancel();
const a = el.animate(frames, { duration: still ? 0 : ms, easing: GLIDE_EASE });
anims.current.set(el, a);
a.onfinish = () => anims.current.get(el) === a && anims.current.delete(el);
return a;
};
const doOpen = (i: number, e: ReactMouseEvent) => {
// Where the picture starts: where it floats now, or at the pointer, or (keyboard, touch) in place.
let box: Box | null = null;
if (peekRef.current === i && pos.current[i]) box = { ...pos.current[i], w, h };
else if (canHover && e.detail > 0) {
const g = aim(i, e.clientX, e.clientY);
if (g) box = { ...g, w, h };
}
window.clearTimeout(timer.current);
if (open !== null && open !== i) startClose(open, false);
// A row still lifting its story away folds now (or, if it is this row, simply opens again).
else if (closing !== null) setClosing(null);
// Only a real keyboard press moves focus to the X, and only a real click may scroll the page.
from.current = { box, i, keys: e.detail === 0 && e.nativeEvent.isTrusted, real: e.nativeEvent.isTrusted };
openedAt.current = e.timeStamp || performance.now();
takeSnap();
peekRef.current = null;
setPeek(null);
setOpen(i);
};
/* Close: the story lifts away first, then the row folds back. */
const startClose = (i: number, refocus: boolean) => {
setOpen((o) => (o === i ? null : o));
setShown((s) => (s === i ? null : s));
setClosing(i);
window.clearTimeout(timer.current);
timer.current = window.setTimeout(() => {
takeSnap();
setClosing(null);
if (refocus) hits.current[i]?.focus({ preventScroll: true });
}, still ? 0 : TEXT_OUT_MS);
};
/** refocus: focus goes back to the row's title (Esc, the X); a click elsewhere leaves focus where it landed. */
const close = useCallback(
(refocus: boolean) => {
if (open !== null) startClose(open, refocus);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[open, still],
);
useEffect(() => () => window.clearTimeout(timer.current), []);
/* After the layout changes: rows glide to their new height, titles to their new place, the picture to its seat. */
useLayoutEffect(() => {
const s = snap.current;
if (!s) return;
snap.current = null;
const ms = open !== null && closing === null ? OPEN_MS : CLOSE_MS;
for (let i = 0; i < n; i++) {
const p = panels.current[i];
const t = titles.current[i];
const row = rows.current[i];
if (!p || !t || !row) continue;
anims.current.get(p)?.cancel();
anims.current.get(t)?.cancel();
const nh = p.offsetHeight;
if (Math.abs(nh - s.h[i]) > 0.5) run(p, [{ height: `${s.h[i]}px` }, { height: `${nh}px` }], nh > s.h[i] ? OPEN_MS : CLOSE_MS);
const nt = within(t, row);
const o = s.t[i];
if (o && (Math.abs(nt.x - o.x) > 0.5 || Math.abs(nt.y - o.y) > 0.5))
run(t, [{ transform: `translate(${o.x - nt.x}px, ${o.y - nt.y}px)` }, { transform: "none" }], nh > s.h[i] ? OPEN_MS : ms);
}
// The picture glides from the pointer into its seat on the left.
const f = from.current;
from.current = null;
if (f && f.i === open) {
const i = f.i;
const pic = pics.current[i];
const slot = slots.current[i];
const row = rows.current[i];
if (pic && slot && row) {
const to = within(slot, row);
// The row's own top margin is still growing; the seat is measured from the row, so it stays put.
pic.style.transform = `translate3d(${to.x}px, ${to.y}px, 0)`;
pic.style.width = `${to.w}px`;
pic.style.height = `${to.h}px`;
if (f.box)
run(
pic,
[
{ transform: `translate3d(${f.box.x}px, ${f.box.y}px, 0)`, width: `${f.box.w}px`, height: `${f.box.h}px` },
{ transform: `translate3d(${to.x}px, ${to.y}px, 0)`, width: `${to.w}px`, height: `${to.h}px` },
],
OPEN_MS,
);
}
// Wait one more frame so the lines are drawn hidden first; then they drop in.
requestAnimationFrame(() => requestAnimationFrame(() => setShown(i)));
if (f.keys) xs.current[i]?.focus({ preventScroll: true });
// If the open row runs off the bottom of the screen, bring it into view once it has grown.
if (f.real)
window.setTimeout(() => {
const r = row?.getBoundingClientRect();
if (r && r.bottom > window.innerHeight) row!.scrollIntoView({ block: "nearest", behavior: still ? "auto" : "smooth" });
}, still ? 0 : OPEN_MS);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, closing]);
/* While open, the picture keeps to its seat if the list changes width. */
useEffect(() => {
if (open === null) return;
const i = open;
const slot = slots.current[i];
const row = rows.current[i];
const pic = pics.current[i];
if (!slot || !row || !pic) return;
const ro = new ResizeObserver(() => {
if (anims.current.get(pic)) return;
const to = within(slot, row);
pic.style.transform = `translate3d(${to.x}px, ${to.y}px, 0)`;
pic.style.width = `${to.w}px`;
pic.style.height = `${to.h}px`;
});
ro.observe(slot);
return () => ro.disconnect();
}, [open, stack]);
/* Like a pop up: Esc or a click anywhere outside the open row closes it. A click on another row opens that one instead. */
useEffect(() => {
if (open === null) return;
const onKey = (e: KeyboardEvent) => e.key === "Escape" && close(true);
const onClick = (e: MouseEvent) => {
if (e.timeStamp <= openedAt.current) return;
const t = e.target as Element | null;
const row = rows.current[open];
if (t && row?.contains(t)) return; // inside the open row (the X closes it itself)
if (t && root.current?.contains(t) && t.closest(".pkl-panel")) return; // another row: it opens itself
close(false);
};
window.addEventListener("keydown", onKey);
document.addEventListener("click", onClick, true);
return () => {
window.removeEventListener("keydown", onKey);
document.removeEventListener("click", onClick, true);
};
}, [open, close]);
if (!n) return null;
const active = open !== null || closing !== null;
return (
<ul
ref={root}
className={["pkl", className].filter(Boolean).join(" ")}
aria-label={ariaLabel}
data-active={active || undefined}
data-hovering={(lit !== null && peekable) || undefined}
onPointerLeave={() => setLit(null)}
data-stack={stack || undefined}
style={{ "--pkl-ratio": String(shape), "--pkl-xy": stack ? "14px" : "22px", ...style } as CSSProperties}
>
<style>{CSS}</style>
{items.map((it, i) => {
const isOpen = open === i;
const isClosing = closing === i;
const expanded = isOpen || isClosing;
const bodyId = `${uid}-${i}`;
return (
<li
key={i}
ref={(el) => {
rows.current[i] = el;
}}
className="pkl-row"
data-peek={(peek === i && peekable) || undefined}
data-lit={lit === i || undefined}
data-open={isOpen || undefined}
data-closing={isClosing || undefined}
data-expanded={expanded || undefined}
data-shown={shown === i || undefined}
>
<div
ref={(el) => {
panels.current[i] = el;
}}
className="pkl-panel"
onPointerMove={onMove(i)}
onPointerEnter={onMove(i)}
onPointerLeave={onLeave(i)}
>
<div className="pkl-body" id={bodyId}>
{expanded && (
<div
ref={(el) => {
slots.current[i] = el;
}}
className="pkl-slot"
role="img"
aria-label={it.image.alt}
/>
)}
<div className="pkl-text">
<h3
ref={(el) => {
titles.current[i] = el;
}}
className="pkl-title"
>
<button
ref={(el) => {
hits.current[i] = el;
}}
type="button"
className="pkl-hit"
aria-expanded={isOpen}
aria-controls={bodyId}
onClick={(e) => (isOpen ? close(e.nativeEvent.isTrusted) : doOpen(i, e))}
>
{it.title}
</button>
</h3>
{expanded && (
<>
<p className="pkl-desc pkl-line" style={{ "--i": 0 } as CSSProperties}>
{it.description}
</p>
{it.cta && (
<div className="pkl-line" style={{ "--i": 1 } as CSSProperties}>
{it.cta.href ? (
<a
className="pkl-cta"
href={it.cta.href}
target={it.cta.target}
rel={it.cta.target === "_blank" ? "noopener noreferrer" : undefined}
onClick={it.cta.onClick}
>
{it.cta.label}
</a>
) : (
<button type="button" className="pkl-cta" onClick={it.cta.onClick}>
{it.cta.label}
</button>
)}
</div>
)}
</>
)}
</div>
</div>
</div>
<button
ref={(el) => {
xs.current[i] = el;
}}
type="button"
className="pkl-x"
aria-label={isOpen ? `Close ${it.title}` : undefined}
aria-hidden={!isOpen || undefined}
tabIndex={isOpen ? 0 : -1}
onClick={(e) => close(e.nativeEvent.isTrusted)}
>
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true">
<path d="M12 4v16M4 12h16" />
</svg>
</button>
<div
ref={(el) => {
pics.current[i] = el;
}}
className="pkl-pic"
aria-hidden="true"
style={{ width: w, height: h }}
>
<div className="pkl-pic-in">
<img src={it.image.src} alt="" loading="lazy" decoding="async" draggable={false} onError={hideBroken} ref={checkBroken} />
</div>
</div>
</li>
);
})}
</ul>
);
}
export default PeekList;
```
Usage
Give each row a short title, one to three sentences and one button. Leave about 100px free above the list, so the picture over the first row has room.
import { PeekList } from "@/components/ui/peek-list";
const ITEMS = [
{
title: "Kitcut button",
description: "A button that cracks open while it loads, then snaps back with a WooHoo.",
image: { src: "/showcase/kitcut-button.webp", alt: "Kitcut button, split in two with dots in the gap" },
cta: { label: "Open component", href: "/docs/kitcut-button" },
},
// one entry per row, 3 to 8 reads best
];
export function Components() {
return <PeekList items={ITEMS} aria-label="Our components" />;
}Keep each picture small (about 1000px wide, 3:2, WebP). Pictures load only when the list comes near the screen.
How it moves
A picture that peeks out where you point, and a row that opens like a pop up.
Point at a row and its picture fades in just above your pointer, growing a little, then trails it softly as you move, always inside that row, while the other rows soften to 70%. Leave the row and it fades away; move to the next row and that row's picture takes over.
Click and the row grows in place, pushing the rows below down while the others dim. The picture glides from your pointer to its seat on the left, the title glides to the top of the story, and the description and button drop in from above, line by line. The + turns into an X in the top right corner of the open row. Close it with the X, Esc or a click anywhere outside, and the row folds back. Phones never show the floating picture: one tap opens the row. With reduced motion nothing glides, things only fade.
Props
| Prop | Default | What it does |
|---|---|---|
items | — | The rows, in order. Each one: title, description, image { src, alt } and cta { label, href or onClick, target }. |
previewWidth | 240 | Width of the floating picture in px. In the open row the picture takes 42% of the width instead. |
ratio | 1.5 | Shape of the pictures, as width / height. 1.5 is 3:2, 4 / 3 is 4:3. |
aria-label | — | What screen readers call the list. |
className / style | — | On the list. It fills its width; leave about 100px free above it for the floating picture. |




