nandi/oripublic Fork 0
4166f8fba899b222fab287c398dcab9bf6f6e5c9
Commits
Clone
git clone https://git.rickub.com/nandi/ori.git
git clone ssh://git@rickub.com/nandi/ori.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

forked from bots-garden/ori

ResizeHandle.tsx · 147 lines · 3.8 KBTypeScript Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
 * ResizeHandle is the thin vertical grip between two columns. Dragging it
 * with the pointer resizes the column on its left; the arrow keys resize it
 * step by step, Home / End jump to the bounds, and a double-click restores
 * the default width. It exposes the ARIA separator pattern so the current
 * width is readable by assistive technology.
 *
 * The handle owns no width itself: it reports the wanted width through
 * `onResize` and the parent decides (and clamps) what to apply.
 *
 * @example
 * <ResizeHandle
 *   label="Resize the file tree"
 *   width={width}
 *   min={120}
 *   max={800}
 *   onResize={setFileTreeWidth}
 *   onReset={resetFileTreeWidth}
 * />
 */

import {
	type KeyboardEvent,
	type PointerEvent as ReactPointerEvent,
	useRef,
	useState,
} from "react";

/** Pixels moved per arrow-key press. */
export const KEYBOARD_RESIZE_STEP = 16;

export interface ResizeHandleProps {
	/** Accessible name of the separator. */
	label: string;
	/** Current width of the resized column, in pixels. */
	width: number;
	/** Narrowest width the parent accepts, in pixels. */
	min: number;
	/** Widest width the parent accepts, in pixels. */
	max: number;
	/** Called with the wanted width whenever the user drags or presses a key. */
	onResize: (width: number) => void;
	/** Called on double-click to restore the default width. */
	onReset: () => void;
}

interface DragOrigin {
	pointerX: number;
	width: number;
}

export function ResizeHandle({
	label,
	width,
	min,
	max,
	onResize,
	onReset,
}: ResizeHandleProps) {
	// The origin of the current drag; null when the pointer is up.
	const dragOrigin = useRef<DragOrigin | null>(null);
	const [dragging, setDragging] = useState(false);

	const startDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
		// Prevents the browser from starting a text selection while dragging.
		event.preventDefault();
		dragOrigin.current = { pointerX: event.clientX, width };
		// Capture keeps the move/up events coming even when the pointer
		// leaves the thin handle (jsdom lacks the API, hence the guard).
		event.currentTarget.setPointerCapture?.(event.pointerId);
		setDragging(true);
	};

	const moveDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
		const origin = dragOrigin.current;
		if (origin === null) {
			return;
		}
		onResize(origin.width + event.clientX - origin.pointerX);
	};

	const endDrag = (event: ReactPointerEvent<HTMLDivElement>) => {
		if (dragOrigin.current === null) {
			return;
		}
		dragOrigin.current = null;
		event.currentTarget.releasePointerCapture?.(event.pointerId);
		setDragging(false);
	};

	const handleKey = (event: KeyboardEvent<HTMLDivElement>) => {
		const wanted = widthAfterKey(event.key, width, min, max);
		if (wanted === null) {
			return;
		}
		event.preventDefault();
		onResize(wanted);
	};

	return (
		<div
			role="separator"
			aria-label={label}
			aria-orientation="vertical"
			aria-valuenow={width}
			aria-valuemin={min}
			aria-valuemax={max}
			tabIndex={0}
			className={`resize-handle${dragging ? " resize-handle-active" : ""}`}
			onPointerDown={startDrag}
			onPointerMove={moveDrag}
			onPointerUp={endDrag}
			onPointerCancel={endDrag}
			onKeyDown={handleKey}
			onDoubleClick={onReset}
		/>
	);
}

/**
 * widthAfterKey maps a key press to the wanted width, or null when the key
 * is not a resize key so the event keeps its default behaviour.
 *
 * @example
 * widthAfterKey("ArrowRight", 200, 120, 800); // 216
 * widthAfterKey("Home", 200, 120, 800);       // 120
 * widthAfterKey("Enter", 200, 120, 800);      // null
 */
export function widthAfterKey(
	key: string,
	width: number,
	min: number,
	max: number,
): number | null {
	switch (key) {
		case "ArrowLeft":
			return width - KEYBOARD_RESIZE_STEP;
		case "ArrowRight":
			return width + KEYBOARD_RESIZE_STEP;
		case "Home":
			return min;
		case "End":
			return max;
		default:
			return null;
	}
}