@fusion.dev/helix-headless 0.238.1 → 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.
@@ -1,5 +1,6 @@
1
1
  import { AriaForwardController, HelixElement, KeyboardNavigationController } from '@fusion.dev/helix-core';
2
- import { type PropertyValues, type TemplateResult } from 'lit';
2
+ import { nothing, type PropertyValues, type TemplateResult } from 'lit';
3
+ import '../../menu/index.js';
3
4
  /**
4
5
  * @summary A set of destinations, with a marker on the current one.
5
6
  *
@@ -29,6 +30,22 @@ import { type PropertyValues, type TemplateResult } from 'lit';
29
30
  * Set `orientation="vertical"` to stack the destinations in a column, for a
30
31
  * sidebar.
31
32
  *
33
+ * **A row too narrow for its destinations folds them into a menu.** The
34
+ * destinations fold from the end, one at a time, into a menu behind a button
35
+ * after the last one on the row, named by `more-label`, until the row fits.
36
+ * The current destination stays on the row, so the marker has somewhere to
37
+ * be: where it would fold, the one before it folds instead. Where the button
38
+ * and the current destination do not both fit, every destination folds and
39
+ * the button names the current one. Where even that does not fit, the button
40
+ * is a glyph alone, named for the current one. Each row in the menu is a link
41
+ * to the same address, and the current one is checked.
42
+ *
43
+ * A destination that folds is `hidden`: the link, or the list item that holds
44
+ * it. A destination that is no link, such as a `Megamenu`, stays on the row.
45
+ * A server serves every destination on the row, and the browser folds what
46
+ * does not fit once it has measured it. Set `overflow="scroll"` to scroll a
47
+ * long row inside itself instead. A column never folds.
48
+ *
32
49
  * Set `arrows` and the arrow keys move focus between the destinations, as
33
50
  * Radix's navigation menu does: left and right along a row, up and down a
34
51
  * column, Home and End to the ends. A `Megamenu` among them is reached at its
@@ -53,10 +70,38 @@ import { type PropertyValues, type TemplateResult } from 'lit';
53
70
  *
54
71
  * @slot - The destinations, as your own links.
55
72
  * @slot indicator - A marker of your own, drawn inside the `indicator` part, where the current destination's place and size reach it.
73
+ * @slot menu-icon - Replaces the glyph on the overflow button where it is too narrow for words.
74
+ * @slot more-icon - Replaces the caret on the button that opens the overflow menu.
56
75
  *
57
76
  * @csspart indicator - The marker that moves to the current destination. It carries the destination's place and size as `--indicator-x`, `--indicator-y`, `--indicator-inline-size` and `--indicator-block-size`, and `data-placed` once it has been placed.
77
+ * @csspart more - The button that opens the overflow menu, drawn after the last destination on the row while any has folded.
78
+ * @csspart more-current - The current destination's name on the button, while every destination has folded.
79
+ * @csspart more-glyph - The glyph on the button where it is too narrow for words, where none is slotted.
80
+ * @csspart more-label - The button's name, `more-label`, while the current destination is still on the row.
81
+ * @csspart more-mark - The caret on the button, where none is slotted.
58
82
  * @csspart nav - The navigation landmark.
83
+ * @csspart overflow - The menu of the destinations that do not fit on the row.
84
+ * @csspart overflow-back - The control that returns from a submenu.
85
+ * @csspart overflow-back-mark - The glyph on the back control of a submenu.
86
+ * @csspart overflow-empty - The text shown when the menu has no items.
87
+ * @csspart overflow-header - The bar above a submenu, holding the way back and its name.
88
+ * @csspart overflow-live - The overflow menu's live region, which repeats the empty and loading text for screen readers.
89
+ * @csspart overflow-loading - The text shown while the items are loading.
90
+ * @csspart overflow-option - One destination in the overflow menu, a link to its address. Checked where it is the current one.
91
+ * @csspart overflow-option-end - A destination's trailing column.
92
+ * @csspart overflow-option-item - A destination's row, an `<a>`.
93
+ * @csspart overflow-option-mark - The check beside the current destination.
94
+ * @csspart overflow-option-shortcut - A destination's key.
95
+ * @csspart overflow-option-shortcut-kbd - The key inside it.
96
+ * @csspart overflow-option-shortcut-key - One key of a destination's chord.
97
+ * @csspart overflow-option-shortcut-separator - The `+` between two keys of a destination's chord.
98
+ * @csspart overflow-option-start - A destination's leading column.
99
+ * @csspart overflow-option-surface - A destination's submenu panel, which a destination does not open.
100
+ * @csspart overflow-search-field - The overflow menu's filter field, drawn with `filterable`.
101
+ * @csspart overflow-surface - The floating panel the overflow menu is drawn on.
102
+ * @csspart overflow-title - The name of the submenu that is showing.
59
103
  *
104
+ * @cssstate folded - While any destination has folded into the overflow menu.
60
105
  * @cssstate open - While a megamenu or another disclosure among the destinations is open.
61
106
  */
62
107
  export declare class HelixHeadlessNavigation extends HelixElement {
@@ -73,6 +118,12 @@ export declare class HelixHeadlessNavigation extends HelixElement {
73
118
  * a link has focus keeps doing so.
74
119
  */
75
120
  arrows: boolean;
121
+ /**
122
+ * The name of the button that opens the overflow menu, where every
123
+ * destination has folded. `{name}` is the current destination's, which the
124
+ * button shows.
125
+ */
126
+ currentLabel: string;
76
127
  /** The name the element gives itself where `label` is empty. */
77
128
  fallbackLabel: string;
78
129
  /**
@@ -110,20 +161,67 @@ export declare class HelixHeadlessNavigation extends HelixElement {
110
161
  * consumer's server markup does not already have.
111
162
  */
112
163
  marker?: 'segmented' | 'underlined';
164
+ /**
165
+ * The words on the button that opens the overflow menu, and the menu's name.
166
+ */
167
+ moreLabel: string;
113
168
  /**
114
169
  * Stacks the destinations in a column, for a sidebar, where unset is a row
115
170
  * for a header or a footer. The marker follows the current destination down
116
171
  * the column as it does along the row.
117
172
  */
118
173
  orientation?: 'vertical';
174
+ /**
175
+ * What a row too narrow for its destinations does. Unset, the destinations
176
+ * that do not fit fold into the overflow menu. `scroll` keeps them all on
177
+ * the row and scrolls it inside itself, for a strip read by swiping.
178
+ *
179
+ * Reflected and left unset by default, so the host carries nothing a
180
+ * consumer's server markup does not already have.
181
+ */
182
+ overflow?: 'scroll';
119
183
  private current;
120
184
  /** The disclosures among the destinations that are open. */
121
185
  private readonly opened;
186
+ /** The destinations this element hid by folding them, so unfolding shows only those. */
187
+ private readonly hid;
188
+ /**
189
+ * Folds the destinations that do not fit on the row into the overflow
190
+ * menu: step `n` folds the first `n` of `foldOrder`.
191
+ */
192
+ private readonly fold;
122
193
  /**
123
194
  * Hears a disclosure among the destinations open or close. In capture,
124
195
  * because `toggle` does not bubble.
125
196
  */
126
197
  private readonly handleToggle;
198
+ /** Whether the row folds what does not fit: a row, without `overflow="scroll"`. */
199
+ private get folds();
200
+ /** The destinations that can fold: each a link, and none the page hid itself. */
201
+ private get foldable();
202
+ /** The current destination among those that can fold, where it is one. */
203
+ private get currentFoldable();
204
+ /**
205
+ * The order destinations fold in: from the end, the current one last, so a
206
+ * step that folds it folds every destination.
207
+ */
208
+ private get foldOrder();
209
+ /** The destinations step `step` folds. */
210
+ private foldedAt;
211
+ /**
212
+ * How the overflow button reads at step `step`. Once every destination has
213
+ * folded it is `named` for the current one, and past that, where even that
214
+ * does not fit, it is `compact`: a glyph alone, named for screen readers.
215
+ */
216
+ private buttonAt;
217
+ /** The overflow button while it shows, for the arrow keys. */
218
+ private get shownMore();
219
+ /**
220
+ * Writes a step onto the row as the render draws it: the folded
221
+ * destinations `hidden`, the menu shown while any has folded, and the
222
+ * button naming the current destination once every one has.
223
+ */
224
+ private applyFold;
127
225
  /** The landmark's name as a screen reader lists it. */
128
226
  private get landmarkName();
129
227
  /**
@@ -142,6 +240,22 @@ export declare class HelixHeadlessNavigation extends HelixElement {
142
240
  connectedCallback(): void;
143
241
  disconnectedCallback(): void;
144
242
  protected firstUpdated(changed: PropertyValues): void;
243
+ protected willUpdate(changed: PropertyValues): void;
244
+ /** The destinations changed: watched again, and what fits measured again. */
245
+ private readonly handleSlotChange;
246
+ /**
247
+ * The overflow menu, after the last destination: a row for each destination
248
+ * that folded, a link to the same address. Kept in the tree while nothing
249
+ * has folded, hidden, so a measure can show it at once.
250
+ *
251
+ * **The button is drawn as a destination**, words and a caret, since it
252
+ * stands in the row of links and the words say what it holds. Once every
253
+ * destination has folded, it names the current one, so the row still says
254
+ * where the reader is.
255
+ */
256
+ protected renderOverflow(): TemplateResult | typeof nothing;
257
+ /** Keeps the check on the current destination: choosing a row follows its link and changes no check. */
258
+ private readonly keepCheck;
145
259
  protected render(): TemplateResult;
146
260
  }
147
261
  declare global {
@@ -1,7 +1,11 @@
1
1
  import { __decorate } from "tslib";
2
- import { AriaForwardController, customElement, focusableElements, focusItemOf, HelixElement, IndicatorController, KeyboardNavigationController, reportRuntime, words, } from '@fusion.dev/helix-core';
3
- import { html } from 'lit';
2
+ import { AriaForwardController, customElement, focusableElements, focusItemOf, formatWords, HelixElement, IndicatorController, insideBox, KeyboardNavigationController, OverflowController, reportRuntime, untilReactHydrated, words, } from '@fusion.dev/helix-core';
3
+ import { caretDown, list } from '@fusion.dev/helix-icons';
4
+ import { nothing } from 'lit';
4
5
  import { property } from 'lit/decorators.js';
6
+ import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
7
+ import { html } from 'lit/static-html.js';
8
+ import '../../menu/index.js';
5
9
  const FOCUSABLE = 'a[href], button, input, select, textarea, [tabindex]:not([tabindex="-1"])';
6
10
  const isList = (element) => element.localName === 'ul' ||
7
11
  element.localName === 'ol' ||
@@ -18,6 +22,10 @@ const navigable = (item) => {
18
22
  },
19
23
  });
20
24
  };
25
+ const linkOf = (item) => item.matches('a[href]')
26
+ ? item
27
+ : item.querySelector(':scope > a[href]');
28
+ const nameOf = (link) => (link.getAttribute('aria-label') ?? link.textContent ?? '').replace(/\s+/g, ' ').trim();
21
29
  const connected = new Set();
22
30
  const warned = new WeakSet();
23
31
  let HelixHeadlessNavigation = class HelixHeadlessNavigation extends HelixElement {
@@ -30,18 +38,26 @@ let HelixHeadlessNavigation = class HelixHeadlessNavigation extends HelixElement
30
38
  const current = markOf(item)?.getAttribute('aria-current');
31
39
  return current !== null && current !== undefined && current !== 'false';
32
40
  };
33
- return (items.find((item) => markOf(item)?.getAttribute('aria-current') === 'page') ??
34
- items.find(marked));
41
+ const current = items.find((item) => markOf(item)?.getAttribute('aria-current') === 'page') ??
42
+ items.find(marked);
43
+ if (current?.hidden) {
44
+ const button = this.buttonAt(this.fold.step);
45
+ return button.named && !button.compact ? (this.partNamed('more') ?? undefined) : undefined;
46
+ }
47
+ return current;
35
48
  },
36
49
  indicator: () => this.partNamed('indicator'),
37
- observe: () => this.items,
50
+ observe: () => [...this.items, ...[this.partNamed('more')].filter((one) => one !== null)],
38
51
  surface: () => this.partNamed('nav'),
39
52
  });
40
53
  this.arrows = false;
54
+ this.currentLabel = '{name}, more destinations';
41
55
  this.fallbackLabel = 'Main';
42
56
  this.label = '';
43
57
  this.keys = new KeyboardNavigationController(this, {
44
- items: () => (this.arrows ? this.items.map(navigable) : []),
58
+ items: () => this.arrows
59
+ ? [...this.items.filter((item) => !item.hidden), ...this.shownMore].map(navigable)
60
+ : [],
45
61
  origin: 'focus',
46
62
  reserved: () => this.orientation === 'vertical' ? ['ArrowLeft', 'ArrowRight'] : ['ArrowDown', 'ArrowUp'],
47
63
  selects: () => false,
@@ -51,7 +67,21 @@ let HelixHeadlessNavigation = class HelixHeadlessNavigation extends HelixElement
51
67
  label: true,
52
68
  target: () => this.partNamed('nav'),
53
69
  });
70
+ this.moreLabel = 'More';
54
71
  this.opened = new Set();
72
+ this.hid = new WeakSet();
73
+ this.fold = new OverflowController(this, {
74
+ apply: (step) => this.applyFold(step),
75
+ box: () => (this.folds ? this.partNamed('nav') : null),
76
+ fits: (box) => {
77
+ const menu = this.partNamed('overflow');
78
+ const more = menu && !menu.hidden ? this.partNamed('more') : null;
79
+ return insideBox(box, [...this.items.filter((item) => !item.hidden), ...(more ? [more] : [])]);
80
+ },
81
+ observe: () => this.items,
82
+ ready: () => untilReactHydrated(this),
83
+ steps: () => (this.folds && this.foldOrder.length > 0 ? this.foldOrder.length + 1 : 0),
84
+ });
55
85
  this.handleToggle = (event) => {
56
86
  const target = event.target;
57
87
  if (target === this || target === null) {
@@ -65,8 +95,105 @@ let HelixHeadlessNavigation = class HelixHeadlessNavigation extends HelixElement
65
95
  }
66
96
  this.toggleState('open', this.opened.size > 0);
67
97
  };
98
+ this.handleSlotChange = () => {
99
+ this.watch();
100
+ this.fold.request();
101
+ this.requestUpdate();
102
+ };
103
+ this.keepCheck = (event) => {
104
+ if (event.target === event.currentTarget) {
105
+ event.preventDefault();
106
+ event.stopPropagation();
107
+ }
108
+ };
68
109
  }
69
110
  static { this.words = 'helix-navigation'; }
111
+ get folds() {
112
+ return this.orientation !== 'vertical' && this.overflow !== 'scroll';
113
+ }
114
+ get foldable() {
115
+ return this.items.filter((item) => linkOf(item) !== null && (!item.hidden || this.hid.has(item)));
116
+ }
117
+ get currentFoldable() {
118
+ const foldable = this.foldable;
119
+ const current = (item) => markOf(item)?.getAttribute('aria-current');
120
+ return (foldable.find((item) => current(item) === 'page') ??
121
+ foldable.find((item) => {
122
+ const value = current(item);
123
+ return value !== null && value !== undefined && value !== 'false';
124
+ }));
125
+ }
126
+ get foldOrder() {
127
+ const current = this.currentFoldable;
128
+ const rest = this.foldable.filter((item) => item !== current).reverse();
129
+ return current ? [...rest, current] : rest;
130
+ }
131
+ foldedAt(step) {
132
+ return new Set(step <= 0 ? [] : this.foldOrder.slice(0, step));
133
+ }
134
+ buttonAt(step) {
135
+ if (step === 0) {
136
+ return { compact: false, label: this.moreLabel, named: false };
137
+ }
138
+ const order = this.foldOrder;
139
+ const current = this.currentFoldable;
140
+ const compact = order.length > 0 && step > order.length;
141
+ const named = current !== undefined && order.length > 0 && step >= order.length;
142
+ const link = named && current ? linkOf(current) : null;
143
+ const name = link ? nameOf(link) : '';
144
+ return {
145
+ compact,
146
+ label: named ? formatWords(this, 'currentLabel', { name }) : this.moreLabel,
147
+ named,
148
+ };
149
+ }
150
+ get shownMore() {
151
+ const more = this.partNamed('more');
152
+ return more && this.fold.step > 0 ? [more] : [];
153
+ }
154
+ applyFold(step) {
155
+ const folded = this.foldedAt(step);
156
+ for (const item of this.foldable) {
157
+ if (folded.has(item)) {
158
+ if (!item.hidden) {
159
+ item.hidden = true;
160
+ this.hid.add(item);
161
+ }
162
+ }
163
+ else if (this.hid.has(item)) {
164
+ item.hidden = false;
165
+ this.hid.delete(item);
166
+ }
167
+ }
168
+ const { compact, label, named } = this.buttonAt(step);
169
+ const menu = this.partNamed('overflow');
170
+ if (menu) {
171
+ menu.hidden = folded.size === 0;
172
+ }
173
+ const more = this.partNamed('more');
174
+ if (more) {
175
+ more.toggleAttribute('data-current', named && !compact);
176
+ more.toggleAttribute('data-compact', compact);
177
+ if (named || compact) {
178
+ more.setAttribute('aria-label', label);
179
+ }
180
+ else {
181
+ more.removeAttribute('aria-label');
182
+ }
183
+ }
184
+ const words = this.partNamed('more-label');
185
+ const current = this.partNamed('more-current');
186
+ const caret = this.renderRoot.querySelector('slot[name="more-icon"]');
187
+ const glyph = this.renderRoot.querySelector('slot[name="menu-icon"]');
188
+ if (words && current && caret && glyph) {
189
+ words.hidden = named || compact;
190
+ current.hidden = !named || compact;
191
+ caret.hidden = compact;
192
+ glyph.hidden = !compact;
193
+ }
194
+ this.toggleState('folded', folded.size > 0);
195
+ this.indicator.place();
196
+ }
70
197
  get landmarkName() {
71
198
  const labeled = this.getAttribute('aria-labelledby');
72
199
  if (labeled) {
@@ -108,7 +235,11 @@ let HelixHeadlessNavigation = class HelixHeadlessNavigation extends HelixElement
108
235
  watch() {
109
236
  this.current?.disconnect();
110
237
  this.indicator.place();
111
- this.current = new MutationObserver(() => this.indicator.place());
238
+ this.current = new MutationObserver(() => {
239
+ this.indicator.place();
240
+ this.fold.request();
241
+ this.requestUpdate();
242
+ });
112
243
  for (const item of this.items) {
113
244
  this.current.observe(item, { attributeFilter: ['aria-current'], subtree: true });
114
245
  }
@@ -134,16 +265,90 @@ let HelixHeadlessNavigation = class HelixHeadlessNavigation extends HelixElement
134
265
  this.watch();
135
266
  this.auditTwin();
136
267
  }
268
+ willUpdate(changed) {
269
+ super.willUpdate(changed);
270
+ this.fold.request();
271
+ }
272
+ renderOverflow() {
273
+ if (!this.folds) {
274
+ return nothing;
275
+ }
276
+ const menu = this.tag('menu');
277
+ const item = this.tag('menu-item');
278
+ const step = this.fold.step;
279
+ const { compact, label, named } = this.buttonAt(step);
280
+ const current = named ? this.currentFoldable : undefined;
281
+ const currentLink = current ? linkOf(current) : null;
282
+ const currentName = currentLink ? nameOf(currentLink) : '';
283
+ const folded = step > 0 ? this.foldedAt(step) : new Set();
284
+ const rows = step > 0 ? this.foldable.filter((one) => folded.has(one)) : [];
285
+ return html `<${menu}
286
+ part="overflow"
287
+ exportparts="back: overflow-back, back-mark: overflow-back-mark, empty: overflow-empty,
288
+ header: overflow-header, live: overflow-live, loading: overflow-loading,
289
+ search-field: overflow-search-field, surface: overflow-surface,
290
+ title: overflow-title"
291
+ label=${this.moreLabel}
292
+ placement=${named || compact ? 'bottom-start' : 'bottom-end'}
293
+ ?hidden=${step === 0}
294
+ >
295
+ <button
296
+ part="more"
297
+ slot="trigger"
298
+ type="button"
299
+ aria-label=${named || compact ? label : nothing}
300
+ ?data-current=${named && !compact}
301
+ ?data-compact=${compact}
302
+ >
303
+ <span part="more-label" ?hidden=${named || compact}>${this.moreLabel}</span>
304
+ <span part="more-current" ?hidden=${!named || compact}>${currentName}</span>
305
+ <slot name="menu-icon" ?hidden=${!compact}
306
+ ><svg part="more-glyph" aria-hidden="true" data-glyph fill="currentColor" viewBox=${list.viewBox}>
307
+ ${unsafeSVG(list.content)}
308
+ </svg></slot
309
+ >
310
+ <slot name="more-icon" ?hidden=${compact}
311
+ ><svg part="more-mark" aria-hidden="true" data-glyph fill="currentColor" viewBox=${caretDown.viewBox}>
312
+ ${unsafeSVG(caretDown.content)}
313
+ </svg></slot
314
+ >
315
+ </button>
316
+ ${rows.map((one) => {
317
+ const link = linkOf(one);
318
+ return html `<${item}
319
+ part="overflow-option"
320
+ exportparts="end: overflow-option-end, item: overflow-option-item, mark: overflow-option-mark,
321
+ shortcut: overflow-option-shortcut, shortcut-kbd: overflow-option-shortcut-kbd,
322
+ shortcut-key: overflow-option-shortcut-key, shortcut-separator: overflow-option-shortcut-separator,
323
+ start: overflow-option-start, surface: overflow-option-surface"
324
+ href=${link.getAttribute('href') ?? ''}
325
+ target=${link.getAttribute('target') ?? nothing}
326
+ rel=${link.getAttribute('rel') ?? nothing}
327
+ ?checkable=${named}
328
+ ?exclusive=${named}
329
+ ?checked=${one === current}
330
+ @helix-before-check=${this.keepCheck}
331
+ >
332
+ ${nameOf(link)}
333
+ </${item}>`;
334
+ })}
335
+ </${menu}>`;
336
+ }
137
337
  render() {
138
338
  return html `<nav part="nav" aria-label=${this.label || this.fallbackLabel}>
139
339
  <div part="indicator" aria-hidden="true"><slot name="indicator"></slot></div>
140
- <slot @slotchange=${() => this.watch()}></slot>
340
+ <slot @slotchange=${this.handleSlotChange}></slot>
341
+ ${this.renderOverflow()}
141
342
  </nav>`;
142
343
  }
143
344
  };
144
345
  __decorate([
145
346
  property({ type: Boolean })
146
347
  ], HelixHeadlessNavigation.prototype, "arrows", void 0);
348
+ __decorate([
349
+ words,
350
+ property({ attribute: 'current-label' })
351
+ ], HelixHeadlessNavigation.prototype, "currentLabel", void 0);
147
352
  __decorate([
148
353
  words,
149
354
  property({ attribute: 'fallback-label' })
@@ -154,9 +359,16 @@ __decorate([
154
359
  __decorate([
155
360
  property({ reflect: true })
156
361
  ], HelixHeadlessNavigation.prototype, "marker", void 0);
362
+ __decorate([
363
+ words,
364
+ property({ attribute: 'more-label' })
365
+ ], HelixHeadlessNavigation.prototype, "moreLabel", void 0);
157
366
  __decorate([
158
367
  property({ reflect: true })
159
368
  ], HelixHeadlessNavigation.prototype, "orientation", void 0);
369
+ __decorate([
370
+ property({ reflect: true })
371
+ ], HelixHeadlessNavigation.prototype, "overflow", void 0);
160
372
  HelixHeadlessNavigation = __decorate([
161
373
  customElement('helix-headless-navigation')
162
374
  ], HelixHeadlessNavigation);