@uniflowed/ui 0.0.0-alpha.12 → 0.0.0-alpha.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/popover.js CHANGED
@@ -58,7 +58,7 @@ import {
58
58
  } from "@uniflowed/react";
59
59
  import { useStableCallback } from "@uniflowed/hooks/lifecycle";
60
60
 
61
- import type { Align, Side } from "./internal/anchor.js";
61
+ import type { Align, LogicalSide } from "./internal/anchor.js";
62
62
  import type { Rest } from "./internal/merge-props.js";
63
63
  import { composeHandlers, composeRefs, withoutComposed } from "./internal/merge-props.js";
64
64
  import { focusable } from "./internal/focus.js";
@@ -66,7 +66,7 @@ import { useAnchor } from "./internal/anchor.js";
66
66
  import { useControlled } from "./internal/controlled-state.js";
67
67
  import { usePresence } from "./internal/disclosure.js";
68
68
 
69
- export type { Align, Side } from "./internal/anchor.js";
69
+ export type { Align, LogicalSide, Side } from "./internal/anchor.js";
70
70
 
71
71
  type PopoverState = {|
72
72
  readonly base: string,
@@ -188,7 +188,15 @@ export component PopoverBody(
188
188
  alignOffset?: number = 0,
189
189
  avoidCollisions?: boolean = true,
190
190
  collisionPadding?: number = 0,
191
- side?: Side = "bottom",
191
+ /**
192
+ * Where focus lands when it opens, when the first focus stop is the wrong
193
+ * answer. The same prop `Dialog.Body` takes, deliberately spelled the same
194
+ * way: `DatePicker.Calendar` fills it with the day that holds the grid's tab
195
+ * stop, because a reader who opened a date picker is looking for the date and
196
+ * not for the button that steps back a month.
197
+ */
198
+ initialFocus?: { current: HTMLElement | null },
199
+ side?: LogicalSide = "bottom",
192
200
  sideOffset?: number = 0,
193
201
  ...rest: Rest
194
202
  ) {
@@ -257,10 +265,15 @@ export component PopoverBody(
257
265
  };
258
266
  document.addEventListener("focusin", onFocusMoved, true);
259
267
 
260
- // The first thing worth acting on, and the popover itself when it holds
261
- // nothing focusable, so focus is inside it either way and Escape reaches
262
- // the handler below.
263
- (focusable(body)[0] ?? body).focus();
268
+ // Where the caller said, then the first thing worth acting on, then the
269
+ // popover itself when it holds nothing focusable - so focus is inside it
270
+ // whichever of the three answers, and Escape reaches the handler below.
271
+ //
272
+ // The named element has to still be *in* this popover, for the reason
273
+ // `dialog.js` gives at the same line: a ref left from a previous opening
274
+ // would move focus somewhere the reader did not open.
275
+ const named = initialFocus?.current ?? null;
276
+ ((named != null && body.contains(named) ? named : focusable(body)[0]) ?? body).focus();
264
277
 
265
278
  return () => {
266
279
  document.removeEventListener("pointerdown", onOutsidePress, true);
@@ -278,7 +291,7 @@ export component PopoverBody(
278
291
  opener?.focus?.();
279
292
  }
280
293
  };
281
- }, [popover.open, triggerRef, close]);
294
+ }, [popover.open, triggerRef, close, initialFocus]);
282
295
 
283
296
  if (!popover.open) {
284
297
  return null;
package/resizable.js CHANGED
@@ -10,8 +10,42 @@
10
10
  //
11
11
  // Almost every resizable panel on the web is pointer-only, which is a WCAG
12
12
  // 2.1.1 failure — no keyboard operation at all — and a 2.5.7 one on top of it.
13
- // If uf ships one, the keyboard is the feature, so the keyboard is what this
14
- // module is: there is no drag here yet, and there is a complete key map.
13
+ // If uf ships one the keyboard is the feature, so the keyboard was written
14
+ // first and shipped on its own: a keyboard-only splitter is a working
15
+ // splitter, where a pointer-only one is not.
16
+ //
17
+ // It was not the finished control. A bar between two panes is a bar that
18
+ // approximately everybody takes hold of first, and this one could not be
19
+ // moved that way at all. Both halves are here now, and the key map is exactly
20
+ // what it was — a drag that had quietly replaced it would be the first failure
21
+ // in the other direction.
22
+ //
23
+ // # The drag, and the step it does not use
24
+ //
25
+ // It is `slider.js`'s `Slider.Track` arithmetic measured against the *group's*
26
+ // box rather than a track's. `pointerdown` captures the pointer, so a drag
27
+ // that wanders off a bar four pixels wide — which every drag does — keeps
28
+ // arriving at the handle instead of being lost to whatever it wandered over;
29
+ // `pointermove` turns the position into a percentage from
30
+ // `getBoundingClientRect`; and the percentage goes through `internal/range.js`
31
+ // like every other value here, so a drag cannot leave the pane anywhere an
32
+ // arrow key could not put it, and `aria-valuenow` is true about it while it
33
+ // moves.
34
+ //
35
+ // Pressing the handle does not move it. A press on a *track* means "put the
36
+ // value here", which is what `Slider.Track` does with one and why it is half
37
+ // of WCAG 2.5.7 there; a press on a handle means "take hold of this". A
38
+ // splitter that also jumped by the distance between the pointer and its own
39
+ // centre would move a little every time it was clicked, which is the one thing
40
+ // a person who clicked it did not ask for.
41
+ //
42
+ // `step` stays the keyboard's, and the pointer has one of its own. `step`
43
+ // defaults to 10 because ten presses of an arrow key ought to cross the pane,
44
+ // and 10 is an absurd granularity for a bar being dragged under a pointer that
45
+ // moves smoothly. The pointer's is one percent, and it is a constant rather
46
+ // than a second prop because the value is already a percentage of the group:
47
+ // one is the smallest move that means anything, and a splitter announcing
48
+ // 47.382 would be reading out its arithmetic rather than its size.
15
49
  //
16
50
  // # The two separators in this package are not the same thing
17
51
  //
@@ -53,9 +87,9 @@
53
87
  //
54
88
  // Each pane carries its share as `--uf-resizable-size`, a percentage, and the
55
89
  // caller's stylesheet decides whether that is a width, a height, a `flex-basis`
56
- // or nothing at all. There is no drag: the pointer half is tracked work, and a
57
- // keyboard-only splitter is a working splitter, where a pointer-only one is
58
- // not.
90
+ // or nothing at all. The group is measured rather than drawn — the drag reads
91
+ // its box and never writes to it — so a caller whose panes are flex children,
92
+ // grid tracks or absolutely positioned gets the same splitter.
59
93
 
60
94
  "use client";
61
95
 
@@ -71,11 +105,21 @@ import {
71
105
  } from "@uniflowed/react";
72
106
 
73
107
  import type { Rest } from "./internal/merge-props.js";
74
- import { composeHandlers, withoutComposed } from "./internal/merge-props.js";
108
+ import { composeHandlers, composeRefs, withoutComposed } from "./internal/merge-props.js";
75
109
  import type { Orientation } from "./internal/roving-focus.js";
76
- import { clamp, isReversed } from "./internal/range.js";
110
+ import { clamp, isReversed, snap } from "./internal/range.js";
77
111
  import { useControlled } from "./internal/controlled-state.js";
78
112
 
113
+ /**
114
+ * How finely a drag may move the splitter, in percentage points.
115
+ *
116
+ * Not `step`, which is the keyboard's and defaults to ten; the module header
117
+ * says why the two cannot be the same number. One is a constant rather than a
118
+ * prop because the value is already a percentage of the group, so one is the
119
+ * smallest move that means anything.
120
+ */
121
+ const POINTER_STEP = 1;
122
+
79
123
  type ResizableState = {|
80
124
  readonly base: string,
81
125
  /** The primary pane's share of the group, as a percentage. */
@@ -89,6 +133,14 @@ type ResizableState = {|
89
133
  readonly disabled: boolean,
90
134
  readonly hasPrimary: boolean,
91
135
  readonly registerPrimary: (present: boolean) => void,
136
+ /**
137
+ * The element a drag is measured against.
138
+ *
139
+ * The group rather than the handle, because the value is the primary pane's
140
+ * share *of the group* — the handle is a few pixels wide and has no idea how
141
+ * much space there is to divide.
142
+ */
143
+ readonly groupRef: { current: HTMLElement | null },
92
144
  |};
93
145
 
94
146
  const ResizableContext: React.Context<ResizableState | null> = createContext(null);
@@ -128,6 +180,7 @@ export component ResizablePanelGroup(
128
180
  const base = useId();
129
181
  const [share, setShare] = useControlled(value, defaultValue, onValueChange);
130
182
  const [hasPrimary, setHasPrimary] = useState(false);
183
+ const groupRef = useRef<HTMLElement | null>(null);
131
184
 
132
185
  const state = useMemo(
133
186
  () => ({
@@ -141,13 +194,23 @@ export component ResizablePanelGroup(
141
194
  disabled,
142
195
  hasPrimary,
143
196
  registerPrimary: setHasPrimary,
197
+ groupRef,
144
198
  }),
145
199
  [base, share, setShare, min, max, step, orientation, disabled, hasPrimary],
146
200
  );
147
201
 
202
+ const passed = withoutComposed(rest, ["ref"]);
203
+
148
204
  return (
149
205
  <ResizableContext.Provider value={state}>
150
- <div {...rest}>{children}</div>
206
+ <div
207
+ {...passed}
208
+ ref={composeRefs(rest.ref, (element) => {
209
+ groupRef.current = element;
210
+ })}
211
+ >
212
+ {children}
213
+ </div>
151
214
  </ResizableContext.Provider>
152
215
  );
153
216
  }
@@ -199,16 +262,61 @@ export component ResizablePanel(children: React.Node, primary?: boolean = false,
199
262
  */
200
263
  export component ResizableHandle(label?: string = "Resize", ...rest: Rest) {
201
264
  const group = useResizable("Resizable.Handle");
202
- const passed = withoutComposed(rest, ["onKeyDown"]);
265
+ const passed = withoutComposed(rest, [
266
+ "onKeyDown",
267
+ "onPointerCancel",
268
+ "onPointerDown",
269
+ "onPointerMove",
270
+ "onPointerUp",
271
+ ]);
203
272
  // Where the pane was before `Enter` collapsed it. A ref because nothing
204
273
  // renders it: it is a fact about the last keystroke, not about the layout.
205
274
  const restoreTo = useRef<number | null>(null);
275
+ // Whether the pointer is down on this handle. Also a ref, and for the same
276
+ // reason: it changes between renders and no render depends on it.
277
+ const dragging = useRef(false);
206
278
 
207
279
  const moveBy = (amount: number) => {
208
280
  restoreTo.current = null;
209
281
  group.setValue(clamp(group.value + amount, group.min, group.max));
210
282
  };
211
283
 
284
+ /** The primary pane's share at the pointer, or null with no box to read. */
285
+ const shareAt = (event: $FlowFixMe): number | null => {
286
+ const element = group.groupRef.current;
287
+ if (element == null) {
288
+ return null;
289
+ }
290
+ const box = element.getBoundingClientRect();
291
+ const vertical = group.orientation === "vertical";
292
+ const size = vertical ? box.height : box.width;
293
+ if (size <= 0) {
294
+ // A group with no box has no percentages in it, and dividing by its
295
+ // width would put `Infinity` into `aria-valuenow`.
296
+ return null;
297
+ }
298
+ // From the top for stacked panes, where `Slider.Track` reads from the
299
+ // bottom: a slider's minimum is at the bottom of its track, and a group's
300
+ // primary pane is the one *before* the handle, which is the top one.
301
+ const along = vertical ? event.clientY - box.top : event.clientX - box.left;
302
+ const part = clamp(along / size, 0, 1);
303
+ // In a right-to-left page the pane before the handle is the one on the
304
+ // right, so the reading runs the other way. `isReversed` mirrors nothing on
305
+ // a stacked group, because writing direction does not flip the vertical
306
+ // axis.
307
+ const forward = isReversed(element, group.orientation) ? 1 - part : part;
308
+ // The value *is* the percentage, so the position becomes one directly
309
+ // rather than being mapped across `min`–`max` the way a slider's is: those
310
+ // two are bounds on how far the pane may be dragged, not the ends of a
311
+ // scale. Mapping them would put the handle somewhere the pointer is not.
312
+ return snap(forward * 100, group.min, group.max, POINTER_STEP);
313
+ };
314
+
315
+ const endDrag = (event: $FlowFixMe) => {
316
+ dragging.current = false;
317
+ event.currentTarget?.releasePointerCapture?.(event.pointerId);
318
+ };
319
+
212
320
  return (
213
321
  <div
214
322
  {...passed}
@@ -265,6 +373,38 @@ export component ResizableHandle(label?: string = "Resize", ...rest: Rest) {
265
373
  moveBy(amount);
266
374
  }
267
375
  })}
376
+ onPointerCancel={composeHandlers(rest.onPointerCancel, endDrag)}
377
+ onPointerDown={composeHandlers(rest.onPointerDown, (event: $FlowFixMe) => {
378
+ if (group.disabled) {
379
+ return;
380
+ }
381
+ // Otherwise the press selects the text in the panes either side on the
382
+ // way past, so a drag paints half the page blue.
383
+ event.preventDefault();
384
+ // Which also means the browser will not focus this element, and a
385
+ // reader who has just dragged the splitter is the reader most likely to
386
+ // want an arrow key next.
387
+ event.currentTarget?.focus?.();
388
+ event.currentTarget?.setPointerCapture?.(event.pointerId);
389
+ dragging.current = true;
390
+ // A drag is a move, so the pane `Enter` would put back is no longer
391
+ // where it was. Leaving it would make the next `Enter` restore a size
392
+ // from before the drag.
393
+ restoreTo.current = null;
394
+ // Deliberately no value change: taking hold of the handle is not asking
395
+ // it to move. The module header says what a press does on a track
396
+ // instead, and why the two are not the same gesture.
397
+ })}
398
+ onPointerMove={composeHandlers(rest.onPointerMove, (event: $FlowFixMe) => {
399
+ if (!dragging.current) {
400
+ return;
401
+ }
402
+ const share = shareAt(event);
403
+ if (share != null) {
404
+ group.setValue(share);
405
+ }
406
+ })}
407
+ onPointerUp={composeHandlers(rest.onPointerUp, endDrag)}
268
408
  role="separator"
269
409
  // A separator that is not in the tab sequence is the WCAG 2.1.1 failure
270
410
  // this module exists to avoid.
package/select.js CHANGED
@@ -146,6 +146,8 @@ import {
146
146
  } from "@uniflowed/react";
147
147
  import { useStableCallback } from "@uniflowed/hooks/lifecycle";
148
148
 
149
+ import type { Align, LogicalSide } from "./internal/anchor.js";
150
+ import { useAnchor } from "./internal/anchor.js";
149
151
  import type { Rest } from "./internal/merge-props.js";
150
152
  import { composeHandlers, composeRefs, withoutComposed } from "./internal/merge-props.js";
151
153
  import type { Movement } from "./internal/roving-focus.js";
@@ -153,6 +155,8 @@ import { isTypeaheadKey, itemsOf, moveTo, useTypeahead } from "./internal/roving
153
155
  import { useControlled } from "./internal/controlled-state.js";
154
156
  import { FormValue } from "./internal/form-value.js";
155
157
 
158
+ export type { Align, LogicalSide, Side } from "./internal/anchor.js";
159
+
156
160
  const OPTION_SELECTOR = '[role="option"]';
157
161
  const LISTBOX_SELECTOR = '[role="listbox"]';
158
162
 
@@ -613,6 +617,12 @@ export component SelectValue(children?: React.Node, placeholder?: React.Node, ..
613
617
  */
614
618
  export component SelectList(
615
619
  children: renders* (SelectOption | SelectGroup | SelectSeparator),
620
+ align?: Align = "start",
621
+ alignOffset?: number = 0,
622
+ avoidCollisions?: boolean = true,
623
+ collisionPadding?: number = 0,
624
+ side?: LogicalSide = "bottom",
625
+ sideOffset?: number = 0,
616
626
  ...rest: Rest
617
627
  ) {
618
628
  const select = useSelect("Select.List");
@@ -622,6 +632,23 @@ export component SelectList(
622
632
  select.setActiveId(null);
623
633
  });
624
634
 
635
+ // The popup a select opens is the one case where the trigger's *width* is
636
+ // part of the design rather than a detail: a list narrower than the button it
637
+ // came out of reads as a different control. `--uf-anchor-trigger-width` is
638
+ // written on this element for a stylesheet to use, which is why the
639
+ // measurement is here and not in the caller.
640
+ const anchored = useAnchor({
641
+ align,
642
+ alignOffset,
643
+ anchorRef: triggerRef,
644
+ avoidCollisions,
645
+ collisionPadding,
646
+ open: select.open,
647
+ overlayRef: listRef,
648
+ side,
649
+ sideOffset,
650
+ });
651
+
625
652
  // No dependency list, for the reason `combobox.js` gives: what this reads is
626
653
  // the *rendered* options, and a caller may render different ones on any
627
654
  // render — a change to `children` that no dependency list can describe. Every
@@ -701,6 +728,8 @@ export component SelectList(
701
728
  <div
702
729
  {...passed}
703
730
  aria-labelledby={select.labelled ? `${select.base}-label` : undefined}
731
+ data-align={anchored.align}
732
+ data-side={anchored.side}
704
733
  id={`${select.base}-list`}
705
734
  ref={composeRefs(rest.ref, (element) => {
706
735
  listRef.current = element;
package/tooltip.js CHANGED
@@ -73,7 +73,7 @@ import { createContext, useContext, useEffect, useId, useMemo, useRef } from "@u
73
73
  import { useEventListener } from "@uniflowed/hooks/dom";
74
74
  import { useStableCallback } from "@uniflowed/hooks/lifecycle";
75
75
 
76
- import type { Align, Side } from "./internal/anchor.js";
76
+ import type { Align, LogicalSide } from "./internal/anchor.js";
77
77
  import type { DelayGroup, HoverIntent } from "./internal/hover-intent.js";
78
78
  import type { Rest } from "./internal/merge-props.js";
79
79
  import { composeRefs, withProps, withoutComposed } from "./internal/merge-props.js";
@@ -89,7 +89,7 @@ import {
89
89
  import { useAnchor } from "./internal/anchor.js";
90
90
  import { useControlled } from "./internal/controlled-state.js";
91
91
 
92
- export type { Align, Side } from "./internal/anchor.js";
92
+ export type { Align, LogicalSide, Side } from "./internal/anchor.js";
93
93
 
94
94
  /** What a `Tooltip.Provider` shares with the tooltips inside it. */
95
95
  type TooltipScope = {|
@@ -337,7 +337,7 @@ export component TooltipBody(
337
337
  alignOffset?: number = 0,
338
338
  avoidCollisions?: boolean = true,
339
339
  collisionPadding?: number = 0,
340
- side?: Side = "top",
340
+ side?: LogicalSide = "top",
341
341
  sideOffset?: number = 0,
342
342
  ...rest: Rest
343
343
  ) {