Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

box_resize: Add library for resizable centered boxes.

Aman Agrawale7f5adaf6164
diff --git a/web/src/box_resize.ts b/web/src/box_resize.ts
new file mode 100644
index 00000000000..d33261c0060
--- /dev/null
+++ b/web/src/box_resize.ts
@@ -0,0 +1,126 @@
+// Attach resize handles to a centered box. The caller keeps the box
+// centered (via CSS, or via the `on_resize` callback for a Tippy
+// overlay that needs `popperInstance.update()` after each change).
+// Because the box stays centered, dragging one edge by `delta` grows
+// the box by `2 * delta` so the cursor tracks the dragged edge.
+// Size bounds and any mobile-disable behavior live in CSS.
+
+type Direction =
+ | "top"
+ | "right"
+ | "bottom"
+ | "left"
+ | "top_left"
+ | "top_right"
+ | "bottom_left"
+ | "bottom_right";
+
+type HandleSpec = {
+ grows_width: boolean;
+ grows_height: boolean;
+ // -1 for handles on the left/top edge, whose pointer moves in the
+ // negative direction to grow the box.
+ dx_sign: 1 | -1;
+ dy_sign: 1 | -1;
+};
+
+const HANDLE_SPECS: Record<Direction, HandleSpec> = {
+ right: {grows_width: true, grows_height: false, dx_sign: 1, dy_sign: 1},
+ left: {grows_width: true, grows_height: false, dx_sign: -1, dy_sign: 1},
+ bottom: {grows_width: false, grows_height: true, dx_sign: 1, dy_sign: 1},
+ top: {grows_width: false, grows_height: true, dx_sign: 1, dy_sign: -1},
+ bottom_right: {grows_width: true, grows_height: true, dx_sign: 1, dy_sign: 1},
+ bottom_left: {grows_width: true, grows_height: true, dx_sign: -1, dy_sign: 1},
+ top_right: {grows_width: true, grows_height: true, dx_sign: 1, dy_sign: -1},
+ top_left: {grows_width: true, grows_height: true, dx_sign: -1, dy_sign: -1},
+};
+
+export function make_resizable(
+ box: HTMLElement,
+ directions: Direction[],
+ on_resize?: () => void,
+): () => void {
+ const handles: HTMLElement[] = [];
+ for (const direction of directions) {
+ const handle = document.createElement("div");
+ handle.classList.add(
+ "resizable-box-handle",
+ `resizable-box-handle-${direction.replaceAll("_", "-")}`,
+ );
+ attach_handle(handle, box, HANDLE_SPECS[direction], on_resize);
+ box.append(handle);
+ handles.push(handle);
+ }
+ return () => {
+ for (const handle of handles) {
+ handle.remove();
+ }
+ };
+}
+
+function parse_px_bound(value: string, fallback: number): number {
+ if (value === "" || value === "auto" || value === "none") {
+ return fallback;
+ }
+ const parsed = Number.parseFloat(value);
+ return Number.isFinite(parsed) ? parsed : fallback;
+}
+
+function clamp(value: number, min: number, max: number): number {
+ return Math.min(max, Math.max(min, value));
+}
+
+function attach_handle(
+ handle: HTMLElement,
+ box: HTMLElement,
+ spec: HandleSpec,
+ on_resize: (() => void) | undefined,
+): void {
+ let start_x = 0;
+ let start_y = 0;
+ let start_width = 0;
+ let start_height = 0;
+
+ function on_pointer_move(e: PointerEvent): void {
+ // Clamp against CSS bounds so dragging past min/max doesn't
+ // build up an invisible overshoot the user then has to drag
+ // back through before the box starts responding again.
+ const style = getComputedStyle(box);
+ if (spec.grows_width) {
+ const delta = spec.dx_sign * (e.clientX - start_x);
+ const min_width = parse_px_bound(style.minWidth, 0);
+ const max_width = parse_px_bound(style.maxWidth, Number.POSITIVE_INFINITY);
+ box.style.width = `${clamp(start_width + 2 * delta, min_width, max_width)}px`;
+ }
+ if (spec.grows_height) {
+ const delta = spec.dy_sign * (e.clientY - start_y);
+ const min_height = parse_px_bound(style.minHeight, 0);
+ const max_height = parse_px_bound(style.maxHeight, Number.POSITIVE_INFINITY);
+ box.style.height = `${clamp(start_height + 2 * delta, min_height, max_height)}px`;
+ }
+ on_resize?.();
+ }
+
+ function end_drag(e: PointerEvent): void {
+ handle.releasePointerCapture(e.pointerId);
+ handle.removeEventListener("pointermove", on_pointer_move);
+ handle.removeEventListener("pointerup", end_drag);
+ // `pointercancel` fires when the browser takes the gesture
+ // away from us (palm rejection, alt-tab, etc.); without
+ // cleaning up, `pointermove` would keep resizing the box.
+ handle.removeEventListener("pointercancel", end_drag);
+ }
+
+ handle.addEventListener("pointerdown", (e: PointerEvent) => {
+ e.preventDefault();
+ const rect = box.getBoundingClientRect();
+ start_x = e.clientX;
+ start_y = e.clientY;
+ start_width = rect.width;
+ start_height = rect.height;
+ handle.setPointerCapture(e.pointerId);
+ handle.addEventListener("pointermove", on_pointer_move);
+ handle.addEventListener("pointerup", end_drag);
+ handle.addEventListener("pointercancel", end_drag);
+ });
+}
diff --git a/web/src/bundles/app.ts b/web/src/bundles/app.ts
index 976b3f6c4e9..f38681cea98 100644
--- a/web/src/bundles/app.ts
+++ b/web/src/bundles/app.ts
@@ -50,6 +50,7 @@ import "../../styles/user_circles.css";
import "../../styles/left_sidebar.css";
import "../../styles/right_sidebar.css";
import "../../styles/lightbox.css";
+import "../../styles/box_resize.css";
import "../../styles/popovers.css";
import "../../styles/recent_view.css";
import "../../styles/typing_notifications.css";
diff --git a/web/styles/app_variables.css b/web/styles/app_variables.css
index 78ac4b0b020..2a7979a5f51 100644
--- a/web/styles/app_variables.css
+++ b/web/styles/app_variables.css
@@ -169,6 +169,12 @@
Chrome 105) that do not support dynamic viewport
units. 100% is the legacy full-height value. */
--full-height-dynamic-viewport: 100%;
+
+ /* Exposed so callers can reserve matching padding on a
+ resizable box, keeping handles out of the clickable content
+ area. Consumed by box_resize.css. */
+ --resizable-box-handle-size: 10px;
+
--gif-scroll-container-height: 80vh;
@supports (height: 100dvh) {
diff --git a/web/styles/box_resize.css b/web/styles/box_resize.css
new file mode 100644
index 00000000000..28022e5e4a8
--- /dev/null
+++ b/web/styles/box_resize.css
@@ -0,0 +1,73 @@
+.resizable-box-handle {
+ position: absolute;
+ user-select: none;
+ touch-action: none;
+ background: transparent;
+
+ &.resizable-box-handle-top,
+ &.resizable-box-handle-bottom {
+ left: 0;
+ width: 100%;
+ height: var(--resizable-box-handle-size);
+ cursor: row-resize;
+ }
+
+ &.resizable-box-handle-left,
+ &.resizable-box-handle-right {
+ top: 0;
+ width: var(--resizable-box-handle-size);
+ height: 100%;
+ cursor: col-resize;
+ }
+
+ &.resizable-box-handle-top {
+ top: 0;
+ }
+
+ &.resizable-box-handle-bottom {
+ bottom: 0;
+ }
+
+ &.resizable-box-handle-left {
+ left: 0;
+ }
+
+ &.resizable-box-handle-right {
+ right: 0;
+ }
+
+ /* Corners sit above edges so they win pointer events in the
+ overlap region. */
+ &.resizable-box-handle-top-left,
+ &.resizable-box-handle-top-right,
+ &.resizable-box-handle-bottom-left,
+ &.resizable-box-handle-bottom-right {
+ width: var(--resizable-box-handle-size);
+ height: var(--resizable-box-handle-size);
+ z-index: 1;
+ }
+
+ &.resizable-box-handle-top-left {
+ top: 0;
+ left: 0;
+ cursor: nwse-resize;
+ }
+
+ &.resizable-box-handle-bottom-right {
+ bottom: 0;
+ right: 0;
+ cursor: nwse-resize;
+ }
+
+ &.resizable-box-handle-top-right {
+ top: 0;
+ right: 0;
+ cursor: nesw-resize;
+ }
+
+ &.resizable-box-handle-bottom-left {
+ bottom: 0;
+ left: 0;
+ cursor: nesw-resize;
+ }
+}
diff --git a/web/tests/box_resize.test.cjs b/web/tests/box_resize.test.cjs
new file mode 100644
index 00000000000..cc07e673f39
--- /dev/null
+++ b/web/tests/box_resize.test.cjs
@@ -0,0 +1,230 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {JSDOM} = require("jsdom");
+
+const {zrequire} = require("./lib/namespace.cjs");
+const {run_test} = require("./lib/test.cjs");
+
+const {window} = new JSDOM("");
+// Only assign globals our library actually reads (document and
+// getComputedStyle). `global.HTMLElement` is intentionally NOT
+// overwritten: index.cjs already points it at its own JSDOM's
+// HTMLElement, and later tests such as `compose_paste.test.cjs`
+// rely on `instanceof HTMLElement` matching elements parsed via
+// their DOMParser.
+global.document = window.document;
+global.getComputedStyle = window.getComputedStyle.bind(window);
+
+// `setPointerCapture` / `releasePointerCapture` are not implemented by
+// jsdom; stub them on this test's own HTMLElement prototype so our
+// pointer event handlers can call them without throwing. We avoid
+// stubbing on `global.HTMLElement` since that prototype is shared
+// with other tests.
+window.HTMLElement.prototype.setPointerCapture = function () {};
+window.HTMLElement.prototype.releasePointerCapture = function () {};
+
+const box_resize = zrequire("box_resize");
+
+function make_box() {
+ const box = document.createElement("div");
+ // Provide a predictable starting rect so tests don't depend on
+ // jsdom's layout (which is a no-op).
+ let current_rect = {width: 100, height: 80};
+ box.getBoundingClientRect = () => ({
+ x: 0,
+ y: 0,
+ left: 0,
+ top: 0,
+ right: current_rect.width,
+ bottom: current_rect.height,
+ width: current_rect.width,
+ height: current_rect.height,
+ });
+ box.set_rect = (width, height) => {
+ current_rect = {width, height};
+ };
+ return box;
+}
+
+function fire(handle, type, {clientX = 0, clientY = 0, pointerId = 1} = {}) {
+ const event = new window.Event(type, {bubbles: true, cancelable: true});
+ Object.assign(event, {clientX, clientY, pointerId});
+ handle.dispatchEvent(event);
+ return event;
+}
+
+run_test("make_resizable creates one handle per direction with expected class", () => {
+ const box = make_box();
+ box_resize.make_resizable(box, ["top", "right", "bottom_left"]);
+
+ assert.equal(box.children.length, 3);
+ const classes = [...box.children].map((h) => h.className);
+ assert.ok(classes[0].includes("resizable-box-handle-top"));
+ assert.ok(classes[1].includes("resizable-box-handle-right"));
+ assert.ok(classes[2].includes("resizable-box-handle-bottom-left"));
+ // Underscores in the direction are converted to hyphens for the class.
+ assert.ok(!classes[2].includes("bottom_left"));
+});
+
+run_test("cleanup removes the handles it added", () => {
+ const box = make_box();
+ const existing_child = document.createElement("span");
+ box.append(existing_child);
+
+ const cleanup = box_resize.make_resizable(box, ["right", "bottom"]);
+ assert.equal(box.children.length, 3);
+
+ cleanup();
+ assert.equal(box.children.length, 1);
+ assert.equal(box.children[0], existing_child);
+});
+
+run_test("right handle grows width by 2 * dx and does not touch height", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ box_resize.make_resizable(box, ["right"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 40});
+ fire(handle, "pointermove", {clientX: 60, clientY: 45});
+
+ // Dragging right by 10: box grows by 20 (10 on each side, centered).
+ assert.equal(box.style.width, "120px");
+ // `height` is left untouched by an edge-only handle.
+ assert.equal(box.style.height, "");
+});
+
+run_test("left handle grows width when pointer moves leftward", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ box_resize.make_resizable(box, ["left"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 40});
+ // Dragging the left edge 15px to the left: the box grows symmetrically
+ // by 30px total.
+ fire(handle, "pointermove", {clientX: 35, clientY: 40});
+
+ assert.equal(box.style.width, "130px");
+});
+
+run_test("top handle grows height when pointer moves upward", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ box_resize.make_resizable(box, ["top"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 40});
+ fire(handle, "pointermove", {clientX: 50, clientY: 30});
+
+ // Dragging the top edge up by 10 => height grows by 20.
+ assert.equal(box.style.height, "100px");
+ assert.equal(box.style.width, "");
+});
+
+run_test("corner handle grows both dimensions with correct signs", () => {
+ const box = make_box();
+ box.set_rect(200, 100);
+ box_resize.make_resizable(box, ["top_left"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 50});
+ // Move 5 left, 8 up: both dx_sign and dy_sign are -1 for top_left,
+ // so width grows by 2*5 = 10 and height grows by 2*8 = 16.
+ fire(handle, "pointermove", {clientX: 45, clientY: 42});
+
+ assert.equal(box.style.width, "210px");
+ assert.equal(box.style.height, "116px");
+});
+
+run_test("on_resize callback fires once per pointermove after pointerdown", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ let call_count = 0;
+ box_resize.make_resizable(box, ["bottom_right"], () => {
+ call_count += 1;
+ });
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 0, clientY: 0});
+ assert.equal(call_count, 0);
+ fire(handle, "pointermove", {clientX: 5, clientY: 5});
+ fire(handle, "pointermove", {clientX: 10, clientY: 10});
+ assert.equal(call_count, 2);
+
+ fire(handle, "pointerup", {clientX: 10, clientY: 10});
+ fire(handle, "pointermove", {clientX: 20, clientY: 20});
+ assert.equal(call_count, 2);
+});
+
+run_test("pointerup stops subsequent pointermove events from resizing", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ box_resize.make_resizable(box, ["right"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 40});
+ fire(handle, "pointermove", {clientX: 60, clientY: 40});
+ assert.equal(box.style.width, "120px");
+
+ fire(handle, "pointerup", {clientX: 60, clientY: 40});
+ fire(handle, "pointermove", {clientX: 200, clientY: 40});
+ // Width should not have changed after pointerup.
+ assert.equal(box.style.width, "120px");
+});
+
+run_test("pointercancel stops subsequent pointermove events from resizing", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ box_resize.make_resizable(box, ["right"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 40});
+ fire(handle, "pointermove", {clientX: 60, clientY: 40});
+ assert.equal(box.style.width, "120px");
+
+ // The browser can take the gesture away at any time; we must
+ // tear the listeners down or subsequent moves would keep resizing.
+ fire(handle, "pointercancel", {clientX: 60, clientY: 40});
+ fire(handle, "pointermove", {clientX: 200, clientY: 40});
+ assert.equal(box.style.width, "120px");
+});
+
+run_test("resize clamps to CSS min and max bounds during a single drag", () => {
+ const box = make_box();
+ box.set_rect(100, 80);
+ // Jsdom does not resolve the element's real computed style from a
+ // stylesheet, so we stub getComputedStyle to return the min/max
+ // bounds we want to test against. The library only calls it on
+ // `box`, so we don't need a passthrough.
+ const original_get_computed_style = global.getComputedStyle;
+ global.getComputedStyle = () => ({
+ minWidth: "60px",
+ maxWidth: "140px",
+ minHeight: "0px",
+ maxHeight: "none",
+ });
+
+ try {
+ box_resize.make_resizable(box, ["right"]);
+ const handle = box.children[0];
+
+ fire(handle, "pointerdown", {clientX: 50, clientY: 40});
+
+ // Overshoot the max: raw would be 100 + 2*100 = 300, clamp to 140.
+ fire(handle, "pointermove", {clientX: 150, clientY: 40});
+ assert.equal(box.style.width, "140px");
+
+ // Pull back within bounds: raw is 100 + 2*10 = 120, no clamp.
+ fire(handle, "pointermove", {clientX: 60, clientY: 40});
+ assert.equal(box.style.width, "120px");
+
+ // Undershoot the min: raw would be 100 - 2*40 = 20, clamp to 60.
+ fire(handle, "pointermove", {clientX: 10, clientY: 40});
+ assert.equal(box.style.width, "60px");
+ } finally {
+ global.getComputedStyle = original_get_computed_style;
+ }
+});