openproject-primer_view_components 0.95.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.
Files changed (183) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +32 -0
  3. data/app/assets/javascripts/primer_view_components.js +11 -1
  4. data/app/assets/javascripts/primer_view_components.js.map +1 -1
  5. data/app/assets/styles/primer_view_components.css +1 -1
  6. data/app/assets/styles/primer_view_components.css.map +1 -1
  7. data/app/components/primer/alpha/action_bar.css +1 -1
  8. data/app/components/primer/alpha/action_bar.css.map +1 -1
  9. data/app/components/primer/alpha/action_list/item.rb +7 -2
  10. data/app/components/primer/alpha/action_list.css +1 -1
  11. data/app/components/primer/alpha/action_list.css.json +16 -27
  12. data/app/components/primer/alpha/action_list.css.map +1 -1
  13. data/app/components/primer/alpha/action_list.html.erb +1 -5
  14. data/app/components/primer/alpha/action_list.pcss +55 -81
  15. data/app/components/primer/alpha/action_list.rb +9 -7
  16. data/app/components/primer/alpha/action_menu/action_menu_element.d.ts +6 -0
  17. data/app/components/primer/alpha/action_menu/action_menu_element.js +239 -84
  18. data/app/components/primer/alpha/action_menu/action_menu_element.ts +261 -81
  19. data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.d.ts +4 -1
  20. data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.js +38 -14
  21. data/app/components/primer/alpha/action_menu/action_menu_focus_zone_stack.ts +42 -13
  22. data/app/components/primer/alpha/action_menu/menu.html.erb +27 -19
  23. data/app/components/primer/alpha/action_menu/menu.rb +7 -0
  24. data/app/components/primer/alpha/action_menu/menu_loader.d.ts +21 -0
  25. data/app/components/primer/alpha/action_menu/menu_loader.js +221 -0
  26. data/app/components/primer/alpha/action_menu/menu_loader.ts +269 -0
  27. data/app/components/primer/alpha/action_menu/primary_menu.rb +15 -0
  28. data/app/components/primer/alpha/action_menu.rb +55 -0
  29. data/app/components/primer/alpha/auto_complete.css +1 -1
  30. data/app/components/primer/alpha/auto_complete.css.map +1 -1
  31. data/app/components/primer/alpha/banner.css +1 -1
  32. data/app/components/primer/alpha/banner.css.map +1 -1
  33. data/app/components/primer/alpha/button_marketing.css +1 -1
  34. data/app/components/primer/alpha/button_marketing.css.map +1 -1
  35. data/app/components/primer/alpha/dialog.css +1 -1
  36. data/app/components/primer/alpha/dialog.css.map +1 -1
  37. data/app/components/primer/alpha/dropdown/menu.rb +1 -1
  38. data/app/components/primer/alpha/dropdown.css +1 -1
  39. data/app/components/primer/alpha/dropdown.css.json +1 -1
  40. data/app/components/primer/alpha/dropdown.css.map +1 -1
  41. data/app/components/primer/alpha/layout.css +1 -1
  42. data/app/components/primer/alpha/layout.css.json +3 -3
  43. data/app/components/primer/alpha/layout.css.map +1 -1
  44. data/app/components/primer/alpha/menu.css +1 -1
  45. data/app/components/primer/alpha/menu.css.json +3 -3
  46. data/app/components/primer/alpha/menu.css.map +1 -1
  47. data/app/components/primer/alpha/overlay.css +1 -1
  48. data/app/components/primer/alpha/overlay.css.map +1 -1
  49. data/app/components/primer/alpha/segmented_control.css +1 -1
  50. data/app/components/primer/alpha/segmented_control.css.json +1 -1
  51. data/app/components/primer/alpha/segmented_control.css.map +1 -1
  52. data/app/components/primer/alpha/select_panel.css +1 -1
  53. data/app/components/primer/alpha/select_panel.css.map +1 -1
  54. data/app/components/primer/alpha/skeleton_box.css +1 -1
  55. data/app/components/primer/alpha/skeleton_box.css.map +1 -1
  56. data/app/components/primer/alpha/stack.css +1 -1
  57. data/app/components/primer/alpha/stack.css.map +1 -1
  58. data/app/components/primer/alpha/stack_item.css +1 -1
  59. data/app/components/primer/alpha/stack_item.css.map +1 -1
  60. data/app/components/primer/alpha/tab_nav.css +1 -1
  61. data/app/components/primer/alpha/tab_nav.css.map +1 -1
  62. data/app/components/primer/alpha/text_field.css +1 -1
  63. data/app/components/primer/alpha/text_field.css.map +1 -1
  64. data/app/components/primer/alpha/toggle_switch.css +1 -1
  65. data/app/components/primer/alpha/toggle_switch.css.map +1 -1
  66. data/app/components/primer/alpha/tree_view.css +1 -1
  67. data/app/components/primer/alpha/tree_view.css.json +3 -3
  68. data/app/components/primer/alpha/tree_view.css.map +1 -1
  69. data/app/components/primer/alpha/underline_nav.css +1 -1
  70. data/app/components/primer/alpha/underline_nav.css.map +1 -1
  71. data/app/components/primer/beta/border_box.css +1 -1
  72. data/app/components/primer/beta/border_box.css.json +5 -5
  73. data/app/components/primer/beta/border_box.css.map +1 -1
  74. data/app/components/primer/beta/button.css +1 -1
  75. data/app/components/primer/beta/button.css.map +1 -1
  76. data/app/components/primer/beta/button.pcss +97 -0
  77. data/app/components/primer/beta/button.rb +15 -3
  78. data/app/components/primer/beta/counter.css +1 -1
  79. data/app/components/primer/beta/counter.css.map +1 -1
  80. data/app/components/primer/beta/flash.css +1 -1
  81. data/app/components/primer/beta/flash.css.map +1 -1
  82. data/app/components/primer/beta/link.css +1 -1
  83. data/app/components/primer/beta/link.css.json +1 -1
  84. data/app/components/primer/beta/link.css.map +1 -1
  85. data/app/components/primer/beta/popover.css +1 -1
  86. data/app/components/primer/beta/popover.css.map +1 -1
  87. data/app/components/primer/beta/progress_bar.css +1 -1
  88. data/app/components/primer/beta/progress_bar.css.map +1 -1
  89. data/app/components/primer/beta/relative_time.rb +7 -1
  90. data/app/components/primer/beta/state.css +1 -1
  91. data/app/components/primer/beta/state.css.json +2 -2
  92. data/app/components/primer/beta/state.css.map +1 -1
  93. data/app/components/primer/beta/timeline_item.css +1 -1
  94. data/app/components/primer/beta/timeline_item.css.map +1 -1
  95. data/app/components/primer/open_project/border_grid.css +1 -1
  96. data/app/components/primer/open_project/border_grid.css.map +1 -1
  97. data/app/components/primer/open_project/color_field.css +1 -1
  98. data/app/components/primer/open_project/color_field.css.map +1 -1
  99. data/app/components/primer/open_project/inline_message.css +1 -1
  100. data/app/components/primer/open_project/inline_message.css.map +1 -1
  101. data/app/components/primer/open_project/input_group.css +1 -1
  102. data/app/components/primer/open_project/input_group.css.map +1 -1
  103. data/app/components/primer/open_project/keybinding_hint/chords.d.ts +5 -0
  104. data/app/components/primer/open_project/keybinding_hint/chords.js +29 -0
  105. data/app/components/primer/open_project/keybinding_hint/chords.ts +38 -0
  106. data/app/components/primer/open_project/keybinding_hint/key_names.d.ts +16 -0
  107. data/app/components/primer/open_project/keybinding_hint/key_names.js +107 -0
  108. data/app/components/primer/open_project/keybinding_hint/key_names.ts +116 -0
  109. data/app/components/primer/open_project/keybinding_hint/localized_names.d.ts +22 -0
  110. data/app/components/primer/open_project/keybinding_hint/localized_names.js +47 -0
  111. data/app/components/primer/open_project/keybinding_hint/localized_names.ts +71 -0
  112. data/app/components/primer/open_project/keybinding_hint/names.rb +92 -0
  113. data/app/components/primer/open_project/keybinding_hint/names_from_scope.d.ts +21 -0
  114. data/app/components/primer/open_project/keybinding_hint/names_from_scope.js +81 -0
  115. data/app/components/primer/open_project/keybinding_hint/names_from_scope.ts +96 -0
  116. data/app/components/primer/open_project/keybinding_hint/platform.d.ts +10 -0
  117. data/app/components/primer/open_project/keybinding_hint/platform.js +24 -0
  118. data/app/components/primer/open_project/keybinding_hint/platform.ts +34 -0
  119. data/app/components/primer/open_project/keybinding_hint/platform_override.d.ts +2 -0
  120. data/app/components/primer/open_project/keybinding_hint/platform_override.js +4 -0
  121. data/app/components/primer/open_project/keybinding_hint/platform_override.ts +7 -0
  122. data/app/components/primer/open_project/keybinding_hint.css +1 -0
  123. data/app/components/primer/open_project/keybinding_hint.css.json +10 -0
  124. data/app/components/primer/open_project/keybinding_hint.css.map +1 -0
  125. data/app/components/primer/open_project/keybinding_hint.html.erb +4 -0
  126. data/app/components/primer/open_project/keybinding_hint.pcss +65 -0
  127. data/app/components/primer/open_project/keybinding_hint.rb +112 -0
  128. data/app/components/primer/open_project/keybinding_hint_element.d.ts +31 -0
  129. data/app/components/primer/open_project/keybinding_hint_element.js +183 -0
  130. data/app/components/primer/open_project/keybinding_hint_element.ts +152 -0
  131. data/app/components/primer/open_project/page_header.css +1 -1
  132. data/app/components/primer/open_project/page_header.css.json +2 -2
  133. data/app/components/primer/open_project/page_header.css.map +1 -1
  134. data/app/components/primer/open_project/pagination.css +1 -1
  135. data/app/components/primer/open_project/pagination.css.json +1 -1
  136. data/app/components/primer/open_project/pagination.css.map +1 -1
  137. data/app/components/primer/open_project/sub_header.css +1 -1
  138. data/app/components/primer/open_project/sub_header.css.map +1 -1
  139. data/app/components/primer/primer.d.ts +1 -0
  140. data/app/components/primer/primer.js +1 -0
  141. data/app/components/primer/primer.pcss +1 -0
  142. data/app/components/primer/primer.ts +1 -0
  143. data/app/components/primer/truncate.css +1 -1
  144. data/app/components/primer/truncate.css.json +3 -4
  145. data/app/components/primer/truncate.css.map +1 -1
  146. data/app/controllers/primer/view_components/action_menu_controller.rb +35 -0
  147. data/app/views/primer/view_components/action_menu/deferred.html.erb +25 -11
  148. data/app/views/primer/view_components/action_menu/deferred_form.html.erb +10 -0
  149. data/app/views/primer/view_components/action_menu/deferred_preload.html.erb +7 -1
  150. data/config/locales/en.yml +179 -0
  151. data/config/routes.rb +1 -0
  152. data/lib/primer/accessibility.rb +1 -2
  153. data/lib/primer/classify.rb +1 -2
  154. data/lib/primer/view_components/version.rb +1 -1
  155. data/previews/primer/alpha/action_list_preview.rb +46 -1
  156. data/previews/primer/alpha/action_menu_preview/deferred_refresh.html.erb +34 -0
  157. data/previews/primer/alpha/action_menu_preview/deferred_refresh_form.html.erb +9 -0
  158. data/previews/primer/alpha/action_menu_preview/sub_menus.html.erb +1 -1
  159. data/previews/primer/alpha/action_menu_preview.rb +19 -2
  160. data/previews/primer/open_project/keybinding_hint_preview/full_format.html.erb +3 -0
  161. data/previews/primer/open_project/keybinding_hint_preview/in_action_list.html.erb +12 -0
  162. data/previews/primer/open_project/keybinding_hint_preview/in_button.html.erb +24 -0
  163. data/previews/primer/open_project/keybinding_hint_preview/platforms.html.erb +8 -0
  164. data/previews/primer/open_project/keybinding_hint_preview/playground.html.erb +4 -0
  165. data/previews/primer/open_project/keybinding_hint_preview/prose.html.erb +3 -0
  166. data/previews/primer/open_project/keybinding_hint_preview/schemes.html.erb +6 -0
  167. data/previews/primer/open_project/keybinding_hint_preview/sequence.html.erb +1 -0
  168. data/previews/primer/open_project/keybinding_hint_preview/sizes.html.erb +4 -0
  169. data/previews/primer/open_project/keybinding_hint_preview.rb +71 -0
  170. data/static/arguments.json +55 -3
  171. data/static/audited_at.json +1 -0
  172. data/static/classes.json +15 -3
  173. data/static/classnames.cjs +5 -1
  174. data/static/classnames.js +5 -1
  175. data/static/constants.json +35 -0
  176. data/static/info_arch.json +255 -10
  177. data/static/previews.json +177 -0
  178. data/static/statuses.json +1 -0
  179. metadata +45 -5
  180. data/app/components/primer/alpha/text_field.css.json +0 -134
  181. data/app/components/primer/alpha/toggle_switch.css.json +0 -40
  182. data/app/components/primer/alpha/underline_nav.css.json +0 -26
  183. data/app/components/primer/beta/button.css.json +0 -86
@@ -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 = {
@@ -18,6 +19,8 @@ export class ActionMenuElement extends HTMLElement {
18
19
  @target includeFragment: IncludeFragmentElement
19
20
  @target overlay: AnchoredPositionElement
20
21
  @target list: HTMLElement
22
+ @target overlayBody: HTMLElement | undefined
23
+ @target loaderTemplate: HTMLTemplateElement | undefined
21
24
 
22
25
  #abortController: AbortController
23
26
  #originalLabel = ''
@@ -26,6 +29,65 @@ export class ActionMenuElement extends HTMLElement {
26
29
  #intersectionObserver: IntersectionObserver
27
30
  #focusZoneStack: ActionMenuFocusZoneStack
28
31
 
32
+ // Loads a deferred primary menu's items; see MenuLoader
33
+ #loader: MenuLoader
34
+
35
+ #scrollUpdater = () => {
36
+ if (this.#isOpen()) {
37
+ this.overlay?.update()
38
+ }
39
+ }
40
+
41
+ constructor() {
42
+ super()
43
+
44
+ // Never disconnected: it observes the element's own subtree, so it keeps
45
+ // working while the element is detached. A local, not a field: the
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
+
61
+ const observer = new MutationObserver(records => {
62
+ // A replaced overlay may have taken open sub-menus with it
63
+ this.#pruneFocusZones()
64
+ this.#loader.mutated(records)
65
+ this.#updateLevels()
66
+ })
67
+ observer.observe(this, {childList: true, subtree: true})
68
+
69
+ this.#focusZoneStack = new ActionMenuFocusZoneStack()
70
+
71
+ // The code below updates the menu (i.e. overlay) position whenever the invoker button
72
+ // changes position within its scroll container.
73
+ //
74
+ // See: https://github.com/primer/view_components/issues/3175
75
+ this.#intersectionObserver = new IntersectionObserver(entries => {
76
+ for (const entry of entries) {
77
+ if (entry.target !== this.invokerElement) continue
78
+
79
+ if (entry.isIntersecting) {
80
+ // eslint-disable-next-line github/prefer-observers
81
+ window.addEventListener('scroll', this.#scrollUpdater, {capture: true})
82
+ } else {
83
+ window.removeEventListener('scroll', this.#scrollUpdater, {capture: true})
84
+ }
85
+ }
86
+ })
87
+ }
88
+
89
+ static observedAttributes = ['src']
90
+
29
91
  static validItemRoles = ['menuitem', 'menuitemcheckbox', 'menuitemradio']
30
92
  static validSelectors = ActionMenuElement.validItemRoles.map(role => `[role="${role}"]`)
31
93
  static menuItemSelectors = ActionMenuElement.validSelectors.map(selector => `:not([hidden]) > ${selector}`)
@@ -71,13 +133,12 @@ export class ActionMenuElement extends HTMLElement {
71
133
 
72
134
  get invokerElement(): HTMLButtonElement | null {
73
135
  const id = this.querySelector('[role=menu]')?.id
74
- if (!id) return null
75
- for (const el of this.querySelectorAll(`[aria-controls]`)) {
76
- if (el.getAttribute('aria-controls') === id) {
77
- return el as HTMLButtonElement
78
- }
136
+ if (id) {
137
+ const el = this.querySelector<HTMLButtonElement>(`[aria-controls="${CSS.escape(id)}"]`)
138
+ if (el) return el
79
139
  }
80
- return null
140
+ const overlayId = this.overlay?.id
141
+ return overlayId ? this.querySelector<HTMLButtonElement>(`[popovertarget="${CSS.escape(overlayId)}"]`) : null
81
142
  }
82
143
 
83
144
  get invokerLabel(): HTMLElement | null {
@@ -103,79 +164,99 @@ export class ActionMenuElement extends HTMLElement {
103
164
  }
104
165
 
105
166
  connectedCallback() {
167
+ this.#bindHostListeners()
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
+ }
174
+ this.#softDisableItems()
175
+ this.#setupSubMenuLoading()
176
+ this.#loader.sync()
177
+ this.#observeInvokerPosition()
178
+
179
+ if (this.#loader.deferred) {
180
+ this.#loader.updateReady()
181
+ } else if (!this.includeFragment?.isConnected) {
182
+ this.setAttribute('data-ready', 'true')
183
+ }
184
+
185
+ this.#updateLevels()
186
+ }
187
+
188
+ disconnectedCallback() {
189
+ this.#abortController.abort()
190
+ this.#focusZoneStack.clear()
191
+ this.#intersectionObserver.disconnect()
192
+ window.removeEventListener('scroll', this.#scrollUpdater, {capture: true})
193
+ }
194
+
195
+ attributeChangedCallback(name: string) {
196
+ if (name === 'src') this.#loader.sync()
197
+ }
198
+
199
+ // Forgets the current request so the menu loads again at the current src:
200
+ // at once when open or preloaded, otherwise when it next opens.
201
+ refresh() {
202
+ this.#loader.refresh()
203
+ }
204
+
205
+ #bindHostListeners() {
106
206
  const {signal} = (this.#abortController = new AbortController())
107
207
  this.addEventListener('keydown', this, {signal})
108
208
  this.addEventListener('click', this, {signal})
109
209
  this.addEventListener('mouseover', this, {signal})
110
210
  this.addEventListener('focusout', this, {signal})
111
211
  this.addEventListener('mousedown', this, {signal})
212
+ this.addEventListener('beforetoggle', this, {signal, capture: true})
112
213
  this.addEventListener('toggle', this, {signal, capture: true})
113
- this.#setDynamicLabel()
114
- this.#updateInput()
115
- this.#softDisableItems()
116
-
117
- if (this.includeFragment) {
118
- this.includeFragment.addEventListener('include-fragment-replaced', this, {
119
- signal,
120
- })
121
- }
122
-
123
- // The code below updates the menu (i.e. overlay) position whenever the invoker button
124
- // changes position within its scroll container.
125
- //
126
- // See: https://github.com/primer/view_components/issues/3175
214
+ }
127
215
 
128
- const scrollUpdater = () => {
129
- if (this.#isOpen()) {
130
- this.overlay?.update()
131
- }
132
- }
216
+ // A static primary menu with a deferred sub-menu
217
+ #setupSubMenuLoading() {
218
+ if (this.#loader.deferred || !this.includeFragment) return
133
219
 
134
- this.#intersectionObserver = new IntersectionObserver(entries => {
135
- for (const entry of entries) {
136
- const elem = entry.target
137
- if (elem === this.invokerElement) {
138
- if (entry.isIntersecting) {
139
- // eslint-disable-next-line github/prefer-observers
140
- window.addEventListener('scroll', scrollUpdater, {capture: true})
141
- } else {
142
- window.removeEventListener('scroll', scrollUpdater, {capture: true})
143
- }
144
- }
145
- }
220
+ this.includeFragment.addEventListener('include-fragment-replaced', this, {
221
+ signal: this.#abortController.signal,
146
222
  })
223
+ }
147
224
 
225
+ #observeInvokerPosition() {
148
226
  observeMutationsUntilConditionMet(
149
227
  this,
150
228
  () => Boolean(this.invokerElement),
151
229
  () => this.#intersectionObserver.observe(this.invokerElement!),
152
230
  )
231
+ }
153
232
 
154
- // If there's no include fragment, then no async fetching will occur and we can
155
- // mark the component as ready.
156
- if (!this.includeFragment) {
157
- this.setAttribute('data-ready', 'true')
233
+ #updateLevels() {
234
+ for (const [index, menu] of this.querySelectorAll('[role=menu]').entries()) {
235
+ menu.setAttribute('data-level', String(index + 1))
158
236
  }
159
-
160
- const levelObserver = new MutationObserver(() => this.#updateLevels())
161
- levelObserver.observe(this, {childList: true, subtree: true})
162
-
163
- this.#updateLevels()
164
-
165
- this.#focusZoneStack = new ActionMenuFocusZoneStack()
166
237
  }
167
238
 
168
- #updateLevels() {
169
- let idx = 1
239
+ // Keeps the open (or opening) menu open while fragment loads into its
240
+ // body. Open sub-menus are torn down first, since their toggle events
241
+ // arrive after the nodes are gone, and focus moves to the fragment's
242
+ // spinner before the focusout frame runs, so it never drops to <body> and
243
+ // closes the menu. Called from beforetoggle, before the popover shows, the
244
+ // focus() is a no-op; the toggle frame then focuses the spinner.
245
+ #keepOpenWhileLoading(fragment: IncludeFragmentElement) {
246
+ this.#pruneFocusZones()
247
+ this.#focusZoneStack.popAbove(this.overlay)
170
248
 
171
- for (const menu of this.querySelectorAll('[role=menu]')) {
172
- menu.setAttribute('data-level', idx.toString())
173
- idx++
249
+ for (const child of this.childPopoverElements) {
250
+ if (child.matches(':popover-open')) child.hidePopover()
174
251
  }
252
+
253
+ fragment.querySelector<HTMLElement>('[role=menuitem]')?.focus()
175
254
  }
176
255
 
177
- disconnectedCallback() {
178
- this.#abortController.abort()
256
+ // Drops focus zones of overlays other code removed, such as a replaced
257
+ // primary overlay and its sub-menus.
258
+ #pruneFocusZones() {
259
+ this.#focusZoneStack.prune(element => element.isConnected && this.contains(element))
179
260
  }
180
261
 
181
262
  #softDisableItems() {
@@ -241,6 +322,11 @@ export class ActionMenuElement extends HTMLElement {
241
322
  const targetIsInvoker = this.invokerElement?.contains(event.target as HTMLElement)
242
323
  const eventIsActivation = this.#isActivation(event)
243
324
 
325
+ if (event.type === 'beforetoggle' && event instanceof ToggleEvent) {
326
+ this.#handleBeforeToggleEvent(event)
327
+ return
328
+ }
329
+
244
330
  if (event.type === 'toggle' && event instanceof ToggleEvent) {
245
331
  this.#handleToggleEvent(event)
246
332
  return
@@ -316,6 +402,7 @@ export class ActionMenuElement extends HTMLElement {
316
402
  }
317
403
 
318
404
  if (event.type === 'include-fragment-replaced') {
405
+ // A static menu's deferred sub-menu
319
406
  this.#handleIncludeFragmentReplaced()
320
407
  return
321
408
  }
@@ -328,48 +415,87 @@ export class ActionMenuElement extends HTMLElement {
328
415
  #handleItemKeyboardEvent(event: KeyboardEvent, item: HTMLElement) {
329
416
  switch (event.key) {
330
417
  case 'ArrowRight': {
418
+ if (item.getAttribute('aria-disabled')) break
419
+
331
420
  const subMenu = this.#subMenuForItem(item)
332
421
  subMenu?.showPopover()
333
422
  break
334
423
  }
335
424
 
336
- case 'ArrowLeft':
337
- if (item.closest('role[menu]') !== this.list) {
338
- const overlay = item.closest('anchored-position') as AnchoredPositionElement | null
339
- overlay?.hidePopover()
425
+ case 'ArrowLeft': {
426
+ const overlay = item.closest('anchored-position') as AnchoredPositionElement | null
427
+ if (overlay && overlay !== this.overlay) {
428
+ overlay.hidePopover()
340
429
  }
341
430
 
342
431
  break
432
+ }
343
433
  }
344
434
  }
345
435
 
346
- #handleToggleEvent(event: ToggleEvent) {
347
- const subMenu = event.target as AnchoredPositionElement
436
+ #handleBeforeToggleEvent(event: ToggleEvent) {
437
+ if (event.newState === 'open') {
438
+ // A closed lazy menu catches up, and a failed load retries, as it opens
439
+ if (event.target === this.overlay) this.#loader.sync(true)
440
+ return
441
+ }
442
+
443
+ if (event.newState !== 'closed') return
444
+
445
+ const menu = this.#toggledMenu(event)
446
+ if (!menu) return
447
+
448
+ this.#focusZoneStack.pop(menu)
348
449
 
349
- // Ignore toggle events from non-anchored-position elements (e.g. tooltips inside the menu)
350
- if (!(event.target instanceof AnchoredPositionElement)) return
450
+ // The primary menu's popover hands focus back to the invoker itself
451
+ if (menu === this.overlay) return
452
+
453
+ // A nested popover does not hand focus back when it closes, so a sub-menu closed
454
+ // with ArrowLeft or Escape would leave focus on the body and the focusout handler
455
+ // would then close the whole menu. Move focus to the sub-menu's item before the
456
+ // sub-menu hides, while focus is still inside it. Doing this synchronously, rather
457
+ // than in the async toggle event, keeps focus from ever reaching the body.
458
+ if (!menu.contains(document.activeElement)) return
459
+
460
+ this.#itemForSubMenu(menu)?.focus()
461
+ }
462
+
463
+ #handleToggleEvent(event: ToggleEvent) {
464
+ const menu = this.#toggledMenu(event)
465
+ if (!menu) return
351
466
 
352
467
  if (event.newState === 'open') {
353
468
  // allow tabbing away from primary menu, but trap focus in sub-menus
354
- const isPrimaryMenu = subMenu === this.overlay
355
- this.#focusZoneStack.push(subMenu, {trapFocus: !isPrimaryMenu})
469
+ const isPrimaryMenu = menu === this.overlay
470
+ this.#focusZoneStack.push(menu, {trapFocus: !isPrimaryMenu})
471
+
472
+ // Picks up a src set by a beforetoggle listener that ran after ours
473
+ if (isPrimaryMenu) this.#loader.sync(true)
356
474
 
357
475
  window.requestAnimationFrame(() => {
358
- const firstItem = subMenu.querySelector(ActionMenuElement.menuItemSelectors.join(',')) as HTMLElement | null
476
+ const firstItem = menu.querySelector(ActionMenuElement.menuItemSelectors.join(',')) as HTMLElement | null
359
477
  firstItem?.focus()
360
478
  })
361
- } else {
362
- // Note that this will also cause focus to return to the invoker button, which is
363
- // desirable
364
- this.#focusZoneStack.pop(subMenu)
365
- const item = this.#itemForSubMenu(subMenu)
366
- const openPopover = document.querySelector(':popover-open')
367
- if (item && !openPopover) {
368
- item.focus()
369
- }
479
+ } else if (menu === this.overlay && !document.querySelector(':popover-open') && this.#focusLostTo(menu)) {
480
+ // A light-dismissed menu (e.g. a click outside it) leaves focus on the body;
481
+ // hand it back to the invoker so the menu stays reachable from the keyboard.
482
+ this.invokerElement?.focus()
370
483
  }
371
484
  }
372
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
+
493
+ // The primary menu or a sub-menu, or null for popovers that are not menus
494
+ // (e.g. tooltips inside the menu)
495
+ #toggledMenu(event: ToggleEvent): AnchoredPositionElement | null {
496
+ return event.target instanceof AnchoredPositionElement ? event.target : null
497
+ }
498
+
373
499
  #handleInvokerActivated(event: Event) {
374
500
  event.preventDefault()
375
501
  /* eslint-disable-next-line no-restricted-syntax */
@@ -470,12 +596,57 @@ export class ActionMenuElement extends HTMLElement {
470
596
  }
471
597
 
472
598
  #handleIncludeFragmentReplaced() {
473
- this.#firstItem?.focus()
599
+ // Selection first: focusing afterwards can run a consumer handler that
600
+ // starts another load
601
+ this.#setDynamicLabel()
602
+ this.#updateInput()
474
603
  this.#softDisableItems()
604
+
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
+
475
638
  this.overlay?.update()
639
+ }
476
640
 
477
- // async items have loaded, so component is ready
478
- this.setAttribute('data-ready', 'true')
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')
649
+ }
479
650
  }
480
651
 
481
652
  // Close when focus leaves menu
@@ -495,15 +666,20 @@ export class ActionMenuElement extends HTMLElement {
495
666
  }
496
667
  }
497
668
 
498
- #isOpen() {
499
- return this.popoverElement?.matches(':popover-open')
669
+ #isOpen(): boolean {
670
+ return this.overlay?.matches(':popover-open') ?? false
500
671
  }
501
672
 
502
673
  #setDynamicLabel() {
503
674
  if (!this.dynamicLabel) return
504
675
  const invokerLabel = this.invokerLabel
505
676
  if (!invokerLabel) return
506
- this.#originalLabel ||= invokerLabel.textContent || ''
677
+ // Kept on the host, since the button of a cloned or restored menu
678
+ // already shows a selection
679
+ if (!this.#originalLabel) {
680
+ this.#originalLabel = this.getAttribute('data-original-label') ?? (invokerLabel.textContent || '')
681
+ this.setAttribute('data-original-label', this.#originalLabel)
682
+ }
507
683
  let itemLabel: string | undefined
508
684
  if (this.selectVariant === 'single') {
509
685
  itemLabel = this.querySelector('[aria-checked=true] .ActionListItem-label')?.textContent?.trim()
@@ -549,6 +725,10 @@ export class ActionMenuElement extends HTMLElement {
549
725
  if (inputs.length > 0) {
550
726
  this.#inputName ||= (inputs[0] as HTMLInputElement).name
551
727
  }
728
+ // Kept on the container too, so a cloned or restored menu with nothing
729
+ // selected still names the inputs it creates
730
+ this.#inputName ||= inputList.getAttribute('data-input-name') ?? ''
731
+ if (this.#inputName) inputList.setAttribute('data-input-name', this.#inputName)
552
732
 
553
733
  for (const selectedItem of this.selectedItems) {
554
734
  const newInput = document.createElement('input')
@@ -1,7 +1,7 @@
1
1
  import AnchoredPositionElement from '../../anchored_position';
2
2
  type StackEntry = {
3
3
  element: AnchoredPositionElement;
4
- abortController?: AbortController;
4
+ abortController: AbortController;
5
5
  };
6
6
  export declare class ActionMenuFocusZoneStack {
7
7
  #private;
@@ -11,6 +11,9 @@ export declare class ActionMenuFocusZoneStack {
11
11
  trapFocus: boolean;
12
12
  }): void;
13
13
  pop(target?: AnchoredPositionElement): void;
14
+ popAbove(element: AnchoredPositionElement): void;
15
+ prune(isLive: (element: AnchoredPositionElement) => boolean): void;
16
+ clear(): void;
14
17
  elementIsMenuItem(element: HTMLElement): boolean;
15
18
  get isEmpty(): boolean;
16
19
  }
@@ -9,7 +9,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _ActionMenuFocusZoneStack_instances, _ActionMenuFocusZoneStack_stack, _ActionMenuFocusZoneStack_setupFocusZone, _ActionMenuFocusZoneStack_validItemRoles_get;
12
+ var _ActionMenuFocusZoneStack_instances, _ActionMenuFocusZoneStack_stack, _ActionMenuFocusZoneStack_popTop, _ActionMenuFocusZoneStack_includes, _ActionMenuFocusZoneStack_setupFocusZone, _ActionMenuFocusZoneStack_validItemRoles_get;
13
13
  import { FocusKeys, focusTrap, focusZone } from '@primer/behaviors';
14
14
  import { ActionMenuElement } from './action_menu_element';
15
15
  export class ActionMenuFocusZoneStack {
@@ -25,15 +25,36 @@ export class ActionMenuFocusZoneStack {
25
25
  const { trapFocus } = options;
26
26
  __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").push({ element: next, abortController: __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_setupFocusZone).call(this, next, trapFocus) });
27
27
  }
28
+ // Pops and aborts the top entry, or the given element and every entry above it.
28
29
  pop(target) {
29
30
  if (target) {
30
- while (__classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").length > 0 && this.current?.element !== target) {
31
- const entry = __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").pop();
32
- entry?.abortController?.abort();
33
- }
31
+ if (!__classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_includes).call(this, target))
32
+ return;
33
+ this.popAbove(target);
34
34
  }
35
- const entry = __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").pop();
36
- entry?.abortController?.abort();
35
+ __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_popTop).call(this);
36
+ }
37
+ // Pops and aborts every entry above (not including) the given element.
38
+ popAbove(element) {
39
+ if (!__classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_includes).call(this, element))
40
+ return;
41
+ while (this.current && this.current.element !== element)
42
+ __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_popTop).call(this);
43
+ }
44
+ // Drops and aborts every entry whose element is no longer live, for example
45
+ // an overlay that other code removed while it was open. Its toggle event
46
+ // never reaches the menu, so nothing else pops it.
47
+ prune(isLive) {
48
+ __classPrivateFieldSet(this, _ActionMenuFocusZoneStack_stack, __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").filter(entry => {
49
+ if (isLive(entry.element))
50
+ return true;
51
+ entry.abortController.abort();
52
+ return false;
53
+ }), "f");
54
+ }
55
+ clear() {
56
+ while (this.current)
57
+ __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "m", _ActionMenuFocusZoneStack_popTop).call(this);
37
58
  }
38
59
  elementIsMenuItem(element) {
39
60
  return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_instances, "a", _ActionMenuFocusZoneStack_validItemRoles_get).includes(element.getAttribute('role') || '');
@@ -42,21 +63,24 @@ export class ActionMenuFocusZoneStack {
42
63
  return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").length === 0;
43
64
  }
44
65
  }
45
- _ActionMenuFocusZoneStack_stack = new WeakMap(), _ActionMenuFocusZoneStack_instances = new WeakSet(), _ActionMenuFocusZoneStack_setupFocusZone = function _ActionMenuFocusZoneStack_setupFocusZone(containerEl, trapFocus) {
66
+ _ActionMenuFocusZoneStack_stack = new WeakMap(), _ActionMenuFocusZoneStack_instances = new WeakSet(), _ActionMenuFocusZoneStack_popTop = function _ActionMenuFocusZoneStack_popTop() {
67
+ __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").pop()?.abortController.abort();
68
+ }, _ActionMenuFocusZoneStack_includes = function _ActionMenuFocusZoneStack_includes(element) {
69
+ return __classPrivateFieldGet(this, _ActionMenuFocusZoneStack_stack, "f").some(entry => entry.element === element);
70
+ }, _ActionMenuFocusZoneStack_setupFocusZone = function _ActionMenuFocusZoneStack_setupFocusZone(containerEl, trapFocus) {
46
71
  const focusZoneAbortController = focusZone(containerEl, {
47
- bindKeys: FocusKeys.ArrowVertical | FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd | FocusKeys.Backspace,
72
+ bindKeys: FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd | FocusKeys.PageUpDown | FocusKeys.Backspace,
48
73
  focusOutBehavior: 'wrap',
49
74
  focusableElementFilter: (element) => {
50
75
  return this.elementIsMenuItem(element) && element.closest('anchored-position') === containerEl;
51
76
  },
52
77
  });
78
+ // focusTrap() returns nothing when given a signal, so hand back the focus zone's
79
+ // controller in both cases. Aborting it tears down the focus trap as well.
53
80
  if (trapFocus) {
54
- const { signal: focusZoneSignal } = focusZoneAbortController;
55
- return focusTrap(containerEl, undefined, focusZoneSignal);
56
- }
57
- else {
58
- return focusZoneAbortController;
81
+ focusTrap(containerEl, undefined, focusZoneAbortController.signal);
59
82
  }
83
+ return focusZoneAbortController;
60
84
  }, _ActionMenuFocusZoneStack_validItemRoles_get = function _ActionMenuFocusZoneStack_validItemRoles_get() {
61
85
  return ActionMenuElement.validItemRoles;
62
86
  };
@@ -4,7 +4,7 @@ import {ActionMenuElement} from './action_menu_element'
4
4
 
5
5
  type StackEntry = {
6
6
  element: AnchoredPositionElement
7
- abortController?: AbortController
7
+ abortController: AbortController
8
8
  }
9
9
 
10
10
  export class ActionMenuFocusZoneStack {
@@ -23,21 +23,49 @@ export class ActionMenuFocusZoneStack {
23
23
  this.#stack.push({element: next, abortController: this.#setupFocusZone(next, trapFocus)})
24
24
  }
25
25
 
26
+ // Pops and aborts the top entry, or the given element and every entry above it.
26
27
  pop(target?: AnchoredPositionElement) {
27
28
  if (target) {
28
- while (this.#stack.length > 0 && this.current?.element !== target) {
29
- const entry = this.#stack.pop()
30
- entry?.abortController?.abort()
31
- }
29
+ if (!this.#includes(target)) return
30
+ this.popAbove(target)
32
31
  }
33
32
 
34
- const entry = this.#stack.pop()
35
- entry?.abortController?.abort()
33
+ this.#popTop()
36
34
  }
37
35
 
38
- #setupFocusZone(containerEl: AnchoredPositionElement, trapFocus: boolean): AbortController | undefined {
36
+ // Pops and aborts every entry above (not including) the given element.
37
+ popAbove(element: AnchoredPositionElement) {
38
+ if (!this.#includes(element)) return
39
+
40
+ while (this.current && this.current.element !== element) this.#popTop()
41
+ }
42
+
43
+ // Drops and aborts every entry whose element is no longer live, for example
44
+ // an overlay that other code removed while it was open. Its toggle event
45
+ // never reaches the menu, so nothing else pops it.
46
+ prune(isLive: (element: AnchoredPositionElement) => boolean) {
47
+ this.#stack = this.#stack.filter(entry => {
48
+ if (isLive(entry.element)) return true
49
+ entry.abortController.abort()
50
+ return false
51
+ })
52
+ }
53
+
54
+ clear() {
55
+ while (this.current) this.#popTop()
56
+ }
57
+
58
+ #popTop() {
59
+ this.#stack.pop()?.abortController.abort()
60
+ }
61
+
62
+ #includes(element: AnchoredPositionElement): boolean {
63
+ return this.#stack.some(entry => entry.element === element)
64
+ }
65
+
66
+ #setupFocusZone(containerEl: AnchoredPositionElement, trapFocus: boolean): AbortController {
39
67
  const focusZoneAbortController = focusZone(containerEl, {
40
- bindKeys: FocusKeys.ArrowVertical | FocusKeys.ArrowHorizontal | FocusKeys.HomeAndEnd | FocusKeys.Backspace,
68
+ bindKeys: FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd | FocusKeys.PageUpDown | FocusKeys.Backspace,
41
69
  focusOutBehavior: 'wrap',
42
70
 
43
71
  focusableElementFilter: (element: HTMLElement): boolean => {
@@ -45,12 +73,13 @@ export class ActionMenuFocusZoneStack {
45
73
  },
46
74
  })
47
75
 
76
+ // focusTrap() returns nothing when given a signal, so hand back the focus zone's
77
+ // controller in both cases. Aborting it tears down the focus trap as well.
48
78
  if (trapFocus) {
49
- const {signal: focusZoneSignal} = focusZoneAbortController
50
- return focusTrap(containerEl, undefined, focusZoneSignal)
51
- } else {
52
- return focusZoneAbortController
79
+ focusTrap(containerEl, undefined, focusZoneAbortController.signal)
53
80
  }
81
+
82
+ return focusZoneAbortController
54
83
  }
55
84
 
56
85
  elementIsMenuItem(element: HTMLElement): boolean {