Draggable bottom sheet
A phone-style bottom sheet with three snap heights: drag the handle, flick it, or use the keyboard, and the map behind it dims as it rises.
"use client";
import {
useRef,
useState,
type CSSProperties,
type KeyboardEvent,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./bottom-sheet.css";
const SNAPS = [0.14, 0.5, 0.92]; // fraction of the frame height that is covered
const LABELS = ["Collapsed", "Half open", "Full"];
const PLACES = [
{ name: "Café Marlowe", meta: "Coffee · 4 min walk", score: "4.8" },
{ name: "Green Room Books", meta: "Bookshop · 6 min walk", score: "4.7" },
{ name: "Tasca do Rio", meta: "Portuguese · 9 min walk", score: "4.6" },
{ name: "Pier 12 Gallery", meta: "Art · 11 min walk", score: "4.5" },
{ name: "Night Owl Records", meta: "Music · 12 min walk", score: "4.9" },
];
/**
* BottomSheet
* - Height is a CSS variable --h (0–1). While dragging it follows the finger with no transition;
* on release it snaps to the nearest point, or the next one if the flick was fast.
* - The handle is a role="slider" so keyboard and screen reader users can change the height.
*/
export function BottomSheet({ theme = "light" }: { theme?: "light" | "dark" }) {
const frame = useRef<HTMLDivElement>(null);
const [snap, setSnap] = useState(1);
const [drag, setDrag] = useState<number | null>(null);
const start = useRef({ y: 0, h: 0, t: 0 });
const h = drag ?? SNAPS[snap];
const down = (e: ReactPointerEvent<HTMLDivElement>) => {
e.currentTarget.setPointerCapture(e.pointerId);
start.current = { y: e.clientY, h: SNAPS[snap], t: performance.now() };
setDrag(SNAPS[snap]);
};
const move = (e: ReactPointerEvent<HTMLDivElement>) => {
if (drag === null || !frame.current) return;
const dy = (start.current.y - e.clientY) / frame.current.clientHeight;
setDrag(Math.min(0.97, Math.max(0.08, start.current.h + dy)));
};
const up = (e: ReactPointerEvent<HTMLDivElement>) => {
if (drag === null || !frame.current) return;
const velocity =
(start.current.y - e.clientY) /
frame.current.clientHeight /
Math.max(1, performance.now() - start.current.t); // per ms
let next = SNAPS.reduce(
(best, s, i) =>
Math.abs(s - drag) < Math.abs(SNAPS[best] - drag) ? i : best,
0,
);
if (velocity > 0.0015)
next = Math.min(SNAPS.length - 1, Math.max(next, snap + 1));
if (velocity < -0.0015) next = Math.max(0, Math.min(next, snap - 1));
setSnap(next);
setDrag(null);
};
const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
if (e.key === "ArrowUp") setSnap((s) => Math.min(2, s + 1));
else if (e.key === "ArrowDown") setSnap((s) => Math.max(0, s - 1));
else if (e.key === "Home") setSnap(0);
else if (e.key === "End") setSnap(2);
else return;
e.preventDefault();
};
return (
<div
ref={frame}
className="gu-sheet"
data-theme={theme}
style={{ "--h": h } as CSSProperties}
data-dragging={drag !== null ? "" : undefined}
>
{/* Code-made "map" behind the sheet */}
<div className="gu-sheet__map" aria-hidden="true">
<svg viewBox="0 0 400 700" preserveAspectRatio="xMidYMid slice">
<path d="M-20 160 C 120 120, 220 260, 420 210" />
<path d="M60 -20 C 90 200, 40 420, 140 720" />
<path d="M-20 470 C 150 430, 260 520, 420 480" />
<path
d="M300 -20 C 260 180, 330 380, 280 720"
className="gu-sheet__river"
/>
{[
[120, 190],
[230, 300],
[90, 420],
[310, 520],
[180, 560],
].map(([x, y], i) => (
<g key={i} transform={`translate(${x} ${y})`}>
<circle r="14" />
<circle r="5" className="gu-sheet__dot" />
</g>
))}
</svg>
<span className="gu-sheet__me" />
</div>
<div className="gu-sheet__scrim" />
<section className="gu-sheet__panel" aria-label="Nearby places">
<div
className="gu-sheet__handle"
role="slider"
tabIndex={0}
aria-label="Sheet height"
aria-valuemin={0}
aria-valuemax={2}
aria-valuenow={snap}
aria-valuetext={LABELS[snap]}
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
onKeyDown={onKey}
>
<i />
<div className="gu-sheet__title">
<b>Open near you</b>
<small>5 places · sorted by rating</small>
</div>
</div>
<ul className="gu-sheet__list" data-lenis-prevent>
{PLACES.map((p) => (
<li key={p.name}>
<span className="gu-sheet__pin" aria-hidden="true">
{p.name[0]}
</span>
<span>
<b>{p.name}</b>
<small>{p.meta}</small>
</span>
<em>★ {p.score}</em>
</li>
))}
</ul>
</section>
</div>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Draggable bottom sheet
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A mobile map screen with a draggable bottom sheet ("Open near you") that snaps to three heights (14%, 50%, 92%). Dragging follows the finger, flicks jump to the next snap, the map zooms back and dims as the sheet rises.
## Layout and look
- Phone frame: 360×640, 8px Ink bezel, radius 44px.
- Map: code-drawn (SVG) roads in a pale tone, a blue river, orange place pins with halos, and a pulsing blue "you are here" dot.
- Sheet: card background, radius 28px top corners, soft upward shadow. Handle: 44×5px grabber pill, title "Open near you" (20px, weight 900) and a muted subtitle.
- List rows 64px: orange 44px letter tile, name + meta, rating on the right.
## Behavior and motion
1. Sheet height = CSS var --h × frame height. While dragging, transitions are off.
2. On release: nearest snap, but a fast upward/downward flick (> 0.0015 frame-heights per ms) moves at least one snap in that direction.
3. Snap with a springy cubic-bezier(0.32, 1.25, 0.5, 1) over 550ms.
4. Map scale 1.06 → 1 and shifts up; a scrim fades in above 50% height.
## Touch
Pointer events with capture; the list scrolls inside with overscroll contained.
## Accessibility
- The handle is a role="slider" (0–2) with "Collapsed / Half open / Full" value text; ArrowUp/Down, Home/End change it.
- prefers-reduced-motion: instant snaps, no ping.
## Props
theme. Edit SNAPS and PLACES.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### BottomSheet.tsx
```tsx
"use client";
import {
useRef,
useState,
type CSSProperties,
type KeyboardEvent,
type PointerEvent as ReactPointerEvent,
} from "react";
import "./bottom-sheet.css";
const SNAPS = [0.14, 0.5, 0.92]; // fraction of the frame height that is covered
const LABELS = ["Collapsed", "Half open", "Full"];
const PLACES = [
{ name: "Café Marlowe", meta: "Coffee · 4 min walk", score: "4.8" },
{ name: "Green Room Books", meta: "Bookshop · 6 min walk", score: "4.7" },
{ name: "Tasca do Rio", meta: "Portuguese · 9 min walk", score: "4.6" },
{ name: "Pier 12 Gallery", meta: "Art · 11 min walk", score: "4.5" },
{ name: "Night Owl Records", meta: "Music · 12 min walk", score: "4.9" },
];
/**
* BottomSheet
* - Height is a CSS variable --h (0–1). While dragging it follows the finger with no transition;
* on release it snaps to the nearest point, or the next one if the flick was fast.
* - The handle is a role="slider" so keyboard and screen reader users can change the height.
*/
export function BottomSheet({ theme = "light" }: { theme?: "light" | "dark" }) {
const frame = useRef<HTMLDivElement>(null);
const [snap, setSnap] = useState(1);
const [drag, setDrag] = useState<number | null>(null);
const start = useRef({ y: 0, h: 0, t: 0 });
const h = drag ?? SNAPS[snap];
const down = (e: ReactPointerEvent<HTMLDivElement>) => {
e.currentTarget.setPointerCapture(e.pointerId);
start.current = { y: e.clientY, h: SNAPS[snap], t: performance.now() };
setDrag(SNAPS[snap]);
};
const move = (e: ReactPointerEvent<HTMLDivElement>) => {
if (drag === null || !frame.current) return;
const dy = (start.current.y - e.clientY) / frame.current.clientHeight;
setDrag(Math.min(0.97, Math.max(0.08, start.current.h + dy)));
};
const up = (e: ReactPointerEvent<HTMLDivElement>) => {
if (drag === null || !frame.current) return;
const velocity =
(start.current.y - e.clientY) /
frame.current.clientHeight /
Math.max(1, performance.now() - start.current.t); // per ms
let next = SNAPS.reduce(
(best, s, i) =>
Math.abs(s - drag) < Math.abs(SNAPS[best] - drag) ? i : best,
0,
);
if (velocity > 0.0015)
next = Math.min(SNAPS.length - 1, Math.max(next, snap + 1));
if (velocity < -0.0015) next = Math.max(0, Math.min(next, snap - 1));
setSnap(next);
setDrag(null);
};
const onKey = (e: KeyboardEvent<HTMLDivElement>) => {
if (e.key === "ArrowUp") setSnap((s) => Math.min(2, s + 1));
else if (e.key === "ArrowDown") setSnap((s) => Math.max(0, s - 1));
else if (e.key === "Home") setSnap(0);
else if (e.key === "End") setSnap(2);
else return;
e.preventDefault();
};
return (
<div
ref={frame}
className="gu-sheet"
data-theme={theme}
style={{ "--h": h } as CSSProperties}
data-dragging={drag !== null ? "" : undefined}
>
{/* Code-made "map" behind the sheet */}
<div className="gu-sheet__map" aria-hidden="true">
<svg viewBox="0 0 400 700" preserveAspectRatio="xMidYMid slice">
<path d="M-20 160 C 120 120, 220 260, 420 210" />
<path d="M60 -20 C 90 200, 40 420, 140 720" />
<path d="M-20 470 C 150 430, 260 520, 420 480" />
<path
d="M300 -20 C 260 180, 330 380, 280 720"
className="gu-sheet__river"
/>
{[
[120, 190],
[230, 300],
[90, 420],
[310, 520],
[180, 560],
].map(([x, y], i) => (
<g key={i} transform={`translate(${x} ${y})`}>
<circle r="14" />
<circle r="5" className="gu-sheet__dot" />
</g>
))}
</svg>
<span className="gu-sheet__me" />
</div>
<div className="gu-sheet__scrim" />
<section className="gu-sheet__panel" aria-label="Nearby places">
<div
className="gu-sheet__handle"
role="slider"
tabIndex={0}
aria-label="Sheet height"
aria-valuemin={0}
aria-valuemax={2}
aria-valuenow={snap}
aria-valuetext={LABELS[snap]}
onPointerDown={down}
onPointerMove={move}
onPointerUp={up}
onPointerCancel={up}
onKeyDown={onKey}
>
<i />
<div className="gu-sheet__title">
<b>Open near you</b>
<small>5 places · sorted by rating</small>
</div>
</div>
<ul className="gu-sheet__list" data-lenis-prevent>
{PLACES.map((p) => (
<li key={p.name}>
<span className="gu-sheet__pin" aria-hidden="true">
{p.name[0]}
</span>
<span>
<b>{p.name}</b>
<small>{p.meta}</small>
</span>
<em>★ {p.score}</em>
</li>
))}
</ul>
</section>
</div>
);
}
```
### bottom-sheet.css
```css
/* BottomSheet: change these colors to match your brand. */
.gu-sheet {
--map: #e9e2d3;
--road: #fbf8f2;
--water: #c9d4ff;
--sheet: #fbf8f2;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.1);
--accent: #ff5a1f;
--focus: #2b35ff;
position: relative;
width: min(100%, 360px);
height: 640px;
overflow: hidden;
border: 8px solid #0e0e10;
border-radius: 44px;
background: var(--map);
color: var(--text);
font-family: system-ui, sans-serif;
touch-action: none;
}
.gu-sheet[data-theme="dark"] {
--map: #1f1f23;
--road: #2c2c31;
--water: #1d2250;
--sheet: #18181b;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.1);
--focus: #d7ff3a;
border-color: #2c2c31;
}
.gu-sheet__map,
.gu-sheet__map svg {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
}
/* The map zooms back slightly as the sheet rises */
.gu-sheet__map {
transform: scale(calc(1.06 - var(--h) * 0.06))
translateY(calc(var(--h) * -12%));
transition: transform 0.55s cubic-bezier(0.32, 1.25, 0.5, 1);
}
.gu-sheet__map path {
fill: none;
stroke: var(--road);
stroke-width: 18;
stroke-linecap: round;
}
.gu-sheet__map .gu-sheet__river {
stroke: var(--water);
stroke-width: 34;
}
.gu-sheet__map circle {
fill: var(--accent);
opacity: 0.25;
}
.gu-sheet__map .gu-sheet__dot {
opacity: 1;
}
.gu-sheet__me {
position: absolute;
top: 44%;
left: 48%;
width: 16px;
height: 16px;
border: 3px solid #fff;
border-radius: 50%;
background: #2b35ff;
box-shadow: 0 0 0 0 rgb(43 53 255 / 0.4);
animation: gu-sheet-ping 2s infinite;
}
@keyframes gu-sheet-ping {
70% {
box-shadow: 0 0 0 18px rgb(43 53 255 / 0);
}
}
.gu-sheet__scrim {
position: absolute;
inset: 0;
background: #0e0e10;
opacity: calc((var(--h) - 0.5) * 0.9);
pointer-events: none;
}
.gu-sheet__panel {
position: absolute;
left: 0;
right: 0;
bottom: 0;
display: grid;
grid-template-rows: auto 1fr;
height: calc(var(--h) * 100%);
border-radius: 28px 28px 0 0;
background: var(--sheet);
box-shadow: 0 -12px 40px -12px rgb(0 0 0 / 0.3);
transition: height 0.55s cubic-bezier(0.32, 1.25, 0.5, 1); /* slight spring overshoot */
}
.gu-sheet[data-dragging] .gu-sheet__panel,
.gu-sheet[data-dragging] .gu-sheet__map {
transition: none;
}
.gu-sheet__handle {
display: grid;
gap: 10px;
padding: 10px 20px 14px;
border-radius: 28px 28px 0 0;
cursor: grab;
user-select: none;
}
.gu-sheet[data-dragging] .gu-sheet__handle {
cursor: grabbing;
}
.gu-sheet__handle:focus-visible {
outline: 3px solid var(--focus);
outline-offset: -3px;
}
.gu-sheet__handle i {
justify-self: center;
width: 44px;
height: 5px;
border-radius: 5px;
background: var(--line);
background: color-mix(in srgb, var(--text) 25%, transparent);
}
.gu-sheet__title {
display: grid;
}
.gu-sheet__title b {
font-size: 1.25rem;
font-weight: 900;
letter-spacing: -0.02em;
}
.gu-sheet__title small {
color: var(--muted);
}
.gu-sheet__list {
margin: 0;
padding: 0 12px 20px;
overflow: auto;
list-style: none;
overscroll-behavior: contain;
touch-action: pan-y;
}
.gu-sheet__list li {
display: grid;
grid-template-columns: 44px 1fr auto;
align-items: center;
gap: 12px;
min-height: 64px;
padding: 0 8px;
border-bottom: 1px solid var(--line);
}
.gu-sheet__list li > span:nth-child(2) {
display: grid;
}
.gu-sheet__list small {
color: var(--muted);
}
.gu-sheet__list em {
font-style: normal;
font-weight: 800;
font-size: 0.85rem;
}
.gu-sheet__pin {
display: grid;
place-items: center;
width: 44px;
height: 44px;
border-radius: 14px;
background: var(--accent);
color: #0e0e10;
font-weight: 900;
}
@media (prefers-reduced-motion: reduce) {
.gu-sheet__panel,
.gu-sheet__map {
transition-duration: 0.01s;
}
.gu-sheet__me {
animation: none;
}
}
```
You might also want
Next piece: Empty states setCart drawer
Components, advanced
Snap carousel with progress
Components, intermediate
Swipe card stack
Components, advanced