Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add an accessible resize separator (#42595)

Sérgio Saquetim8f85810c40c8
diff --git a/app/assets/stylesheets/common/components/docked-composer.scss b/app/assets/stylesheets/common/components/docked-composer.scss
index c94e6f3c74f..c61d1c09b0f 100644
--- a/app/assets/stylesheets/common/components/docked-composer.scss
+++ b/app/assets/stylesheets/common/components/docked-composer.scss
@@ -38,7 +38,6 @@
margin: 0 auto;
box-sizing: border-box;
height: 0.5em;
- cursor: ns-resize;
display: flex;
align-items: center;
justify-content: center;
diff --git a/app/assets/stylesheets/common/ui-kit/_index.scss b/app/assets/stylesheets/common/ui-kit/_index.scss
index d24aba62645..75c6e665b28 100644
--- a/app/assets/stylesheets/common/ui-kit/_index.scss
+++ b/app/assets/stylesheets/common/ui-kit/_index.scss
@@ -4,3 +4,4 @@
ui-kit styles belong here. */
@import "d-pointer-drag";
+@import "d-resize-separator";
diff --git a/app/assets/stylesheets/common/ui-kit/d-resize-separator.scss b/app/assets/stylesheets/common/ui-kit/d-resize-separator.scss
new file mode 100644
index 00000000000..3199a8a53ad
--- /dev/null
+++ b/app/assets/stylesheets/common/ui-kit/d-resize-separator.scss
@@ -0,0 +1,32 @@
+/* The handle's own cursor, keyed off the component's class and its axis attribute
+ rather than `aria-orientation`: that describes the element to assistive
+ technology, and what a thing promises AT should not decide how it looks. Note the
+ attribute names the axis being RESIZED, which is the opposite of the separator's
+ announced orientation. */
+
+.d-resize-separator[data-resize-axis="vertical"] {
+ cursor: ns-resize;
+}
+
+.d-resize-separator[data-resize-axis="horizontal"] {
+ cursor: ew-resize;
+}
+
+/* Held on the body by `DResizeSeparator` for the length of a gesture.
+
+ The handle's own cursor is not enough: whether the cursor of the element a drag
+ began on survives the pointer leaving it is engine-dependent, and these handles
+ are only a few pixels thick, so the pointer spends most of a resize outside the
+ one it grabbed. Stating it on the body keeps the cursor for the whole gesture.
+
+ It reaches only what inherits `cursor`, so a link or a button passed over
+ mid-drag still shows its own. A gesture that must hold the cursor absolutely
+ everywhere wants an overlay rather than this. */
+
+body.d-resizing-ns {
+ cursor: ns-resize;
+}
+
+body.d-resizing-ew {
+ cursor: ew-resize;
+}
diff --git a/frontend/discourse/app/components/docked-composer.gjs b/frontend/discourse/app/components/docked-composer.gjs
index 57b564dc5fb..fdccb479d23 100644
--- a/frontend/discourse/app/components/docked-composer.gjs
+++ b/frontend/discourse/app/components/docked-composer.gjs
@@ -18,8 +18,8 @@ import UppyMediaOptimization from "discourse/lib/uppy-media-optimization-plugin"
import { clipboardHelpers } from "discourse/lib/utilities";
import DButton from "discourse/ui-kit/d-button";
import DEditor from "discourse/ui-kit/d-editor";
+import DResizeSeparator from "discourse/ui-kit/d-resize-separator";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
-import dResizeEdge from "discourse/ui-kit/modifiers/d-resize-edge";
import { i18n } from "discourse-i18n";
// Reusable chat-style "docked" composer. There is deliberately no
@@ -392,24 +392,16 @@ export default class DockedComposer extends Component {
{{willDestroy this.teardown}}
>
{{#if this.resizable}}
- <div
+ <DResizeSeparator
class="docked-composer__resize-handle"
- role="separator"
- aria-orientation="horizontal"
- aria-label={{i18n "composer.resize"}}
- aria-valuenow={{this.dragOffset}}
- aria-valuemin="0"
- aria-valuemax={{this.resizeAriaMax}}
- tabindex="0"
- {{dResizeEdge
- value=this.dragOffset
- min=0
- max=this.resizeAriaMax
- axis="vertical"
- side="end"
- onResize=this.onResize
- }}
- ></div>
+ @axis="vertical"
+ @side="end"
+ @value={{this.dragOffset}}
+ @min={{0}}
+ @max={{this.resizeAriaMax}}
+ @label={{i18n "composer.resize"}}
+ @onResize={{this.onResize}}
+ />
{{/if}}
{{#if (has-block "header")}}
<div class="docked-composer__header">
diff --git a/frontend/discourse/app/ui-kit/d-resize-separator.gts b/frontend/discourse/app/ui-kit/d-resize-separator.gts
new file mode 100644
index 00000000000..3ff63fecdf6
--- /dev/null
+++ b/frontend/discourse/app/ui-kit/d-resize-separator.gts
@@ -0,0 +1,396 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import { modifier } from "ember-modifier";
+import dResizeEdge, {
+ type ResizeReportMeta,
+} from "discourse/ui-kit/modifiers/d-resize-edge";
+
+/**
+ * A size, or a function returning one for a value the template cannot observe.
+ * `null` is the unmeasured state — the component withholds `aria-valuenow`
+ * until a size is known, so a measurement function may honestly say "not yet".
+ */
+type Measurement = number | null | (() => number | null);
+
+/**
+ * A bound, which unlike a size is never unmeasured: there is nothing to clamp
+ * against until it is known, so an unmeasured bound would silently become `0`
+ * in the gesture's arithmetic rather than mean "no limit".
+ *
+ * Documentation rather than enforcement. `strictNullChecks` is off repo-wide, so
+ * `number | null` collapses to `number` and a nullable measurement still
+ * type-checks here. What actually refuses one is the gesture's own guard.
+ */
+type Bound = number | (() => number);
+
+/**
+ * The box being resized: the element itself, or a function receiving the
+ * separator's own element and returning it.
+ */
+type ObserveTarget =
+ | Element
+ | ((separator: HTMLElement) => Element | null | undefined);
+
+/**
+ * Which cursor is held while a gesture on each axis runs, mapped by
+ * `app/assets/stylesheets/common/ui-kit/d-resize-separator.scss`.
+ */
+const CURSOR_CLASS = {
+ vertical: "d-resizing-ns",
+ horizontal: "d-resizing-ew",
+};
+
+interface ObserveResizedSignature {
+ Element: HTMLElement;
+ Args: {
+ Positional: [separator: DResizeSeparator, target?: ObserveTarget];
+ };
+}
+
+/**
+ * Watches the box being resized so the announced size keeps up with changes no
+ * gesture caused.
+ *
+ * Re-runs when `target` changes, which is what lets a consumer pass the element
+ * itself once it exists. A function `target` is resolved on each run, so a stable
+ * function reference is never retried — that form suits a box already around the
+ * separator when it renders.
+ */
+const observeResized = modifier<ObserveResizedSignature>(
+ (separator, [separatorComponent, target]) => {
+ const element = typeof target === "function" ? target(separator) : target;
+ if (!element) {
+ return;
+ }
+
+ const observer = new ResizeObserver(() => separatorComponent.refresh());
+ observer.observe(element);
+
+ return () => observer.disconnect();
+ }
+);
+
+interface RefreshOnViewportChangeSignature {
+ Element: HTMLElement;
+ Args: {
+ Positional: [separator: DResizeSeparator];
+ };
+}
+
+/**
+ * Re-reads the size when the viewport changes. Separate from watching the box
+ * because bounds are commonly derived from the viewport — the largest a box may
+ * grow is usually what is left of the window — and a window change need not alter
+ * the box's own size, so nothing else would notice it.
+ */
+const refreshOnViewportChange = modifier<RefreshOnViewportChangeSignature>(
+ (_element, [separatorComponent]) => {
+ const onViewportChange = () => separatorComponent.refresh();
+ window.addEventListener("resize", onViewportChange);
+
+ return () => window.removeEventListener("resize", onViewportChange);
+ }
+);
+
+interface DResizeSeparatorSignature {
+ /** The separator itself. Attributes and modifiers pass through to it. */
+ Element: HTMLDivElement;
+ Args: {
+ /**
+ * `"vertical"` to resize height, `"horizontal"` to resize width. Defaults to
+ * `"vertical"`.
+ */
+ axis?: "vertical" | "horizontal";
+
+ /**
+ * The edge of the resized box the handle sits on, naming the edge OPPOSITE
+ * the one that moves: a handle at the top of a bottom-docked box is `"end"`.
+ * Defaults to `"start"`.
+ */
+ side?: "start" | "end";
+
+ /**
+ * The current size. Pass a function whenever the number is a live
+ * measurement rather than tracked state.
+ */
+ value: Measurement;
+
+ /** The smallest size the box may be dragged to. */
+ min: Bound;
+
+ /** The largest size the box may be dragged to. */
+ max: Bound;
+
+ /**
+ * What the separator is called, already translated. Required: a focusable
+ * `role="separator"` with no accessible name is announced as a bare
+ * "splitter", which says nothing about what it resizes.
+ */
+ label: string;
+
+ /** The gesture began. A notification; the return is ignored. */
+ onResizeStart?: () => void;
+
+ /** Fired throughout the gesture. Preview here. */
+ onResize?: (size: number) => void;
+
+ /** Fired once at the end. Commit here. */
+ onResizeEnd?: (size: number) => void;
+
+ /**
+ * The box being resized, so the announced size keeps up with changes no
+ * gesture caused, such as a panel opening or a preview toggling. Viewport
+ * changes are picked up regardless and need no arg.
+ *
+ * Either the element, or a function receiving the separator's own element and
+ * returning it. Pass the element when it may not exist at first render, since
+ * a change to it re-attaches the observer; a function is resolved once, which
+ * suits a box already around the separator when it renders.
+ */
+ observe?: ObserveTarget;
+ };
+ Blocks: {
+ /**
+ * Rendered inside the handle, for the ones that draw themselves with a real
+ * element rather than a pseudo-element.
+ */
+ default: [];
+ };
+}
+
+/**
+ * A one-axis resize handle that is operable and announced: the separator semantics
+ * and the gesture bound together, so a consumer supplies only the size and what to
+ * do with it.
+ *
+ * Wraps {@link dResizeEdge}, which keeps the value math, the bounds, the keyboard
+ * path and the writing-direction flip. What this adds is everything a consumer
+ * would otherwise hand-write beside that modifier, and can get subtly wrong:
+ *
+ * - The separator contract — `role`, one tab stop, and an `aria-orientation` that
+ * is the OPPOSITE of the axis, because a separator between vertically stacked
+ * regions is itself a horizontal bar.
+ * - A tracked mirror of the size for `aria-valuenow`/`min`/`max`. These cannot
+ * reuse the modifier's args as they stand: a live measurement has to reach the
+ * modifier as a function or it would be read once and every later gesture would
+ * work from a stale value, while assistive technology needs a value the template
+ * can re-render from.
+ * - Withholding `aria-valuenow` until a size is known. Reporting `0` would claim
+ * the box has no extent; an absent value says "not measured yet".
+ * - Holding the cursor for the length of the gesture. Whether the cursor of the
+ * element a drag began on survives the pointer leaving it is engine-dependent,
+ * so the intent is stated on the page instead of left to the handle.
+ *
+ * Sizes carry no unit of their own: they are whatever `@value`, `@min` and `@max`
+ * are expressed in, which is a height for a stacked splitter and a width for a
+ * side panel.
+ *
+ * @example
+ * ```gjs
+ * <DResizeSeparator
+ * @axis="vertical"
+ * @side="end"
+ * @value={{this.height}}
+ * @min={{this.minHeight}}
+ * @max={{this.maxHeight}}
+ * @label={{i18n "composer.resize"}}
+ * @onResize={{this.preview}}
+ * @onResizeEnd={{this.commit}}
+ * class="my-block__handle"
+ * />
+ * ```
+ *
+ * Attributes pass through, so a consumer keeps its own class alongside this one —
+ * the two are merged rather than replaced — and may add its own modifiers.
+ *
+ * @see {@link dResizeEdge}, the modifier underneath. Use it directly only when the
+ * element and its separator semantics are already yours to own.
+ * @see The `dOnResize` modifier to merely OBSERVE a size change. It is not a gesture.
+ */
+export default class DResizeSeparator extends Component<DResizeSeparatorSignature> {
+ /** The size and bounds as last read, for assistive technology to announce. */
+ @tracked
+ _announced: {
+ now: number | undefined;
+ min: number | undefined;
+ max: number | undefined;
+ } | null = null;
+
+ get axis() {
+ return this.args.axis ?? "vertical";
+ }
+
+ /** A separator's orientation is its own direction, not the axis it moves. */
+ get orientation() {
+ return this.axis === "horizontal" ? "vertical" : "horizontal";
+ }
+
+ get cursorClass() {
+ return CURSOR_CLASS[this.axis];
+ }
+
+ /**
+ * The size to announce, or `undefined` while it is unknown so that the attribute
+ * is left off rather than reporting a number nothing measured.
+ */
+ get valueNow() {
+ const now = this.#announce("value", "now");
+ if (now === undefined) {
+ return undefined;
+ }
+
+ // ARIA requires the announced size to lie within the announced bounds, and a
+ // bound derived from the viewport can drop below a size the consumer has not
+ // reduced yet. Clamped in the same order as the gesture's own clamp, so the
+ // two agree about which bound wins when they conflict.
+ const min = this.valueMin;
+ const max = this.valueMax;
+ return Math.max(Math.min(now, max ?? now), min ?? now);
+ }
+
+ get valueMin() {
+ return this.#announce("min", "min");
+ }
+
+ get valueMax() {
+ return this.#announce("max", "max");
+ }
+
+ /** Re-reads the size. Called on insert, after every report, and by the observer. */
+ @action
+ refresh() {
+ this.#snapshot();
+ }
+
+ /**
+ * Re-reads the size only for a keyboard gesture.
+ *
+ * The modifier reports a move and then, on the last one, the commit — both in the
+ * same stack. Dirtying tracked state that the template consumes in between opens
+ * a runloop, and a consumer callback reached with one already open is invoked
+ * directly rather than through the wrapper that routes exceptions to the
+ * application error handler, so a consumer that throws on commit has its exception escape as
+ * an unhandled error instead. Mid-gesture there is nothing worth announcing
+ * anyway: the size is still moving, and the commit re-reads it.
+ *
+ * @param size - The size the gesture is passing through.
+ * @param meta - What produced the report. A held key is one gesture spanning
+ * every repeat, so it is the only source whose announcement cannot wait for
+ * the commit.
+ */
+ @action
+ onResize(size: number, meta: ResizeReportMeta) {
+ this.args.onResize?.(size);
+
+ // A held arrow key is one gesture spanning every repeat, so the commit that
+ // normally refreshes this does not arrive until release. Without the
+ // snapshot here the announced value would be stale for the whole hold,
+ // which is exactly the path this component exists to provide. The runloop
+ // concern above does not apply: a key report is not inside a frame/commit
+ // stack with a consumer callback after it.
+ if (meta.source === "keyboard") {
+ this.#snapshot(size);
+ }
+ }
+
+ @action
+ onResizeEnd(size: number) {
+ this.args.onResizeEnd?.(size);
+ this.#snapshot(size);
+ }
+
+ /**
+ * Resolves one announced number, from the mirror or from the arg directly.
+ *
+ * A number given as a number is already reactive, so it is read straight through:
+ * mirroring it would freeze what is announced until something else happened to
+ * trigger a re-read. Only a function needs the mirror, because a measurement has
+ * no tracked state for the template to notice changing.
+ *
+ * @param argName - Which arg to resolve.
+ * @param mirrorKey - Its key in the mirror.
+ */
+ #announce(
+ argName: "value" | "min" | "max",
+ mirrorKey: "now" | "min" | "max"
+ ): number | undefined {
+ const arg = this.args[argName];
+ if (typeof arg === "function") {
+ return this._announced?.[mirrorKey] ?? undefined;
+ }
+ return arg ?? undefined;
+ }
+
+ /**
+ * Reads the size and bounds as they stand. Taken after every report rather than
+ * derived, because what matters here are measurements the template cannot observe.
+ */
+ #snapshot(reportedSize?: number) {
+ // The reported size wins over re-reading the arg. A consumer whose `value`
+ // measures the DOM applies the new size through tracked state, which has not
+ // rendered yet while this runs inside its callback, so re-reading would
+ // announce the size the box is moving away from.
+ const now = reportedSize ?? this.#read(this.args.value) ?? undefined;
+ const min = this.#read(this.args.min);
+ const max = this.#read(this.args.max);
+ const announced = this._announced;
+
+ // Assigning invalidates whether or not the numbers differ, so a refresh that
+ // read the same three would re-render the announced values for no change.
+ // That is the common case rather than the rare one: the box observer and the
+ // viewport listener both refresh during a window resize, landing two
+ // refreshes in one frame, and a viewport change need not resize the box at
+ // all.
+ if (
+ announced &&
+ announced.now === now &&
+ announced.min === min &&
+ announced.max === max
+ ) {
+ return;
+ }
+
+ this._announced = { now, min, max };
+ }
+
+ #read(arg: Measurement): number | null {
+ return typeof arg === "function" ? arg() : arg;
+ }
+
+ <template>
+ {{! Above the splat is a default a consumer may replace; below it is something
+ this component guarantees. The tabindex is deliberately open, because a roving
+ composite legitimately makes its inactive items unreachable, and lowering it
+ invalidates nothing else. The role is not, because the value attributes below
+ are only meaningful on a separator, so overriding it would leave the element
+ carrying attributes its role does not allow. }}
+ <div
+ class="d-resize-separator"
+ tabindex="0"
+ ...attributes
+ role="separator"
+ data-resize-axis={{this.axis}}
+ aria-orientation={{this.orientation}}
+ aria-label={{@label}}
+ aria-valuenow={{this.valueNow}}
+ aria-valuemin={{this.valueMin}}
+ aria-valuemax={{this.valueMax}}
+ {{didInsert this.refresh}}
+ {{observeResized this @observe}}
+ {{refreshOnViewportChange this}}
+ {{dResizeEdge
+ value=@value
+ min=@min
+ max=@max
+ axis=this.axis
+ side=@side
+ bodyClass=this.cursorClass
+ onResizeStart=@onResizeStart
+ onResize=this.onResize
+ onResizeEnd=this.onResizeEnd
+ }}
+ >{{yield}}</div>
+ </template>
+}
diff --git a/frontend/discourse/app/ui-kit/modifiers/d-resize-edge.ts b/frontend/discourse/app/ui-kit/modifiers/d-resize-edge.ts
index 2b9c40045f4..18f1fdd8190 100644
--- a/frontend/discourse/app/ui-kit/modifiers/d-resize-edge.ts
+++ b/frontend/discourse/app/ui-kit/modifiers/d-resize-edge.ts
@@ -1,23 +1,52 @@
import { registerDestructor } from "@ember/destroyable";
import type Owner from "@ember/owner";
import Modifier, { type ArgsFor } from "ember-modifier";
+import {
+ type DPointerDragArgs,
+ registerPointerDrag,
+} from "discourse/ui-kit/modifiers/d-pointer-drag";
-// How far a single arrow key press moves the edge, in pixels.
+/** How far a single arrow key press moves the edge, in pixels. */
const KEYBOARD_STEP = 16;
+/**
+ * What produced a size report. A consumer that mirrors the size for assistive
+ * technology needs this: a held arrow key is one gesture spanning every repeat,
+ * so waiting for the commit would leave the announced value stale for as long as
+ * the key is down, where a pointer gesture reports through a frame and settles
+ * on release.
+ */
+export interface ResizeReportMeta {
+ source: "pointer" | "keyboard";
+}
+
interface DResizeEdgeSignature {
/** The element acting as the edge. */
Element: HTMLElement;
Args: {
Named: {
- /** The current size, in pixels. */
- value: number;
+ /**
+ * The current size, in pixels, or a function returning it. Read once at the
+ * start of each gesture — a press, or the first of a run of key repeats —
+ * and never again while one is in flight.
+ *
+ * A measurement may return `null` to say it has nothing yet. There is no
+ * size to resize *from* in that state, so the gesture is refused rather
+ * than started from an assumed zero.
+ */
+ value: number | null | (() => number | null);
- /** The smallest size the edge may be dragged to. */
- min: number;
+ /**
+ * The smallest size the edge may be dragged to, or a function returning it.
+ * Read every time a size is clamped.
+ */
+ min: number | (() => number);
- /** The largest size the edge may be dragged to. */
- max: number;
+ /**
+ * The largest size the edge may be dragged to, or a function returning it.
+ * Read every time a size is clamped.
+ */
+ max: number | (() => number);
/**
* The axis the edge moves along. Defaults to `"horizontal"`.
@@ -33,20 +62,57 @@ interface DResizeEdgeSignature {
* Combined with the writing direction this decides whether moving the
* pointer away from that edge makes it larger or smaller. Defaults to
* `"start"`.
+ *
+ * Read live, unlike `value`, which is frozen for the length of a gesture.
+ * Changing this or `axis` mid-gesture measures the new orientation against
+ * an origin captured in the old one, so the result is undefined.
*/
side?: "start" | "end";
/**
- * Called while dragging, at most once per animation frame. Suitable for
- * updating the rendered size.
+ * A class held on `document.body` for the length of a gesture, for a cursor
+ * that has to survive the pointer leaving the edge. Whether the cursor of the
+ * element a drag began on does that by itself is engine-dependent, so it
+ * cannot be left to the edge's own rule.
*/
- onResize?: (size: number) => void;
+ bodyClass?: string;
/**
- * Called once when the interaction finishes. Suitable for persisting the
- * size.
+ * Called once when a resize begins, immediately before the first size is
+ * reported. Purely a notification — the return value is ignored, and a
+ * caller whose size is a live measurement supplies it by passing a
+ * function as `value`.
+ *
+ * Fires on the first report rather than on the press, so a press that
+ * never becomes a resize opens nothing. Anything held for the length of
+ * the gesture belongs here, because it is always closed by exactly one
+ * `onResizeEnd`.
*/
- onResizeEnd?: (size: number) => void;
+ onResizeStart?: () => void;
+
+ /**
+ * Called while the size is changing: at most once per animation frame while
+ * dragging, and once per key press or repeat. Suitable for updating the
+ * rendered size.
+ */
+ onResize?: (size: number, meta: ResizeReportMeta) => void;
+
+ /**
+ * Called once when the gesture finishes. Suitable for work that should
+ * happen once per resize rather than once per report — persisting the
+ * size, or undoing something held for the length of the gesture.
+ *
+ * Every one of these is preceded by exactly one `onResizeStart`, and a
+ * gesture that reported no size fires neither: a press released without
+ * moving is a click rather than a resize, and opens nothing.
+ *
+ * The converse does not hold. Teardown mid-gesture fires no consumer
+ * callback at all, so a gesture that had reported and is then torn down
+ * leaves its `onResizeStart` unclosed. State held for the length of a
+ * resize therefore has to be released on the consumer's own destruction
+ * too, not on this alone.
+ */
+ onResizeEnd?: (size: number, meta: ResizeReportMeta) => void;
};
Positional: [];
};
@@ -63,11 +129,25 @@ interface DResizeEdgeSignature {
* and `max` are expressed in, which is a width for a side panel but an offset
* from a resting height for a composer.
*
+ * `value`, `min` and `max` may each be a function instead of a number, which is
+ * what a caller whose size or bounds are a live measurement of the DOM or the
+ * window must use — see `#read`.
+ *
* Both pointer and keyboard interaction are supported, which is what the
* splitter pattern requires: a resize that can only be performed by dragging
- * is unusable without a pointing device. Pointer events cover mouse, touch,
- * and pen alike, and the modifier sets `touch-action: none` on the element so
- * that a touch drag is not claimed by the browser as a scroll gesture.
+ * is unusable without a pointing device. The pointer half is delegated to
+ * `registerPointerDrag`, the shared press-drag-transform engine, which covers
+ * mouse, touch and pen alike and marks the element so that a touch drag is not
+ * claimed by the browser as a scroll gesture. This modifier keeps the value
+ * semantics: clamping, the logical `side` handling, and the keyboard path.
+ *
+ * Both halves report the same shape — one `onResizeStart`, any number of
+ * `onResize`, one `onResizeEnd` — so a caller can hold state for the length of a
+ * resize without caring which input performed it. A held arrow key is therefore
+ * one gesture rather than one per repeat, and its running size is kept here
+ * instead of re-read from `value`: a caller whose size is a live measurement
+ * cannot settle it between repeats, and stepping from a box still moving toward
+ * the previous size would swallow most of each step.
*
* ```hbs
* <div
@@ -82,46 +162,46 @@ interface DResizeEdgeSignature {
* min=this.minWidth
* max=this.maxWidth
* side="start"
+ * bodyClass="d-resizing-ew"
+ * onResizeStart=this.suppressTransitions
* onResize=this.previewWidth
* onResizeEnd=this.commitWidth
* }}
* ></div>
* ```
+ *
+ * @see The `DResizeSeparator` component, which wraps this and supplies the whole block of
+ * separator markup above. Prefer it; reach for this modifier directly only when
+ * the element and its semantics are already yours to own.
+ * @see The `dOnResize` modifier to merely OBSERVE a size change. It is not a gesture.
*/
export default class DResizeEdgeModifier extends Modifier<DResizeEdgeSignature> {
/** The options from the most recent invocation, set by `modify`. */
#named: DResizeEdgeSignature["Args"]["Named"];
- #onPointerDown = (event: PointerEvent) => {
- // Ignore anything that is not a primary button press, so that a right
- // click or a secondary pointer cannot begin a resize.
- if (event.button !== 0) {
- return;
+ #onDragStart = (event: PointerEvent) => {
+ // A pointer taking over closes any key still being held, so the two inputs
+ // cannot leave two gestures open against a single end.
+ this.#endKeyboardGesture();
+
+ const startValue = this.#read(this.#named.value);
+ if (!Number.isFinite(startValue)) {
+ // Nothing to resize from. Vetoing hands the press back rather than
+ // starting a gesture whose first report would be `null + delta` clamped
+ // to a bound, which moves the box somewhere nobody dragged it.
+ return false;
}
- // A drag is already in progress. Taking this one over would overwrite the
- // tracked pointer and strand the first one's capture, since its release
- // would then no longer match.
- if (this.#pointerId !== null) {
- return;
- }
-
- event.preventDefault();
-
- this.#pointerId = event.pointerId;
+ this.#pointerActive = true;
+ this.#rtl = undefined;
+ this.#started = false;
this.#startCoordinate = this.#coordinate(event);
- this.#startValue = this.#named.value;
-
- this.#element.setPointerCapture(event.pointerId);
- this.#element.addEventListener("pointermove", this.#onPointerMove);
- this.#element.addEventListener("pointerup", this.#onPointerUp);
- this.#element.addEventListener("pointercancel", this.#onPointerUp);
+ // Reset so a cancellation before any movement reads the origin, not the
+ // previous gesture's last position.
+ this.#pendingCoordinate = this.#startCoordinate;
+ this.#startValue = startValue;
};
- #onPointerMove = (event: PointerEvent) => {
- if (event.pointerId !== this.#pointerId) {
- return;
- }
-
+ #onDrag = (event: PointerEvent) => {
// A pointer can move several times between paints, so only the latest
// position is kept and reported on the next frame. A size computed for the
// positions in between would be replaced before anything rendered it.
@@ -131,55 +211,158 @@ export default class DResizeEdgeModifier extends Modifier<DResizeEdgeSignature>
this.#reportMove(this.#pendingCoordinate);
});
};
- #onPointerUp = (event: PointerEvent) => {
- if (event.pointerId !== this.#pointerId) {
- return;
- }
-
+ #onDragEnd = (event: PointerEvent) => {
// The committed size is the one the pointer was released at, so any frame
- // still pending is dropped in favour of reporting that position now.
+ // still pending is dropped first: letting it fire afterwards would report a
+ // stale intermediate size over the committed one.
this.#cancelFrame();
- this.#reportMove(this.#coordinate(event), { final: true });
- this.#releasePointer();
+ this.#pointerActive = false;
+
+ // A cancellation's own coordinates are not the pointer's last position —
+ // A cancelled pointer event may carry (0,0), while a browser-fired
+ // `lostpointercapture` carries no meaningful position either — so the
+ // commit falls back to the last position the pointer actually reached.
+ // Recomputing from the event would snap the size to a clamp bound.
+ const cancelled =
+ event.type === "pointercancel" || event.type === "lostpointercapture";
+ const coordinate = cancelled
+ ? this.#pendingCoordinate
+ : this.#coordinate(event);
+ // A press that reported nothing and was released where it landed is a click,
+ // not a resize. Reporting it would hand the consumer the size it already
+ // had, which is enough to make it persist one. The coordinate is checked as
+ // well as `#started` because a browser may coalesce a short drag into no
+ // intermediate pointer move, and that is still a resize.
+ //
+ // A gesture that DID report is always closed, whatever the release
+ // coordinate: a drag that returned to its origin, or one along the other
+ // axis, has already told the consumer a resize was under way.
+ if (!this.#started && coordinate === this.#startCoordinate) {
+ return;
+ }
+ this.#reportMove(coordinate, { final: true });
};
#onKeyDown = (event: KeyboardEvent) => {
- const { value, min, max } = this.#named;
+ // The mirror of the pointer closing a held key: a key must not open a second
+ // gesture over a drag in flight either, or its release would report the
+ // resize ended while the pointer is still dragging.
+ if (this.#pointerActive) {
+ return;
+ }
+
+ // A modified arrow belongs to whatever binding claims it — back, word-wise
+ // motion, selection — and the separator merely happens to hold focus. The
+ // window-splitter pattern defines no modified bindings of its own.
+ if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) {
+ return;
+ }
+
const [shrinkKey, growKey] =
this.#named.axis === "vertical"
? ["ArrowUp", "ArrowDown"]
: ["ArrowLeft", "ArrowRight"];
+
+ if (![shrinkKey, growKey, "Home", "End"].includes(event.key)) {
+ return;
+ }
+
+ if (this.#keyboardValue === null) {
+ const startValue = this.#read(this.#named.value);
+ // Checked before the key is claimed: with no size to step from there is
+ // nothing to handle, so the key belongs to whatever else would act on it.
+ if (!Number.isFinite(startValue)) {
+ return;
+ }
+
+ event.preventDefault();
+ this.#rtl = undefined;
+ this.#keyboardValue = startValue;
+ this.#keyboardKey = event.key;
+ this.#named.onResizeStart?.();
+ } else {
+ event.preventDefault();
+ }
+
let next;
switch (event.key) {
case shrinkKey:
- next = value - KEYBOARD_STEP * this.#growthDirection;
+ next = this.#keyboardValue - KEYBOARD_STEP * this.#growthDirection;
break;
case growKey:
- next = value + KEYBOARD_STEP * this.#growthDirection;
+ next = this.#keyboardValue + KEYBOARD_STEP * this.#growthDirection;
break;
case "Home":
- next = min;
- break;
- case "End":
- next = max;
+ next = this.#read(this.#named.min);
break;
default:
- return;
+ next = this.#read(this.#named.max);
+ break;
}
- event.preventDefault();
+ this.#keyboardValue = this.#clamp(next);
+ this.#named.onResize?.(this.#keyboardValue, { source: "keyboard" });
+ };
+ #onKeyUp = (event: KeyboardEvent) => {
+ if (event.key !== this.#keyboardKey) {
+ return;
+ }
- const clamped = this.#clamp(next);
- this.#named.onResize?.(clamped);
- this.#named.onResizeEnd?.(clamped);
+ this.#endKeyboardGesture();
};
+ /**
+ * The keyup would land on whatever took focus, so without this the gesture
+ * would stay open and never commit. Bound to the window as well as the
+ * element: focus leaving the page entirely, or the page being replaced, takes
+ * the keyup with it without the element ever seeing a blur.
+ */
+ #onBlur = () => this.#endKeyboardGesture();
#element: HTMLElement;
#frame?: number;
+
+ /** The key holding the current keyboard gesture open, if any. */
+ #keyboardKey: string | null = null;
+
+ /**
+ * The running size of the current keyboard gesture, or `null` when no key is
+ * down. Held rather than re-read from `value` on each repeat, for the reason
+ * given on the class.
+ */
+ #keyboardValue: number | null = null;
+
#pendingCoordinate = 0;
- #pointerId: number | null = null;
+
+ /**
+ * Whether a pointer gesture is in flight, which locks the keyboard path out
+ * for its duration.
+ */
+ #pointerActive = false;
+
+ #releaseGesture: (() => void) | null = null;
+
+ /**
+ * Whether the element reads right-to-left, sampled on the first read within
+ * the current gesture. Cleared at the start of each one so a document that
+ * changed direction in between is measured again.
+ */
+ #rtl?: boolean;
+
#startCoordinate = 0;
+
+ /**
+ * Whether the pointer gesture in flight has reported a size, and so whether
+ * `onResizeStart` has been fired and is owed an `onResizeEnd`.
+ */
+ #started = false;
+
#startValue = 0;
+ /**
+ * The gesture args handed to `registerPointerDrag`. Typed from the engine's own
+ * args rather than restated, so narrowing one of them cannot leave this behind.
+ */
+ #gestureArgs: DPointerDragArgs;
+
constructor(owner: Owner, args: ArgsFor<DResizeEdgeSignature>) {
super(owner, args);
registerDestructor(this, (instance) => instance.cleanup());
@@ -193,22 +376,49 @@ export default class DResizeEdgeModifier extends Modifier<DResizeEdgeSignature>
this.#element = element;
this.#named = named;
- // A touch drag would otherwise be claimed by the browser as a scroll and
- // abort the resize through `pointercancel`. Applied here rather than left
- // to each caller's stylesheet because the interaction does not work
- // without it.
- element.style.touchAction = "none";
+ this.#gestureArgs = {
+ onDragStart: this.#onDragStart,
+ onDrag: this.#onDrag,
+ onDragEnd: this.#onDragEnd,
+ // An OS-interrupted resize keeps the size the user dragged to rather than
+ // snapping back, so cancellation commits.
+ cancelCommits: true,
+ // A touch drag would otherwise be claimed by the browser as a scroll and
+ // abort the resize through `pointercancel`. An edge is small enough that
+ // suppressing every touch gesture on it costs the user nothing.
+ touchAction: "none",
+ bodyClass: named.bodyClass,
+ };
+
+ // The gesture engine owns pointer identity, capture, and the primary-button
+ // gate; this modifier keeps the value semantics and the keyboard path.
+ this.#releaseGesture ??= registerPointerDrag(
+ element,
+ () => this.#gestureArgs
+ );
- element.addEventListener("pointerdown", this.#onPointerDown);
element.addEventListener("keydown", this.#onKeyDown);
+ element.addEventListener("keyup", this.#onKeyUp);
+ element.addEventListener("blur", this.#onBlur);
+ window.addEventListener("blur", this.#onBlur);
+ window.addEventListener("pagehide", this.#onBlur);
}
cleanup() {
this.#cancelFrame();
- this.#element.removeEventListener("pointerdown", this.#onPointerDown);
- this.#element.removeEventListener("keydown", this.#onKeyDown);
- this.#releasePointer();
+ this.#releaseGesture?.();
+ this.#releaseGesture = null;
+ // Dropped rather than committed: the element is going away, so a caller
+ // undoing gesture state has nothing left to undo it on.
+ this.#keyboardKey = null;
+ this.#keyboardValue = null;
+ this.#pointerActive = false;
+ this.#element?.removeEventListener("keydown", this.#onKeyDown);
+ this.#element?.removeEventListener("keyup", this.#onKeyUp);
+ this.#element?.removeEventListener("blur", this.#onBlur);
+ window.removeEventListener("blur", this.#onBlur);
+ window.removeEventListener("pagehide", this.#onBlur);
}
/**
@@ -230,9 +440,14 @@ export default class DResizeEdgeModifier extends Modifier<DResizeEdgeSignature>
return logical;
}
- const rtl = getComputedStyle(this.#element).direction === "rtl";
+ // Sampled once per gesture rather than on every read: this runs for each
+ // reported frame, and the consumer is changing sizes in that same frame, so
+ // every read forces a style recalculation. `side` and `axis` above stay live
+ // reads — only the writing direction is held, and it is not something that
+ // changes while a pointer is down.
+ this.#rtl ??= getComputedStyle(this.#element).direction === "rtl";
- return logical * (rtl ? -1 : 1);
+ return logical * (this.#rtl ? -1 : 1);
}
/**
@@ -248,15 +463,64 @@ export default class DResizeEdgeModifier extends Modifier<DResizeEdgeSignature>
const delta = (coordinate - this.#startCoordinate) * this.#growthDirection;
const size = this.#clamp(this.#startValue + delta);
- this.#named.onResize?.(size);
+ // Opened on the first report rather than on the press, so a press that never
+ // becomes a resize opens nothing and needs nothing closed. A consumer that
+ // suppresses transitions or announces a resize for the length of the gesture
+ // would otherwise be left holding that state after a plain click.
+ if (!this.#started) {
+ this.#started = true;
+ this.#named.onResizeStart?.();
+ }
+
+ this.#named.onResize?.(size, { source: "pointer" });
if (final) {
- this.#named.onResizeEnd?.(size);
+ this.#named.onResizeEnd?.(size, { source: "pointer" });
}
}
+ /**
+ * Commits the keyboard gesture currently open, if there is one.
+ *
+ * The state is cleared before the callback runs, so a caller that opens another
+ * gesture from it does not find this one still standing.
+ */
+ #endKeyboardGesture() {
+ if (this.#keyboardValue === null) {
+ return;
+ }
+
+ const size = this.#keyboardValue;
+
+ this.#keyboardKey = null;
+ this.#keyboardValue = null;
+ this.#named.onResizeEnd?.(size, { source: "keyboard" });
+ }
+
+ /**
+ * Resolves a size arg that may be given as a function.
+ *
+ * A caller whose size or bounds are a live measurement of the DOM or the window
+ * has to pass a function: an arg whose compute reads no tracked state is cached
+ * for the modifier's lifetime, so a plain number would be frozen at its first
+ * value and every later gesture would work from a stale one.
+ *
+ * @param arg - A size, or a function returning one.
+ * @returns The size in pixels.
+ */
+ #read(arg: number | (() => number)) {
+ return typeof arg === "function" ? arg() : arg;
+ }
+
#clamp(size: number) {
- return Math.min(Math.max(size, this.#named.min), this.#named.max);
+ // The minimum is applied last, so it wins when the two bounds conflict — a
+ // short viewport can put `max` below `min`, and the stylesheet's own min-height
+ // would still hold the box open. Letting `max` win would report a size the
+ // element never takes.
+ return Math.max(
+ Math.min(size, this.#read(this.#named.max)),
+ this.#read(this.#named.min)
+ );
}
#cancelFrame() {
@@ -267,19 +531,4 @@ export default class DResizeEdgeModifier extends Modifier<DResizeEdgeSignature>
cancelAnimationFrame(this.#frame);
this.#frame = undefined;
}
-
- #releasePointer() {
- if (this.#pointerId === null) {
- return;
- }
-
- if (this.#element.hasPointerCapture(this.#pointerId)) {
- this.#element.releasePointerCapture(this.#pointerId);
- }
-
- this.#element.removeEventListener("pointermove", this.#onPointerMove);
- this.#element.removeEventListener("pointerup", this.#onPointerUp);
- this.#element.removeEventListener("pointercancel", this.#onPointerUp);
- this.#pointerId = null;
- }
}
diff --git a/frontend/discourse/tests/integration/modifiers/d-resize-edge-test.gjs b/frontend/discourse/tests/integration/modifiers/d-resize-edge-test.gjs
index 94b6b9e1b78..cd9b0a51cdc 100644
--- a/frontend/discourse/tests/integration/modifiers/d-resize-edge-test.gjs
+++ b/frontend/discourse/tests/integration/modifiers/d-resize-edge-test.gjs
@@ -3,43 +3,33 @@ import {
clearRender,
find,
render,
+ settled,
triggerEvent,
triggerKeyEvent,
} from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { stubPointerCapture } from "discourse/tests/helpers/ui-kit/pointer-gesture-helper";
import dResizeEdge from "discourse/ui-kit/modifiers/d-resize-edge";
const EDGE = ".resize-edge";
/**
- * Replaces the pointer capture API with an observable stand-in.
+ * Records what the modifier reported, and holds the size it reports against.
*
- * The real methods reject synthetic pointer IDs, which is all a test can
- * dispatch, so capture has to be recorded rather than performed.
+ * The size is tracked because the modifier reads it back at the start of every
+ * gesture, so a test that drags twice has to see the first drag's result.
*/
-function installPointerCaptureSpy(element) {
- const captured = new Set();
- const released = [];
-
- element.setPointerCapture = (pointerId) => captured.add(pointerId);
- element.hasPointerCapture = (pointerId) => captured.has(pointerId);
- element.releasePointerCapture = (pointerId) => {
- captured.delete(pointerId);
- released.push(pointerId);
- };
-
- return { captured, released };
-}
-
class Harness {
@tracked value;
resizes = [];
resizeEnds = [];
+ resizeStarts = 0;
constructor(value = 300) {
this.value = value;
+ this.onResizeStart = () => this.resizeStarts++;
this.onResize = (size) => {
this.resizes.push(size);
this.value = size;
@@ -70,7 +60,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- installPointerCaptureSpy(edge);
+ stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -107,6 +97,73 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
});
+ test("grippie regression clamp conflict pointer honors minimum", async function (assert) {
+ const state = new Harness(220);
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=255
+ max=190
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 220,
+ pointerId: 1,
+ });
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 230,
+ pointerId: 1,
+ });
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [255],
+ "a pointer clamp never reports a value below the minimum"
+ );
+ });
+
+ test("grippie regression clamp conflict keyboard honors minimum", async function (assert) {
+ const state = new Harness(220);
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=255
+ max=190
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [255],
+ "a keyboard clamp never reports a value below the minimum"
+ );
+ });
+
test("the element is opted out of browser touch gestures", async function (assert) {
const state = new Harness();
@@ -145,7 +202,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- const capture = installPointerCaptureSpy(edge);
+ const capture = stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 2,
@@ -182,7 +239,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- const capture = installPointerCaptureSpy(edge);
+ const capture = stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -230,7 +287,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- const capture = installPointerCaptureSpy(edge);
+ const capture = stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -278,7 +335,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- const capture = installPointerCaptureSpy(edge);
+ const capture = stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -321,26 +378,31 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
</template>
);
- await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ const press = async (key) => {
+ await triggerKeyEvent(EDGE, "keydown", key);
+ await triggerKeyEvent(EDGE, "keyup", key);
+ };
+
+ await press("ArrowRight");
assert.strictEqual(
state.value,
316,
"ArrowRight grows a start-docked edge"
);
- await triggerKeyEvent(EDGE, "keydown", "ArrowLeft");
+ await press("ArrowLeft");
assert.strictEqual(state.value, 300, "ArrowLeft shrinks it again");
- await triggerKeyEvent(EDGE, "keydown", "End");
+ await press("End");
assert.strictEqual(state.value, 720, "End jumps to the maximum");
- await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await press("ArrowRight");
assert.strictEqual(state.value, 720, "growing past the maximum is clamped");
- await triggerKeyEvent(EDGE, "keydown", "Home");
+ await press("Home");
assert.strictEqual(state.value, 240, "Home jumps to the minimum");
- await triggerKeyEvent(EDGE, "keydown", "ArrowLeft");
+ await press("ArrowLeft");
assert.strictEqual(
state.value,
240,
@@ -350,7 +412,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
assert.strictEqual(
state.resizeEnds.length,
state.resizes.length,
- "every keyboard step is committed as well as previewed"
+ "every discrete keyboard step is committed as well as previewed"
);
});
@@ -380,6 +442,285 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
});
+ test("a modified arrow key is left to the browser", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ // Each of these is a browser or OS binding a user expects to work while the
+ // separator happens to hold focus: back, word-wise motion, and selection.
+ for (const modifier of ["metaKey", "ctrlKey", "altKey", "shiftKey"]) {
+ await triggerKeyEvent(EDGE, "keydown", "ArrowLeft", {
+ [modifier]: true,
+ });
+ }
+
+ assert.deepEqual(
+ state.resizes,
+ [],
+ "a modified arrow does not resize, so the binding it belongs to still runs"
+ );
+ });
+
+ test("a press that never moves reports nothing", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+ stubPointerCapture(EDGE);
+
+ await triggerEvent(EDGE, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 300,
+ clientY: 0,
+ });
+ await triggerEvent(EDGE, "pointerup", {
+ pointerId: 1,
+ clientX: 300,
+ clientY: 0,
+ });
+
+ assert.deepEqual(
+ state.resizes,
+ [],
+ "a click is not a resize, so nothing is previewed"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [],
+ "and nothing is committed, so a consumer does not persist a size the user never chose"
+ );
+ assert.strictEqual(
+ state.resizeStarts,
+ 0,
+ "and no gesture is opened, so nothing is left waiting to be closed"
+ );
+ });
+
+ test("a gesture that reports opens and closes exactly once", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ axis="vertical"
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ // Sideways on a vertical resize: the projected coordinate never changes, so
+ // the release lands on the press coordinate even though the gesture reported.
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 100,
+ clientY: 300,
+ });
+ await triggerEvent(edge, "pointermove", {
+ button: 0,
+ pointerId: 1,
+ clientX: 200,
+ clientY: 300,
+ });
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+ await triggerEvent(edge, "pointerup", {
+ pointerId: 1,
+ clientX: 200,
+ clientY: 300,
+ });
+
+ assert.strictEqual(state.resizeStarts, 1, "the gesture opened once");
+ assert.strictEqual(
+ state.resizeEnds.length,
+ 1,
+ "and closed once, so a consumer holding state for the gesture can release it"
+ );
+ });
+
+ test("a key pressed during a pointer drag does not open a second gesture", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ axis="vertical"
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 300,
+ });
+ await triggerEvent(edge, "pointermove", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+
+ // A key reaching the edge while the pointer still holds it. Releasing that
+ // key must not close anything: the pointer's gesture is still open, and a
+ // consumer told the resize ended would drop the state it is holding for a
+ // drag that is still in flight.
+ await triggerKeyEvent(edge, "keydown", "ArrowDown");
+ await triggerKeyEvent(edge, "keyup", "ArrowDown");
+
+ assert.strictEqual(
+ state.resizeStarts,
+ 1,
+ "the pointer's gesture is the only one open"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [],
+ "and it is still open, because the pointer has not been released"
+ );
+
+ await triggerEvent(edge, "pointerup", { pointerId: 1, clientY: 400 });
+
+ assert.strictEqual(
+ state.resizeEnds.length,
+ 1,
+ "the release closes it, once"
+ );
+ });
+
+ test("a held key is committed when the window loses focus", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ tabindex="0"
+ {{dResizeEdge
+ axis="vertical"
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+
+ await triggerKeyEvent(edge, "keydown", "ArrowDown");
+
+ assert.strictEqual(state.resizeStarts, 1, "the key opened a gesture");
+
+ // The keyup lands on whatever has focus now, which is no longer this page,
+ // so the gesture has to be closed here or it stays open indefinitely.
+ window.dispatchEvent(new Event("blur"));
+ await settled();
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [state.value],
+ "losing the window closes it at the size reached"
+ );
+ });
+
+ test("a drag returning to its origin still closes its gesture", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ axis="vertical"
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 300,
+ });
+ await triggerEvent(edge, "pointermove", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+ await triggerEvent(edge, "pointerup", { pointerId: 1, clientY: 300 });
+
+ assert.strictEqual(state.resizeStarts, 1, "the gesture opened once");
+ assert.strictEqual(
+ state.resizeEnds.length,
+ 1,
+ "and closed once, even though the pointer was released where it started"
+ );
+ });
+
test("an end-docked edge inverts the growth direction", async function (assert) {
const state = new Harness();
@@ -400,7 +741,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- installPointerCaptureSpy(edge);
+ stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -454,7 +795,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- installPointerCaptureSpy(edge);
+ stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -474,7 +815,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const verticalEdge = find(".vertical-edge");
- installPointerCaptureSpy(verticalEdge);
+ stubPointerCapture(verticalEdge);
await triggerEvent(verticalEdge, "pointerdown", {
button: 0,
@@ -494,6 +835,72 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
});
+ test("a writing direction change between gestures is picked up", async function (assert) {
+ const state = new Harness();
+ const container = new (class {
+ @tracked dir = "ltr";
+ })();
+
+ await render(
+ <template>
+ <div dir={{container.dir}}>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 300,
+ pointerId: 1,
+ });
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 340,
+ pointerId: 1,
+ });
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [340],
+ "moving right grows a start-docked element under LTR"
+ );
+
+ container.dir = "rtl";
+ await settled();
+
+ // The direction is held for the length of a gesture, not for the length of
+ // the modifier: a document that switches direction between two drags has to
+ // be measured again rather than resized the old way.
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 300,
+ pointerId: 2,
+ });
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 340,
+ pointerId: 2,
+ });
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [340, 260],
+ "the same movement shrinks it once the document reads right-to-left"
+ );
+ });
+
test("a vertical edge follows the pointer and the vertical arrows", async function (assert) {
const state = new Harness();
@@ -514,7 +921,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- installPointerCaptureSpy(edge);
+ stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -570,7 +977,7 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
);
const edge = find(EDGE);
- installPointerCaptureSpy(edge);
+ stubPointerCapture(edge);
await triggerEvent(edge, "pointerdown", {
button: 0,
@@ -592,4 +999,798 @@ module("Integration | Modifier | d-resize-edge", function (hooks) {
await triggerKeyEvent(EDGE, "keydown", "ArrowUp");
assert.strictEqual(state.value, 136, "ArrowUp grows it too");
});
+
+ module("live argument reads", function () {
+ test("a function value is read at the start of every pointer gesture", async function (assert) {
+ const state = new Harness();
+ let value = 500;
+ let reads = 0;
+ const readValue = () => {
+ reads++;
+ return value;
+ };
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=readValue
+ min=240
+ max=720
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 100,
+ pointerId: 1,
+ });
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 150,
+ pointerId: 1,
+ });
+
+ value = 400;
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 200,
+ pointerId: 2,
+ });
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 180,
+ pointerId: 2,
+ });
+
+ assert.strictEqual(reads, 2, "value is read once for each gesture");
+ assert.deepEqual(
+ state.resizeEnds,
+ [550, 380],
+ "each gesture starts from the value read when it begins"
+ );
+ });
+
+ test("onResizeStart fires once and its return value is ignored", async function (assert) {
+ const state = new Harness();
+ let starts = 0;
+ const onResizeStart = () => {
+ starts++;
+ return 500;
+ };
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 100,
+ pointerId: 1,
+ });
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 150,
+ pointerId: 1,
+ });
+
+ assert.strictEqual(starts, 1, "the callback runs at gesture time");
+ assert.deepEqual(
+ state.resizeEnds,
+ [350],
+ "the gesture starts from value regardless of the callback return"
+ );
+ });
+
+ test("mixed numeric and function bounds are read for every pointer clamp", async function (assert) {
+ const state = new Harness();
+ let max = 360;
+ let maxReads = 0;
+ const readMax = () => {
+ maxReads++;
+ return max;
+ };
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=readMax
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 300,
+ pointerId: 1,
+ });
+ await triggerEvent(edge, "pointermove", {
+ button: 0,
+ clientX: 400,
+ pointerId: 1,
+ });
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+
+ max = 340;
+ await triggerEvent(edge, "pointerup", {
+ button: 0,
+ clientX: 420,
+ pointerId: 1,
+ });
+
+ assert.strictEqual(maxReads, 2, "max is read for each pointer clamp");
+ assert.deepEqual(
+ state.resizes,
+ [360, 340],
+ "each pointer clamp uses the current maximum"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [340],
+ "the gesture commits once at the current maximum"
+ );
+ });
+
+ test("an interrupted resize keeps the size it was dragged to", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 300,
+ pointerId: 1,
+ });
+ await triggerEvent(edge, "pointermove", {
+ button: 0,
+ clientX: 400,
+ pointerId: 1,
+ });
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+
+ // The OS taking the pointer away mid-gesture. The modifier asks the gesture
+ // engine to commit on cancel rather than revert, because snapping a pane
+ // back to where it started would discard a resize the user watched happen.
+ // Some browsers initialize `pointercancel` coordinates to (0,0), so the
+ // event's own position must play no part in what gets committed.
+ await triggerEvent(edge, "pointercancel", {
+ button: 0,
+ clientX: 0,
+ pointerId: 1,
+ });
+
+ assert.strictEqual(state.resizeStarts, 1, "the gesture opened once");
+ assert.deepEqual(
+ state.resizes,
+ [400, 400],
+ "the drag reported the size it reached, and the commit reports it again as a release would"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [400],
+ "and cancellation commits the size the pointer dragged to, not one recomputed from the cancel event"
+ );
+ });
+
+ test("an interrupted press that never moved commits nothing", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 300,
+ pointerId: 1,
+ });
+
+ // A press the OS interrupts before any movement is still a click, not a
+ // resize — the (0,0) the cancel event carries must not defeat the guard.
+ await triggerEvent(edge, "pointercancel", {
+ button: 0,
+ clientX: 0,
+ pointerId: 1,
+ });
+
+ assert.strictEqual(state.resizeStarts, 0, "no gesture opened");
+ assert.deepEqual(state.resizes, [], "nothing was reported");
+ assert.deepEqual(state.resizeEnds, [], "nothing was committed");
+ });
+
+ test("coalesces the pointer moves sharing a frame into one report", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ clientX: 300,
+ pointerId: 1,
+ });
+
+ // Dispatched without awaiting in between: an await gives the browser room
+ // to paint, which would put each move in its own frame and test the
+ // opposite of the coalescing.
+ for (const clientX of [320, 340, 360]) {
+ edge.dispatchEvent(
+ new PointerEvent("pointermove", {
+ bubbles: true,
+ button: 0,
+ clientX,
+ pointerId: 1,
+ })
+ );
+ }
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+
+ assert.deepEqual(
+ state.resizes,
+ [360],
+ "the positions in between are dropped and only the latest is reported"
+ );
+
+ edge.dispatchEvent(
+ new PointerEvent("pointermove", {
+ bubbles: true,
+ button: 0,
+ clientX: 380,
+ pointerId: 1,
+ })
+ );
+ await new Promise((resolve) => requestAnimationFrame(resolve));
+
+ assert.deepEqual(
+ state.resizes,
+ [360, 380],
+ "the next frame reports again, so a frame is coalesced rather than the gesture reporting once"
+ );
+ });
+
+ test("function value and bounds are read for every arrow key press", async function (assert) {
+ const state = new Harness();
+ let value = 300;
+ let min = 240;
+ let max = 720;
+ const reads = { value: 0, min: 0, max: 0 };
+ const readValue = () => {
+ reads.value++;
+ return value;
+ };
+ const readMin = () => {
+ reads.min++;
+ return min;
+ };
+ const readMax = () => {
+ reads.max++;
+ return max;
+ };
+ state.onResize = (size) => {
+ state.resizes.push(size);
+ value = size;
+ };
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=readValue
+ min=readMin
+ max=readMax
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ // Released between presses, so each is its own gesture. A held key is one
+ // gesture and re-reads `value` only at its start — covered separately.
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ max = 320;
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ min = 310;
+ await triggerKeyEvent(EDGE, "keydown", "ArrowLeft");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowLeft");
+
+ assert.deepEqual(
+ reads,
+ { value: 3, min: 3, max: 3 },
+ "value and both bounds are read for each discrete arrow key press"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [316, 320, 310],
+ "arrow keys clamp against bounds that changed since render"
+ );
+ });
+ });
+
+ module("keyboard gesture", function () {
+ test("a held arrow key steps once per repeat, not once per settled size", async function (assert) {
+ const state = new Harness();
+ // Deliberately frozen: a caller whose size is a live measurement reports a
+ // box that is still animating toward the last size, so re-reading it on
+ // every repeat would fold most of the step away.
+ const stuckValue = () => 300;
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=stuckValue
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight", { repeat: true });
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight", { repeat: true });
+
+ assert.deepEqual(
+ state.resizes,
+ [316, 332, 348],
+ "each repeat steps from the size last reported, not from one that has not caught up"
+ );
+
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ assert.strictEqual(
+ state.resizeStarts,
+ 1,
+ "the whole burst opens exactly one gesture"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [348],
+ "and commits once, at the size it ended on"
+ );
+ });
+
+ test("each discrete press is its own gesture", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ assert.strictEqual(state.resizeStarts, 2, "one gesture per press");
+ assert.deepEqual(
+ state.resizeEnds,
+ [316, 332],
+ "each press commits its own size"
+ );
+ });
+
+ test("losing focus mid-burst ends the gesture", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ tabindex="0"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ // The keyup lands on whatever took focus, so without this the gesture
+ // would stay open forever and never commit.
+ await triggerEvent(EDGE, "blur");
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [316],
+ "the gesture commits the size it reached"
+ );
+
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [316],
+ "and a keyup arriving afterwards does not commit a second time"
+ );
+ });
+
+ test("every started gesture ends exactly once, whichever input began it", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ tabindex="0"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ // A pointer taking over mid-burst must close the keyboard gesture rather
+ // than open a second one on top of it.
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 100,
+ clientY: 0,
+ });
+ await triggerEvent(edge, "pointerup", {
+ pointerId: 1,
+ clientX: 140,
+ clientY: 0,
+ });
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ assert.strictEqual(
+ state.resizeStarts,
+ state.resizeEnds.length,
+ "starts and ends stay balanced across both inputs"
+ );
+ assert.strictEqual(state.resizeStarts, 2, "one keyboard, one pointer");
+ });
+ });
+
+ test("bodyClass marks the page for the length of the gesture", async function (assert) {
+ await render(
+ <template>
+ <div
+ class="dre-edge"
+ {{dResizeEdge value=300 min=100 max=500 bodyClass="d-resizing-ns"}}
+ ></div>
+ </template>
+ );
+
+ const edge = find(".dre-edge");
+ stubPointerCapture(edge);
+
+ assert.dom(document.body).doesNotHaveClass("d-resizing-ns");
+
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ // A splitter's cursor has to outlive the pointer leaving the handle, and
+ // whether that happens on its own is engine-dependent.
+ assert.dom(document.body).hasClass("d-resizing-ns");
+
+ await triggerEvent(edge, "pointerup", { pointerId: 1, clientY: 400 });
+ assert.dom(document.body).doesNotHaveClass("d-resizing-ns");
+ });
+
+ module(
+ "coverage for paths the suite could not previously distinguish",
+ function () {
+ /**
+ * Dispatches a real cancelable KeyboardEvent, because `triggerKeyEvent`'s
+ * return value does not expose whether the handler cancelled it, and
+ * cancellation is the whole contract under test here.
+ */
+ async function pressKey(selector, key, options = {}) {
+ const event = new KeyboardEvent("keydown", {
+ key,
+ bubbles: true,
+ cancelable: true,
+ ...options,
+ });
+ find(selector).dispatchEvent(event);
+ await settled();
+ return event;
+ }
+
+ test("a key the separator acts on is claimed, and one it ignores is left alone", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ // Claiming a key means cancelling it. Without that, an arrow also scrolls
+ // the page under the splitter and Cmd+Left still navigates back.
+ const handled = await pressKey(EDGE, "ArrowRight");
+ assert.true(handled.defaultPrevented, "a resize key is claimed");
+
+ const unhandled = await pressKey(EDGE, "ArrowUp");
+ assert.false(
+ unhandled.defaultPrevented,
+ "a key for the other axis is left to the browser"
+ );
+
+ const modified = await pressKey(EDGE, "ArrowRight", { metaKey: true });
+ assert.false(
+ modified.defaultPrevented,
+ "a modified arrow belongs to whatever binding claims it"
+ );
+
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+ });
+
+ test("releasing a different key does not end the gesture the held one owns", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowLeft");
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [],
+ "a stray release does not commit the gesture still being held"
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight", { repeat: true });
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ assert.strictEqual(
+ state.resizeStarts,
+ 1,
+ "the hold was one gesture throughout"
+ );
+ assert.deepEqual(
+ state.resizeEnds,
+ [332],
+ "and it commits once, when its own key is released"
+ );
+ });
+
+ test("losing pointer capture commits the last position the pointer reached", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ const edge = find(EDGE);
+ stubPointerCapture(edge);
+
+ await triggerEvent(edge, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 300,
+ });
+ await triggerEvent(edge, "pointermove", { pointerId: 1, clientX: 340 });
+ await new Promise(requestAnimationFrame);
+ await settled();
+
+ // A browser-fired capture loss carries no meaningful position, so
+ // recomputing from the event would snap the size to a clamp bound.
+ await triggerEvent(edge, "lostpointercapture", {
+ pointerId: 1,
+ clientX: 0,
+ });
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [340],
+ "the commit uses the last real position, not the event's own"
+ );
+ });
+
+ test("arrow keys follow the writing direction, as the pointer does", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div dir="rtl">
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ side="start"
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ await triggerKeyEvent(EDGE, "keyup", "ArrowRight");
+
+ // In RTL the inline-start edge sits on the right, so moving right shrinks
+ // the box. The keyboard has to agree with the pointer about that, or the
+ // two inputs disagree on the same handle.
+ assert.deepEqual(
+ state.resizes,
+ [284],
+ "ArrowRight shrinks a start-docked box under RTL, matching the drag direction"
+ );
+ });
+
+ test("teardown mid-keyboard-gesture drops it rather than committing", async function (assert) {
+ const state = new Harness();
+
+ await render(
+ <template>
+ <div
+ class="resize-edge"
+ {{dResizeEdge
+ value=state.value
+ min=240
+ max=720
+ onResizeStart=state.onResizeStart
+ onResize=state.onResize
+ onResizeEnd=state.onResizeEnd
+ }}
+ ></div>
+ </template>
+ );
+
+ await triggerKeyEvent(EDGE, "keydown", "ArrowRight");
+ assert.strictEqual(state.resizeStarts, 1, "the hold opened a gesture");
+
+ await clearRender();
+
+ assert.deepEqual(
+ state.resizeEnds,
+ [],
+ "teardown drops the held gesture instead of committing a size the user never released"
+ );
+ });
+ }
+ );
});
diff --git a/frontend/discourse/tests/integration/ui-kit/d-resize-separator-test.gjs b/frontend/discourse/tests/integration/ui-kit/d-resize-separator-test.gjs
new file mode 100644
index 00000000000..b07ff5dbfbd
--- /dev/null
+++ b/frontend/discourse/tests/integration/ui-kit/d-resize-separator-test.gjs
@@ -0,0 +1,783 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
+import { trustHTML } from "@ember/template";
+import {
+ find,
+ render,
+ settled,
+ triggerEvent,
+ triggerKeyEvent,
+ waitUntil,
+} from "@ember/test-helpers";
+import { module, test } from "qunit";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+import { stubPointerCapture } from "discourse/tests/helpers/ui-kit/pointer-gesture-helper";
+import DResizeSeparator from "discourse/ui-kit/d-resize-separator";
+
+module("Integration | ui-kit | DResizeSeparator", function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("a vertical resize is announced as a horizontal separator", async function (assert) {
+ const size = () => 300;
+ const min = () => 100;
+ const max = () => 500;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{min}}
+ @max={{max}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ assert
+ .dom(".my-handle")
+ .hasAttribute("role", "separator", "it is a separator")
+ .hasAttribute(
+ "aria-orientation",
+ "horizontal",
+ "a separator between vertically stacked regions is itself a horizontal bar, so the orientation is the opposite of the axis"
+ )
+ .hasAttribute("tabindex", "0", "it is a single tab stop")
+ .hasAttribute(
+ "aria-label",
+ "Resize the thing",
+ "the label names what is being resized"
+ )
+ .hasAttribute("aria-valuenow", "300", "the current size is announced")
+ .hasAttribute("aria-valuemin", "100", "the lower bound is announced")
+ .hasAttribute("aria-valuemax", "500", "the upper bound is announced");
+ });
+
+ test("carries a styling hook that does not depend on the ARIA", async function (assert) {
+ const size = () => 300;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ // Stylesheets key off these, never off `role` or `aria-orientation`: those are
+ // what the element promises assistive technology, and a consumer may override
+ // them through attributes without meaning to restyle anything.
+ assert
+ .dom(".my-handle")
+ .hasClass("d-resize-separator", "it carries its own class")
+ .hasAttribute("data-resize-axis", "vertical");
+ assert
+ .dom(".my-handle")
+ .hasClass(
+ "my-handle",
+ "and keeps the consumer's, rather than replacing it"
+ );
+ });
+
+ test("the styling hook names the axis being resized, not the bar's direction", async function (assert) {
+ const size = () => 300;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="horizontal"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the panel"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ assert.dom(".my-handle").hasAttribute("data-resize-axis", "horizontal");
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-orientation",
+ "vertical",
+ "which is the opposite of what it announces, so the two must not be conflated"
+ );
+ });
+
+ test("a horizontal resize is announced as a vertical separator", async function (assert) {
+ const size = () => 240;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="horizontal"
+ @value={{size}}
+ @min={{0}}
+ @max={{800}}
+ @label="Resize the panel"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ assert
+ .dom(".my-handle")
+ .hasAttribute("aria-orientation", "vertical")
+ .hasAttribute("aria-valuenow", "240")
+ .hasAttribute("aria-valuemin", "0", "a plain number is accepted too")
+ .hasAttribute("aria-valuemax", "800");
+ });
+
+ test("nothing is announced as the size until it has been measured", async function (assert) {
+ const unmeasured = () => null;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{unmeasured}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ // Reporting zero would claim the box has no height. An absent value says
+ // "not known yet", which is the honest answer before a measurement lands.
+ assert.dom(".my-handle").doesNotHaveAttribute("aria-valuenow");
+ assert.dom(".my-handle").hasAttribute("aria-valuemin", "100");
+ });
+
+ test("a pointer drag reports the new size and updates what is announced", async function (assert) {
+ const reports = [];
+ let current = 300;
+ const size = () => current;
+ const onResize = (next) => {
+ current = next;
+ reports.push(`resize:${next}`);
+ };
+ const onResizeEnd = (next) => reports.push(`end:${next}`);
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{onResize}}
+ @onResizeEnd={{onResizeEnd}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 0,
+ clientY: 400,
+ });
+ // Docked against the block-end, so dragging up grows the box.
+ await triggerEvent(handle, "pointermove", {
+ pointerId: 1,
+ clientX: 0,
+ clientY: 360,
+ });
+ await triggerEvent(handle, "pointerup", {
+ pointerId: 1,
+ clientX: 0,
+ clientY: 360,
+ });
+
+ // Asserted on the outcome rather than the cadence: how many previews precede a
+ // commit is the modifier's contract and is pinned by its own suite.
+ assert.deepEqual(
+ reports.filter((report) => report.startsWith("end:")),
+ ["end:340"],
+ "the gesture commits exactly once, at the size it ended on"
+ );
+ assert.true(
+ reports.every((report) => report.endsWith(":340")),
+ "the consumer is told the size, not the pointer position"
+ );
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-valuenow",
+ "340",
+ "and the announced size follows without the consumer wiring it"
+ );
+ });
+
+ test("renders block content, for a handle that shows an affordance", async function (assert) {
+ const size = () => 300;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ class="my-handle"
+ ><span class="my-grip">grip</span></DResizeSeparator>
+ </template>
+ );
+
+ // Some handles draw themselves with a pseudo-element, others put a real icon
+ // inside. Both have to be possible without giving up the separator semantics.
+ assert
+ .dom(".my-handle .my-grip")
+ .exists("content passed to the separator is rendered inside it");
+ });
+
+ test("the announced size settles on commit rather than following every move", async function (assert) {
+ let current = 300;
+ const size = () => current;
+ const onResize = (next) => (current = next);
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{onResize}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ await triggerEvent(handle, "pointermove", { pointerId: 1, clientY: 360 });
+
+ // Deliberately unchanged mid-gesture. The modifier reports the last move and the
+ // commit in one stack, so writing tracked state in between opens a runloop, and
+ // a consumer callback reached with one already open loses the wrapper that hands
+ // its exceptions to the framework. The composer's persistence-failure acceptance
+ // test is what guards that consequence; this pins the behaviour that avoids it.
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-valuenow",
+ "300",
+ "nothing is announced while it moves"
+ );
+
+ await triggerEvent(handle, "pointerup", { pointerId: 1, clientY: 360 });
+
+ assert
+ .dom(".my-handle")
+ .hasAttribute("aria-valuenow", "340", "the committed size is announced");
+ });
+
+ test("the cursor is held on the page for the length of the gesture", async function (assert) {
+ const size = () => 300;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+
+ assert.dom(document.body).doesNotHaveClass("d-resizing-ns");
+
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 0,
+ clientY: 400,
+ });
+
+ // Keeping the cursor of the element a drag began on is engine-dependent, so
+ // the intent is stated on the page for as long as the gesture lasts.
+ assert
+ .dom(document.body)
+ .hasClass("d-resizing-ns", "the axis decides which cursor is held");
+
+ await triggerEvent(handle, "pointerup", { pointerId: 1, clientY: 400 });
+
+ assert
+ .dom(document.body)
+ .doesNotHaveClass("d-resizing-ns", "and it is given back on release");
+ });
+
+ test("a horizontal gesture holds the other cursor", async function (assert) {
+ const size = () => 300;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="horizontal"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the panel"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientX: 200,
+ clientY: 0,
+ });
+
+ assert.dom(document.body).hasClass("d-resizing-ew");
+ assert.dom(document.body).doesNotHaveClass("d-resizing-ns");
+
+ await triggerEvent(handle, "pointerup", { pointerId: 1, clientX: 200 });
+ assert.dom(document.body).doesNotHaveClass("d-resizing-ew");
+ });
+
+ test("a held arrow key keeps the announced size in step", async function (assert) {
+ let current = 300;
+ const size = () => current;
+ const onResize = (next) => (current = next);
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{onResize}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ // A held key is one gesture spanning every repeat, so the commit that would
+ // otherwise refresh the announcement does not arrive until release. Assistive
+ // technology reading the value in between must not be told a stale one.
+ await triggerKeyEvent(".my-handle", "keydown", "ArrowUp");
+ await triggerKeyEvent(".my-handle", "keydown", "ArrowUp");
+
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-valuenow",
+ "332",
+ "two repeats of a 16px step are announced before the key is released"
+ );
+ });
+
+ test("arrow keys resize without a pointer", async function (assert) {
+ const reports = [];
+ let current = 300;
+ const size = () => current;
+ const onResize = (next) => {
+ current = next;
+ reports.push(next);
+ };
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{onResize}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ await triggerKeyEvent(".my-handle", "keydown", "ArrowUp");
+ // The announced size settles when the key is released, the same way it
+ // settles when a pointer is.
+ await triggerKeyEvent(".my-handle", "keyup", "ArrowUp");
+
+ assert.deepEqual(
+ reports,
+ [316],
+ "one press reports once, a single 16px step above the starting size"
+ );
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-valuenow",
+ "316",
+ "the keyboard path keeps the announced size in step too"
+ );
+ });
+
+ test("re-reads the size when the box it resizes changes on its own", async function (assert) {
+ const size = () => find(".the-box")?.clientHeight ?? null;
+ const resolve = (separator) => separator.parentElement;
+
+ await render(
+ <template>
+ <div class="the-box" style="height: 300px">
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @observe={{resolve}}
+ class="my-handle"
+ />
+ </div>
+ </template>
+ );
+
+ assert.dom(".my-handle").hasAttribute("aria-valuenow", "300");
+
+ // The box can be resized by things no gesture caused — a panel opening, a
+ // preview toggling, the window changing — and the announced size has to follow
+ // without the consumer wiring anything up.
+ find(".the-box").style.height = "420px";
+ await waitUntil(
+ () => find(".my-handle").getAttribute("aria-valuenow") === "420"
+ );
+
+ assert.dom(".my-handle").hasAttribute("aria-valuenow", "420");
+ });
+
+ test("a plain value is announced as it changes, without waiting for a gesture", async function (assert) {
+ const state = new (class {
+ @tracked ceiling = 400;
+ })();
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{120}}
+ @min={{0}}
+ @max={{state.ceiling}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ assert.dom(".my-handle").hasAttribute("aria-valuemax", "400");
+
+ // A number given directly is already reactive, so it is read straight through
+ // rather than mirrored: snapshotting it would freeze it until something else
+ // happened to trigger a re-read.
+ state.ceiling = 900;
+ await settled();
+
+ assert.dom(".my-handle").hasAttribute("aria-valuemax", "900");
+ });
+
+ test("re-reads the bounds when the viewport changes", async function (assert) {
+ let ceiling = 500;
+ const size = () => 300;
+ const max = () => ceiling;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{100}}
+ @max={{max}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ assert.dom(".my-handle").hasAttribute("aria-valuemax", "500");
+
+ // How large a box may grow is usually whatever is left of the window, and a
+ // window change need not alter the box itself, so nothing else would notice.
+ ceiling = 900;
+ window.dispatchEvent(new Event("resize"));
+ await settled();
+
+ assert.dom(".my-handle").hasAttribute("aria-valuemax", "900");
+ });
+
+ test("starts observing a box that only exists after the first render", async function (assert) {
+ const state = new (class {
+ @tracked target = null;
+ })();
+ const size = () => find(".late-box")?.clientHeight ?? null;
+
+ await render(
+ <template>
+ <div class="late-box" style="height: 200px"></div>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @observe={{state.target}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ // Nothing was being observed yet, so a change went unnoticed.
+ find(".late-box").style.height = "260px";
+ await settled();
+ assert.dom(".my-handle").hasAttribute("aria-valuenow", "200");
+
+ // A consumer that builds its own box after render hands it over once it exists.
+ state.target = find(".late-box");
+ await settled();
+
+ find(".late-box").style.height = "340px";
+ await waitUntil(
+ () => find(".my-handle").getAttribute("aria-valuenow") === "340"
+ );
+
+ assert.dom(".my-handle").hasAttribute("aria-valuenow", "340");
+ });
+
+ test("a drag on an unmeasured size reports nothing rather than resizing from zero", async function (assert) {
+ const unmeasured = () => null;
+ const reports = [];
+ const record = (size) => reports.push(size);
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{unmeasured}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{record}}
+ @onResizeEnd={{record}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ await triggerEvent(handle, "pointermove", { pointerId: 1, clientY: 360 });
+ await new Promise(requestAnimationFrame);
+ await settled();
+ await triggerEvent(handle, "pointerup", { pointerId: 1, clientY: 360 });
+
+ // Without a starting size there is nothing to resize *from*, so the honest
+ // answer is to refuse. Reporting would clamp `null + delta` to a bound and
+ // move the box somewhere the user never dragged.
+ assert.deepEqual(
+ reports,
+ [],
+ "an unmeasured size refuses the gesture instead of reporting a clamped one"
+ );
+ });
+
+ test("resize keys on an unmeasured size are left to the browser", async function (assert) {
+ const unmeasured = () => null;
+ const reports = [];
+ const record = (size) => reports.push(size);
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{unmeasured}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{record}}
+ @onResizeEnd={{record}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ await triggerKeyEvent(".my-handle", "keydown", "ArrowUp");
+ await triggerKeyEvent(".my-handle", "keyup", "ArrowUp");
+
+ assert.deepEqual(
+ reports,
+ [],
+ "an unmeasured size opens no keyboard gesture"
+ );
+ });
+
+ test("onResizeStart reaches the consumer, once, before the first size", async function (assert) {
+ const events = [];
+ let current = 300;
+ const size = () => current;
+
+ const onResizeStart = () => events.push("start");
+ const onResize = (next) => {
+ current = next;
+ events.push(`resize:${next}`);
+ };
+ const onResizeEnd = (next) => events.push(`end:${next}`);
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{size}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResizeStart={{onResizeStart}}
+ @onResize={{onResize}}
+ @onResizeEnd={{onResizeEnd}}
+ class="my-handle"
+ />
+ </template>
+ );
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ await triggerEvent(handle, "pointermove", { pointerId: 1, clientY: 360 });
+ await triggerEvent(handle, "pointerup", { pointerId: 1, clientY: 360 });
+
+ // Consumers that suppress transitions or open an overlay for the length of a
+ // gesture hang off this, so it has to survive the component forwarding it.
+ assert.strictEqual(
+ events[0],
+ "start",
+ "the gesture opens before it reports"
+ );
+ assert.strictEqual(
+ events.filter((event) => event === "start").length,
+ 1,
+ "and opens exactly once"
+ );
+ assert.strictEqual(events.at(-1), "end:340", "with the commit last");
+ });
+
+ test("the announced size keeps up with a value that is a live DOM measurement", async function (assert) {
+ // The suite's other fixtures are synchronous closures, which update before
+ // the component re-reads them. A real measurement cannot: it only changes
+ // once the consumer's tracked write has rendered, which is precisely the
+ // case the function form of `@value` exists for.
+ class Host extends Component {
+ @tracked height = 300;
+ registerBox = (element) => (this.#box = element);
+ measure = () => (this.#box ? this.#box.offsetHeight : null);
+ onResize = (size) => (this.height = size);
+ #box;
+
+ get boxStyle() {
+ return trustHTML(`height: ${this.height}px`);
+ }
+
+ <template>
+ <div
+ class="the-box"
+ style={{this.boxStyle}}
+ {{didInsert this.registerBox}}
+ ></div>
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{this.measure}}
+ @min={{100}}
+ @max={{500}}
+ @label="Resize the thing"
+ @onResize={{this.onResize}}
+ class="my-handle"
+ />
+ </template>
+ }
+
+ await render(<template><Host /></template>);
+
+ const { element: handle } = stubPointerCapture(".my-handle");
+ await triggerEvent(handle, "pointerdown", {
+ button: 0,
+ pointerId: 1,
+ clientY: 400,
+ });
+ await triggerEvent(handle, "pointermove", { pointerId: 1, clientY: 360 });
+ await triggerEvent(handle, "pointerup", { pointerId: 1, clientY: 360 });
+
+ assert.dom(".the-box").hasStyle({ height: "340px" }, "the box resized");
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-valuenow",
+ "340",
+ "and the announcement matches it rather than trailing a render behind"
+ );
+ });
+
+ test("the announced size stays inside the announced bounds", async function (assert) {
+ // A bound derived from the viewport can drop below a size the consumer has
+ // not yet reduced. ARIA requires valuenow to lie within valuemin..valuemax,
+ // and a screen reader reading 500 of a 400 maximum is nonsense.
+ const shrinkingMax = () => 400;
+
+ await render(
+ <template>
+ <DResizeSeparator
+ @axis="vertical"
+ @value={{500}}
+ @min={{100}}
+ @max={{shrinkingMax}}
+ @label="Resize the thing"
+ class="my-handle"
+ />
+ </template>
+ );
+
+ assert
+ .dom(".my-handle")
+ .hasAttribute(
+ "aria-valuenow",
+ "400",
+ "a size beyond the maximum is announced as the maximum"
+ );
+ });
+});
diff --git a/frontend/discourse/type-tests/ui-kit/d-resize-separator-errors-test.gts b/frontend/discourse/type-tests/ui-kit/d-resize-separator-errors-test.gts
new file mode 100644
index 00000000000..49b203d438d
--- /dev/null
+++ b/frontend/discourse/type-tests/ui-kit/d-resize-separator-errors-test.gts
@@ -0,0 +1,24 @@
+// Negative type assertions for DResizeSeparator: every invocation below must
+// fail to compile. They are quarantined here because a Glint error directive
+// suppresses reporting elsewhere in its file, which would let a positive
+// assertion pass against a broken declaration. Positives live in
+// d-resize-separator-test.gts.
+import DResizeSeparator from "discourse/ui-kit/d-resize-separator";
+
+declare const px: number;
+
+const Negatives = <template>
+ {{! @glint-expect-error - a focusable separator with no accessible name is announced as a bare splitter, so the label is required }}
+ <DResizeSeparator @value={{px}} @min={{px}} @max={{px}} />
+
+ <DResizeSeparator
+ {{! @glint-expect-error - a separator resizes along one of two axes }}
+ @axis="diagonal"
+ @value={{px}}
+ @min={{px}}
+ @max={{px}}
+ @label="Resize"
+ />
+</template>;
+
+export default Negatives;
diff --git a/frontend/discourse/type-tests/ui-kit/d-resize-separator-test.gts b/frontend/discourse/type-tests/ui-kit/d-resize-separator-test.gts
new file mode 100644
index 00000000000..5d74c131c44
--- /dev/null
+++ b/frontend/discourse/type-tests/ui-kit/d-resize-separator-test.gts
@@ -0,0 +1,44 @@
+// Positive template invocations for DResizeSeparator. Keep this file free of
+// Glint directives: a single `@glint-expect-error` anywhere makes Glint stop
+// reporting every other error in the file, so a broken declaration would pass
+// unnoticed. Negatives live in d-resize-separator-errors-test.gts.
+import DResizeSeparator from "discourse/ui-kit/d-resize-separator";
+
+declare const noop: () => void;
+declare const size: number;
+declare const measure: () => number;
+declare const measureMaybe: () => number | null;
+declare const box: HTMLElement;
+
+const Positives = <template>
+ {{! A separator takes numbers or measurement functions for its size }}
+ <DResizeSeparator
+ @axis="vertical"
+ @side="end"
+ @value={{size}}
+ @min={{size}}
+ @max={{size}}
+ @label="Resize"
+ @observe={{box}}
+ @onResizeStart={{noop}}
+ class="my-block__handle"
+ />
+ <DResizeSeparator
+ @axis="horizontal"
+ @value={{measure}}
+ @min={{measure}}
+ @max={{measure}}
+ @label="Resize"
+ >grip</DResizeSeparator>
+ {{! A measured SIZE may honestly say "not yet": null withholds aria-valuenow.
+ Bounds may not, so they stay non-nullable here. }}
+ <DResizeSeparator
+ @axis="horizontal"
+ @value={{measureMaybe}}
+ @min={{size}}
+ @max={{measure}}
+ @label="Resize"
+ />
+</template>;
+
+export default Positives;