@fusion.dev/helix-core 0.238.0 → 0.239.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.
@@ -0,0 +1,108 @@
1
+ import type { ReactiveController, ReactiveControllerHost } from 'lit';
2
+ /** What a host hands the controller: the box to fit, and how each step folds. */
3
+ export interface OverflowOptions {
4
+ /**
5
+ * Writes step `step` onto the page, synchronously: `0` folds nothing, and
6
+ * each step folds more than the one before it. The controller reads the box
7
+ * after each write, so a step is on the page the moment this returns.
8
+ *
9
+ * The host's render draws the step the controller chose, and draws it the way
10
+ * this writes it, so the render that follows a measure changes nothing.
11
+ */
12
+ apply: (step: number) => void;
13
+ /**
14
+ * The box the row has to fit. The row fits while its content does not reach
15
+ * past the box's inline end, so the box takes its width from its container
16
+ * and never from what is in it.
17
+ */
18
+ box: () => HTMLElement | null | undefined;
19
+ /**
20
+ * Whether the row fits the box as it stands. Unset, the box's content must
21
+ * not reach past its inline end. A host whose box holds something placed
22
+ * after a measure, such as a marker under the current item, says here which
23
+ * elements to read, since that one still stands where the last step put it.
24
+ */
25
+ fits?: (box: HTMLElement) => boolean;
26
+ /** Elements besides the box whose size calls for a new measure, such as the items. */
27
+ observe?: () => Element[];
28
+ /**
29
+ * Resolves once the controller may write: the first measure waits for it. A
30
+ * host that folds elements a consumer wrote waits here for React to hydrate
31
+ * them, since a write before then is a mismatch React reports and never
32
+ * repairs.
33
+ */
34
+ ready?: () => Promise<void>;
35
+ /** How many steps there are past `0`. With none, nothing folds. */
36
+ steps: () => number;
37
+ }
38
+ /**
39
+ * Whether every one of `elements` lies inside the box's content edges, read
40
+ * as if the box were scrolled to its start. For a host whose box holds
41
+ * something else that overflows it, such as a marker placed after a measure.
42
+ */
43
+ export declare const insideBox: (box: HTMLElement, elements: Element[]) => boolean;
44
+ /**
45
+ * Folds a row of controls that does not fit its box into a menu, step by step,
46
+ * and keeps the least folded step that fits.
47
+ *
48
+ * **Measured, never listed.** The host says what each step folds, and the
49
+ * controller finds the first step whose row fits. A list of controls to fold
50
+ * written by the author is right at one width, in one language, for one
51
+ * pointer: a block name in German, the wider gaps of a touch screen, or a
52
+ * control a page adds at the end changes what fits. `Video` lists its overflow
53
+ * because which controls a video offers is the author's choice. A toolbar's
54
+ * tools and a navigation's links are chosen already, so what is left is the
55
+ * width.
56
+ *
57
+ * **Each step is written and read at once.** A step that folds a tool changes
58
+ * every gap and divider after it, so the controller writes a step through
59
+ * `apply` and asks the box whether its content still overflows, rather than
60
+ * adding widths that leave the gaps out. The search starts from the step
61
+ * already on the page and moves one step at a time, so a measure that changes
62
+ * nothing reads the layout twice.
63
+ *
64
+ * **A resize is answered in the next frame.** The box can shrink when it folds,
65
+ * where its width follows its content, and a `ResizeObserver` callback that
66
+ * changes the size of what it observes ends the frame with "ResizeObserver
67
+ * loop completed with undelivered notifications". So the observer books a
68
+ * frame, and the measure runs there. A host's own change is measured at once,
69
+ * after its render and before the paint, by calling `request()` from
70
+ * `willUpdate`.
71
+ *
72
+ * Client only. A server cannot measure, so it serves step `0`, every control
73
+ * on the row, and the browser folds what does not fit once it has measured.
74
+ */
75
+ export declare class OverflowController implements ReactiveController {
76
+ private readonly host;
77
+ private readonly options;
78
+ /** Whether the controller may measure: connected, and past `ready`. */
79
+ private armed;
80
+ /** Whether the host asked for a measure after its next render. */
81
+ private asked;
82
+ /** Whether a measure has run since the controller armed, which the first render after it owes. */
83
+ private measured;
84
+ /** The booked frame for a measure, or `0`. */
85
+ private frame;
86
+ /** The elements the observer watches now. */
87
+ private readonly observed;
88
+ private sized;
89
+ /** The step on the page, `0` until a measure folds anything. */
90
+ private current;
91
+ constructor(host: ReactiveControllerHost & HTMLElement, options: OverflowOptions);
92
+ /** The step the last measure chose: `0` while nothing is folded. */
93
+ get step(): number;
94
+ /** Asks for a measure after the host's next render. Call it from `willUpdate`. */
95
+ request(): void;
96
+ /**
97
+ * Measures now and keeps the least folded step that fits, or the last step
98
+ * where none does. Asks the host to render where the step changed.
99
+ */
100
+ measure(): void;
101
+ /** Watches the box and the host's other elements, adding and dropping as they change. */
102
+ private watch;
103
+ /** Books one measure for the next frame. */
104
+ private readonly book;
105
+ hostConnected(): void;
106
+ hostDisconnected(): void;
107
+ hostUpdated(): void;
108
+ }
@@ -0,0 +1,119 @@
1
+ const overflows = (box) => box.scrollWidth > box.clientWidth;
2
+ export const insideBox = (box, elements) => {
3
+ const bounds = box.getBoundingClientRect();
4
+ const style = getComputedStyle(box);
5
+ const start = bounds.left + box.clientLeft + Number.parseFloat(style.paddingLeft);
6
+ const end = bounds.left + box.clientLeft + box.clientWidth - Number.parseFloat(style.paddingRight);
7
+ const shift = box.scrollLeft;
8
+ return elements.every((element) => {
9
+ const rect = element.getBoundingClientRect();
10
+ return rect.left + shift >= start - 0.5 && rect.right + shift <= end + 0.5;
11
+ });
12
+ };
13
+ export class OverflowController {
14
+ constructor(host, options) {
15
+ this.armed = false;
16
+ this.asked = false;
17
+ this.measured = false;
18
+ this.frame = 0;
19
+ this.observed = new Set();
20
+ this.current = 0;
21
+ this.book = () => {
22
+ if (this.frame !== 0) {
23
+ return;
24
+ }
25
+ this.frame = requestAnimationFrame(() => {
26
+ this.frame = 0;
27
+ this.measure();
28
+ });
29
+ };
30
+ this.host = host;
31
+ this.options = options;
32
+ host.addController(this);
33
+ }
34
+ get step() {
35
+ return this.current;
36
+ }
37
+ request() {
38
+ this.asked = true;
39
+ }
40
+ measure() {
41
+ const box = this.options.box();
42
+ const steps = this.options.steps();
43
+ if (!this.armed || !box?.isConnected) {
44
+ return;
45
+ }
46
+ const fits = (step) => {
47
+ this.options.apply(step);
48
+ return this.options.fits ? this.options.fits(box) : !overflows(box);
49
+ };
50
+ let step = Math.min(this.current, steps);
51
+ if (fits(step)) {
52
+ while (step > 0 && fits(step - 1)) {
53
+ step -= 1;
54
+ }
55
+ }
56
+ else {
57
+ while (step < steps && !fits(step + 1)) {
58
+ step += 1;
59
+ }
60
+ step = Math.min(step + 1, steps);
61
+ }
62
+ this.options.apply(step);
63
+ this.measured = true;
64
+ this.watch(box);
65
+ if (step !== this.current) {
66
+ this.current = step;
67
+ this.host.requestUpdate();
68
+ }
69
+ }
70
+ watch(box) {
71
+ if (!this.sized) {
72
+ return;
73
+ }
74
+ const wanted = new Set([box, ...(this.options.observe?.() ?? [])]);
75
+ for (const element of this.observed) {
76
+ if (!wanted.has(element)) {
77
+ this.sized.unobserve(element);
78
+ this.observed.delete(element);
79
+ }
80
+ }
81
+ for (const element of wanted) {
82
+ if (!this.observed.has(element)) {
83
+ this.sized.observe(element);
84
+ this.observed.add(element);
85
+ }
86
+ }
87
+ }
88
+ hostConnected() {
89
+ if (typeof ResizeObserver === 'undefined') {
90
+ return;
91
+ }
92
+ this.sized = new ResizeObserver(this.book);
93
+ void (this.options.ready?.() ?? Promise.resolve()).then(() => {
94
+ if (!this.host.isConnected || !this.sized) {
95
+ return;
96
+ }
97
+ this.armed = true;
98
+ this.measure();
99
+ void document.fonts?.ready.then(() => this.book());
100
+ });
101
+ }
102
+ hostDisconnected() {
103
+ this.armed = false;
104
+ this.measured = false;
105
+ this.sized?.disconnect();
106
+ this.sized = undefined;
107
+ this.observed.clear();
108
+ if (this.frame !== 0) {
109
+ cancelAnimationFrame(this.frame);
110
+ this.frame = 0;
111
+ }
112
+ }
113
+ hostUpdated() {
114
+ if (this.asked || !this.measured) {
115
+ this.asked = false;
116
+ this.measure();
117
+ }
118
+ }
119
+ }
@@ -39,7 +39,7 @@ export interface ToolbarControllerOptions {
39
39
  */
40
40
  words?: (control: HTMLElement) => string;
41
41
  /**
42
- * Where a change to `disabled` or `aria-hidden` is watched for, which is
42
+ * Where a change to `disabled`, `aria-hidden`, or `hidden` is watched for, which is
43
43
  * what moves the stop off a control that leaves the set. The host by
44
44
  * default, whose light DOM a page's toolbar holds; the render root for
45
45
  * controls the host draws itself.
@@ -5,13 +5,12 @@ const KEYS = new Set(['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'Home',
5
5
  const ownsTabStop = (node) => 'focusable' in node;
6
6
  const disabledOut = (node) => node.hasAttribute('disabled') &&
7
7
  node.focusableWhenDisabled !== true;
8
+ const leftOut = (node) => disabledOut(node) || node.hasAttribute('hidden') || node.getAttribute('aria-hidden') === 'true';
8
9
  const NATIVE = 'a[href], button, input, select, textarea, [tabindex], [contenteditable=""], [contenteditable="true"]';
9
10
  const TABBABLE = NATIVE.replace('[tabindex]', '[tabindex]:not([tabindex="-1"])');
10
11
  const innerStops = new WeakMap();
11
12
  const takesFocus = (node) => {
12
- if (!(node instanceof HTMLElement) ||
13
- disabledOut(node) ||
14
- node.getAttribute('aria-hidden') === 'true') {
13
+ if (!(node instanceof HTMLElement) || leftOut(node)) {
15
14
  return false;
16
15
  }
17
16
  return (node.matches(NATIVE) ||
@@ -139,9 +138,7 @@ export class ToolbarController {
139
138
  get controls() {
140
139
  const controls = [];
141
140
  for (const child of this.members) {
142
- if (!(child instanceof HTMLElement) ||
143
- disabledOut(child) ||
144
- child.getAttribute('aria-hidden') === 'true') {
141
+ if (!(child instanceof HTMLElement) || leftOut(child)) {
145
142
  continue;
146
143
  }
147
144
  if (takesFocus(child)) {
@@ -316,7 +313,7 @@ export class ToolbarController {
316
313
  const watched = this.options.watch?.() ?? this.host;
317
314
  this.watcher = new MutationObserver(() => this.restore());
318
315
  this.watcher.observe(watched, {
319
- attributeFilter: ['disabled', 'focusable-when-disabled', 'aria-hidden'],
316
+ attributeFilter: ['disabled', 'focusable-when-disabled', 'aria-hidden', 'hidden'],
320
317
  attributes: true,
321
318
  subtree: true,
322
319
  });
package/dist/index.d.ts CHANGED
@@ -27,6 +27,7 @@ export { ListedController } from './controllers/listed-controller/listed-control
27
27
  export { MediaSessionController, type SessionHandlers, SKIP_SECONDS, } from './controllers/media-session-controller/media-session-controller.js';
28
28
  export { animationsFinished, animationsSettled, type OpenPhase, OpenPhaseController, type OpenPhaseOptions, } from './controllers/open-phase-controller/open-phase-controller.js';
29
29
  export { OptionWindowController, type OptionWindowOptions, } from './controllers/option-window-controller/option-window-controller.js';
30
+ export { insideBox, OverflowController, type OverflowOptions, } from './controllers/overflow-controller/overflow-controller.js';
30
31
  export { MAX_FRAME_STEP, PaintGateController, type PaintGateOptions, } from './controllers/paint-gate-controller/paint-gate-controller.js';
31
32
  export { PlacementController } from './controllers/placement-controller/placement-controller.js';
32
33
  export { hintInside, hintMayShow, onPanelOpen, PopoverController, type PopoverOptions, } from './controllers/popover-controller/popover-controller.js';
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ export { ListedController } from './controllers/listed-controller/listed-control
27
27
  export { MediaSessionController, SKIP_SECONDS, } from './controllers/media-session-controller/media-session-controller.js';
28
28
  export { animationsFinished, animationsSettled, OpenPhaseController, } from './controllers/open-phase-controller/open-phase-controller.js';
29
29
  export { OptionWindowController, } from './controllers/option-window-controller/option-window-controller.js';
30
+ export { insideBox, OverflowController, } from './controllers/overflow-controller/overflow-controller.js';
30
31
  export { MAX_FRAME_STEP, PaintGateController, } from './controllers/paint-gate-controller/paint-gate-controller.js';
31
32
  export { PlacementController } from './controllers/placement-controller/placement-controller.js';
32
33
  export { hintInside, hintMayShow, onPanelOpen, PopoverController, } from './controllers/popover-controller/popover-controller.js';
package/dist/version.d.ts CHANGED
@@ -5,4 +5,4 @@
5
5
  * `package.json`, so the two always agree. Nothing else in the running code
6
6
  * knows which release it is.
7
7
  */
8
- export declare const VERSION = "0.238.0";
8
+ export declare const VERSION = "0.239.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.238.0';
1
+ export const VERSION = '0.239.0';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fusion.dev/helix-core",
3
3
  "description": "Helix element primitives: base classes, mixins, decorators, utilities.",
4
- "version": "0.238.0",
4
+ "version": "0.239.0",
5
5
  "license": "MIT",
6
6
  "author": "Fusion Development Group",
7
7
  "repository": {
@@ -87,12 +87,12 @@
87
87
  "bugs": "https://github.com/project-fusion/fusion/issues",
88
88
  "dependencies": {
89
89
  "@floating-ui/dom": "^1.8.0",
90
- "@fusion.dev/helix-icons": "0.238.0",
90
+ "@fusion.dev/helix-icons": "0.239.0",
91
91
  "tslib": "^2.8.1"
92
92
  },
93
93
  "devDependencies": {
94
- "@fusion.dev/helix-cem": "0.238.0",
95
- "@fusion.dev/helix-testing": "0.238.0",
94
+ "@fusion.dev/helix-cem": "0.239.0",
95
+ "@fusion.dev/helix-testing": "0.239.0",
96
96
  "@lit-labs/ssr": "^4.1.0",
97
97
  "lit": "^3.2.1"
98
98
  },