|
| 1 | +import { type KeyboardEvent, type PointerEvent, useRef } from "react"; |
| 2 | +import { useT } from "../../i18n"; |
| 3 | +import { |
| 4 | + SIDEBAR_MAX_PX, |
| 5 | + SIDEBAR_MIN_PX, |
| 6 | + sidebarWidth, |
| 7 | + useSidebarWidth, |
| 8 | +} from "../../shell/sidebarWidth"; |
| 9 | + |
| 10 | +const KEY_STEP_PX = 24; |
| 11 | + |
| 12 | +/** |
| 13 | + * The right edge of the crews list: drag it, or use the arrow keys, to make |
| 14 | + * the list wider or narrower; double click goes back to the usual width. The |
| 15 | + * parent is `relative`. |
| 16 | + */ |
| 17 | +export function SidebarResizer() { |
| 18 | + const t = useT(); |
| 19 | + const width = useSidebarWidth(); |
| 20 | + const drag = useRef<{ x: number; width: number; scale: number } | null>(null); |
| 21 | + |
| 22 | + const start = (event: PointerEvent<HTMLDivElement>) => { |
| 23 | + const list = event.currentTarget.parentElement; |
| 24 | + if (!list) { |
| 25 | + return; |
| 26 | + } |
| 27 | + event.currentTarget.setPointerCapture(event.pointerId); |
| 28 | + // The window may be drawn larger than its layout units (zoom). |
| 29 | + const scale = list.offsetWidth ? list.getBoundingClientRect().width / list.offsetWidth : 1; |
| 30 | + drag.current = { x: event.clientX, width: sidebarWidth.get(), scale: scale || 1 }; |
| 31 | + }; |
| 32 | + const move = (event: PointerEvent<HTMLDivElement>) => { |
| 33 | + const from = drag.current; |
| 34 | + if (from) { |
| 35 | + sidebarWidth.set(from.width + (event.clientX - from.x) / from.scale); |
| 36 | + } |
| 37 | + }; |
| 38 | + const end = () => { |
| 39 | + if (drag.current) { |
| 40 | + drag.current = null; |
| 41 | + sidebarWidth.set(sidebarWidth.get(), true); |
| 42 | + } |
| 43 | + }; |
| 44 | + const onKey = (event: KeyboardEvent<HTMLDivElement>) => { |
| 45 | + const step = event.shiftKey ? KEY_STEP_PX * 4 : KEY_STEP_PX; |
| 46 | + if (event.key === "ArrowRight") { |
| 47 | + sidebarWidth.set(sidebarWidth.get() + step, true); |
| 48 | + } else if (event.key === "ArrowLeft") { |
| 49 | + sidebarWidth.set(sidebarWidth.get() - step, true); |
| 50 | + } else { |
| 51 | + return; |
| 52 | + } |
| 53 | + event.preventDefault(); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + // biome-ignore lint/a11y/useSemanticElements: a separator that can be dragged and focused is a widget, not an <hr> |
| 58 | + <div |
| 59 | + role="separator" |
| 60 | + aria-orientation="vertical" |
| 61 | + aria-label={t.common.resize} |
| 62 | + aria-valuemin={SIDEBAR_MIN_PX} |
| 63 | + aria-valuemax={SIDEBAR_MAX_PX} |
| 64 | + aria-valuenow={width} |
| 65 | + tabIndex={0} |
| 66 | + onPointerDown={start} |
| 67 | + onPointerMove={move} |
| 68 | + onPointerUp={end} |
| 69 | + onPointerCancel={end} |
| 70 | + onDoubleClick={() => sidebarWidth.reset()} |
| 71 | + onKeyDown={onKey} |
| 72 | + className="group absolute inset-y-0 -right-1 z-30 w-2 cursor-col-resize touch-none focus-visible:outline-none" |
| 73 | + > |
| 74 | + <span className="absolute inset-y-0 right-[3px] w-0.5 bg-transparent transition-colors group-hover:bg-line-strong group-focus-visible:bg-work group-active:bg-work" /> |
| 75 | + </div> |
| 76 | + ); |
| 77 | +} |
0 commit comments