openproject-primer_view_components 0.96.0 → 0.97.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/app/assets/javascripts/primer_view_components.js +5 -5
- data/app/assets/javascripts/primer_view_components.js.map +1 -1
- data/app/assets/styles/primer_view_components.css +1 -1
- data/app/assets/styles/primer_view_components.css.map +1 -1
- data/app/components/primer/alpha/action_list/item.rb +2 -1
- data/app/components/primer/alpha/action_list.css +1 -1
- data/app/components/primer/alpha/action_list.css.json +9 -22
- data/app/components/primer/alpha/action_list.css.map +1 -1
- data/app/components/primer/alpha/action_list.html.erb +1 -5
- data/app/components/primer/alpha/action_list.pcss +43 -81
- data/app/components/primer/alpha/action_list.rb +9 -7
- data/app/components/primer/alpha/action_menu/action_menu_element.js +84 -234
- data/app/components/primer/alpha/action_menu/action_menu_element.ts +92 -273
- data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.js +1 -1
- data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.ts +1 -1
- data/app/components/primer/alpha/action_menu/menu_loader.d.ts +21 -0
- data/app/components/primer/alpha/action_menu/menu_loader.js +221 -0
- data/app/components/primer/alpha/action_menu/menu_loader.ts +269 -0
- data/lib/primer/view_components/version.rb +1 -1
- data/previews/primer/alpha/action_list_preview.rb +46 -1
- data/previews/primer/alpha/action_menu_preview/sub_menus.html.erb +1 -1
- data/previews/primer/alpha/action_menu_preview.rb +5 -2
- data/static/arguments.json +2 -2
- data/static/classes.json +3 -3
- data/static/classnames.cjs +1 -1
- data/static/classnames.js +1 -1
- data/static/info_arch.json +21 -2
- data/static/previews.json +13 -0
- metadata +4 -1
|
@@ -5,6 +5,7 @@ import AnchoredPositionElement from '../../anchored_position'
|
|
|
5
5
|
import {observeMutationsUntilConditionMet} from '../../utils'
|
|
6
6
|
import {ActionMenuFocusZoneStack} from './action_menu_focus_zone_stack'
|
|
7
7
|
import {ClipboardCopyElement} from '@github/clipboard-copy-element'
|
|
8
|
+
import {MenuLoader} from './menu_loader'
|
|
8
9
|
|
|
9
10
|
type SelectVariant = 'none' | 'single' | 'multiple' | null
|
|
10
11
|
type SelectedItem = {
|
|
@@ -13,8 +14,6 @@ type SelectedItem = {
|
|
|
13
14
|
element: Element
|
|
14
15
|
}
|
|
15
16
|
|
|
16
|
-
type LoaderRequest = Readonly<{loader: IncludeFragmentElement; src: string}>
|
|
17
|
-
|
|
18
17
|
@controller('action-menu')
|
|
19
18
|
export class ActionMenuElement extends HTMLElement {
|
|
20
19
|
@target includeFragment: IncludeFragmentElement
|
|
@@ -30,19 +29,8 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
30
29
|
#intersectionObserver: IntersectionObserver
|
|
31
30
|
#focusZoneStack: ActionMenuFocusZoneStack
|
|
32
31
|
|
|
33
|
-
//
|
|
34
|
-
|
|
35
|
-
// The src is recorded because the loader's attribute is configuration a
|
|
36
|
-
// morph may rewrite while the request is in flight; the record is what was
|
|
37
|
-
// requested, so a rewritten loader cannot pass an old response off as new.
|
|
38
|
-
#request: LoaderRequest | null = null
|
|
39
|
-
// Every primary loader this element created or adopted, so completions of
|
|
40
|
-
// superseded ones can be told apart from sub-menu fragments.
|
|
41
|
-
#ownedLoaders = new WeakSet<IncludeFragmentElement>()
|
|
42
|
-
// Loaders whose response had no menu items, with the body they left
|
|
43
|
-
// empty. That body staying empty is not a reason to load again before the
|
|
44
|
-
// next open; a body other code puts in its place is.
|
|
45
|
-
#emptyLoaders = new WeakMap<IncludeFragmentElement, HTMLElement>()
|
|
32
|
+
// Loads a deferred primary menu's items; see MenuLoader
|
|
33
|
+
#loader: MenuLoader
|
|
46
34
|
|
|
47
35
|
#scrollUpdater = () => {
|
|
48
36
|
if (this.#isOpen()) {
|
|
@@ -56,8 +44,24 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
56
44
|
// Never disconnected: it observes the element's own subtree, so it keeps
|
|
57
45
|
// working while the element is detached. A local, not a field: the
|
|
58
46
|
// registration keeps it alive as long as the element is.
|
|
47
|
+
this.#loader = new MenuLoader({
|
|
48
|
+
element: this,
|
|
49
|
+
body: () => this.overlayBody,
|
|
50
|
+
template: () => this.loaderTemplate,
|
|
51
|
+
itemSelector: ActionMenuElement.menuItemSelectors.join(','),
|
|
52
|
+
isOpen: () => this.#isOpen(),
|
|
53
|
+
onLoadStart: (fragment, keepOpen) => {
|
|
54
|
+
// The response decides the form field name
|
|
55
|
+
this.#inputName = ''
|
|
56
|
+
if (keepOpen) this.#keepOpenWhileLoading(fragment)
|
|
57
|
+
},
|
|
58
|
+
onLoaded: () => this.#handleIncludeFragmentReplaced(),
|
|
59
|
+
})
|
|
60
|
+
|
|
59
61
|
const observer = new MutationObserver(records => {
|
|
60
|
-
|
|
62
|
+
// A replaced overlay may have taken open sub-menus with it
|
|
63
|
+
this.#pruneFocusZones()
|
|
64
|
+
this.#loader.mutated(records)
|
|
61
65
|
this.#updateLevels()
|
|
62
66
|
})
|
|
63
67
|
observer.observe(this, {childList: true, subtree: true})
|
|
@@ -161,14 +165,20 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
161
165
|
|
|
162
166
|
connectedCallback() {
|
|
163
167
|
this.#bindHostListeners()
|
|
164
|
-
|
|
165
|
-
|
|
168
|
+
// Kept inputs stand for the selection until a response replaces them;
|
|
169
|
+
// the body holds no items to sync from meanwhile
|
|
170
|
+
if (!this.#loader.keepingInputs) {
|
|
171
|
+
this.#setDynamicLabel()
|
|
172
|
+
this.#updateInput()
|
|
173
|
+
}
|
|
166
174
|
this.#softDisableItems()
|
|
167
175
|
this.#setupSubMenuLoading()
|
|
168
|
-
this.#sync()
|
|
176
|
+
this.#loader.sync()
|
|
169
177
|
this.#observeInvokerPosition()
|
|
170
178
|
|
|
171
|
-
if (this.#
|
|
179
|
+
if (this.#loader.deferred) {
|
|
180
|
+
this.#loader.updateReady()
|
|
181
|
+
} else if (!this.includeFragment?.isConnected) {
|
|
172
182
|
this.setAttribute('data-ready', 'true')
|
|
173
183
|
}
|
|
174
184
|
|
|
@@ -183,14 +193,13 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
183
193
|
}
|
|
184
194
|
|
|
185
195
|
attributeChangedCallback(name: string) {
|
|
186
|
-
if (name === 'src') this.#sync()
|
|
196
|
+
if (name === 'src') this.#loader.sync()
|
|
187
197
|
}
|
|
188
198
|
|
|
189
199
|
// Forgets the current request so the menu loads again at the current src:
|
|
190
200
|
// at once when open or preloaded, otherwise when it next opens.
|
|
191
201
|
refresh() {
|
|
192
|
-
this.#
|
|
193
|
-
this.#sync()
|
|
202
|
+
this.#loader.refresh()
|
|
194
203
|
}
|
|
195
204
|
|
|
196
205
|
#bindHostListeners() {
|
|
@@ -206,7 +215,7 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
206
215
|
|
|
207
216
|
// A static primary menu with a deferred sub-menu
|
|
208
217
|
#setupSubMenuLoading() {
|
|
209
|
-
if (this.#deferred || !this.includeFragment) return
|
|
218
|
+
if (this.#loader.deferred || !this.includeFragment) return
|
|
210
219
|
|
|
211
220
|
this.includeFragment.addEventListener('include-fragment-replaced', this, {
|
|
212
221
|
signal: this.#abortController.signal,
|
|
@@ -227,202 +236,6 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
227
236
|
}
|
|
228
237
|
}
|
|
229
238
|
|
|
230
|
-
// A primary menu with `src` renders its loader as a template
|
|
231
|
-
get #deferred(): boolean {
|
|
232
|
-
return Boolean(this.loaderTemplate)
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
get #preload(): boolean {
|
|
236
|
-
return this.loaderTemplate?.content.firstElementChild?.getAttribute('loading') === 'eager'
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// The primary overlay's body. Looked up on every call, since other code may
|
|
240
|
-
// replace the body or the overlay.
|
|
241
|
-
get #body(): HTMLElement | null {
|
|
242
|
-
return this.overlayBody ?? null
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// The one rule: a load starts when there is no current request, when the
|
|
246
|
-
// host src differs from the current request's, or when the body holds no
|
|
247
|
-
// element (other code emptied it, or the last response was empty); the
|
|
248
|
-
// current loader still in the body is retried. A closed lazy menu waits
|
|
249
|
-
// for the open moment (`opening`) instead.
|
|
250
|
-
#sync(opening = false) {
|
|
251
|
-
const src = (this.getAttribute('src') ?? '').trim()
|
|
252
|
-
if (!this.#deferred || !src) return
|
|
253
|
-
|
|
254
|
-
if (!(opening || this.#isOpen() || this.#preload)) {
|
|
255
|
-
// A pending or failed loader for another src must not render meanwhile;
|
|
256
|
-
// a completed one is kept, so setting its src again does not reload
|
|
257
|
-
if (this.#request && src !== this.#request.src && this.#request.loader.parentElement === this.#body) {
|
|
258
|
-
this.#invalidate()
|
|
259
|
-
}
|
|
260
|
-
return
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const loader = this.#request?.loader
|
|
264
|
-
if (!this.#request || src !== this.#request.src || !this.#body?.firstElementChild) {
|
|
265
|
-
this.#load(src, opening)
|
|
266
|
-
} else if (loader?.isConnected) {
|
|
267
|
-
// A retry fetches the loader's own src, which a morph may have rewritten
|
|
268
|
-
if ((loader.getAttribute('src') ?? '').trim() === this.#request.src) {
|
|
269
|
-
loader.classList.remove('is-error')
|
|
270
|
-
loader.refetch()
|
|
271
|
-
} else {
|
|
272
|
-
this.#load(src, opening)
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
// Drops the current request. Its loader is removed even while the menu is
|
|
278
|
-
// detached, since upstream still completes the fetch there.
|
|
279
|
-
#invalidate() {
|
|
280
|
-
this.#request?.loader.remove()
|
|
281
|
-
this.#request = null
|
|
282
|
-
this.#updateReady()
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
// Starts a request for src with a fresh loader from the template, which
|
|
286
|
-
// replaces whatever the body holds.
|
|
287
|
-
#load(src: string, opening = false) {
|
|
288
|
-
const body = this.#body
|
|
289
|
-
const template = this.loaderTemplate?.content.firstElementChild
|
|
290
|
-
if (!body || !template) return
|
|
291
|
-
|
|
292
|
-
// Imported, not cloned, so the loader is upgraded before it connects
|
|
293
|
-
const loader = this.ownerDocument.importNode(template, true) as IncludeFragmentElement
|
|
294
|
-
loader.setAttribute('src', src)
|
|
295
|
-
loader.setAttribute('loading', 'eager')
|
|
296
|
-
|
|
297
|
-
this.#request = {loader, src}
|
|
298
|
-
this.#ownedLoaders.add(loader)
|
|
299
|
-
this.#listen(loader)
|
|
300
|
-
// The response decides the form field name
|
|
301
|
-
this.#inputName = ''
|
|
302
|
-
this.#keepInputs(body)
|
|
303
|
-
|
|
304
|
-
body.prepend(loader)
|
|
305
|
-
if (opening || this.#isOpen()) {
|
|
306
|
-
this.#keepOpenWhileLoading(loader)
|
|
307
|
-
// A focus handler may have started a newer load meanwhile
|
|
308
|
-
if (this.#request?.loader !== loader) return
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
for (const child of Array.from(body.childNodes)) {
|
|
312
|
-
if (child !== loader) child.remove()
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
this.#updateReady()
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
// Keeps the current form inputs submitting while a load replaces the body,
|
|
319
|
-
// also when it fails: they move to the host until a response brings its own
|
|
320
|
-
#keepInputs(body: HTMLElement) {
|
|
321
|
-
const inputs = body.querySelector('[data-list-inputs]')
|
|
322
|
-
if (!inputs) return
|
|
323
|
-
|
|
324
|
-
this.#dropKeptInputs()
|
|
325
|
-
this.append(inputs)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
#dropKeptInputs() {
|
|
329
|
-
for (const inputs of this.querySelectorAll(':scope > [data-list-inputs]')) inputs.remove()
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// Loaders are never reused, so the listeners need no lifetime of their own
|
|
333
|
-
#listen(fragment: IncludeFragmentElement) {
|
|
334
|
-
fragment.addEventListener('include-fragment-replace', this)
|
|
335
|
-
fragment.addEventListener('include-fragment-replaced', this)
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
// No primary loader still waiting in the body and, for a static menu, no
|
|
339
|
-
// sub-menu fragment. A deferred menu's sub-menus load when they open.
|
|
340
|
-
#noLoadPending(): boolean {
|
|
341
|
-
if (!this.#deferred && this.includeFragment?.isConnected) return false
|
|
342
|
-
|
|
343
|
-
const loader = this.#request?.loader
|
|
344
|
-
return !loader || loader.parentElement !== this.#body
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// `data-ready` means no load is pending
|
|
348
|
-
#updateReady() {
|
|
349
|
-
if (this.#noLoadPending()) {
|
|
350
|
-
this.setAttribute('data-ready', 'true')
|
|
351
|
-
} else {
|
|
352
|
-
this.removeAttribute('data-ready')
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
// Picks up loaders other code inserts into the body (the last one wins, so
|
|
357
|
-
// a loader appended next to a pending one supersedes it), and loads again
|
|
358
|
-
// when other code empties the body, such as a morph of the server markup,
|
|
359
|
-
// which renders the body empty.
|
|
360
|
-
#onBodyMutation(records: MutationRecord[]) {
|
|
361
|
-
// A replaced overlay may have taken open sub-menus with it
|
|
362
|
-
this.#pruneFocusZones()
|
|
363
|
-
|
|
364
|
-
if (!this.#deferred) return
|
|
365
|
-
|
|
366
|
-
const body = this.#body
|
|
367
|
-
if (!body) return
|
|
368
|
-
|
|
369
|
-
if (records.some(record => record.addedNodes.length > 0)) {
|
|
370
|
-
const fragments = Array.from(body.querySelectorAll<IncludeFragmentElement>(':scope > include-fragment')).filter(
|
|
371
|
-
fragment => !this.#ownedLoaders.has(fragment),
|
|
372
|
-
)
|
|
373
|
-
const fragment = fragments.at(-1)
|
|
374
|
-
if (fragment) this.#adopt(fragment)
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// An empty body was emptied or replaced by other code, unless it is the
|
|
378
|
-
// one the current loader left empty; loading again there would loop on a
|
|
379
|
-
// preloaded menu, so that one waits for the next open
|
|
380
|
-
const loader = this.#request?.loader
|
|
381
|
-
if (!body.firstElementChild && !(loader && this.#emptyLoaders.get(loader) === body)) this.#sync()
|
|
382
|
-
|
|
383
|
-
this.#updateReady()
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
// Makes an inserted loader the current request. Its src is reflected onto
|
|
387
|
-
// the host, so the rule sees the request as current: an open or preloaded
|
|
388
|
-
// menu loads it at once (upstream's busy guard drops the duplicate for an
|
|
389
|
-
// eager loader), a closed lazy one when it opens, unless the loader is eager
|
|
390
|
-
// and has already started itself. Idiomorph inserts a loader
|
|
391
|
-
// before setting its attributes, but sets them synchronously, so the loader
|
|
392
|
-
// arrives here with its src; one that gets its src only in a later task is
|
|
393
|
-
// replaced with a load at the host src. The template is not touched.
|
|
394
|
-
#adopt(fragment: IncludeFragmentElement) {
|
|
395
|
-
const body = this.#body
|
|
396
|
-
if (!body) return
|
|
397
|
-
|
|
398
|
-
const src = (fragment.getAttribute('src') ?? '').trim()
|
|
399
|
-
|
|
400
|
-
this.#request = {loader: fragment, src}
|
|
401
|
-
this.#ownedLoaders.add(fragment)
|
|
402
|
-
this.#listen(fragment)
|
|
403
|
-
// The response decides the form field name
|
|
404
|
-
this.#inputName = ''
|
|
405
|
-
this.#keepInputs(body)
|
|
406
|
-
|
|
407
|
-
if (this.#isOpen()) {
|
|
408
|
-
this.#keepOpenWhileLoading(fragment)
|
|
409
|
-
// A focus handler may have started a newer load meanwhile
|
|
410
|
-
if (this.#request?.loader !== fragment) return
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
for (const child of Array.from(body.childNodes)) {
|
|
414
|
-
if (child !== fragment) child.remove()
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
if (src && src !== this.getAttribute('src')) {
|
|
418
|
-
this.setAttribute('src', src)
|
|
419
|
-
} else if (this.#isOpen() || this.#preload) {
|
|
420
|
-
// An unchanged src does not run the rule, and a lazy loader inserted
|
|
421
|
-
// before its attributes never starts on its own
|
|
422
|
-
this.#sync()
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
239
|
// Keeps the open (or opening) menu open while fragment loads into its
|
|
427
240
|
// body. Open sub-menus are torn down first, since their toggle events
|
|
428
241
|
// arrive after the nodes are gone, and focus moves to the fragment's
|
|
@@ -588,36 +401,8 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
588
401
|
return
|
|
589
402
|
}
|
|
590
403
|
|
|
591
|
-
if (event.type === 'include-fragment-replace') {
|
|
592
|
-
const loader = event.target as IncludeFragmentElement
|
|
593
|
-
if (loader !== this.#request?.loader) return
|
|
594
|
-
|
|
595
|
-
// Other code removed the current loader before it completed, eg. by
|
|
596
|
-
// emptying the body, or rewrote its src, eg. by a morph: its response is
|
|
597
|
-
// not the one requested and must not render or mark the menu ready
|
|
598
|
-
const body = this.#body
|
|
599
|
-
if (!body || loader.parentElement !== body || (loader.getAttribute('src') ?? '').trim() !== this.#request.src) {
|
|
600
|
-
event.preventDefault()
|
|
601
|
-
return
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
// A response without menu items is discarded, so the body stays empty
|
|
605
|
-
// and the menu loads again when it next opens
|
|
606
|
-
const fragment: unknown = event instanceof CustomEvent ? event.detail?.fragment : null
|
|
607
|
-
if (!(fragment instanceof DocumentFragment)) return
|
|
608
|
-
|
|
609
|
-
if (!fragment.querySelector(ActionMenuElement.menuItemSelectors.join(','))) {
|
|
610
|
-
fragment.replaceChildren()
|
|
611
|
-
this.#emptyLoaders.set(loader, body)
|
|
612
|
-
}
|
|
613
|
-
return
|
|
614
|
-
}
|
|
615
|
-
|
|
616
404
|
if (event.type === 'include-fragment-replaced') {
|
|
617
|
-
// A
|
|
618
|
-
const loader = event.target as IncludeFragmentElement
|
|
619
|
-
if (this.#ownedLoaders.has(loader) && loader !== this.#request?.loader) return
|
|
620
|
-
|
|
405
|
+
// A static menu's deferred sub-menu
|
|
621
406
|
this.#handleIncludeFragmentReplaced()
|
|
622
407
|
return
|
|
623
408
|
}
|
|
@@ -630,6 +415,8 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
630
415
|
#handleItemKeyboardEvent(event: KeyboardEvent, item: HTMLElement) {
|
|
631
416
|
switch (event.key) {
|
|
632
417
|
case 'ArrowRight': {
|
|
418
|
+
if (item.getAttribute('aria-disabled')) break
|
|
419
|
+
|
|
633
420
|
const subMenu = this.#subMenuForItem(item)
|
|
634
421
|
subMenu?.showPopover()
|
|
635
422
|
break
|
|
@@ -649,7 +436,7 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
649
436
|
#handleBeforeToggleEvent(event: ToggleEvent) {
|
|
650
437
|
if (event.newState === 'open') {
|
|
651
438
|
// A closed lazy menu catches up, and a failed load retries, as it opens
|
|
652
|
-
if (event.target === this.overlay) this.#sync(true)
|
|
439
|
+
if (event.target === this.overlay) this.#loader.sync(true)
|
|
653
440
|
return
|
|
654
441
|
}
|
|
655
442
|
|
|
@@ -683,19 +470,26 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
683
470
|
this.#focusZoneStack.push(menu, {trapFocus: !isPrimaryMenu})
|
|
684
471
|
|
|
685
472
|
// Picks up a src set by a beforetoggle listener that ran after ours
|
|
686
|
-
if (isPrimaryMenu) this.#sync(true)
|
|
473
|
+
if (isPrimaryMenu) this.#loader.sync(true)
|
|
687
474
|
|
|
688
475
|
window.requestAnimationFrame(() => {
|
|
689
476
|
const firstItem = menu.querySelector(ActionMenuElement.menuItemSelectors.join(',')) as HTMLElement | null
|
|
690
477
|
firstItem?.focus()
|
|
691
478
|
})
|
|
692
|
-
} else if (menu === this.overlay && !document.querySelector(':popover-open')) {
|
|
479
|
+
} else if (menu === this.overlay && !document.querySelector(':popover-open') && this.#focusLostTo(menu)) {
|
|
693
480
|
// A light-dismissed menu (e.g. a click outside it) leaves focus on the body;
|
|
694
481
|
// hand it back to the invoker so the menu stays reachable from the keyboard.
|
|
695
482
|
this.invokerElement?.focus()
|
|
696
483
|
}
|
|
697
484
|
}
|
|
698
485
|
|
|
486
|
+
// Focus left on the body, or still in the closed menu. Focus that other code
|
|
487
|
+
// moved elsewhere, eg. a consumer's completion listener, stays there.
|
|
488
|
+
#focusLostTo(menu: HTMLElement): boolean {
|
|
489
|
+
const active = document.activeElement
|
|
490
|
+
return !active || active === document.body || menu.contains(active)
|
|
491
|
+
}
|
|
492
|
+
|
|
699
493
|
// The primary menu or a sub-menu, or null for popovers that are not menus
|
|
700
494
|
// (e.g. tooltips inside the menu)
|
|
701
495
|
#toggledMenu(event: ToggleEvent): AnchoredPositionElement | null {
|
|
@@ -802,31 +596,56 @@ export class ActionMenuElement extends HTMLElement {
|
|
|
802
596
|
}
|
|
803
597
|
|
|
804
598
|
#handleIncludeFragmentReplaced() {
|
|
805
|
-
//
|
|
806
|
-
|
|
807
|
-
const firstItem = this.#firstItem
|
|
808
|
-
|
|
809
|
-
if (firstItem) {
|
|
810
|
-
firstItem.focus()
|
|
811
|
-
} else if (this.#isOpen()) {
|
|
812
|
-
// Nothing to focus in an empty response: close and return to the
|
|
813
|
-
// button.
|
|
814
|
-
this.overlay.hidePopover()
|
|
815
|
-
this.invokerElement?.focus()
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
this.overlay?.update()
|
|
819
|
-
}
|
|
820
|
-
if (this.#deferred) this.#dropKeptInputs()
|
|
599
|
+
// Selection first: focusing afterwards can run a consumer handler that
|
|
600
|
+
// starts another load
|
|
821
601
|
this.#setDynamicLabel()
|
|
822
602
|
this.#updateInput()
|
|
823
603
|
this.#softDisableItems()
|
|
824
604
|
|
|
825
|
-
//
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
605
|
+
// Focus moves in a microtask: after the list's focus zone has processed
|
|
606
|
+
// the new items (its observer would otherwise drop the focused item's
|
|
607
|
+
// tab stop), and before the focusout frame would close the menu
|
|
608
|
+
const activeOnLoad = document.activeElement
|
|
609
|
+
queueMicrotask(() => this.#focusLoadedMenu(activeOnLoad))
|
|
610
|
+
|
|
611
|
+
// A deferred menu's loader decides readiness
|
|
612
|
+
if (!this.#loader.deferred) this.setAttribute('data-ready', 'true')
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
#focusLoadedMenu(activeOnLoad: Element | null) {
|
|
616
|
+
// Content can finish loading while the menu is detached
|
|
617
|
+
if (!this.isConnected) return
|
|
618
|
+
|
|
619
|
+
// A consumer's completion listener, or a load it started, moved focus
|
|
620
|
+
// meanwhile: keep it there
|
|
621
|
+
const active = document.activeElement
|
|
622
|
+
if (active !== activeOnLoad) {
|
|
623
|
+
this.#restoreTabStop(active)
|
|
624
|
+
return
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const firstItem = this.#firstItem
|
|
628
|
+
|
|
629
|
+
if (firstItem) {
|
|
630
|
+
firstItem.focus()
|
|
631
|
+
} else if (this.#isOpen()) {
|
|
632
|
+
// Nothing to focus in an empty response: close and return to the
|
|
633
|
+
// button.
|
|
634
|
+
this.overlay.hidePopover()
|
|
635
|
+
this.invokerElement?.focus()
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
this.overlay?.update()
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// An item focused before the focus zone processed it lost its tab stop
|
|
642
|
+
#restoreTabStop(element: Element | null) {
|
|
643
|
+
if (
|
|
644
|
+
element instanceof HTMLElement &&
|
|
645
|
+
this.overlay?.contains(element) &&
|
|
646
|
+
element.matches(ActionMenuElement.validSelectors.join(','))
|
|
647
|
+
) {
|
|
648
|
+
element.setAttribute('tabindex', '0')
|
|
830
649
|
}
|
|
831
650
|
}
|
|
832
651
|
|
|
@@ -69,7 +69,7 @@ _ActionMenuFocusZoneStack_stack = new WeakMap(), _ActionMenuFocusZoneStack_insta
|
|
|
69
69
|
return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").some(entry => entry.element === element);
|
|
70
70
|
}, _ActionMenuFocusZoneStack_setupFocusZone = function _ActionMenuFocusZoneStack_setupFocusZone(containerEl, trapFocus) {
|
|
71
71
|
const focusZoneAbortController = focusZone(containerEl, {
|
|
72
|
-
bindKeys: FocusKeys.ArrowVertical | FocusKeys.
|
|
72
|
+
bindKeys: FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd | FocusKeys.PageUpDown | FocusKeys.Backspace,
|
|
73
73
|
focusOutBehavior: 'wrap',
|
|
74
74
|
focusableElementFilter: (element) => {
|
|
75
75
|
return this.elementIsMenuItem(element) && element.closest('anchored-position') === containerEl;
|
|
@@ -65,7 +65,7 @@ export class ActionMenuFocusZoneStack {
|
|
|
65
65
|
|
|
66
66
|
#setupFocusZone(containerEl: AnchoredPositionElement, trapFocus: boolean): AbortController {
|
|
67
67
|
const focusZoneAbortController = focusZone(containerEl, {
|
|
68
|
-
bindKeys: FocusKeys.ArrowVertical | FocusKeys.
|
|
68
|
+
bindKeys: FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd | FocusKeys.PageUpDown | FocusKeys.Backspace,
|
|
69
69
|
focusOutBehavior: 'wrap',
|
|
70
70
|
|
|
71
71
|
focusableElementFilter: (element: HTMLElement): boolean => {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { IncludeFragmentElement } from '@github/include-fragment-element';
|
|
2
|
+
export type MenuLoaderHost = {
|
|
3
|
+
element: HTMLElement;
|
|
4
|
+
body(): HTMLElement | undefined;
|
|
5
|
+
template(): HTMLTemplateElement | undefined;
|
|
6
|
+
itemSelector: string;
|
|
7
|
+
isOpen(): boolean;
|
|
8
|
+
onLoadStart(fragment: IncludeFragmentElement, keepOpen: boolean): void;
|
|
9
|
+
onLoaded(): void;
|
|
10
|
+
};
|
|
11
|
+
export declare class MenuLoader {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(host: MenuLoaderHost);
|
|
14
|
+
get deferred(): boolean;
|
|
15
|
+
get pending(): boolean;
|
|
16
|
+
updateReady(): void;
|
|
17
|
+
sync(opening?: boolean): void;
|
|
18
|
+
refresh(): void;
|
|
19
|
+
mutated(records: MutationRecord[]): void;
|
|
20
|
+
get keepingInputs(): boolean;
|
|
21
|
+
}
|