import { VStack } from "@seed-design/react"; import { useActivityZIndexBase } from "@seed-design/stackflow"; import { type StaticActivityComponentType, useActivity, useFlow } from "@stackflow/react/future"; import * as React from "react"; import { ActionButton } from "seed-design/ui/action-button"; import { BottomSheetBody, BottomSheetContent, BottomSheetFooter, BottomSheetRoot, } from "seed-design/ui/bottom-sheet"; import { TextField, TextFieldInput, TextFieldTextarea } from "seed-design/ui/text-field"; declare module "@stackflow/config" { interface Register { ActivityBottomSheetKeyboardPlayground: {}; } } /** * Playground for the keyboard reposition logic in `useDrawer`. Every knob below maps to a branch * the reposition code takes, and the readout shows the numbers it derives its decisions from, so a * regression is visible on-device without attaching a debugger. * * Reading the panel: * * - `bottom` is the lift the drawer applies; it should settle at `innerH - vvH - vvTop`. * - `height` should stay at the sheet's natural height until the sheet no longer fits, then clamp. * It must never grow past that, and it must come back when the keyboard closes. * - `visual top` is where the sheet's top edge actually sits on screen. Negative means the sheet is * cut off above the visible area. * - `visual bottom` should land on `vvH` — the sheet resting exactly on the keyboard. */ const CONTENT_MODES = ["short", "tall", "scroll"] as const; const FIELD_MODES = ["single", "multiple", "textarea", "editable", "no-keyboard"] as const; type ContentMode = (typeof CONTENT_MODES)[number]; type FieldMode = (typeof FIELD_MODES)[number]; const CONTENT_HINT = { short: "Fits with room to spare — height must not change at all.", tall: "Taller than the strip left above the keyboard — height must clamp, not overflow.", scroll: "Fixed-height scrollable body — the body scrolls, the sheet itself must not resize.", } as const satisfies Record; const FIELD_HINT = { single: "Baseline.", multiple: "Move between fields — the keyboard stays up, so the sheet must not drop.", textarea: "Content grows while open; natural height is captured once, so watch for drift.", editable: "contentEditable — exercises the third branch of the drawer's input check.", "no-keyboard": "Checkbox and button open no keyboard — the sheet must not move.", } as const satisfies Record; interface Metrics { innerH: number; vvH: number; vvTop: number; scale: number; bottom: string; height: string; visualTop: number; visualBottom: number; } function readMetrics(el: HTMLElement | null): Metrics | null { const vv = window.visualViewport; if (!el || !vv) return null; const rect = el.getBoundingClientRect(); return { innerH: window.innerHeight, vvH: Math.round(vv.height), vvTop: Math.round(vv.offsetTop), scale: Math.round(vv.scale * 100) / 100, bottom: el.style.bottom || "—", height: el.style.height || "—", visualTop: Math.round(rect.top - vv.offsetTop), visualBottom: Math.round(rect.bottom - vv.offsetTop), }; } /** * Polls per frame rather than listening for viewport events. The sheet also moves under a CSS * transition, which produces no events of its own until it ends, and a readout that lags the thing * it is measuring is worse than no readout. Re-rendering is gated on the numbers actually changing, * so a settled sheet costs one rect read per frame and nothing else. */ function useMetrics(open: boolean) { const contentRef = React.useRef(null); const [metrics, setMetrics] = React.useState(null); React.useEffect(() => { if (!open) return; let frame = 0; const tick = () => { const next = readMetrics(contentRef.current); setMetrics((prev) => { if (!next) return prev; if (prev && (Object.keys(next) as (keyof Metrics)[]).every((k) => prev[k] === next[k])) { return prev; } return next; }); frame = requestAnimationFrame(tick); }; frame = requestAnimationFrame(tick); return () => cancelAnimationFrame(frame); }, [open]); return { contentRef, metrics }; } function Row({ label, children }: { label: string; children: React.ReactNode }) { return ( {label}
{children}
); } function Choice({ values, value, onChange, }: { values: readonly T[]; value: T; onChange: (next: T) => void; }) { return ( <> {values.map((item) => ( onChange(item)} > {item} ))} ); } function Toggle({ label, value, onChange, }: { label: string; value: boolean; onChange: (next: boolean) => void; }) { return ( onChange(!value)} > {label}: {value ? "on" : "off"} ); } function Readout({ metrics }: { metrics: Metrics | null }) { if (!metrics) return null; // `bottom` should equal this; a mismatch is the pan-compensation regression. const expectedBottom = Math.max(metrics.innerH - metrics.vvH - metrics.vvTop, 0); // Both checks below only mean anything once something is covering the viewport. With the keyboard // down the sheet is meant to rest on the bottom of the screen, not on a keyboard that isn't there. const keyboardUp = metrics.innerH - metrics.vvH - metrics.vvTop > 60 || metrics.vvTop > 0; const clipped = metrics.visualTop < 0; const restingOnKeyboard = Math.abs(metrics.visualBottom - metrics.vvH) <= 1; return (
innerH {metrics.innerH} · vvH {metrics.vvH} · vvTop {metrics.vvTop} · scale {metrics.scale}
bottom {metrics.bottom} (expect {expectedBottom}px) · height {metrics.height}
visual top {metrics.visualTop} {clipped ? "⚠ CLIPPED" : "ok"} · visual bottom{" "} {metrics.visualBottom}{" "} {!keyboardUp ? "(keyboard down)" : restingOnKeyboard ? "ok" : "⚠ NOT ON KEYBOARD"}
); } function Fields({ mode }: { mode: FieldMode }) { switch (mode) { case "multiple": return ( ); case "textarea": return ( ); case "editable": return (
contentEditable — tap to focus
); case "no-keyboard": return ( ); default: return ( ); } } const ActivityBottomSheetKeyboardPlayground: StaticActivityComponentType< "ActivityBottomSheetKeyboardPlayground" > = () => { const { pop } = useFlow(); const activity = useActivity(); const [contentMode, setContentMode] = React.useState("short"); const [fieldMode, setFieldMode] = React.useState("single"); const [fixed, setFixed] = React.useState(false); const [withSnapPoints, setWithSnapPoints] = React.useState(false); const [repositionInputs, setRepositionInputs] = React.useState(true); const { contentRef, metrics } = useMetrics(activity.isActive); // Remounting on every structural change keeps the drawer from carrying a natural height it // captured under different settings, which would make the readout lie. const instanceKey = `${contentMode}-${fieldMode}-${fixed}-${withSnapPoints}-${repositionInputs}`; return ( !open && pop()} fixed={fixed} repositionInputs={repositionInputs} {...(withSnapPoints && { snapPoints: ["360px", 1] })} > {CONTENT_HINT[contentMode]} {FIELD_HINT[fieldMode]}
{contentMode !== "short" && Array.from( { length: contentMode === "tall" ? 8 : 14 }, (_, i) => `filler ${i + 1}`, ).map((label) => (
{label}
))}
pop()}> Close
); }; export default ActivityBottomSheetKeyboardPlayground;