@react-x11/components 0.3.0 → 0.5.0
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/README.md +131 -4
- package/dist/charts/components.d.ts +3 -1
- package/dist/charts/components.d.ts.map +1 -1
- package/dist/charts/components.js +8 -4
- package/dist/charts/components.js.map +1 -1
- package/dist/charts/node.d.ts +13 -6
- package/dist/charts/node.d.ts.map +1 -1
- package/dist/charts/node.js +75 -24
- package/dist/charts/node.js.map +1 -1
- package/dist/charts/render.d.ts +4 -0
- package/dist/charts/render.d.ts.map +1 -1
- package/dist/charts/render.js +11 -6
- package/dist/charts/render.js.map +1 -1
- package/dist/code-editor/node.d.ts +47 -16
- package/dist/code-editor/node.d.ts.map +1 -1
- package/dist/code-editor/node.js +121 -51
- package/dist/code-editor/node.js.map +1 -1
- package/dist/flow/draw.d.ts +19 -1
- package/dist/flow/draw.d.ts.map +1 -1
- package/dist/flow/draw.js +64 -35
- package/dist/flow/draw.js.map +1 -1
- package/dist/flow/index.d.ts.map +1 -1
- package/dist/flow/index.js +24 -8
- package/dist/flow/index.js.map +1 -1
- package/dist/flow/model.d.ts +4 -2
- package/dist/flow/model.d.ts.map +1 -1
- package/dist/flow/model.js +4 -2
- package/dist/flow/model.js.map +1 -1
- package/dist/flow/node.d.ts +40 -9
- package/dist/flow/node.d.ts.map +1 -1
- package/dist/flow/node.js +142 -53
- package/dist/flow/node.js.map +1 -1
- package/dist/flow/types.d.ts +46 -21
- package/dist/flow/types.d.ts.map +1 -1
- package/dist/formula/index.d.ts +4 -2
- package/dist/formula/index.d.ts.map +1 -1
- package/dist/formula/index.js.map +1 -1
- package/dist/formula/node.d.ts +5 -1
- package/dist/formula/node.d.ts.map +1 -1
- package/dist/formula/node.js +19 -3
- package/dist/formula/node.js.map +1 -1
- package/dist/html/controls.d.ts +4 -1
- package/dist/html/controls.d.ts.map +1 -1
- package/dist/html/controls.js.map +1 -1
- package/dist/html/css/cascade.d.ts +11 -5
- package/dist/html/css/cascade.d.ts.map +1 -1
- package/dist/html/css/cascade.js +18 -8
- package/dist/html/css/cascade.js.map +1 -1
- package/dist/html/css/parse.d.ts +15 -10
- package/dist/html/css/parse.d.ts.map +1 -1
- package/dist/html/css/parse.js +41 -16
- package/dist/html/css/parse.js.map +1 -1
- package/dist/html/css/style.d.ts +3 -0
- package/dist/html/css/style.d.ts.map +1 -1
- package/dist/html/css/style.js +3 -1
- package/dist/html/css/style.js.map +1 -1
- package/dist/html/css/values.d.ts +14 -1
- package/dist/html/css/values.d.ts.map +1 -1
- package/dist/html/css/values.js +11 -8
- package/dist/html/css/values.js.map +1 -1
- package/dist/html/index.d.ts.map +1 -1
- package/dist/html/index.js +1 -0
- package/dist/html/index.js.map +1 -1
- package/dist/html/layout/boxes.d.ts +12 -5
- package/dist/html/layout/boxes.d.ts.map +1 -1
- package/dist/html/layout/boxes.js +7 -4
- package/dist/html/layout/boxes.js.map +1 -1
- package/dist/html/layout/inline.d.ts.map +1 -1
- package/dist/html/layout/inline.js +22 -1
- package/dist/html/layout/inline.js.map +1 -1
- package/dist/html/node.d.ts +22 -8
- package/dist/html/node.d.ts.map +1 -1
- package/dist/html/node.js +97 -15
- package/dist/html/node.js.map +1 -1
- package/dist/html/paint.d.ts +3 -0
- package/dist/html/paint.d.ts.map +1 -1
- package/dist/html/paint.js +2 -2
- package/dist/html/paint.js.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -1
- package/dist/maps/clip.d.ts +43 -0
- package/dist/maps/clip.d.ts.map +1 -0
- package/dist/maps/clip.js +171 -0
- package/dist/maps/clip.js.map +1 -0
- package/dist/maps/gzip.d.ts +15 -0
- package/dist/maps/gzip.d.ts.map +1 -0
- package/dist/maps/gzip.js +56 -0
- package/dist/maps/gzip.js.map +1 -0
- package/dist/maps/index.d.ts +55 -0
- package/dist/maps/index.d.ts.map +1 -0
- package/dist/maps/index.js +141 -0
- package/dist/maps/index.js.map +1 -0
- package/dist/maps/labels.d.ts +123 -0
- package/dist/maps/labels.d.ts.map +1 -0
- package/dist/maps/labels.js +323 -0
- package/dist/maps/labels.js.map +1 -0
- package/dist/maps/mvt.d.ts +244 -0
- package/dist/maps/mvt.d.ts.map +1 -0
- package/dist/maps/mvt.js +820 -0
- package/dist/maps/mvt.js.map +1 -0
- package/dist/maps/node.d.ts +278 -0
- package/dist/maps/node.d.ts.map +1 -0
- package/dist/maps/node.js +1459 -0
- package/dist/maps/node.js.map +1 -0
- package/dist/maps/overlay.d.ts +157 -0
- package/dist/maps/overlay.d.ts.map +1 -0
- package/dist/maps/overlay.js +472 -0
- package/dist/maps/overlay.js.map +1 -0
- package/dist/maps/paint.d.ts +226 -0
- package/dist/maps/paint.d.ts.map +1 -0
- package/dist/maps/paint.js +705 -0
- package/dist/maps/paint.js.map +1 -0
- package/dist/maps/proj.d.ts +281 -0
- package/dist/maps/proj.d.ts.map +1 -0
- package/dist/maps/proj.js +451 -0
- package/dist/maps/proj.js.map +1 -0
- package/dist/maps/sources.d.ts +256 -0
- package/dist/maps/sources.d.ts.map +1 -0
- package/dist/maps/sources.js +210 -0
- package/dist/maps/sources.js.map +1 -0
- package/dist/maps/style.d.ts +141 -0
- package/dist/maps/style.d.ts.map +1 -0
- package/dist/maps/style.js +147 -0
- package/dist/maps/style.js.map +1 -0
- package/dist/maps/styles.d.ts +82 -0
- package/dist/maps/styles.d.ts.map +1 -0
- package/dist/maps/styles.js +898 -0
- package/dist/maps/styles.js.map +1 -0
- package/dist/maps/tiles.d.ts +283 -0
- package/dist/maps/tiles.d.ts.map +1 -0
- package/dist/maps/tiles.js +469 -0
- package/dist/maps/tiles.js.map +1 -0
- package/dist/maps/types.d.ts +244 -0
- package/dist/maps/types.d.ts.map +1 -0
- package/dist/maps/types.js +2 -0
- package/dist/maps/types.js.map +1 -0
- package/dist/qml/containers.d.ts +3 -0
- package/dist/qml/containers.d.ts.map +1 -0
- package/dist/qml/containers.js +331 -0
- package/dist/qml/containers.js.map +1 -0
- package/dist/qml/controls.d.ts +12 -0
- package/dist/qml/controls.d.ts.map +1 -0
- package/dist/qml/controls.js +21 -0
- package/dist/qml/controls.js.map +1 -0
- package/dist/qml/globals.d.ts +6 -0
- package/dist/qml/globals.d.ts.map +1 -0
- package/dist/qml/globals.js +25 -0
- package/dist/qml/globals.js.map +1 -0
- package/dist/qml/index.d.ts +15 -0
- package/dist/qml/index.d.ts.map +1 -0
- package/dist/qml/index.js +25 -0
- package/dist/qml/index.js.map +1 -0
- package/dist/qml/interaction.d.ts +12 -0
- package/dist/qml/interaction.d.ts.map +1 -0
- package/dist/qml/interaction.js +246 -0
- package/dist/qml/interaction.js.map +1 -0
- package/dist/qml/ir.d.ts +106 -0
- package/dist/qml/ir.d.ts.map +1 -0
- package/dist/qml/ir.js +7 -0
- package/dist/qml/ir.js.map +1 -0
- package/dist/qml/layouts.d.ts +3 -0
- package/dist/qml/layouts.d.ts.map +1 -0
- package/dist/qml/layouts.js +122 -0
- package/dist/qml/layouts.js.map +1 -0
- package/dist/qml/models.d.ts +14 -0
- package/dist/qml/models.d.ts.map +1 -0
- package/dist/qml/models.js +80 -0
- package/dist/qml/models.js.map +1 -0
- package/dist/qml/objects.d.ts +228 -0
- package/dist/qml/objects.d.ts.map +1 -0
- package/dist/qml/objects.js +1103 -0
- package/dist/qml/objects.js.map +1 -0
- package/dist/qml/parse.d.ts +10 -0
- package/dist/qml/parse.d.ts.map +1 -0
- package/dist/qml/parse.js +607 -0
- package/dist/qml/parse.js.map +1 -0
- package/dist/qml/qtquick.d.ts +7 -0
- package/dist/qml/qtquick.d.ts.map +1 -0
- package/dist/qml/qtquick.js +347 -0
- package/dist/qml/qtquick.js.map +1 -0
- package/dist/qml/react.d.ts +83 -0
- package/dist/qml/react.d.ts.map +1 -0
- package/dist/qml/react.js +323 -0
- package/dist/qml/react.js.map +1 -0
- package/dist/qml/resolver.d.ts +38 -0
- package/dist/qml/resolver.d.ts.map +1 -0
- package/dist/qml/resolver.js +64 -0
- package/dist/qml/resolver.js.map +1 -0
- package/dist/qml/slots.d.ts +65 -0
- package/dist/qml/slots.d.ts.map +1 -0
- package/dist/qml/slots.js +230 -0
- package/dist/qml/slots.js.map +1 -0
- package/dist/qml/states.d.ts +5 -0
- package/dist/qml/states.d.ts.map +1 -0
- package/dist/qml/states.js +276 -0
- package/dist/qml/states.js.map +1 -0
- package/dist/qml/view-utils.d.ts +42 -0
- package/dist/qml/view-utils.d.ts.map +1 -0
- package/dist/qml/view-utils.js +121 -0
- package/dist/qml/view-utils.js.map +1 -0
- package/dist/reorder/hx.d.ts +18 -0
- package/dist/reorder/hx.d.ts.map +1 -0
- package/dist/reorder/hx.js +10 -0
- package/dist/reorder/hx.js.map +1 -0
- package/dist/reorder/index.d.ts +314 -0
- package/dist/reorder/index.d.ts.map +1 -0
- package/dist/reorder/index.js +1539 -0
- package/dist/reorder/index.js.map +1 -0
- package/dist/reorder/model.d.ts +107 -0
- package/dist/reorder/model.d.ts.map +1 -0
- package/dist/reorder/model.js +162 -0
- package/dist/reorder/model.js.map +1 -0
- package/dist/richtext/node.d.ts +15 -5
- package/dist/richtext/node.d.ts.map +1 -1
- package/dist/richtext/node.js +40 -11
- package/dist/richtext/node.js.map +1 -1
- package/dist/richtext/runs.d.ts +35 -13
- package/dist/richtext/runs.d.ts.map +1 -1
- package/dist/richtext/runs.js +83 -27
- package/dist/richtext/runs.js.map +1 -1
- package/dist/tabs/hx.d.ts +18 -0
- package/dist/tabs/hx.d.ts.map +1 -0
- package/dist/tabs/hx.js +10 -0
- package/dist/tabs/hx.js.map +1 -0
- package/dist/tabs/index.d.ts +160 -0
- package/dist/tabs/index.d.ts.map +1 -0
- package/dist/tabs/index.js +1265 -0
- package/dist/tabs/index.js.map +1 -0
- package/dist/terminal/vt/fonts.d.ts +45 -15
- package/dist/terminal/vt/fonts.d.ts.map +1 -1
- package/dist/terminal/vt/fonts.js +89 -19
- package/dist/terminal/vt/fonts.js.map +1 -1
- package/dist/terminal/vt/index.d.ts +1 -1
- package/dist/terminal/vt/index.d.ts.map +1 -1
- package/dist/terminal/vt/index.js +3 -3
- package/dist/terminal/vt/index.js.map +1 -1
- package/dist/terminal/vt/node.d.ts +42 -0
- package/dist/terminal/vt/node.d.ts.map +1 -1
- package/dist/terminal/vt/node.js +95 -11
- package/dist/terminal/vt/node.js.map +1 -1
- package/dist/terminal/vt/pty.d.ts +16 -0
- package/dist/terminal/vt/pty.d.ts.map +1 -1
- package/dist/terminal/vt/pty.js +186 -0
- package/dist/terminal/vt/pty.js.map +1 -1
- package/dist/terminal/vt/renderer.d.ts.map +1 -1
- package/dist/terminal/vt/renderer.js +13 -3
- package/dist/terminal/vt/renderer.js.map +1 -1
- package/package.json +23 -3
- package/src/charts/components.ts +14 -7
- package/src/charts/node.ts +81 -26
- package/src/charts/render.ts +16 -6
- package/src/code-editor/node.ts +147 -53
- package/src/flow/draw.ts +104 -35
- package/src/flow/index.ts +33 -13
- package/src/flow/model.ts +4 -2
- package/src/flow/node.ts +158 -59
- package/src/flow/types.ts +46 -21
- package/src/formula/index.ts +4 -2
- package/src/formula/node.ts +24 -5
- package/src/html/controls.ts +4 -1
- package/src/html/css/cascade.ts +18 -7
- package/src/html/css/parse.ts +46 -21
- package/src/html/css/style.ts +6 -1
- package/src/html/css/values.ts +25 -9
- package/src/html/index.ts +1 -0
- package/src/html/layout/boxes.ts +19 -9
- package/src/html/layout/inline.ts +23 -1
- package/src/html/node.ts +110 -17
- package/src/html/paint.ts +17 -2
- package/src/index.ts +142 -0
- package/src/maps/clip.ts +184 -0
- package/src/maps/gzip.ts +66 -0
- package/src/maps/index.ts +316 -0
- package/src/maps/labels.ts +442 -0
- package/src/maps/mvt.ts +857 -0
- package/src/maps/node.ts +1819 -0
- package/src/maps/overlay.ts +639 -0
- package/src/maps/paint.ts +978 -0
- package/src/maps/proj.ts +641 -0
- package/src/maps/sources.ts +425 -0
- package/src/maps/style.ts +309 -0
- package/src/maps/styles.ts +1040 -0
- package/src/maps/tiles.ts +676 -0
- package/src/maps/types.ts +260 -0
- package/src/qml/containers.tsx +380 -0
- package/src/qml/controls.tsx +47 -0
- package/src/qml/globals.ts +33 -0
- package/src/qml/index.ts +67 -0
- package/src/qml/interaction.tsx +317 -0
- package/src/qml/ir.ts +80 -0
- package/src/qml/layouts.tsx +141 -0
- package/src/qml/models.ts +101 -0
- package/src/qml/objects.ts +1431 -0
- package/src/qml/parse.ts +624 -0
- package/src/qml/qtquick.tsx +413 -0
- package/src/qml/react.tsx +454 -0
- package/src/qml/resolver.ts +99 -0
- package/src/qml/slots.ts +249 -0
- package/src/qml/states.ts +318 -0
- package/src/qml/view-utils.ts +140 -0
- package/src/reorder/hx.ts +31 -0
- package/src/reorder/index.ts +2223 -0
- package/src/reorder/model.ts +232 -0
- package/src/richtext/node.ts +49 -14
- package/src/richtext/runs.ts +115 -30
- package/src/tabs/hx.ts +31 -0
- package/src/tabs/index.ts +1686 -0
- package/src/terminal/vt/fonts.ts +128 -36
- package/src/terminal/vt/index.ts +4 -2
- package/src/terminal/vt/node.ts +105 -12
- package/src/terminal/vt/pty.ts +243 -0
- package/src/terminal/vt/renderer.ts +13 -3
|
@@ -0,0 +1,1265 @@
|
|
|
1
|
+
// <Tabs> — one visible panel at a time, switched by a strip of triggers.
|
|
2
|
+
//
|
|
3
|
+
// The API is [Chakra UI's Tabs](https://chakra-ui.com/docs/components/tabs)
|
|
4
|
+
// with its parts spelled flat — `Tabs.Root` is `<Tabs>`, `Tabs.Trigger` is
|
|
5
|
+
// `<TabsTrigger>` — because that is how this package names a composition (see
|
|
6
|
+
// `/timeline` and `/charts`). The vocabulary is Chakra's too: `value` /
|
|
7
|
+
// `defaultValue` / `onValueChange({ value })`, the five variants (`line`,
|
|
8
|
+
// `subtle`, `enclosed`, `outline`, `plain`), `size`, `orientation`,
|
|
9
|
+
// `activationMode`, `fitted`, `justify`, `lazyMount` and `unmountOnExit` — so
|
|
10
|
+
// a snippet copied from Chakra's docs is the same tree with the dots removed.
|
|
11
|
+
// `asChild` is the one part of the surface deliberately absent: it exists to
|
|
12
|
+
// merge props into somebody else's DOM element, and there is no DOM here.
|
|
13
|
+
//
|
|
14
|
+
// `overflow` is the one prop with no counterpart there. A horizontal strip
|
|
15
|
+
// narrower than its tabs keeps the ones that fit and drops the rest into a
|
|
16
|
+
// menu at its end rather than running off its own edge — on by default,
|
|
17
|
+
// because a strip cut off mid-label is nobody's intention. It is the one
|
|
18
|
+
// thing here that has to measure, and `useOverflow` below is where that and
|
|
19
|
+
// its consequences live.
|
|
20
|
+
//
|
|
21
|
+
// The behaviour is core's `<Tabs>` — the widget this one supersedes: a single
|
|
22
|
+
// tab stop with roving focus, arrows that wrap and skip disabled triggers,
|
|
23
|
+
// Home/End, visual arrows in an RTL strip, and `activationMode="manual"` for
|
|
24
|
+
// a panel too expensive to build on every keystroke.
|
|
25
|
+
//
|
|
26
|
+
// It is pure composition of `<box>` and `<text>`, so there is no
|
|
27
|
+
// `registerElement` here and nothing for the reconciler to learn. The line
|
|
28
|
+
// under a `line` strip, the marker on the selected trigger, the chip behind
|
|
29
|
+
// an `enclosed` strip — each is a `<box>`, positioned absolutely where it has
|
|
30
|
+
// to sit on one edge, because borders alone cannot say "this side only" and a
|
|
31
|
+
// box can.
|
|
32
|
+
import React from 'react';
|
|
33
|
+
import { Icon, useDirection, useTheme } from 'react-x11';
|
|
34
|
+
import { interpolate } from 'react-x11/style';
|
|
35
|
+
import { XK_DOWN, XK_END, XK_ESCAPE, XK_HOME, XK_LEFT, XK_RETURN, XK_RIGHT, XK_UP, } from 'react-x11/keysyms';
|
|
36
|
+
import { afterLayout, cancelAfterLayout } from '../internal/timers.js';
|
|
37
|
+
import { useDismissOnWindowBlur } from '../internal/widget.js';
|
|
38
|
+
import { hx } from './hx.js';
|
|
39
|
+
const h = React.createElement;
|
|
40
|
+
// --- geometry ---------------------------------------------------------------
|
|
41
|
+
/** The selected trigger's marker in the `line` variant. */
|
|
42
|
+
const INDICATOR = 2;
|
|
43
|
+
/** The strip's own line in the `line` and `outline` variants. */
|
|
44
|
+
const RULE = 1;
|
|
45
|
+
/** Inside a trigger, between a glyph and the label beside it. */
|
|
46
|
+
const TRIGGER_GAP = 6;
|
|
47
|
+
/** Between a strip and its panel. */
|
|
48
|
+
const PANEL_GAP = 8;
|
|
49
|
+
/** The `enclosed` strip's chip, and an `outline` tab's shoulders — one
|
|
50
|
+
* radius, so the two variants agree about how round this notebook is. */
|
|
51
|
+
const CORNER = 6;
|
|
52
|
+
/** The rounding on everything that is a wash rather than a tab: the `subtle`
|
|
53
|
+
* fill, the `line` strip's hover, an `enclosed` trigger, a menu row. */
|
|
54
|
+
const WASH_CORNER = 4;
|
|
55
|
+
/** The gap the overflow menu leaves between two rows. Read twice: once to
|
|
56
|
+
* lay the sheet out, and once to work out how tall it comes to. */
|
|
57
|
+
const MENU_ROW_GAP = 1;
|
|
58
|
+
/** The overflow menu's sheet: a hairline where it meets the desktop, as the
|
|
59
|
+
* other popups out here use, and the inset that makes a row read as a pill
|
|
60
|
+
* on the sheet rather than a band across it. */
|
|
61
|
+
const SHEET_BORDER = 1;
|
|
62
|
+
const SHEET_PAD = 4;
|
|
63
|
+
/** A menu row is narrower than a tab is tall — it is a list entry, not a
|
|
64
|
+
* target on a strip — so it takes its own padding rather than the size's. */
|
|
65
|
+
const MENU_ROW_PX = 10;
|
|
66
|
+
const MENU_ROW_PY = 7;
|
|
67
|
+
/** So a menu of one short label is still a menu rather than a chip. */
|
|
68
|
+
const MENU_MIN_WIDTH = 140;
|
|
69
|
+
/**
|
|
70
|
+
* Between two triggers on the strip — none in `outline`, where the tabs meet
|
|
71
|
+
* shoulder to shoulder. Read twice: once to lay the strip out, and once by
|
|
72
|
+
* the overflow arithmetic, which has to add up to the same number the strip
|
|
73
|
+
* did or it will fit one tab too many.
|
|
74
|
+
*/
|
|
75
|
+
function stripGap(variant) {
|
|
76
|
+
return variant === 'outline' ? 0 : 2;
|
|
77
|
+
}
|
|
78
|
+
// Chakra's proportions, and looser than a first guess would make them: the
|
|
79
|
+
// labels are cap-trimmed, so `py` is the *whole* visible gap over the caps
|
|
80
|
+
// and under the baseline — padding sized for an untrimmed line box reads as
|
|
81
|
+
// cramped once the box is the letters. The type steps off the theme's own
|
|
82
|
+
// `fontSize` rather than a fixed scale, so a theme that sets 16 gets tabs in
|
|
83
|
+
// 16 (the same call `/timeline` makes).
|
|
84
|
+
const SIZES = {
|
|
85
|
+
sm: { px: 12, py: 8, step: -1 },
|
|
86
|
+
md: { px: 16, py: 10, step: 0 },
|
|
87
|
+
lg: { px: 20, py: 12, step: 1 },
|
|
88
|
+
};
|
|
89
|
+
const LookContext = React.createContext(null);
|
|
90
|
+
const StripContext = React.createContext(null);
|
|
91
|
+
function useLook(part) {
|
|
92
|
+
const look = React.useContext(LookContext);
|
|
93
|
+
if (!look) {
|
|
94
|
+
throw new Error(`@react-x11/components: <${part}> has to be inside a <Tabs>. ` +
|
|
95
|
+
'It reads the selected value, the variant and the size from it.');
|
|
96
|
+
}
|
|
97
|
+
return look;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* A colour prop, resolved against the palette — `$token` is normally the
|
|
101
|
+
* renderer's job, but `interpolate()` is arithmetic over real colours and
|
|
102
|
+
* runs here, before any of that. Same helper as `/timeline`'s.
|
|
103
|
+
*/
|
|
104
|
+
function resolveColor(value, theme, fallback) {
|
|
105
|
+
if (!value)
|
|
106
|
+
return fallback;
|
|
107
|
+
if (!value.startsWith('$'))
|
|
108
|
+
return value;
|
|
109
|
+
const named = theme[value.slice(1)];
|
|
110
|
+
return typeof named === 'string' ? named : fallback;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `accent` laid over `ground`, opaquely — a weak wash at low `amount`.
|
|
114
|
+
* Opaque rather than translucent for the same reason `/timeline` mixes: a
|
|
115
|
+
* wash sits over the strip's own chrome, and light through it would show the
|
|
116
|
+
* rule running behind a selected trigger.
|
|
117
|
+
*/
|
|
118
|
+
function over(ground, accent, amount) {
|
|
119
|
+
const mixed = interpolate(ground, accent, amount);
|
|
120
|
+
return typeof mixed === 'string' ? mixed : accent;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* A single-line label's box trimmed to its letters — core's `capTrim`, the
|
|
124
|
+
* rule every core widget label follows (`labelContent` in
|
|
125
|
+
* `components/theme.js`). A line box is the font's ascent plus descent plus
|
|
126
|
+
* line gap, and the space over a capital differs from the space under the
|
|
127
|
+
* baseline by `(ascent - capHeight) - descent` — so a centred untrimmed
|
|
128
|
+
* label sits visibly low beside the icon centred on its own middle. Trimming
|
|
129
|
+
* makes the box *be* the letters, and centring centres what can be seen.
|
|
130
|
+
* Labels only: the icon is a `<canvas>` and panel prose is a paragraph,
|
|
131
|
+
* and neither wants it.
|
|
132
|
+
*/
|
|
133
|
+
const CAP_TRIM = { textBoxTrim: 'cap-alphabetic' };
|
|
134
|
+
/**
|
|
135
|
+
* Strings and numbers are only legal inside `<text>`, and a trigger's label
|
|
136
|
+
* is prose — `<TabsTrigger value="a">Members</TabsTrigger>` is the shortest
|
|
137
|
+
* thing that works and has to keep working. A primitive child is wrapped; an
|
|
138
|
+
* element child is left exactly as written, and inherits the same ink.
|
|
139
|
+
*/
|
|
140
|
+
function withText(children, style) {
|
|
141
|
+
let wrapped = false;
|
|
142
|
+
const mapped = React.Children.map(children, (child) => {
|
|
143
|
+
if (typeof child !== 'string' && typeof child !== 'number')
|
|
144
|
+
return child;
|
|
145
|
+
wrapped = true;
|
|
146
|
+
return hx('text', style ? { style } : null, child);
|
|
147
|
+
});
|
|
148
|
+
return wrapped ? mapped : children;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* The part's own style, then the caller's — later entries win, which is how
|
|
152
|
+
* every `style` prop in this renderer resolves precedence.
|
|
153
|
+
*/
|
|
154
|
+
function styled(base, extra) {
|
|
155
|
+
if (!extra)
|
|
156
|
+
return [base];
|
|
157
|
+
return Array.isArray(extra) ? [base, ...extra] : [base, extra];
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The hover fill on a `line` strip — the wash a `subtle` trigger wears, on
|
|
161
|
+
* the variant whose selected mark is a rule rather than a fill.
|
|
162
|
+
*
|
|
163
|
+
* Two things about it are deliberate and neither is obvious.
|
|
164
|
+
*
|
|
165
|
+
* **It is the size a `subtle` trigger's fill is** — the label with the same
|
|
166
|
+
* padding round it — and it keeps that same distance again off the panel
|
|
167
|
+
* edge, which is what {@link railGap} adds to a `line` trigger's padding to
|
|
168
|
+
* make room for. The gap is not decoration: the strip's rule runs *under*
|
|
169
|
+
* its triggers, so a fill over the whole box would take a bite out of that
|
|
170
|
+
* line for exactly the width of whatever the pointer was on, and one that
|
|
171
|
+
* merely cleared the rule would read as a block resting on it. Which is also
|
|
172
|
+
* why it is a box and not a `backgroundColor` — a background fills the box
|
|
173
|
+
* it is on, and this must not.
|
|
174
|
+
*
|
|
175
|
+
* And the hover that raises it is **state**, not a `:hover` block. `:hover`
|
|
176
|
+
* lights the node under the pointer and its ancestors (react-x11's
|
|
177
|
+
* `_updateHover`), and this box is the label's *sibling* — so a wash that
|
|
178
|
+
* styled itself would go out every time the pointer crossed a letter.
|
|
179
|
+
*/
|
|
180
|
+
function hoverWash(look) {
|
|
181
|
+
const gap = railGap(look);
|
|
182
|
+
return hx('box', {
|
|
183
|
+
key: 'wash',
|
|
184
|
+
style: [
|
|
185
|
+
{
|
|
186
|
+
position: 'absolute',
|
|
187
|
+
top: 0,
|
|
188
|
+
start: 0,
|
|
189
|
+
borderRadius: WASH_CORNER,
|
|
190
|
+
backgroundColor: '$surfaceHover',
|
|
191
|
+
},
|
|
192
|
+
look.vertical ? { bottom: 0, end: gap } : { bottom: gap, end: 0 },
|
|
193
|
+
],
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* The air a `line` trigger keeps between its wash and the strip's rule, and
|
|
198
|
+
* therefore the padding it carries beyond the label's own on that one side.
|
|
199
|
+
* It is the label's padding on the axis the rule crosses, so that the wash's
|
|
200
|
+
* two distances — text to its edge, its edge to the line — come out the same.
|
|
201
|
+
*
|
|
202
|
+
* `0` for every other variant: they have no wash to stand off the rule, and
|
|
203
|
+
* an `outline` tab's fill is *supposed* to reach it.
|
|
204
|
+
*/
|
|
205
|
+
function railGap(look) {
|
|
206
|
+
if (look.variant !== 'line')
|
|
207
|
+
return 0;
|
|
208
|
+
return look.vertical ? look.px : look.py;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Everything a stop on the strip looks like, per variant.
|
|
212
|
+
*
|
|
213
|
+
* Shared because the strip has two kinds of stop and they have to be
|
|
214
|
+
* indistinguishable: a `<TabsTrigger>`, and the overflow button, which wears
|
|
215
|
+
* the selected look whenever the tab that is selected is one of the ones it
|
|
216
|
+
* is holding. Two copies of this switch would drift on the first variant
|
|
217
|
+
* anybody touched.
|
|
218
|
+
*
|
|
219
|
+
* Each selected state is opaque where it has to cover the strip's rule, and
|
|
220
|
+
* every ink change is a `$token` so a `theme` prop above still wins.
|
|
221
|
+
*/
|
|
222
|
+
function tabChrome(look, indicator, state) {
|
|
223
|
+
const { selected, disabled, hovered, grow } = state;
|
|
224
|
+
const vertical = look.vertical;
|
|
225
|
+
const variantStyle = [];
|
|
226
|
+
const hover = {};
|
|
227
|
+
switch (look.variant) {
|
|
228
|
+
case 'line':
|
|
229
|
+
case 'plain':
|
|
230
|
+
Object.assign(hover, { ':hover': { color: '$text' } });
|
|
231
|
+
break;
|
|
232
|
+
case 'subtle':
|
|
233
|
+
variantStyle.push({ borderRadius: WASH_CORNER });
|
|
234
|
+
if (selected)
|
|
235
|
+
variantStyle.push({
|
|
236
|
+
backgroundColor: over(look.ground, look.accent, 0.15),
|
|
237
|
+
});
|
|
238
|
+
else
|
|
239
|
+
Object.assign(hover, {
|
|
240
|
+
':hover': { backgroundColor: '$surfaceHover' },
|
|
241
|
+
':active': { backgroundColor: '$surfaceActive' },
|
|
242
|
+
});
|
|
243
|
+
break;
|
|
244
|
+
case 'enclosed':
|
|
245
|
+
// Every trigger carries the border so selecting one cannot change its
|
|
246
|
+
// size — only the selected one inks it.
|
|
247
|
+
variantStyle.push({
|
|
248
|
+
borderRadius: WASH_CORNER,
|
|
249
|
+
borderWidth: 1,
|
|
250
|
+
borderColor: 'transparent',
|
|
251
|
+
});
|
|
252
|
+
if (selected)
|
|
253
|
+
variantStyle.push({
|
|
254
|
+
backgroundColor: look.ground,
|
|
255
|
+
borderColor: '$border',
|
|
256
|
+
});
|
|
257
|
+
else
|
|
258
|
+
Object.assign(hover, { ':hover': { color: '$text' } });
|
|
259
|
+
break;
|
|
260
|
+
case 'outline':
|
|
261
|
+
if (selected)
|
|
262
|
+
variantStyle.push({
|
|
263
|
+
// The fill is the ground, so the tab and its panel read as one
|
|
264
|
+
// surface — and it is what covers the strip's rule underneath.
|
|
265
|
+
// Rounded so the fill stays inside the frame's shoulders, but
|
|
266
|
+
// deliberately **borderless**: this renderer paints a node's
|
|
267
|
+
// border *after* its children (`Node.paint` — children, then
|
|
268
|
+
// `_paintBorder`), so a border here could never be opened by a
|
|
269
|
+
// child laid over it. The frame below carries the stroke instead.
|
|
270
|
+
backgroundColor: look.ground,
|
|
271
|
+
borderRadius: CORNER,
|
|
272
|
+
});
|
|
273
|
+
else
|
|
274
|
+
Object.assign(hover, {
|
|
275
|
+
':hover': { backgroundColor: '$surfaceHover' },
|
|
276
|
+
});
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
// The `line` strip's hover — see {@link hoverWash} for why it is a box and
|
|
280
|
+
// why `hovered` is state rather than a `:hover` block.
|
|
281
|
+
const wash = look.variant === 'line' && hovered && !disabled ? hoverWash(look) : null;
|
|
282
|
+
// The `outline` tab's shape, from two stacked children — a shape the style
|
|
283
|
+
// vocabulary cannot say in one box. `borderRadius` is one number and
|
|
284
|
+
// requires a uniform border, so "rounded shoulders, open bottom" is drawn
|
|
285
|
+
// as: a *frame* (inset-0 box carrying the full rounded border) and, after
|
|
286
|
+
// it, a *skirt* (a strip of ground over the frame's panel edge, covering
|
|
287
|
+
// the border and the two corners that would curl toward the panel, and
|
|
288
|
+
// redrawing the straight side walls over itself). The skirt can cover the
|
|
289
|
+
// frame only because they are siblings — a node's own border paints over
|
|
290
|
+
// its children, which is why the frame is not the trigger's border.
|
|
291
|
+
const outlineShape = look.variant === 'outline' && selected
|
|
292
|
+
? [
|
|
293
|
+
hx('box', {
|
|
294
|
+
key: 'frame',
|
|
295
|
+
style: {
|
|
296
|
+
position: 'absolute',
|
|
297
|
+
top: 0,
|
|
298
|
+
bottom: 0,
|
|
299
|
+
start: 0,
|
|
300
|
+
end: 0,
|
|
301
|
+
borderWidth: 1,
|
|
302
|
+
borderColor: '$border',
|
|
303
|
+
borderRadius: CORNER,
|
|
304
|
+
},
|
|
305
|
+
}),
|
|
306
|
+
hx('box', {
|
|
307
|
+
key: 'skirt',
|
|
308
|
+
style: [
|
|
309
|
+
{
|
|
310
|
+
position: 'absolute',
|
|
311
|
+
backgroundColor: look.ground,
|
|
312
|
+
borderColor: '$border',
|
|
313
|
+
},
|
|
314
|
+
vertical
|
|
315
|
+
? {
|
|
316
|
+
top: 0,
|
|
317
|
+
bottom: 0,
|
|
318
|
+
end: 0,
|
|
319
|
+
width: CORNER + 1,
|
|
320
|
+
borderTopWidth: 1,
|
|
321
|
+
borderBottomWidth: 1,
|
|
322
|
+
}
|
|
323
|
+
: {
|
|
324
|
+
start: 0,
|
|
325
|
+
end: 0,
|
|
326
|
+
bottom: 0,
|
|
327
|
+
height: CORNER + 1,
|
|
328
|
+
borderStartWidth: 1,
|
|
329
|
+
borderEndWidth: 1,
|
|
330
|
+
},
|
|
331
|
+
],
|
|
332
|
+
}),
|
|
333
|
+
]
|
|
334
|
+
: null;
|
|
335
|
+
// The selected marker in the `line` variant: a 2px box riding the panel
|
|
336
|
+
// edge of the stop, over the strip's 1px rule.
|
|
337
|
+
const marker = look.variant === 'line'
|
|
338
|
+
? hx('box', {
|
|
339
|
+
style: [
|
|
340
|
+
{
|
|
341
|
+
position: 'absolute',
|
|
342
|
+
backgroundColor: selected ? look.accent : 'transparent',
|
|
343
|
+
transition: { backgroundColor: 100 },
|
|
344
|
+
},
|
|
345
|
+
vertical
|
|
346
|
+
? { top: 0, bottom: 0, end: 0, width: INDICATOR }
|
|
347
|
+
: { left: 0, right: 0, bottom: 0, height: INDICATOR },
|
|
348
|
+
],
|
|
349
|
+
})
|
|
350
|
+
: null;
|
|
351
|
+
// A `<TabsIndicator>` in the strip is drawn here, by the selected stop,
|
|
352
|
+
// behind its label. Layout owns its geometry — see the part's own comment.
|
|
353
|
+
const chip = indicator && selected
|
|
354
|
+
? hx('box', {
|
|
355
|
+
key: 'chip',
|
|
356
|
+
style: styled({
|
|
357
|
+
position: 'absolute',
|
|
358
|
+
top: 0,
|
|
359
|
+
bottom: 0,
|
|
360
|
+
left: 0,
|
|
361
|
+
right: 0,
|
|
362
|
+
zIndex: -1,
|
|
363
|
+
borderRadius: WASH_CORNER,
|
|
364
|
+
backgroundColor: over(look.ground, look.accent, 0.15),
|
|
365
|
+
}, indicator.style),
|
|
366
|
+
})
|
|
367
|
+
: null;
|
|
368
|
+
const ink = disabled
|
|
369
|
+
? '$textMuted'
|
|
370
|
+
: selected
|
|
371
|
+
? look.variant === 'line' || look.variant === 'subtle'
|
|
372
|
+
? look.accent
|
|
373
|
+
: '$text'
|
|
374
|
+
: '$textMuted';
|
|
375
|
+
return {
|
|
376
|
+
style: {
|
|
377
|
+
flexDirection: 'row',
|
|
378
|
+
alignItems: 'center',
|
|
379
|
+
justifyContent: 'center',
|
|
380
|
+
flexShrink: 0,
|
|
381
|
+
...(grow && { flexGrow: 1 }),
|
|
382
|
+
gap: TRIGGER_GAP,
|
|
383
|
+
// Logical rather than left/right, because the one side that differs is
|
|
384
|
+
// the panel side: a `line` trigger carries the wash's air off the rule
|
|
385
|
+
// as padding, so the label keeps the same room a `subtle` one gives it
|
|
386
|
+
// and the wash still has somewhere to stop.
|
|
387
|
+
paddingTop: look.py,
|
|
388
|
+
paddingBottom: look.py + (vertical ? 0 : railGap(look)),
|
|
389
|
+
paddingStart: look.px,
|
|
390
|
+
paddingEnd: look.px + (vertical ? railGap(look) : 0),
|
|
391
|
+
fontSize: look.font,
|
|
392
|
+
color: ink,
|
|
393
|
+
...(disabled ? {} : { cursor: 'pointer' }),
|
|
394
|
+
transition: { backgroundColor: 100, color: 100 },
|
|
395
|
+
...Object.assign({}, ...variantStyle),
|
|
396
|
+
...(disabled ? {} : hover),
|
|
397
|
+
},
|
|
398
|
+
behind: [chip, wash, outlineShape],
|
|
399
|
+
after: marker,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Tabs.
|
|
404
|
+
*
|
|
405
|
+
* ```jsx
|
|
406
|
+
* <Tabs defaultValue="members">
|
|
407
|
+
* <TabsList>
|
|
408
|
+
* <TabsTrigger value="members">Members</TabsTrigger>
|
|
409
|
+
* <TabsTrigger value="projects">Projects</TabsTrigger>
|
|
410
|
+
* </TabsList>
|
|
411
|
+
* <TabsContent value="members">…</TabsContent>
|
|
412
|
+
* <TabsContent value="projects">…</TabsContent>
|
|
413
|
+
* </Tabs>
|
|
414
|
+
* ```
|
|
415
|
+
*/
|
|
416
|
+
export function Tabs(props) {
|
|
417
|
+
// `Theme` is an interface, so it has no implicit index signature — the same
|
|
418
|
+
// widening `hx.ts` documents for the `theme` prop.
|
|
419
|
+
const theme = useTheme();
|
|
420
|
+
const [uncontrolled, setUncontrolled] = React.useState(props.defaultValue);
|
|
421
|
+
const [focused, setFocused] = React.useState(null);
|
|
422
|
+
const selected = props.value ?? uncontrolled;
|
|
423
|
+
const vertical = props.orientation === 'vertical';
|
|
424
|
+
const variant = props.variant ?? 'line';
|
|
425
|
+
const size = SIZES[props.size ?? 'md'] ?? SIZES.md;
|
|
426
|
+
const accent = resolveColor(props.accent, theme, String(theme.accent));
|
|
427
|
+
const ground = resolveColor(props.ground, theme, String(theme.background));
|
|
428
|
+
const body = typeof theme.fontSize === 'number' ? theme.fontSize : 14;
|
|
429
|
+
const { onValueChange } = props;
|
|
430
|
+
const controlled = props.value !== undefined;
|
|
431
|
+
// The registry the keyboard walks. A ref rather than state: a trigger
|
|
432
|
+
// mounting must not re-render the tree, and the walk only happens inside
|
|
433
|
+
// an event handler, where `current` is always current.
|
|
434
|
+
const triggers = React.useRef(new Map());
|
|
435
|
+
// Which values have ever been selected, for `lazyMount`. Written during
|
|
436
|
+
// render on purpose — adding the current selection is idempotent, and an
|
|
437
|
+
// effect would run a frame after the panel already needed the answer.
|
|
438
|
+
const seen = React.useRef(new Set());
|
|
439
|
+
if (selected !== undefined)
|
|
440
|
+
seen.current.add(selected);
|
|
441
|
+
const select = React.useCallback((value) => {
|
|
442
|
+
if (value === selected)
|
|
443
|
+
return;
|
|
444
|
+
if (!controlled)
|
|
445
|
+
setUncontrolled(value);
|
|
446
|
+
onValueChange?.({ value });
|
|
447
|
+
}, [selected, controlled, onValueChange]);
|
|
448
|
+
const look = React.useMemo(() => ({
|
|
449
|
+
variant,
|
|
450
|
+
vertical,
|
|
451
|
+
manual: props.activationMode === 'manual',
|
|
452
|
+
fitted: props.fitted ?? false,
|
|
453
|
+
justify: props.justify,
|
|
454
|
+
overflow: props.overflow ?? 'menu',
|
|
455
|
+
accent,
|
|
456
|
+
ground,
|
|
457
|
+
text: String(theme.text),
|
|
458
|
+
font: body + size.step,
|
|
459
|
+
px: size.px,
|
|
460
|
+
py: size.py,
|
|
461
|
+
selected,
|
|
462
|
+
focused,
|
|
463
|
+
select,
|
|
464
|
+
setFocused,
|
|
465
|
+
triggers: triggers.current,
|
|
466
|
+
everSelected: (value) => seen.current.has(value),
|
|
467
|
+
lazyMount: props.lazyMount ?? false,
|
|
468
|
+
unmountOnExit: props.unmountOnExit ?? false,
|
|
469
|
+
}), [
|
|
470
|
+
variant,
|
|
471
|
+
vertical,
|
|
472
|
+
props.activationMode,
|
|
473
|
+
props.fitted,
|
|
474
|
+
props.justify,
|
|
475
|
+
props.overflow,
|
|
476
|
+
props.lazyMount,
|
|
477
|
+
props.unmountOnExit,
|
|
478
|
+
accent,
|
|
479
|
+
ground,
|
|
480
|
+
theme.text,
|
|
481
|
+
body,
|
|
482
|
+
size,
|
|
483
|
+
selected,
|
|
484
|
+
focused,
|
|
485
|
+
select,
|
|
486
|
+
]);
|
|
487
|
+
return h(LookContext.Provider, { value: look }, hx('box', {
|
|
488
|
+
// The same fill-the-space default core's `<Tabs>` had: a notebook is
|
|
489
|
+
// usually the main region of whatever pane it is in. `style` wins for
|
|
490
|
+
// the one that is not.
|
|
491
|
+
style: styled({
|
|
492
|
+
flexGrow: 1,
|
|
493
|
+
minHeight: 0,
|
|
494
|
+
minWidth: 0,
|
|
495
|
+
flexDirection: vertical ? 'row' : 'column',
|
|
496
|
+
}, props.style),
|
|
497
|
+
'data-testname': props['data-testname'],
|
|
498
|
+
}, props.children));
|
|
499
|
+
}
|
|
500
|
+
/** The strip's tab stop when the focus is on the overflow button rather than
|
|
501
|
+
* on a tab. A NUL is not a value any trigger can be given. */
|
|
502
|
+
const MORE = '\u0000more';
|
|
503
|
+
const NONE = [];
|
|
504
|
+
function sameValues(a, b) {
|
|
505
|
+
return a.length === b.length && a.every((value, i) => b[i] === value);
|
|
506
|
+
}
|
|
507
|
+
/** Every primitive child, joined — see {@link OverflowItem.key}. */
|
|
508
|
+
function labelKey(children) {
|
|
509
|
+
const parts = [];
|
|
510
|
+
React.Children.forEach(children, (child) => {
|
|
511
|
+
parts.push(typeof child === 'string' || typeof child === 'number'
|
|
512
|
+
? String(child)
|
|
513
|
+
: '\u0000');
|
|
514
|
+
});
|
|
515
|
+
return parts.join('\u0001');
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Which tabs do not fit, in the order they were written.
|
|
519
|
+
*
|
|
520
|
+
* The whole thing runs off what layout actually did, because nothing else
|
|
521
|
+
* here can answer it: a tab's width is its label in the theme's face at the
|
|
522
|
+
* size the theme is set to, plus whatever the app hung beside it. So the
|
|
523
|
+
* strip measures the triggers it has drawn and remembers what each one came
|
|
524
|
+
* out at, and the tabs in the menu are chosen from those remembered widths —
|
|
525
|
+
* they are not on the strip to be measured while they are in the menu.
|
|
526
|
+
*
|
|
527
|
+
* **Two passes, not one.** A tab whose width is not known yet (a first
|
|
528
|
+
* render, a tab just added, a label just changed) puts the strip back to
|
|
529
|
+
* showing everything for one pass, which is what gives it a width; the pass
|
|
530
|
+
* after that is the one that decides. That costs a frame of a strip
|
|
531
|
+
* overflowing its own edge — clipped, since the strip clips while the menu
|
|
532
|
+
* is on — and buys a fixed point: what is hidden is a pure function of the
|
|
533
|
+
* remembered widths and the room, so the answer does not move unless
|
|
534
|
+
* something really did.
|
|
535
|
+
*
|
|
536
|
+
* **When it re-runs.** After every commit, through `afterLayout` — react-x11
|
|
537
|
+
* lays out on a frame flush rather than in the commit, so an effect reads the
|
|
538
|
+
* *previous* pass's geometry. And after every layout this component did not
|
|
539
|
+
* cause, through the owning window's `onAnchorChange`: the strip getting
|
|
540
|
+
* narrower is usually the window being resized, which re-lays out without
|
|
541
|
+
* re-rendering anything and would otherwise leave the menu answering a width
|
|
542
|
+
* the strip stopped having.
|
|
543
|
+
*/
|
|
544
|
+
function useOverflow(active, items, look, listRef, moreRef) {
|
|
545
|
+
const widths = React.useRef(new Map());
|
|
546
|
+
// What `abs` was in when those were taken, so the menu can ask for them
|
|
547
|
+
// back in the logical pixels a style length speaks.
|
|
548
|
+
const measured = React.useRef(1);
|
|
549
|
+
const [hidden, setHidden] = React.useState(NONE);
|
|
550
|
+
// Read and written straight, beside the state: two `onAnchorChange`
|
|
551
|
+
// notifications can land in one flush, and the second has to see what the
|
|
552
|
+
// first decided rather than the render's stale copy.
|
|
553
|
+
const hiddenRef = React.useRef(hidden);
|
|
554
|
+
hiddenRef.current = hidden;
|
|
555
|
+
const measure = () => {
|
|
556
|
+
const list = listRef.current;
|
|
557
|
+
const room = list?.contentBox?.().width ?? 0;
|
|
558
|
+
// Nothing laid out yet, or laid out inside a hidden panel (`display:
|
|
559
|
+
// 'none'` is out of layout, so the strip is 0 wide there). Answering
|
|
560
|
+
// "everything overflows" would empty the strip into the menu and flash
|
|
561
|
+
// it back the moment the panel is shown.
|
|
562
|
+
if (!list || !(room > 0))
|
|
563
|
+
return;
|
|
564
|
+
// `abs` and `contentBox()` are device pixels; a style length is logical.
|
|
565
|
+
// Everything below is device, so the style numbers convert once, here.
|
|
566
|
+
const scale = list.scale && list.scale > 0 ? list.scale : 1;
|
|
567
|
+
measured.current = scale;
|
|
568
|
+
const gap = stripGap(look.variant) * scale;
|
|
569
|
+
const drawn = (value) => {
|
|
570
|
+
const node = look.triggers.get(value)?.node;
|
|
571
|
+
return node && node.abs.width > 0 ? node : null;
|
|
572
|
+
};
|
|
573
|
+
// A `fitted` strip with nothing hidden has already grown its triggers to
|
|
574
|
+
// fill it, so those widths are the strip shared out rather than what a
|
|
575
|
+
// tab needs, and writing them down would be writing down the answer to
|
|
576
|
+
// the wrong question. The exception is the pass that matters: a `fitted`
|
|
577
|
+
// strip that is *overflowing* had no free space to grow into, so the
|
|
578
|
+
// widths are honest — and that is exactly the pass the menu appears on.
|
|
579
|
+
let live = 0;
|
|
580
|
+
let count = 0;
|
|
581
|
+
for (const item of items) {
|
|
582
|
+
const node = drawn(item.value);
|
|
583
|
+
if (!node)
|
|
584
|
+
continue;
|
|
585
|
+
live += node.abs.width;
|
|
586
|
+
count += 1;
|
|
587
|
+
}
|
|
588
|
+
live += Math.max(0, count - 1) * gap;
|
|
589
|
+
if (!look.fitted || hiddenRef.current.length > 0 || live > room) {
|
|
590
|
+
for (const item of items) {
|
|
591
|
+
const node = drawn(item.value);
|
|
592
|
+
if (!node)
|
|
593
|
+
continue;
|
|
594
|
+
widths.current.set(item.value, {
|
|
595
|
+
key: item.key,
|
|
596
|
+
width: node.abs.width,
|
|
597
|
+
height: node.abs.height,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
// A tab that has left the strip has no width worth keeping — and this is
|
|
602
|
+
// the only thing that ever empties the map.
|
|
603
|
+
const present = new Set(items.map((item) => item.value));
|
|
604
|
+
for (const value of [...widths.current.keys()]) {
|
|
605
|
+
if (!present.has(value))
|
|
606
|
+
widths.current.delete(value);
|
|
607
|
+
}
|
|
608
|
+
const known = (item) => {
|
|
609
|
+
const seen = widths.current.get(item.value);
|
|
610
|
+
return seen && seen.key === item.key ? seen.width : null;
|
|
611
|
+
};
|
|
612
|
+
let natural = Math.max(0, items.length - 1) * gap;
|
|
613
|
+
let unmeasured = false;
|
|
614
|
+
for (const item of items) {
|
|
615
|
+
const width = known(item);
|
|
616
|
+
if (width === null)
|
|
617
|
+
unmeasured = true;
|
|
618
|
+
else
|
|
619
|
+
natural += width;
|
|
620
|
+
}
|
|
621
|
+
// The measuring pass — everything on the strip, nothing decided yet —
|
|
622
|
+
// is `NONE`, which is also the answer when it all fits.
|
|
623
|
+
let next = NONE;
|
|
624
|
+
if (!unmeasured && natural > room) {
|
|
625
|
+
// The button is measured once it exists; before that it is guessed at
|
|
626
|
+
// its own padding plus a chevron, and the pass after it mounts
|
|
627
|
+
// corrects the guess.
|
|
628
|
+
const more = moreRef.current?.abs.width || (look.px * 2 + look.font) * scale;
|
|
629
|
+
const budget = room - more - gap;
|
|
630
|
+
const shown = new Set();
|
|
631
|
+
let used = 0;
|
|
632
|
+
const take = (value, width) => {
|
|
633
|
+
used += width + (shown.size ? gap : 0);
|
|
634
|
+
shown.add(value);
|
|
635
|
+
};
|
|
636
|
+
// Straight down the strip, and the first tab that does not fit ends
|
|
637
|
+
// it: which tabs are on the strip is a question about room, and
|
|
638
|
+
// nothing else. The selected tab is not pulled out of the menu to keep
|
|
639
|
+
// its place — the button wears the selected look instead, so the strip
|
|
640
|
+
// does not reshuffle itself every time a tab is picked out of it.
|
|
641
|
+
for (const item of items) {
|
|
642
|
+
if (shown.has(item.value))
|
|
643
|
+
continue;
|
|
644
|
+
const width = known(item) ?? 0;
|
|
645
|
+
if (used + width + (shown.size ? gap : 0) > budget)
|
|
646
|
+
break;
|
|
647
|
+
take(item.value, width);
|
|
648
|
+
}
|
|
649
|
+
next = items
|
|
650
|
+
.filter((item) => !shown.has(item.value))
|
|
651
|
+
.map((item) => item.value);
|
|
652
|
+
}
|
|
653
|
+
if (sameValues(next, hiddenRef.current))
|
|
654
|
+
return;
|
|
655
|
+
hiddenRef.current = next;
|
|
656
|
+
setHidden(next);
|
|
657
|
+
};
|
|
658
|
+
// Re-read through a ref so the subscriptions below run this render's
|
|
659
|
+
// closure without being torn down and rebuilt for every one of them.
|
|
660
|
+
const measureRef = React.useRef(measure);
|
|
661
|
+
measureRef.current = measure;
|
|
662
|
+
// Deliberately no dependency array: any commit can move a tab's width.
|
|
663
|
+
React.useEffect(() => {
|
|
664
|
+
if (!active)
|
|
665
|
+
return undefined;
|
|
666
|
+
const tick = afterLayout(() => measureRef.current());
|
|
667
|
+
return () => cancelAfterLayout(tick);
|
|
668
|
+
});
|
|
669
|
+
React.useEffect(() => {
|
|
670
|
+
if (!active)
|
|
671
|
+
return undefined;
|
|
672
|
+
const root = listRef.current?.root;
|
|
673
|
+
if (!root?.onAnchorChange)
|
|
674
|
+
return undefined;
|
|
675
|
+
return root.onAnchorChange(() => measureRef.current());
|
|
676
|
+
}, [active, listRef]);
|
|
677
|
+
// `overflow="clip"` turned on mid-life, or the strip turned vertical: hand
|
|
678
|
+
// the tabs back rather than leaving them in a menu nothing draws.
|
|
679
|
+
React.useEffect(() => {
|
|
680
|
+
if (!active && hiddenRef.current.length > 0) {
|
|
681
|
+
hiddenRef.current = NONE;
|
|
682
|
+
setHidden(NONE);
|
|
683
|
+
}
|
|
684
|
+
}, [active]);
|
|
685
|
+
const sizeOf = React.useCallback((value, key) => {
|
|
686
|
+
const seen = widths.current.get(value);
|
|
687
|
+
if (!seen || seen.key !== key)
|
|
688
|
+
return null;
|
|
689
|
+
const scale = measured.current || 1;
|
|
690
|
+
return { width: seen.width / scale, height: seen.height / scale };
|
|
691
|
+
}, []);
|
|
692
|
+
return { hidden: active ? hidden : NONE, sizeOf };
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* The overflow menu's own size, worked out from the measurements the strip
|
|
696
|
+
* already took rather than left to the popup.
|
|
697
|
+
*
|
|
698
|
+
* A `<popup>` can size itself from its content, and that is normally the
|
|
699
|
+
* right answer — but its placement is then a function of a number nothing
|
|
700
|
+
* outside it can see, which is a bad thing for the one popup here whose
|
|
701
|
+
* anchor moves every time the strip re-fits. A menu row is the same content
|
|
702
|
+
* as the tab it stands for — same label, same face, same gap, same glyph
|
|
703
|
+
* beside it — laid out with the row's padding instead of the tab's, so the
|
|
704
|
+
* strip's own measurement answers it exactly.
|
|
705
|
+
*
|
|
706
|
+
* `null` when a tab in the menu has not been measured, which is the pass
|
|
707
|
+
* where nothing is decided anyway; the popup sizes itself for that frame.
|
|
708
|
+
*/
|
|
709
|
+
function menuSheet(look, items, sizeOf) {
|
|
710
|
+
if (items.length === 0)
|
|
711
|
+
return null;
|
|
712
|
+
let content = 0;
|
|
713
|
+
let rows = 0;
|
|
714
|
+
for (const item of items) {
|
|
715
|
+
const size = sizeOf(item.value, item.key);
|
|
716
|
+
if (!size)
|
|
717
|
+
return null;
|
|
718
|
+
content = Math.max(content, size.width - look.px * 2 + MENU_ROW_PX * 2);
|
|
719
|
+
// …and off the height, the air a `line` trigger keeps under its wash on
|
|
720
|
+
// top of its padding: a menu row has no rule to stand off.
|
|
721
|
+
rows += size.height - look.py * 2 - railGap(look) + MENU_ROW_PY * 2;
|
|
722
|
+
}
|
|
723
|
+
const frame = (SHEET_PAD + SHEET_BORDER) * 2;
|
|
724
|
+
return {
|
|
725
|
+
width: Math.ceil(Math.max(content + frame, MENU_MIN_WIDTH)),
|
|
726
|
+
height: Math.ceil(rows + (items.length - 1) * MENU_ROW_GAP + frame),
|
|
727
|
+
};
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* The button at the end of a strip that ran out of room, and the menu it
|
|
731
|
+
* drops.
|
|
732
|
+
*
|
|
733
|
+
* **It is a stop on the strip like any other**, drawn from the same
|
|
734
|
+
* {@link tabChrome} the triggers are — so when the selected tab is one of
|
|
735
|
+
* the ones it is holding, the button wears the selected look (the accent
|
|
736
|
+
* marker under a `line` strip, the chip on a `subtle` one) and the menu
|
|
737
|
+
* marks the row. That is what a tab picked out of the menu does instead of
|
|
738
|
+
* displacing a tab that fitted: the strip keeps the tabs it has room for,
|
|
739
|
+
* and the button says the selection is behind it. It also takes the strip's
|
|
740
|
+
* tab stop while it holds the selection, since the trigger that would
|
|
741
|
+
* normally have it is not on the strip to hold anything.
|
|
742
|
+
*
|
|
743
|
+
* The menu is a `<popup>` anchored to the button and sized from its own
|
|
744
|
+
* content — override-redirect, grabbing the pointer, dismissed by a press
|
|
745
|
+
* anywhere else. Which means it never takes the focus, so **the button keeps
|
|
746
|
+
* the keyboard** and hands the menu its keys, the way `<DatePicker>`'s
|
|
747
|
+
* trigger does: Down/Up open it and walk it, Enter and Space commit, Escape
|
|
748
|
+
* shuts it. A press elsewhere in the window blurs the button and that shuts
|
|
749
|
+
* it too, and `useDismissOnWindowBlur` covers the one case a blur cannot —
|
|
750
|
+
* the *window* going to the background, which leaves the button focused.
|
|
751
|
+
*/
|
|
752
|
+
function TabsMore(props) {
|
|
753
|
+
const { items, look, moreRef, sheet } = props;
|
|
754
|
+
const strip = React.useContext(StripContext);
|
|
755
|
+
const theme = useTheme();
|
|
756
|
+
const [open, setOpen] = React.useState(false);
|
|
757
|
+
// -1 is "nothing yet", which is what a menu dropped with the mouse opens
|
|
758
|
+
// on: a row lit before the pointer or the arrows have said anything is a
|
|
759
|
+
// row the next Enter would commit by accident.
|
|
760
|
+
const [active, setActive] = React.useState(-1);
|
|
761
|
+
// The button wears whatever the tabs beside it wear — including the reason
|
|
762
|
+
// a `line` strip's fill has to be a box ({@link hoverWash}), which is why
|
|
763
|
+
// this is tracked rather than declared.
|
|
764
|
+
const [hovered, setHovered] = React.useState(false);
|
|
765
|
+
const lit = hovered || open;
|
|
766
|
+
/** Where the selection is, when it is behind this button. */
|
|
767
|
+
const chosen = items.findIndex((item) => item.value === look.selected);
|
|
768
|
+
const holds = chosen >= 0;
|
|
769
|
+
const close = () => setOpen(false);
|
|
770
|
+
useDismissOnWindowBlur(moreRef, open, close);
|
|
771
|
+
// The menu is drawn from the tabs the strip could not fit, and that set
|
|
772
|
+
// moves under it — a resize while it is open can take one back. Keeping
|
|
773
|
+
// the cursor in range is cheaper than closing on every re-fit.
|
|
774
|
+
const at = items.length ? Math.min(active, items.length - 1) : -1;
|
|
775
|
+
const choose = (value) => {
|
|
776
|
+
close();
|
|
777
|
+
look.select(value);
|
|
778
|
+
// The focus stays here: the tab that was picked is still in this menu,
|
|
779
|
+
// and this button is the stop on the strip that now stands for it.
|
|
780
|
+
};
|
|
781
|
+
const step = (delta) => {
|
|
782
|
+
const n = items.length;
|
|
783
|
+
if (n === 0)
|
|
784
|
+
return -1;
|
|
785
|
+
// From "nothing yet", Down means the first row and Up means the last —
|
|
786
|
+
// which is what starting one step outside either end comes to.
|
|
787
|
+
const from = at < 0 ? (delta > 0 ? -1 : 0) : at;
|
|
788
|
+
for (let i = 1; i <= n; i++) {
|
|
789
|
+
const index = (((from + delta * i) % n) + n) % n;
|
|
790
|
+
if (!items[index]?.disabled)
|
|
791
|
+
return index;
|
|
792
|
+
}
|
|
793
|
+
return at;
|
|
794
|
+
};
|
|
795
|
+
const commit = () => {
|
|
796
|
+
const item = items[at];
|
|
797
|
+
if (item && !item.disabled)
|
|
798
|
+
choose(item.value);
|
|
799
|
+
};
|
|
800
|
+
const onKeyDown = (ev) => {
|
|
801
|
+
if (ev.keysym === XK_ESCAPE) {
|
|
802
|
+
if (!open)
|
|
803
|
+
return;
|
|
804
|
+
close();
|
|
805
|
+
ev.preventDefault();
|
|
806
|
+
ev.stopPropagation();
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (!open) {
|
|
810
|
+
if (ev.keysym !== XK_DOWN && ev.keysym !== XK_UP)
|
|
811
|
+
return;
|
|
812
|
+
// Opened from the keyboard, so it opens on a row: the selected one if
|
|
813
|
+
// the selection is in here, and otherwise the end the key came from.
|
|
814
|
+
setActive(holds ? chosen : ev.keysym === XK_UP ? items.length - 1 : 0);
|
|
815
|
+
setOpen(true);
|
|
816
|
+
ev.preventDefault();
|
|
817
|
+
ev.stopPropagation();
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
// An open menu owns the keyboard: the strip's own arrow walk is one
|
|
821
|
+
// bubble away, and a Down that both moved the menu's cursor and roved
|
|
822
|
+
// the focus off the button would close the menu it had just moved.
|
|
823
|
+
if (ev.keysym === XK_DOWN)
|
|
824
|
+
setActive(step(1));
|
|
825
|
+
else if (ev.keysym === XK_UP)
|
|
826
|
+
setActive(step(-1));
|
|
827
|
+
else if (ev.keysym === XK_RETURN || ev.codepoint === 32)
|
|
828
|
+
commit();
|
|
829
|
+
else
|
|
830
|
+
return;
|
|
831
|
+
ev.preventDefault();
|
|
832
|
+
ev.stopPropagation();
|
|
833
|
+
};
|
|
834
|
+
const row = (item, index) => hx('box', {
|
|
835
|
+
key: item.value,
|
|
836
|
+
// A menu of tabs is a set of mutually exclusive choices, which is
|
|
837
|
+
// what this role says — and it is the only place a screen reader
|
|
838
|
+
// hears about a tab that is not on the strip to carry `role="tab"`.
|
|
839
|
+
role: 'menuitemradio',
|
|
840
|
+
'aria-checked': index === chosen,
|
|
841
|
+
disabled: item.disabled || undefined,
|
|
842
|
+
onMouseEnter: () => {
|
|
843
|
+
if (!item.disabled)
|
|
844
|
+
setActive(index);
|
|
845
|
+
},
|
|
846
|
+
onClick: () => {
|
|
847
|
+
if (!item.disabled)
|
|
848
|
+
choose(item.value);
|
|
849
|
+
},
|
|
850
|
+
style: {
|
|
851
|
+
flexDirection: 'row',
|
|
852
|
+
alignItems: 'center',
|
|
853
|
+
gap: TRIGGER_GAP,
|
|
854
|
+
paddingLeft: MENU_ROW_PX,
|
|
855
|
+
paddingRight: MENU_ROW_PX,
|
|
856
|
+
paddingTop: MENU_ROW_PY,
|
|
857
|
+
paddingBottom: MENU_ROW_PY,
|
|
858
|
+
borderRadius: WASH_CORNER,
|
|
859
|
+
fontSize: look.font,
|
|
860
|
+
color: item.disabled ? '$textMuted' : '$text',
|
|
861
|
+
...(item.disabled
|
|
862
|
+
? {}
|
|
863
|
+
: {
|
|
864
|
+
cursor: 'pointer',
|
|
865
|
+
// The lit row is state rather than a `:hover` block because
|
|
866
|
+
// the arrows move it too, and a menu with two lit rows —
|
|
867
|
+
// the pointer's and the keyboard's — is worse than one.
|
|
868
|
+
...(index === at && { backgroundColor: '$surfaceHover' }),
|
|
869
|
+
}),
|
|
870
|
+
},
|
|
871
|
+
},
|
|
872
|
+
// The selected row's marker: the strip's own accent bar, turned to
|
|
873
|
+
// stand along a row instead of under a tab. It is a mark and not a
|
|
874
|
+
// fill so it cannot be confused with the row the cursor is on — both
|
|
875
|
+
// can be true of the same row.
|
|
876
|
+
index === chosen
|
|
877
|
+
? hx('box', {
|
|
878
|
+
key: 'mark',
|
|
879
|
+
style: {
|
|
880
|
+
position: 'absolute',
|
|
881
|
+
start: 0,
|
|
882
|
+
top: MENU_ROW_PY / 2,
|
|
883
|
+
bottom: MENU_ROW_PY / 2,
|
|
884
|
+
width: INDICATOR,
|
|
885
|
+
borderRadius: INDICATOR / 2,
|
|
886
|
+
backgroundColor: look.accent,
|
|
887
|
+
},
|
|
888
|
+
})
|
|
889
|
+
: null, withText(item.children, CAP_TRIM));
|
|
890
|
+
const chrome = tabChrome(look, strip?.indicator ?? null, {
|
|
891
|
+
selected: holds,
|
|
892
|
+
disabled: false,
|
|
893
|
+
hovered: lit,
|
|
894
|
+
// Never: the button only exists while something is hidden, and a
|
|
895
|
+
// `fitted` strip has stopped sharing its width out by then.
|
|
896
|
+
grow: false,
|
|
897
|
+
});
|
|
898
|
+
return hx('box', {
|
|
899
|
+
ref: moreRef,
|
|
900
|
+
role: 'button',
|
|
901
|
+
'aria-haspopup': 'menu',
|
|
902
|
+
'aria-expanded': open,
|
|
903
|
+
'aria-label': `${items.length} more tab${items.length === 1 ? '' : 's'}`,
|
|
904
|
+
focusable: true,
|
|
905
|
+
// The strip is one tab stop. A tab holds it — unless the selected tab
|
|
906
|
+
// is one of the ones behind this button, and then this holds it.
|
|
907
|
+
tabIndex: holds ? 0 : -1,
|
|
908
|
+
// On the press, not the release: a control whose whole purpose is to
|
|
909
|
+
// be looked at has nothing to gain from waiting out the click.
|
|
910
|
+
onMouseDown: () => {
|
|
911
|
+
setActive(-1);
|
|
912
|
+
setOpen((was) => !was);
|
|
913
|
+
},
|
|
914
|
+
onMouseEnter: () => setHovered(true),
|
|
915
|
+
onMouseLeave: () => setHovered(false),
|
|
916
|
+
onFocus: () => look.setFocused(MORE),
|
|
917
|
+
onBlur: () => {
|
|
918
|
+
look.setFocused(null);
|
|
919
|
+
close();
|
|
920
|
+
},
|
|
921
|
+
onKeyDown,
|
|
922
|
+
style: [
|
|
923
|
+
chrome.style,
|
|
924
|
+
// An open button reads as engaged in every variant, not only the one
|
|
925
|
+
// whose wash is a box — and not when it is already wearing the
|
|
926
|
+
// selected look, which outranks a hover.
|
|
927
|
+
lit && !holds
|
|
928
|
+
? look.variant === 'line'
|
|
929
|
+
? { color: '$text' }
|
|
930
|
+
: { color: '$text', backgroundColor: '$surfaceHover' }
|
|
931
|
+
: {},
|
|
932
|
+
],
|
|
933
|
+
'data-testname': 'tabs-more',
|
|
934
|
+
}, chrome.behind, h(Icon, { name: 'chevronDown', size: look.font - 4 }), chrome.after, open &&
|
|
935
|
+
items.length > 0 &&
|
|
936
|
+
hx('popup', {
|
|
937
|
+
theme,
|
|
938
|
+
// Sized here rather than from its own content, so where it lands is
|
|
939
|
+
// arithmetic this component can see — {@link menuSheet}.
|
|
940
|
+
...(sheet ?? {}),
|
|
941
|
+
anchor: {
|
|
942
|
+
to: moreRef,
|
|
943
|
+
placement: 'bottom',
|
|
944
|
+
// The menu hangs off the end of the strip because the button
|
|
945
|
+
// does, and `end` mirrors on its own in an RTL subtree.
|
|
946
|
+
align: 'end',
|
|
947
|
+
},
|
|
948
|
+
grab: true,
|
|
949
|
+
onDismiss: close,
|
|
950
|
+
// ARGB where the display has it, so the corners the sheet gives up
|
|
951
|
+
// are the desktop rather than a colour — the same call
|
|
952
|
+
// `<DatePicker>`'s sheet makes, and the same reason the window
|
|
953
|
+
// paints nothing itself when it can be seen through.
|
|
954
|
+
transparent: true,
|
|
955
|
+
style: {
|
|
956
|
+
backgroundColor: theme.background,
|
|
957
|
+
'@supports transparency': { backgroundColor: 'transparent' },
|
|
958
|
+
},
|
|
959
|
+
}, hx('box', {
|
|
960
|
+
role: 'menu',
|
|
961
|
+
style: {
|
|
962
|
+
flexGrow: 1,
|
|
963
|
+
minWidth: MENU_MIN_WIDTH,
|
|
964
|
+
padding: SHEET_PAD,
|
|
965
|
+
gap: MENU_ROW_GAP,
|
|
966
|
+
borderWidth: SHEET_BORDER,
|
|
967
|
+
borderColor: '$border',
|
|
968
|
+
backgroundColor: theme.background,
|
|
969
|
+
'@supports transparency': { borderRadius: theme.radiusPopup },
|
|
970
|
+
},
|
|
971
|
+
}, items.map(row))));
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* The strip of triggers — a single tab stop, walked with the arrows.
|
|
975
|
+
*
|
|
976
|
+
* The arrows are visual, the list is logical: in a mirrored horizontal strip
|
|
977
|
+
* the *next* trigger is the one to the left, and in either direction the walk
|
|
978
|
+
* follows where layout actually put each trigger (sorted by position, not by
|
|
979
|
+
* mount order), wraps at the ends, and skips disabled triggers. Home/End jump
|
|
980
|
+
* to the ends. In `manual` mode focus moves without selecting, and Enter or
|
|
981
|
+
* Space commits — those keys are not handled here, because core makes them
|
|
982
|
+
* the click the trigger's own `onClick` already is.
|
|
983
|
+
*
|
|
984
|
+
* A horizontal strip with more tabs than room keeps the ones that fit and
|
|
985
|
+
* drops the rest into a menu at its end (`overflow`, on the root). The
|
|
986
|
+
* menu's button is the last stop on the same walk.
|
|
987
|
+
*/
|
|
988
|
+
export function TabsList(props) {
|
|
989
|
+
const look = useLook('TabsList');
|
|
990
|
+
const rtl = useDirection() === 'rtl';
|
|
991
|
+
const { vertical } = look;
|
|
992
|
+
const listRef = React.useRef(null);
|
|
993
|
+
const moreRef = React.useRef(null);
|
|
994
|
+
// A `<TabsIndicator>` written into the strip, Chakra-style, is read here
|
|
995
|
+
// and handed to the triggers — the selected one draws it. It has to be a
|
|
996
|
+
// direct child for this to see it; anything else in the strip renders
|
|
997
|
+
// where it was written.
|
|
998
|
+
//
|
|
999
|
+
// The triggers come off the same walk, for the overflow menu: it draws the
|
|
1000
|
+
// children the app wrote on the trigger, so a tab that moves into the menu
|
|
1001
|
+
// keeps its label without the app naming it twice. Anything in the strip
|
|
1002
|
+
// that is *not* a trigger stays on the strip and is not accounted for —
|
|
1003
|
+
// the arithmetic is about tabs.
|
|
1004
|
+
const children = React.Children.toArray(props.children);
|
|
1005
|
+
let indicator = null;
|
|
1006
|
+
const items = [];
|
|
1007
|
+
for (const child of children) {
|
|
1008
|
+
if (!React.isValidElement(child))
|
|
1009
|
+
continue;
|
|
1010
|
+
if (child.type === TabsIndicator) {
|
|
1011
|
+
indicator = child.props;
|
|
1012
|
+
}
|
|
1013
|
+
else if (child.type === TabsTrigger) {
|
|
1014
|
+
const trigger = child.props;
|
|
1015
|
+
items.push({
|
|
1016
|
+
value: trigger.value,
|
|
1017
|
+
disabled: trigger.disabled ?? false,
|
|
1018
|
+
key: labelKey(trigger.children),
|
|
1019
|
+
children: trigger.children,
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
// One tab cannot overflow into a menu that would take more room than it
|
|
1024
|
+
// does, so the machinery does not start until there are two.
|
|
1025
|
+
const { hidden, sizeOf } = useOverflow(look.overflow === 'menu' && !vertical && items.length > 1, items, look, listRef, moreRef);
|
|
1026
|
+
const hiddenSet = hidden.length > 0 ? new Set(hidden) : null;
|
|
1027
|
+
const overflowed = hiddenSet
|
|
1028
|
+
? items.filter((item) => hiddenSet.has(item.value))
|
|
1029
|
+
: [];
|
|
1030
|
+
const onKeyDown = (ev) => {
|
|
1031
|
+
// Where layout put each trigger is the one ordering that cannot drift
|
|
1032
|
+
// from what the user sees — mount order can, after a remove and re-add.
|
|
1033
|
+
// The overflow button is a stop on the same walk and sorts in with the
|
|
1034
|
+
// rest, which is what puts it at the end without it being told.
|
|
1035
|
+
const stops = [...look.triggers.entries()].map(([value, entry]) => ({
|
|
1036
|
+
value,
|
|
1037
|
+
...entry,
|
|
1038
|
+
}));
|
|
1039
|
+
if (moreRef.current) {
|
|
1040
|
+
stops.push({ value: MORE, node: moreRef.current, disabled: false });
|
|
1041
|
+
}
|
|
1042
|
+
const entries = stops.sort((a, b) => vertical ? a.node.abs.y - b.node.abs.y : a.node.abs.x - b.node.abs.x);
|
|
1043
|
+
// Visual order back to logical: an RTL strip reads right to left. A
|
|
1044
|
+
// vertical strip never mirrors — Up is Up.
|
|
1045
|
+
const swap = !vertical && rtl;
|
|
1046
|
+
if (swap)
|
|
1047
|
+
entries.reverse();
|
|
1048
|
+
/** The next enabled trigger `delta` steps away, wrapping. */
|
|
1049
|
+
const step = (from, delta) => {
|
|
1050
|
+
const n = entries.length;
|
|
1051
|
+
if (n === 0)
|
|
1052
|
+
return null;
|
|
1053
|
+
for (let i = 1; i <= n; i++) {
|
|
1054
|
+
const entry = entries[(((from + delta * i) % n) + n) % n];
|
|
1055
|
+
if (entry && !entry.disabled)
|
|
1056
|
+
return entry;
|
|
1057
|
+
}
|
|
1058
|
+
return null;
|
|
1059
|
+
};
|
|
1060
|
+
const goTo = (entry) => {
|
|
1061
|
+
if (!entry)
|
|
1062
|
+
return;
|
|
1063
|
+
entry.node.focus();
|
|
1064
|
+
// The overflow button selects nothing: it is the way to the tabs that
|
|
1065
|
+
// did not fit, not one of them.
|
|
1066
|
+
if (!look.manual && entry.value !== MORE)
|
|
1067
|
+
look.select(entry.value);
|
|
1068
|
+
};
|
|
1069
|
+
const back = vertical ? XK_UP : swap ? XK_RIGHT : XK_LEFT;
|
|
1070
|
+
const forward = vertical ? XK_DOWN : swap ? XK_LEFT : XK_RIGHT;
|
|
1071
|
+
const current = entries.findIndex((entry) => entry.value === (look.focused ?? look.selected));
|
|
1072
|
+
switch (ev.keysym) {
|
|
1073
|
+
case back:
|
|
1074
|
+
goTo(step(current === -1 ? 0 : current, -1));
|
|
1075
|
+
return;
|
|
1076
|
+
case forward:
|
|
1077
|
+
goTo(step(current === -1 ? 0 : current, 1));
|
|
1078
|
+
return;
|
|
1079
|
+
case XK_HOME:
|
|
1080
|
+
goTo(entries.find((entry) => !entry.disabled) ?? null);
|
|
1081
|
+
return;
|
|
1082
|
+
case XK_END:
|
|
1083
|
+
goTo([...entries].reverse().find((entry) => !entry.disabled) ?? null);
|
|
1084
|
+
return;
|
|
1085
|
+
default:
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
// What the strip itself draws, per variant. The `line` and `outline` rules
|
|
1089
|
+
// are an absolutely positioned 1px box on the panel edge rather than a
|
|
1090
|
+
// border: borders are all-edges here, and this has to sit on one side.
|
|
1091
|
+
// `zIndex: -1` paints it under the triggers, so a selected `outline`
|
|
1092
|
+
// trigger's opaque fill is what cuts it.
|
|
1093
|
+
const rule = look.variant === 'line' || look.variant === 'outline'
|
|
1094
|
+
? hx('box', {
|
|
1095
|
+
style: [
|
|
1096
|
+
{ position: 'absolute', backgroundColor: '$border', zIndex: -1 },
|
|
1097
|
+
vertical
|
|
1098
|
+
? { top: 0, bottom: 0, end: 0, width: RULE }
|
|
1099
|
+
: { left: 0, right: 0, bottom: 0, height: RULE },
|
|
1100
|
+
],
|
|
1101
|
+
})
|
|
1102
|
+
: null;
|
|
1103
|
+
const chrome = look.variant === 'enclosed'
|
|
1104
|
+
? {
|
|
1105
|
+
// Chakra's `bg.muted` chip, mixed from the palette this theme
|
|
1106
|
+
// actually has — opaque, for the reason `over` gives.
|
|
1107
|
+
backgroundColor: over(look.ground, look.text, 0.07),
|
|
1108
|
+
borderRadius: CORNER,
|
|
1109
|
+
padding: 3,
|
|
1110
|
+
gap: 2,
|
|
1111
|
+
}
|
|
1112
|
+
: { gap: stripGap(look.variant) };
|
|
1113
|
+
return h(StripContext.Provider, {
|
|
1114
|
+
value: {
|
|
1115
|
+
indicator,
|
|
1116
|
+
// A `fitted` strip stops growing its tabs while any of them is in
|
|
1117
|
+
// the menu — see {@link TabsStrip}.
|
|
1118
|
+
grow: look.fitted && hidden.length === 0,
|
|
1119
|
+
},
|
|
1120
|
+
}, hx('box', {
|
|
1121
|
+
ref: listRef,
|
|
1122
|
+
role: 'tablist',
|
|
1123
|
+
'aria-orientation': look.vertical ? 'vertical' : 'horizontal',
|
|
1124
|
+
onKeyDown,
|
|
1125
|
+
style: styled({
|
|
1126
|
+
flexShrink: 0,
|
|
1127
|
+
flexDirection: vertical ? 'column' : 'row',
|
|
1128
|
+
alignItems: 'stretch',
|
|
1129
|
+
...(look.justify && {
|
|
1130
|
+
justifyContent: look.justify === 'start'
|
|
1131
|
+
? 'flex-start'
|
|
1132
|
+
: look.justify === 'end'
|
|
1133
|
+
? 'flex-end'
|
|
1134
|
+
: 'center',
|
|
1135
|
+
}),
|
|
1136
|
+
// A pass that has not decided yet draws every tab at its natural
|
|
1137
|
+
// width, which is wider than the strip by definition — clipped
|
|
1138
|
+
// rather than spilling over the panel for the frame it takes.
|
|
1139
|
+
...(look.overflow === 'menu' && !vertical
|
|
1140
|
+
? { overflow: 'hidden' }
|
|
1141
|
+
: {}),
|
|
1142
|
+
...chrome,
|
|
1143
|
+
}, props.style),
|
|
1144
|
+
'data-testname': props['data-testname'],
|
|
1145
|
+
}, rule, hiddenSet
|
|
1146
|
+
? children.filter((child) => !(React.isValidElement(child) &&
|
|
1147
|
+
child.type === TabsTrigger &&
|
|
1148
|
+
hiddenSet.has(child.props.value)))
|
|
1149
|
+
: children, overflowed.length > 0
|
|
1150
|
+
? h(TabsMore, {
|
|
1151
|
+
key: 'tabs-more',
|
|
1152
|
+
items: overflowed,
|
|
1153
|
+
look,
|
|
1154
|
+
moreRef,
|
|
1155
|
+
sheet: menuSheet(look, overflowed, sizeOf),
|
|
1156
|
+
})
|
|
1157
|
+
: null));
|
|
1158
|
+
}
|
|
1159
|
+
/**
|
|
1160
|
+
* One tab. Prose and glyphs alike are legal children — a string is wrapped
|
|
1161
|
+
* in `<text>`, and an `<Icon>` inherits the trigger's ink (its `size` does
|
|
1162
|
+
* not inherit and has to be named).
|
|
1163
|
+
*/
|
|
1164
|
+
export function TabsTrigger(props) {
|
|
1165
|
+
const look = useLook('TabsTrigger');
|
|
1166
|
+
const strip = React.useContext(StripContext);
|
|
1167
|
+
const indicator = strip?.indicator ?? null;
|
|
1168
|
+
const { value, disabled = false } = props;
|
|
1169
|
+
const selected = look.selected === value;
|
|
1170
|
+
// Tracked rather than declared, because the wash is a box of its own —
|
|
1171
|
+
// {@link hoverWash}.
|
|
1172
|
+
const [hovered, setHovered] = React.useState(false);
|
|
1173
|
+
// Registration is the ref callback: a new closure each render, so React
|
|
1174
|
+
// re-runs it and a `disabled` that changed is re-registered. Order in the
|
|
1175
|
+
// map churns with that, which is why the keyboard sorts by position
|
|
1176
|
+
// instead of trusting it.
|
|
1177
|
+
const register = (node) => {
|
|
1178
|
+
if (node)
|
|
1179
|
+
look.triggers.set(value, { node, disabled });
|
|
1180
|
+
else
|
|
1181
|
+
look.triggers.delete(value);
|
|
1182
|
+
};
|
|
1183
|
+
// Everything it looks like, shared with the overflow button beside it —
|
|
1184
|
+
// {@link tabChrome}.
|
|
1185
|
+
const chrome = tabChrome(look, indicator, {
|
|
1186
|
+
selected,
|
|
1187
|
+
disabled,
|
|
1188
|
+
hovered,
|
|
1189
|
+
grow: strip ? strip.grow : look.fitted,
|
|
1190
|
+
});
|
|
1191
|
+
return hx('box', {
|
|
1192
|
+
role: 'tab',
|
|
1193
|
+
'aria-selected': selected,
|
|
1194
|
+
ref: register,
|
|
1195
|
+
focusable: !disabled,
|
|
1196
|
+
// Roving focus: the selected trigger is the strip's tab stop. With
|
|
1197
|
+
// nothing selected yet every trigger is reachable, which is the lesser
|
|
1198
|
+
// evil — a strip nobody can tab into is worse than two stops.
|
|
1199
|
+
tabIndex: look.selected === undefined ? 0 : selected ? 0 : -1,
|
|
1200
|
+
disabled,
|
|
1201
|
+
onFocus: () => look.setFocused(value),
|
|
1202
|
+
onBlur: () => look.setFocused(null),
|
|
1203
|
+
onMouseEnter: () => setHovered(true),
|
|
1204
|
+
onMouseLeave: () => setHovered(false),
|
|
1205
|
+
onClick: () => !disabled && look.select(value),
|
|
1206
|
+
style: styled(chrome.style, props.style),
|
|
1207
|
+
'data-testname': props['data-testname'],
|
|
1208
|
+
}, chrome.behind, withText(props.children, CAP_TRIM), chrome.after);
|
|
1209
|
+
}
|
|
1210
|
+
/**
|
|
1211
|
+
* A marker that follows the selected trigger — Chakra's `Tabs.Indicator`,
|
|
1212
|
+
* and like there it is what gives the `plain` variant a look.
|
|
1213
|
+
*
|
|
1214
|
+
* Write it as a direct child of `<TabsList>`. It renders nothing where it
|
|
1215
|
+
* stands: the selected trigger draws it, as a box behind its own label — so
|
|
1216
|
+
* layout owns the geometry and a resize, a font change or an RTL mirror can
|
|
1217
|
+
* never leave the marker where a trigger used to be. What that costs is the
|
|
1218
|
+
* slide: this renderer cannot animate between two mounts, so the marker
|
|
1219
|
+
* moves in one step.
|
|
1220
|
+
*/
|
|
1221
|
+
export function TabsIndicator(props) {
|
|
1222
|
+
useLook('TabsIndicator');
|
|
1223
|
+
void props;
|
|
1224
|
+
return null;
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* One panel. Every panel mounts up front and the unselected ones are
|
|
1228
|
+
* `display: 'none'` — out of layout, still alive, so a form half-filled on
|
|
1229
|
+
* another tab keeps its state. `lazyMount` defers a panel until its first
|
|
1230
|
+
* selection; `unmountOnExit` gives the state up when it hides. Both are on
|
|
1231
|
+
* the root, because they are a policy about the set, not about one panel.
|
|
1232
|
+
*/
|
|
1233
|
+
export function TabsContent(props) {
|
|
1234
|
+
const look = useLook('TabsContent');
|
|
1235
|
+
const selected = look.selected === props.value;
|
|
1236
|
+
if (!selected) {
|
|
1237
|
+
if (look.unmountOnExit)
|
|
1238
|
+
return null;
|
|
1239
|
+
if (look.lazyMount && !look.everSelected(props.value))
|
|
1240
|
+
return null;
|
|
1241
|
+
}
|
|
1242
|
+
return hx('box', {
|
|
1243
|
+
role: 'tabpanel',
|
|
1244
|
+
// A hidden panel is still mounted (that is the point), but it must not
|
|
1245
|
+
// be read: `display: 'none'` takes it out of layout, and this takes it
|
|
1246
|
+
// out of the accessibility tree.
|
|
1247
|
+
'aria-hidden': selected ? undefined : true,
|
|
1248
|
+
style: styled({
|
|
1249
|
+
display: selected ? 'flex' : 'none',
|
|
1250
|
+
flexDirection: 'column',
|
|
1251
|
+
flexGrow: 1,
|
|
1252
|
+
minHeight: 0,
|
|
1253
|
+
minWidth: 0,
|
|
1254
|
+
// Prose in a panel is body text. Named here because a bare
|
|
1255
|
+
// `<text>`'s default ink is not the palette's — it has to inherit
|
|
1256
|
+
// one, or a dark theme reads it in the light theme's black.
|
|
1257
|
+
color: '$text',
|
|
1258
|
+
...(look.vertical
|
|
1259
|
+
? { paddingStart: PANEL_GAP }
|
|
1260
|
+
: { paddingTop: PANEL_GAP }),
|
|
1261
|
+
}, props.style),
|
|
1262
|
+
'data-testname': props['data-testname'],
|
|
1263
|
+
}, withText(props.children));
|
|
1264
|
+
}
|
|
1265
|
+
//# sourceMappingURL=index.js.map
|