|
| 1 | +import { |
| 2 | + type KeyboardEvent as ReactKeyboardEvent, |
| 3 | + type ReactNode, |
| 4 | + useCallback, |
| 5 | + useEffect, |
| 6 | + useId, |
| 7 | + useRef, |
| 8 | +} from "react"; |
| 9 | +import { Hairline, Icons, SectionHeader } from "../design-system"; |
| 10 | + |
| 11 | +export const DRAWER_MIN_WIDTH = 360; |
| 12 | +export const DRAWER_MAX_WIDTH = 640; |
| 13 | +export const DRAWER_DEFAULT_WIDTH = 420; |
| 14 | +const KEYBOARD_STEP = 10; |
| 15 | + |
| 16 | +export interface DrawerProps { |
| 17 | + open: boolean; |
| 18 | + width: number; |
| 19 | + onToggle: () => void; |
| 20 | + onWidthChange: (width: number) => void; |
| 21 | + children?: ReactNode; |
| 22 | + /** DOM id used by the topbar toggle `aria-controls`. */ |
| 23 | + id?: string; |
| 24 | +} |
| 25 | + |
| 26 | +function clampWidth(width: number) { |
| 27 | + return Math.max(DRAWER_MIN_WIDTH, Math.min(DRAWER_MAX_WIDTH, Math.round(width))); |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Docked, resizable chat drawer. Sits inside the app-shell grid — not overlay. |
| 32 | + * The primitive `design-system/Drawer` remains for modal contexts. |
| 33 | + */ |
| 34 | +export function Drawer({ open, width, onToggle, onWidthChange, children, id }: DrawerProps) { |
| 35 | + const generatedId = useId(); |
| 36 | + const drawerId = id ?? `chat-drawer-${generatedId}`; |
| 37 | + const asideRef = useRef<HTMLElement>(null); |
| 38 | + const draggingRef = useRef(false); |
| 39 | + const startXRef = useRef(0); |
| 40 | + const startWidthRef = useRef(0); |
| 41 | + |
| 42 | + const onPointerMove = useCallback( |
| 43 | + (e: PointerEvent) => { |
| 44 | + if (!draggingRef.current) return; |
| 45 | + const delta = startXRef.current - e.clientX; |
| 46 | + onWidthChange(clampWidth(startWidthRef.current + delta)); |
| 47 | + }, |
| 48 | + [onWidthChange], |
| 49 | + ); |
| 50 | + |
| 51 | + const onPointerUp = useCallback(() => { |
| 52 | + if (!draggingRef.current) return; |
| 53 | + draggingRef.current = false; |
| 54 | + document.body.style.cursor = ""; |
| 55 | + document.body.style.userSelect = ""; |
| 56 | + window.removeEventListener("pointermove", onPointerMove); |
| 57 | + window.removeEventListener("pointerup", onPointerUp); |
| 58 | + }, [onPointerMove]); |
| 59 | + |
| 60 | + useEffect( |
| 61 | + () => () => { |
| 62 | + window.removeEventListener("pointermove", onPointerMove); |
| 63 | + window.removeEventListener("pointerup", onPointerUp); |
| 64 | + }, |
| 65 | + [onPointerMove, onPointerUp], |
| 66 | + ); |
| 67 | + |
| 68 | + const onHandlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => { |
| 69 | + e.preventDefault(); |
| 70 | + draggingRef.current = true; |
| 71 | + startXRef.current = e.clientX; |
| 72 | + startWidthRef.current = width; |
| 73 | + document.body.style.cursor = "col-resize"; |
| 74 | + document.body.style.userSelect = "none"; |
| 75 | + window.addEventListener("pointermove", onPointerMove); |
| 76 | + window.addEventListener("pointerup", onPointerUp); |
| 77 | + }; |
| 78 | + |
| 79 | + const onHandleKeyDown = (e: ReactKeyboardEvent<HTMLDivElement>) => { |
| 80 | + let next: number | null = null; |
| 81 | + switch (e.key) { |
| 82 | + case "ArrowLeft": |
| 83 | + next = clampWidth(width + KEYBOARD_STEP); |
| 84 | + break; |
| 85 | + case "ArrowRight": |
| 86 | + next = clampWidth(width - KEYBOARD_STEP); |
| 87 | + break; |
| 88 | + case "Home": |
| 89 | + next = DRAWER_MIN_WIDTH; |
| 90 | + break; |
| 91 | + case "End": |
| 92 | + next = DRAWER_MAX_WIDTH; |
| 93 | + break; |
| 94 | + default: |
| 95 | + return; |
| 96 | + } |
| 97 | + e.preventDefault(); |
| 98 | + onWidthChange(next); |
| 99 | + }; |
| 100 | + |
| 101 | + return ( |
| 102 | + <aside |
| 103 | + ref={asideRef} |
| 104 | + id={drawerId} |
| 105 | + aria-label="Chat drawer" |
| 106 | + className="relative h-full overflow-hidden border-l border-[var(--ds-border)] bg-[var(--ds-bg-surface)]" |
| 107 | + style={{ |
| 108 | + width: open ? `${width}px` : 0, |
| 109 | + transition: `width var(--ds-motion-base) var(--ds-ease-out)`, |
| 110 | + }} |
| 111 | + > |
| 112 | + {open && ( |
| 113 | + // biome-ignore lint/a11y/useSemanticElements: separator must be interactive (draggable + keyboard resize) — <hr> cannot carry pointer/keyboard handlers |
| 114 | + <div |
| 115 | + role="separator" |
| 116 | + aria-orientation="vertical" |
| 117 | + aria-valuenow={width} |
| 118 | + aria-valuemin={DRAWER_MIN_WIDTH} |
| 119 | + aria-valuemax={DRAWER_MAX_WIDTH} |
| 120 | + aria-label="Resize chat drawer" |
| 121 | + tabIndex={0} |
| 122 | + onPointerDown={onHandlePointerDown} |
| 123 | + onKeyDown={onHandleKeyDown} |
| 124 | + className="absolute left-0 top-0 z-10 h-full w-1.5 -translate-x-1/2 cursor-col-resize outline-none focus-visible:bg-[var(--ds-accent)]/60 hover:bg-[var(--ds-accent)]/40" |
| 125 | + style={{ touchAction: "none" }} |
| 126 | + /> |
| 127 | + )} |
| 128 | + {open && ( |
| 129 | + <div className="flex h-full w-full flex-col" style={{ width: `${width}px` }}> |
| 130 | + <header className="flex items-center justify-between gap-4 border-b border-[var(--ds-border)] px-4 py-3"> |
| 131 | + <SectionHeader label="Chat" size="sm" /> |
| 132 | + <button |
| 133 | + type="button" |
| 134 | + onClick={onToggle} |
| 135 | + aria-label="Collapse chat drawer" |
| 136 | + className="inline-flex h-7 w-7 items-center justify-center rounded-[var(--ds-radius-sm)] text-[var(--ds-fg-muted)] transition-colors duration-[var(--ds-motion-fast)] hover:bg-[var(--ds-bg-hover)] hover:text-[var(--ds-fg-primary)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--ds-accent-ring)]" |
| 137 | + > |
| 138 | + <Icons.PanelRightClose className="size-4" /> |
| 139 | + </button> |
| 140 | + </header> |
| 141 | + {children ? ( |
| 142 | + <div className="flex-1 overflow-auto">{children}</div> |
| 143 | + ) : ( |
| 144 | + <div className="flex flex-1 flex-col gap-4 overflow-auto p-4"> |
| 145 | + <Hairline label="Awaiting wire" /> |
| 146 | + <p className="text-[var(--ds-text-sm)] text-[var(--ds-fg-muted)]"> |
| 147 | + Chat shell mounts here in M6.5. |
| 148 | + </p> |
| 149 | + </div> |
| 150 | + )} |
| 151 | + </div> |
| 152 | + )} |
| 153 | + </aside> |
| 154 | + ); |
| 155 | +} |
0 commit comments