@uniflowed/ui 0.0.0-alpha.8 → 0.0.0-alpha.9
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/dialog.js +1 -22
- package/hover-card.js +334 -0
- package/index.js +99 -5
- package/internal/anchor.js +500 -0
- package/internal/focus.js +64 -0
- package/internal/hover-intent.js +259 -0
- package/internal/merge-props.js +32 -0
- package/package.json +7 -4
- package/popover.js +326 -0
- package/tooltip.js +411 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
//
|
|
3
|
+
// Where an overlay goes, given where its trigger is.
|
|
4
|
+
//
|
|
5
|
+
// A popover, a tooltip and a hover card are the same three sentences: a box
|
|
6
|
+
// appears next to another box, it stays on the screen when there is no room
|
|
7
|
+
// where it was asked to go, and it says where it ended up so a stylesheet can
|
|
8
|
+
// point an arrow at the trigger. Written into each component, those three
|
|
9
|
+
// sentences are three copies that agree until one of them is fixed.
|
|
10
|
+
//
|
|
11
|
+
// # Flip *and* shift, because a flip alone is not enough
|
|
12
|
+
//
|
|
13
|
+
// There are two answers to "it does not fit", and a positioning layer needs
|
|
14
|
+
// both:
|
|
15
|
+
//
|
|
16
|
+
// * **Flip** — the main axis. A menu whose trigger is near the bottom of the
|
|
17
|
+
// viewport opens upwards instead of off the page, and reports
|
|
18
|
+
// `data-side="top"` so the arrow moves with it.
|
|
19
|
+
// * **Shift** — the cross axis. A menu wider than its trigger, aligned to a
|
|
20
|
+
// trigger near the right edge, has nowhere to flip *to*: both alignments
|
|
21
|
+
// overflow when the overlay is wider than the room on either side of the
|
|
22
|
+
// anchor. Sliding it back along the trigger is the only answer that keeps
|
|
23
|
+
// it on screen, and it is what Floating UI calls `shift`.
|
|
24
|
+
//
|
|
25
|
+
// Flipping the *alignment* — `start` becomes `end` when the aligned edge would
|
|
26
|
+
// leave the viewport — is the thing that looks like a shift and is not. It
|
|
27
|
+
// moves the overlay by its own width, which is a jump the reader sees, and it
|
|
28
|
+
// still overflows in exactly the case above. So the alignment never flips here:
|
|
29
|
+
// the side flips, the cross axis slides, and `data-align` keeps saying what the
|
|
30
|
+
// caller asked for.
|
|
31
|
+
//
|
|
32
|
+
// # One tier, measured, and why there is no declarative second one
|
|
33
|
+
//
|
|
34
|
+
// The platform grew this capability: CSS anchor positioning places an element
|
|
35
|
+
// against an `anchor-name` in the compositor, with `position-try-fallbacks` for
|
|
36
|
+
// the collision case and no JavaScript on the scroll path. It is the better
|
|
37
|
+
// mechanism and it is deliberately not used here, because it cannot express
|
|
38
|
+
// half of what this module promises:
|
|
39
|
+
//
|
|
40
|
+
// * `position-area` places a box in one of a grid of regions around the
|
|
41
|
+
// anchor. There is no spelling that says "and then move it back inside the
|
|
42
|
+
// viewport": the property chooses a region, it does not clamp a coordinate.
|
|
43
|
+
// * `position-try-fallbacks`, with the built-in `flip-block` / `flip-inline`
|
|
44
|
+
// or a hand-written `@position-try` block, tries another *placement* when
|
|
45
|
+
// the first overflows. Every one of those is a flip. None is a slide.
|
|
46
|
+
// * `position-area: span-all` with `justify-self: anchor-center` centres the
|
|
47
|
+
// overlay on the anchor and then needs a `margin-inline` to pull it back
|
|
48
|
+
// inside — and that margin is a number only a measurement produces, so the
|
|
49
|
+
// declarative path would be a measured path wearing a stylesheet.
|
|
50
|
+
//
|
|
51
|
+
// A tier that flips and a tier that flips *and* slides put the same overlay in
|
|
52
|
+
// two different places, and which one a reader gets depends on their browser.
|
|
53
|
+
// Shipping one tier that both engines run is worth more than shipping the
|
|
54
|
+
// faster one to some of them, so this module measures. It is the reason
|
|
55
|
+
// `packages/ui` has no `CSS.supports` in it: there is nothing to branch on.
|
|
56
|
+
//
|
|
57
|
+
// The measuring is kept cheap rather than clever. A placement is computed on
|
|
58
|
+
// open, on a scroll anywhere in the page, on a viewport resize and on a resize
|
|
59
|
+
// of either box — and each of those is one `getBoundingClientRect` per box
|
|
60
|
+
// followed by the arithmetic below, which reads nothing else.
|
|
61
|
+
//
|
|
62
|
+
// # Why the arithmetic is a function and not a hook
|
|
63
|
+
//
|
|
64
|
+
// `placeOverlay` takes three rectangles and returns a placement. It touches no
|
|
65
|
+
// element, no window and no React, so the whole of the collision behaviour —
|
|
66
|
+
// every flip, every slide, right-to-left alignment, the overlay that is wider
|
|
67
|
+
// than the viewport — is testable by calling it with numbers. That matters more
|
|
68
|
+
// here than in most modules: the DOM these components are tested in computes no
|
|
69
|
+
// layout at all, so a test that went through the hook could only assert *that*
|
|
70
|
+
// a position was applied, never that it was the right one.
|
|
71
|
+
//
|
|
72
|
+
// # Why an overlay is `position: fixed`
|
|
73
|
+
//
|
|
74
|
+
// Because the coordinates are the viewport's. It also gets most of what a
|
|
75
|
+
// portal is usually reached for: a fixed box is not clipped by an ancestor's
|
|
76
|
+
// `overflow: hidden`, so a menu in a table row or a scroll container is not cut
|
|
77
|
+
// in half — without moving the element away from its trigger in the
|
|
78
|
+
// accessibility tree, which is the trade `dialog.js`'s header refuses to make.
|
|
79
|
+
// The exception is an ancestor with `transform`, `filter` or `will-change`,
|
|
80
|
+
// which becomes the containing block for a fixed descendant; that is the CSS
|
|
81
|
+
// the top layer would answer, and answering it is ubugeeei-prod/uf#256's
|
|
82
|
+
// remaining half rather than this module's.
|
|
83
|
+
//
|
|
84
|
+
// # Why this is `internal/` and not a subpath
|
|
85
|
+
//
|
|
86
|
+
// The same reason `roving-focus.js` gives. This is not a positioning library,
|
|
87
|
+
// it is the relationship the overlay parts build: a `data-side` that agrees
|
|
88
|
+
// with the coordinates, one set of custom-property names for a stylesheet to
|
|
89
|
+
// read, and one definition of what `start` means in a right-to-left page.
|
|
90
|
+
// Exported, a consumer could build a part that positions itself differently and
|
|
91
|
+
// still calls itself an overlay.
|
|
92
|
+
|
|
93
|
+
import { useEffect, useState } from "@uniflowed/react";
|
|
94
|
+
import { useStableCallback } from "@uniflowed/hooks/lifecycle";
|
|
95
|
+
|
|
96
|
+
import type { Direction } from "./roving-focus.js";
|
|
97
|
+
import { directionOf } from "./roving-focus.js";
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Which side of its trigger an overlay opens onto.
|
|
101
|
+
*
|
|
102
|
+
* Physical rather than logical, which is the opposite of `Align` below and is
|
|
103
|
+
* deliberate: `side` answers "above or below", and the two horizontal ones are
|
|
104
|
+
* named after the screen because a design that puts a popover to the right of
|
|
105
|
+
* a toolbar means the right of the toolbar in any writing direction. What the
|
|
106
|
+
* writing direction changes is the *alignment*, which is where the reading
|
|
107
|
+
* order actually lives.
|
|
108
|
+
*/
|
|
109
|
+
export type Side = "top" | "right" | "bottom" | "left";
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Where the overlay sits along the trigger's other axis.
|
|
113
|
+
*
|
|
114
|
+
* Logical: `start` is the left edge in a left-to-right page and the right edge
|
|
115
|
+
* in a right-to-left one, so a menu aligned to the start of its trigger opens
|
|
116
|
+
* the way the reader reads in both.
|
|
117
|
+
*/
|
|
118
|
+
export type Align = "start" | "center" | "end";
|
|
119
|
+
|
|
120
|
+
/** A box, in viewport coordinates: what `getBoundingClientRect` reports. */
|
|
121
|
+
export type Rect = {|
|
|
122
|
+
readonly x: number,
|
|
123
|
+
readonly y: number,
|
|
124
|
+
readonly width: number,
|
|
125
|
+
readonly height: number,
|
|
126
|
+
|};
|
|
127
|
+
|
|
128
|
+
/** Everything `placeOverlay` needs, and nothing it could read for itself. */
|
|
129
|
+
export type Anchoring = {|
|
|
130
|
+
/** The trigger. */
|
|
131
|
+
readonly anchor: Rect,
|
|
132
|
+
/** The overlay. Only its size is used; where it is now does not matter. */
|
|
133
|
+
readonly overlay: Rect,
|
|
134
|
+
/** What it has to stay inside, which is the viewport for a fixed element. */
|
|
135
|
+
readonly viewport: Rect,
|
|
136
|
+
readonly side: Side,
|
|
137
|
+
readonly align: Align,
|
|
138
|
+
/** The gap between the trigger and the overlay, in pixels. */
|
|
139
|
+
readonly sideOffset: number,
|
|
140
|
+
/** A nudge along the cross axis, in the direction the page reads. */
|
|
141
|
+
readonly alignOffset: number,
|
|
142
|
+
/** Whether to flip and slide at all. `false` places it exactly as asked. */
|
|
143
|
+
readonly avoidCollisions: boolean,
|
|
144
|
+
/** How close to the viewport edge the overlay may come. */
|
|
145
|
+
readonly collisionPadding: number,
|
|
146
|
+
readonly direction: Direction,
|
|
147
|
+
|};
|
|
148
|
+
|
|
149
|
+
/** Where the overlay goes, and what the placement had to do to get there. */
|
|
150
|
+
export type Placement = {|
|
|
151
|
+
readonly x: number,
|
|
152
|
+
readonly y: number,
|
|
153
|
+
/** The side it ended up on, which is the requested one unless it flipped. */
|
|
154
|
+
readonly side: Side,
|
|
155
|
+
/** The requested alignment. It never changes; see the module header. */
|
|
156
|
+
readonly align: Align,
|
|
157
|
+
/** How far it slid along the cross axis, so an arrow can be moved back. */
|
|
158
|
+
readonly shift: number,
|
|
159
|
+
/** The room between the trigger and the viewport edge, and across it. */
|
|
160
|
+
readonly availableWidth: number,
|
|
161
|
+
readonly availableHeight: number,
|
|
162
|
+
|};
|
|
163
|
+
|
|
164
|
+
/** What `useAnchor` reports back for `data-side` and `data-align`. */
|
|
165
|
+
export type Anchored = {|
|
|
166
|
+
readonly side: Side,
|
|
167
|
+
readonly align: Align,
|
|
168
|
+
|};
|
|
169
|
+
|
|
170
|
+
/** What `useAnchor` is told, on top of the geometry `placeOverlay` needs. */
|
|
171
|
+
export type AnchorRequest = {|
|
|
172
|
+
readonly anchorRef: { current: HTMLElement | null },
|
|
173
|
+
readonly overlayRef: { current: HTMLElement | null },
|
|
174
|
+
/** Nothing is measured while it is closed: there is nothing to measure. */
|
|
175
|
+
readonly open: boolean,
|
|
176
|
+
readonly side: Side,
|
|
177
|
+
readonly align: Align,
|
|
178
|
+
readonly sideOffset: number,
|
|
179
|
+
readonly alignOffset: number,
|
|
180
|
+
readonly avoidCollisions: boolean,
|
|
181
|
+
readonly collisionPadding: number,
|
|
182
|
+
|};
|
|
183
|
+
|
|
184
|
+
/** The side a flip goes to. */
|
|
185
|
+
const OPPOSITE: { readonly [Side]: Side } = {
|
|
186
|
+
top: "bottom",
|
|
187
|
+
bottom: "top",
|
|
188
|
+
left: "right",
|
|
189
|
+
right: "left",
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/** Whether a side stacks the overlay above the trigger or beside it. */
|
|
193
|
+
function isVertical(side: Side): boolean {
|
|
194
|
+
return side === "top" || side === "bottom";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* The space between each edge of the anchor and the edge of the viewport,
|
|
199
|
+
* already minus the padding the overlay must not come inside.
|
|
200
|
+
*
|
|
201
|
+
* Negative for an anchor that is itself off the screen, which is a real state
|
|
202
|
+
* — a trigger scrolled halfway out of a container — and is why nothing below
|
|
203
|
+
* assumes these are positive.
|
|
204
|
+
*/
|
|
205
|
+
function roomAround(
|
|
206
|
+
anchor: Rect,
|
|
207
|
+
viewport: Rect,
|
|
208
|
+
collisionPadding: number,
|
|
209
|
+
): { readonly [Side]: number } {
|
|
210
|
+
return {
|
|
211
|
+
top: anchor.y - (viewport.y + collisionPadding),
|
|
212
|
+
bottom: viewport.y + viewport.height - collisionPadding - (anchor.y + anchor.height),
|
|
213
|
+
left: anchor.x - (viewport.x + collisionPadding),
|
|
214
|
+
right: viewport.x + viewport.width - collisionPadding - (anchor.x + anchor.width),
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The side the overlay actually fits on.
|
|
220
|
+
*
|
|
221
|
+
* The requested side wins whenever it fits, because a component that moved an
|
|
222
|
+
* overlay it did not have to move is one a designer cannot lay out against.
|
|
223
|
+
* When it does not fit and the opposite side does, it flips. When *neither*
|
|
224
|
+
* fits — a tall overlay next to a trigger in the middle of a short viewport —
|
|
225
|
+
* it takes the side with more room, so the reader sees as much of it as the
|
|
226
|
+
* page allows and `availableHeight` tells the stylesheet how much that was.
|
|
227
|
+
*/
|
|
228
|
+
function sideThatFits(anchoring: Anchoring, room: { readonly [Side]: number }): Side {
|
|
229
|
+
const { overlay, side, sideOffset } = anchoring;
|
|
230
|
+
const needed = (isVertical(side) ? overlay.height : overlay.width) + sideOffset;
|
|
231
|
+
const opposite = OPPOSITE[side];
|
|
232
|
+
if (room[side] >= needed) {
|
|
233
|
+
return side;
|
|
234
|
+
}
|
|
235
|
+
if (room[opposite] >= needed) {
|
|
236
|
+
return opposite;
|
|
237
|
+
}
|
|
238
|
+
return room[opposite] > room[side] ? opposite : side;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Where the overlay's leading edge goes along the cross axis, before sliding.
|
|
243
|
+
*
|
|
244
|
+
* `start` is the anchor's leading edge, `end` puts the overlay's trailing edge
|
|
245
|
+
* on the anchor's, and `center` splits the difference. Which edge is "leading"
|
|
246
|
+
* is the writing direction's business, and only on a horizontal cross axis: a
|
|
247
|
+
* page that reads right to left still reads top to bottom, so an overlay beside
|
|
248
|
+
* its trigger aligns to the top for `start` either way.
|
|
249
|
+
*/
|
|
250
|
+
function alignedAt(
|
|
251
|
+
align: Align,
|
|
252
|
+
mirrored: boolean,
|
|
253
|
+
anchorStart: number,
|
|
254
|
+
anchorSize: number,
|
|
255
|
+
overlaySize: number,
|
|
256
|
+
): number {
|
|
257
|
+
const resolved = mirrored ? mirror(align) : align;
|
|
258
|
+
return match (resolved) {
|
|
259
|
+
"start" => anchorStart,
|
|
260
|
+
"center" => anchorStart + anchorSize / 2 - overlaySize / 2,
|
|
261
|
+
"end" => anchorStart + anchorSize - overlaySize,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** `start` and `end` swapped, for a right-to-left page. */
|
|
266
|
+
function mirror(align: Align): Align {
|
|
267
|
+
return match (align) {
|
|
268
|
+
"start" => "end",
|
|
269
|
+
"center" => "center",
|
|
270
|
+
"end" => "start",
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Place `overlay` against `anchor` inside `viewport`.
|
|
276
|
+
*
|
|
277
|
+
* Pure arithmetic over three rectangles: no element, no window, no React. See
|
|
278
|
+
* the module header for why that is the point rather than a convenience.
|
|
279
|
+
*/
|
|
280
|
+
export function placeOverlay(anchoring: Anchoring): Placement {
|
|
281
|
+
const {
|
|
282
|
+
align,
|
|
283
|
+
alignOffset,
|
|
284
|
+
anchor,
|
|
285
|
+
avoidCollisions,
|
|
286
|
+
collisionPadding,
|
|
287
|
+
direction,
|
|
288
|
+
overlay,
|
|
289
|
+
sideOffset,
|
|
290
|
+
viewport,
|
|
291
|
+
} = anchoring;
|
|
292
|
+
|
|
293
|
+
const room = roomAround(anchor, viewport, collisionPadding);
|
|
294
|
+
const side = avoidCollisions ? sideThatFits(anchoring, room) : anchoring.side;
|
|
295
|
+
const vertical = isVertical(side);
|
|
296
|
+
|
|
297
|
+
// The main axis: hard against the trigger's edge, plus the gap. The overlay
|
|
298
|
+
// is never pushed along this axis, because pushing it would slide it over
|
|
299
|
+
// the trigger it is meant to be pointing at.
|
|
300
|
+
const main = match (side) {
|
|
301
|
+
"top" => anchor.y - overlay.height - sideOffset,
|
|
302
|
+
"bottom" => anchor.y + anchor.height + sideOffset,
|
|
303
|
+
"left" => anchor.x - overlay.width - sideOffset,
|
|
304
|
+
"right" => anchor.x + anchor.width + sideOffset,
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// The cross axis: aligned, nudged, then slid back inside if it has to be.
|
|
308
|
+
const anchorStart = vertical ? anchor.x : anchor.y;
|
|
309
|
+
const anchorSize = vertical ? anchor.width : anchor.height;
|
|
310
|
+
const overlaySize = vertical ? overlay.width : overlay.height;
|
|
311
|
+
const viewportStart = vertical ? viewport.x : viewport.y;
|
|
312
|
+
const viewportSize = vertical ? viewport.width : viewport.height;
|
|
313
|
+
// A positive `alignOffset` moves the overlay the way the page reads, so the
|
|
314
|
+
// same number nudges a menu in the same visual direction as its own text.
|
|
315
|
+
const reading = vertical && direction === "rtl" ? -1 : 1;
|
|
316
|
+
const wanted =
|
|
317
|
+
alignedAt(align, vertical && direction === "rtl", anchorStart, anchorSize, overlaySize) +
|
|
318
|
+
alignOffset * reading;
|
|
319
|
+
|
|
320
|
+
const lowest = viewportStart + collisionPadding;
|
|
321
|
+
const highest = viewportStart + viewportSize - collisionPadding - overlaySize;
|
|
322
|
+
// `Math.max` last, so an overlay *wider* than the viewport — where `highest`
|
|
323
|
+
// is below `lowest` and no position satisfies both — is pinned to the leading
|
|
324
|
+
// edge rather than pushed off the far one. `availableWidth` is what a
|
|
325
|
+
// stylesheet reads to stop it being that wide in the first place.
|
|
326
|
+
const cross = avoidCollisions ? Math.max(lowest, Math.min(wanted, highest)) : wanted;
|
|
327
|
+
|
|
328
|
+
const alongSide = Math.max(0, room[side] - sideOffset);
|
|
329
|
+
const acrossSide = Math.max(0, viewportSize - collisionPadding * 2);
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
align,
|
|
333
|
+
availableHeight: vertical ? alongSide : acrossSide,
|
|
334
|
+
availableWidth: vertical ? acrossSide : alongSide,
|
|
335
|
+
shift: cross - wanted,
|
|
336
|
+
side,
|
|
337
|
+
x: vertical ? cross : main,
|
|
338
|
+
y: vertical ? main : cross,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Write a placement onto the overlay.
|
|
344
|
+
*
|
|
345
|
+
* Imperatively, and only these seven properties. They change on every scroll
|
|
346
|
+
* frame while an overlay is open, and putting them in state would re-render the
|
|
347
|
+
* overlay and everything in it sixty times a second to move a box — which is
|
|
348
|
+
* exactly the cost `index.js`'s header says this package does not pay. React
|
|
349
|
+
* owns nothing written here: it never sets `left`, `top` or a custom property
|
|
350
|
+
* on these elements, and a caller who passes a `style` of their own keeps every
|
|
351
|
+
* property in it except the ones this component's position is made of.
|
|
352
|
+
*
|
|
353
|
+
* The two measurements are the ones a caller cannot compute for themselves. A
|
|
354
|
+
* `Select` popup that must match its trigger's width needs the trigger
|
|
355
|
+
* measured; a menu near the bottom of the page needs to know how much room is
|
|
356
|
+
* left before it can decide to scroll rather than overflow.
|
|
357
|
+
*/
|
|
358
|
+
function write(overlay: HTMLElement, anchor: Rect, placement: Placement): void {
|
|
359
|
+
const style = overlay.style;
|
|
360
|
+
style.position = "fixed";
|
|
361
|
+
style.left = `${placement.x}px`;
|
|
362
|
+
style.top = `${placement.y}px`;
|
|
363
|
+
style.setProperty("--uf-anchor-trigger-width", `${anchor.width}px`);
|
|
364
|
+
style.setProperty("--uf-anchor-trigger-height", `${anchor.height}px`);
|
|
365
|
+
style.setProperty("--uf-anchor-available-width", `${placement.availableWidth}px`);
|
|
366
|
+
style.setProperty("--uf-anchor-available-height", `${placement.availableHeight}px`);
|
|
367
|
+
// How far the slide moved it, so an arrow drawn by a stylesheet can be moved
|
|
368
|
+
// back the other way and keep pointing at the trigger.
|
|
369
|
+
style.setProperty("--uf-anchor-shift", `${placement.shift}px`);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** The rectangle of an element, as the arithmetic above wants it. */
|
|
373
|
+
function rectOf(element: HTMLElement): Rect {
|
|
374
|
+
const box = element.getBoundingClientRect();
|
|
375
|
+
return { height: box.height, width: box.width, x: box.left, y: box.top };
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Keep an overlay against its trigger for as long as it is open.
|
|
380
|
+
*
|
|
381
|
+
* Returns the side and alignment it settled on, which the part renders as
|
|
382
|
+
* `data-side` and `data-align`. Those are state — they change rarely, a
|
|
383
|
+
* stylesheet has to see them, and a flip is exactly the moment an arrow has to
|
|
384
|
+
* move — while the coordinates are written straight to the element; `write`
|
|
385
|
+
* says why.
|
|
386
|
+
*/
|
|
387
|
+
export hook useAnchor(request: AnchorRequest): Anchored {
|
|
388
|
+
const {
|
|
389
|
+
align,
|
|
390
|
+
alignOffset,
|
|
391
|
+
anchorRef,
|
|
392
|
+
avoidCollisions,
|
|
393
|
+
collisionPadding,
|
|
394
|
+
open,
|
|
395
|
+
overlayRef,
|
|
396
|
+
side,
|
|
397
|
+
sideOffset,
|
|
398
|
+
} = request;
|
|
399
|
+
const [settled, setSettled] = useState<Anchored>({ align, side });
|
|
400
|
+
|
|
401
|
+
const reflow = useStableCallback(() => {
|
|
402
|
+
const anchor = anchorRef.current;
|
|
403
|
+
const overlay = overlayRef.current;
|
|
404
|
+
const view = overlay?.ownerDocument?.defaultView;
|
|
405
|
+
if (anchor == null || overlay == null || view == null) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const box = rectOf(anchor);
|
|
409
|
+
const placement = placeOverlay({
|
|
410
|
+
align,
|
|
411
|
+
alignOffset,
|
|
412
|
+
anchor: box,
|
|
413
|
+
avoidCollisions,
|
|
414
|
+
collisionPadding,
|
|
415
|
+
direction: directionOf(anchor),
|
|
416
|
+
overlay: rectOf(overlay),
|
|
417
|
+
side,
|
|
418
|
+
sideOffset,
|
|
419
|
+
// The viewport of a fixed element, which is the whole of it: a fixed box
|
|
420
|
+
// is positioned against the viewport rather than against whatever is
|
|
421
|
+
// scrolled around it.
|
|
422
|
+
viewport: { height: view.innerHeight, width: view.innerWidth, x: 0, y: 0 },
|
|
423
|
+
});
|
|
424
|
+
write(overlay, box, placement);
|
|
425
|
+
setSettled((current) =>
|
|
426
|
+
current.side === placement.side && current.align === placement.align
|
|
427
|
+
? // The same object, so a scroll that changes nothing renders nothing.
|
|
428
|
+
current
|
|
429
|
+
: { align: placement.align, side: placement.side },
|
|
430
|
+
);
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
useEffect(() => {
|
|
434
|
+
const anchor = anchorRef.current;
|
|
435
|
+
const overlay = overlayRef.current;
|
|
436
|
+
const view = overlay?.ownerDocument?.defaultView;
|
|
437
|
+
if (!open || anchor == null || overlay == null || view == null) {
|
|
438
|
+
// Forget the measurement when the overlay closes. It is only read while
|
|
439
|
+
// `open`, but a body that stays mounted across a close — `PopoverBody`
|
|
440
|
+
// does — reopens in a commit where `open` is already `true`, and would
|
|
441
|
+
// report the *previous* opening's side until `reflow` corrects it from
|
|
442
|
+
// an effect, which runs after paint. That is one frame of an arrow
|
|
443
|
+
// drawn from `data-side` pointing the wrong way, after a reopen that
|
|
444
|
+
// follows a flip.
|
|
445
|
+
if (!open) {
|
|
446
|
+
setSettled((current) =>
|
|
447
|
+
current.side === side && current.align === align ? current : { align, side },
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
reflow();
|
|
453
|
+
|
|
454
|
+
const document = overlay.ownerDocument;
|
|
455
|
+
const moved = () => reflow();
|
|
456
|
+
// Capture, because a scroll event does not bubble: a trigger inside a
|
|
457
|
+
// scrolling panel would otherwise move under an overlay that never heard
|
|
458
|
+
// about it. Passive, because this never prevents the scroll it is watching.
|
|
459
|
+
document.addEventListener("scroll", moved, { capture: true, passive: true });
|
|
460
|
+
view.addEventListener("resize", moved);
|
|
461
|
+
// A trigger that grows — a button whose label changed, a field that gained
|
|
462
|
+
// a second line — moves the overlay without any scroll or resize event
|
|
463
|
+
// being fired at all.
|
|
464
|
+
//
|
|
465
|
+
// Read off the window rather than through a local: a capitalised name
|
|
466
|
+
// holding a constructor is read as a React component by `uf lint`, and it
|
|
467
|
+
// is right to — the rule cannot tell this one from a component, and the
|
|
468
|
+
// window's own property is the thing being asked about anyway.
|
|
469
|
+
const host: $FlowFixMe = view;
|
|
470
|
+
const sizes = typeof host.ResizeObserver === "function" ? new host.ResizeObserver(moved) : null;
|
|
471
|
+
sizes?.observe(anchor);
|
|
472
|
+
sizes?.observe(overlay);
|
|
473
|
+
|
|
474
|
+
return () => {
|
|
475
|
+
document.removeEventListener("scroll", moved, true);
|
|
476
|
+
view.removeEventListener("resize", moved);
|
|
477
|
+
sizes?.disconnect();
|
|
478
|
+
};
|
|
479
|
+
// The measurements are read through `reflow`, which is stable and always
|
|
480
|
+
// has the latest of them; they are named here so that changing one — a
|
|
481
|
+
// caller flipping `side` on a breakpoint — measures again.
|
|
482
|
+
}, [
|
|
483
|
+
align,
|
|
484
|
+
alignOffset,
|
|
485
|
+
anchorRef,
|
|
486
|
+
avoidCollisions,
|
|
487
|
+
collisionPadding,
|
|
488
|
+
open,
|
|
489
|
+
overlayRef,
|
|
490
|
+
reflow,
|
|
491
|
+
side,
|
|
492
|
+
sideOffset,
|
|
493
|
+
]);
|
|
494
|
+
|
|
495
|
+
// What was asked for until there is something measured to report, so the
|
|
496
|
+
// first commit — and the server's markup, which measures nothing at all —
|
|
497
|
+
// says the requested side rather than the last one some other opening
|
|
498
|
+
// happened to settle on.
|
|
499
|
+
return open ? settled : { align, side };
|
|
500
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
//
|
|
3
|
+
// What a reader can reach, in the order they reach it.
|
|
4
|
+
//
|
|
5
|
+
// One list, asked for by two components that want opposite things from it.
|
|
6
|
+
// `Dialog.Body` uses it to keep focus *in*: the first and last entries are
|
|
7
|
+
// where `Tab` and `Shift+Tab` wrap. `Popover.Body` uses it to put focus in
|
|
8
|
+
// once and then leaves it alone, because tabbing out of a popover is how a
|
|
9
|
+
// reader leaves one. The list has to be the same list for those two to be
|
|
10
|
+
// describable as different policies over the same fact rather than as two
|
|
11
|
+
// components that disagree about what focusable means.
|
|
12
|
+
//
|
|
13
|
+
// # The selector is the browser's rule, written down
|
|
14
|
+
//
|
|
15
|
+
// A disabled control is out because the browser will not focus one, and
|
|
16
|
+
// `tabindex="-1"` is out because it means "focusable by script, not by Tab" —
|
|
17
|
+
// which is what every roving tab stop in this package uses, so a menu inside a
|
|
18
|
+
// dialog would otherwise report thirty items as focus stops and the trap would
|
|
19
|
+
// wrap between two of them instead of at the dialog's edges.
|
|
20
|
+
//
|
|
21
|
+
// The three ancestor checks are the ones a selector cannot make. `hidden`,
|
|
22
|
+
// `inert` and `aria-hidden="true"` each hide a whole subtree, and reading them
|
|
23
|
+
// off the element alone returned a button inside `<div aria-hidden="true">` as
|
|
24
|
+
// a focus stop — after which the trap moved focus to a control no screen
|
|
25
|
+
// reader exposes and the reader was somewhere they could not be told about.
|
|
26
|
+
//
|
|
27
|
+
// # Why this is `internal/` and not a subpath
|
|
28
|
+
//
|
|
29
|
+
// The same reason `merge-props.js` gives. A public `focusable()` is a
|
|
30
|
+
// general-purpose DOM utility, and a second, weaker copy of one is how two
|
|
31
|
+
// parts of a package come to disagree about which elements exist. What is
|
|
32
|
+
// shipped here is narrower: the definition this package's focus behaviour is
|
|
33
|
+
// written against.
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The elements a browser will move focus to with `Tab`.
|
|
37
|
+
*
|
|
38
|
+
* Exported as the selector as well as through `focusable`, because one
|
|
39
|
+
* question is asked about a single element rather than about a subtree: a
|
|
40
|
+
* tooltip's trigger has to *be* one of these or the tooltip is one only a mouse
|
|
41
|
+
* can reach, and `element.matches(FOCUS_STOPS)` is that question.
|
|
42
|
+
*/
|
|
43
|
+
export const FOCUS_STOPS: string =
|
|
44
|
+
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The focus stops inside an element, in document order.
|
|
48
|
+
*
|
|
49
|
+
* Document order rather than mount order, for the reason
|
|
50
|
+
* `internal/roving-focus.js` gives about items: the two stop agreeing the first
|
|
51
|
+
* time something is rendered conditionally, and the reader's `Tab` follows the
|
|
52
|
+
* document.
|
|
53
|
+
*/
|
|
54
|
+
export function focusable(root: HTMLElement): Array<HTMLElement> {
|
|
55
|
+
return Array.from(root.querySelectorAll(FOCUS_STOPS)).filter(
|
|
56
|
+
(element: $FlowFixMe) =>
|
|
57
|
+
// All three hide a whole subtree, so all three are asked of the
|
|
58
|
+
// ancestors; see the module header for what reading them off the element
|
|
59
|
+
// alone let through.
|
|
60
|
+
element.closest("[hidden]") == null &&
|
|
61
|
+
element.closest("[inert]") == null &&
|
|
62
|
+
element.closest('[aria-hidden="true"]') == null,
|
|
63
|
+
);
|
|
64
|
+
}
|