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
@@ -1,24 +1,32 @@
1
- <%= render(@overlay) do |overlay| %>
2
- <% overlay.with_body(padding: :none) do %>
3
- <% if @src.present? %>
4
- <%= render(Primer::Alpha::IncludeFragment.new(src: @src, loading: preload? ? :eager : :lazy, "data-target": "action-menu.includeFragment")) do %>
5
- <%= render(Primer::Alpha::ActionMenu::List.new(id: "#{@menu_id}-list", menu_id: @menu_id)) do |list| %>
6
- <% list.with_item(
7
- aria: { disabled: true },
8
- content_arguments: {
9
- display: :flex,
10
- align_items: :center,
11
- justify_content: :center,
12
- text_align: :center,
13
- autofocus: true
14
- }
15
- ) do %>
16
- <%= render Primer::Beta::Spinner.new(aria: { label: I18n.t(:label_loading) }, size: :small) %>
17
- <% end %>
18
- <% end %>
1
+ <% if @src.present? %>
2
+ <% loader = capture do %>
3
+ <%= render(Primer::Alpha::ActionMenu::List.new(id: "#{@menu_id}-list", menu_id: @menu_id)) do |list| %>
4
+ <% list.with_item(
5
+ aria: { disabled: true },
6
+ content_arguments: {
7
+ display: :flex,
8
+ align_items: :center,
9
+ justify_content: :center,
10
+ text_align: :center,
11
+ autofocus: true
12
+ }
13
+ ) do %>
14
+ <%= render Primer::Beta::Spinner.new(aria: { label: I18n.t(:label_loading) }, size: :small) %>
19
15
  <% end %>
20
- <% else %>
16
+ <% end %>
17
+ <% end %>
18
+ <% end %>
19
+ <%= render(@overlay) do |overlay| %>
20
+ <% overlay.with_body(padding: :none, **@body_arguments) do %>
21
+ <% if @src.present? && !primary? %>
22
+ <%= render(Primer::Alpha::IncludeFragment.new(src: @src, loading: preload? ? :eager : :lazy, "data-target": "action-menu.includeFragment")) { loader } %>
23
+ <% elsif @src.blank? %>
21
24
  <%= render(@list) %>
22
25
  <% end %>
23
26
  <% end %>
24
27
  <% end %>
28
+ <% if @src.present? && primary? %>
29
+ <template data-target="action-menu.loaderTemplate">
30
+ <%= render(Primer::Alpha::IncludeFragment.new(src: @src, loading: preload? ? :eager : :lazy)) { loader } %>
31
+ </template>
32
+ <% end %>
@@ -64,6 +64,7 @@ module Primer
64
64
  # @param form_arguments [Hash] Allows an `ActionMenu` to act as a select list in multi- and single-select modes. Pass the `builder:` and `name:` options to this hash. `builder:` should be an instance of `ActionView::Helpers::FormBuilder`, which are created by the standard Rails `#form_with` and `#form_for` helpers. The `name:` option is the desired name of the field that will be included in the params sent to the server on form submission.
65
65
  # @param overlay_arguments [Hash] Arguments to pass to the underlying <%= link_to_component(Primer::Alpha::Overlay) %>
66
66
  # @param list_arguments [Hash] Arguments to pass to the underlying <%= link_to_component(Primer::Alpha::ActionMenu::List) %>
67
+ # @param body_arguments [Hash] Arguments to pass to the underlying overlay's `body` slot.
67
68
  # @param system_arguments [Hash] <%= link_to_system_arguments_docs %>.
68
69
  def initialize(
69
70
  anchor_align:,
@@ -77,9 +78,11 @@ module Primer
77
78
  form_arguments: {},
78
79
  overlay_arguments: {},
79
80
  list_arguments: {},
81
+ body_arguments: {},
80
82
  **system_arguments
81
83
  )
82
84
  @menu_id = menu_id
85
+ @body_arguments = body_arguments
83
86
  @src = src
84
87
  @preload = fetch_or_fallback_boolean(preload, DEFAULT_PRELOAD)
85
88
  @anchor_side = anchor_side
@@ -131,6 +134,10 @@ module Primer
131
134
 
132
135
  private
133
136
 
137
+ def primary?
138
+ false
139
+ end
140
+
134
141
  def before_render
135
142
  raise ArgumentError, "`items` cannot be set when `src` is specified" if src.present? && items.any?
136
143
  end
@@ -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
+ }
@@ -0,0 +1,221 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _MenuLoader_instances, _MenuLoader_host, _MenuLoader_request, _MenuLoader_ownedLoaders, _MenuLoader_emptyLoaders, _MenuLoader_preload_get, _MenuLoader_invalidate, _MenuLoader_load, _MenuLoader_adopt, _MenuLoader_own, _MenuLoader_onReplace, _MenuLoader_onReplaced, _MenuLoader_keepInputs, _MenuLoader_dropKeptInputs;
13
+ // Loads a deferred menu's items. The one rule (`sync`): a load starts when
14
+ // there is no current request, when the host src differs from the current
15
+ // request's, or when the body holds no element (other code emptied it, or
16
+ // the last response was empty); the current loader still in the body is
17
+ // retried. A closed lazy menu waits for the open moment (`opening`) instead.
18
+ export class MenuLoader {
19
+ constructor(host) {
20
+ _MenuLoader_instances.add(this);
21
+ _MenuLoader_host.set(this, void 0);
22
+ // The current request: the loader it was made with and the src it was
23
+ // made for. Never updated; a new request replaces it. The src is recorded
24
+ // because the loader's attribute is configuration a morph may rewrite while
25
+ // the request is in flight; the record is what was requested, so a
26
+ // rewritten loader cannot pass an old response off as new.
27
+ _MenuLoader_request.set(this, null
28
+ // Every loader this module created or adopted, so completions of
29
+ // superseded ones can be told apart
30
+ );
31
+ // Every loader this module created or adopted, so completions of
32
+ // superseded ones can be told apart
33
+ _MenuLoader_ownedLoaders.set(this, new WeakSet()
34
+ // Loaders whose response had no menu items, with the body they left
35
+ // empty. That body staying empty is not a reason to load again before the
36
+ // next open; a body other code puts in its place is.
37
+ );
38
+ // Loaders whose response had no menu items, with the body they left
39
+ // empty. That body staying empty is not a reason to load again before the
40
+ // next open; a body other code puts in its place is.
41
+ _MenuLoader_emptyLoaders.set(this, new WeakMap());
42
+ __classPrivateFieldSet(this, _MenuLoader_host, host, "f");
43
+ }
44
+ // A primary menu with `src` renders its loader as a template
45
+ get deferred() {
46
+ return Boolean(__classPrivateFieldGet(this, _MenuLoader_host, "f").template());
47
+ }
48
+ // The current loader is still waiting in the body
49
+ get pending() {
50
+ const loader = __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader;
51
+ return Boolean(loader) && loader.parentElement === (__classPrivateFieldGet(this, _MenuLoader_host, "f").body() ?? null);
52
+ }
53
+ // `data-ready` means no load is pending
54
+ updateReady() {
55
+ if (!this.deferred)
56
+ return;
57
+ if (this.pending) {
58
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").element.removeAttribute('data-ready');
59
+ }
60
+ else {
61
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").element.setAttribute('data-ready', 'true');
62
+ }
63
+ }
64
+ sync(opening = false) {
65
+ const src = (__classPrivateFieldGet(this, _MenuLoader_host, "f").element.getAttribute('src') ?? '').trim();
66
+ if (!this.deferred || !src)
67
+ return;
68
+ if (!(opening || __classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen() || __classPrivateFieldGet(this, _MenuLoader_instances, "a", _MenuLoader_preload_get))) {
69
+ // A pending or failed loader for another src must not render meanwhile;
70
+ // a completed one is kept, so setting its src again does not reload
71
+ if (__classPrivateFieldGet(this, _MenuLoader_request, "f") && src !== __classPrivateFieldGet(this, _MenuLoader_request, "f").src && this.pending)
72
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_invalidate).call(this);
73
+ return;
74
+ }
75
+ const loader = __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader;
76
+ if (!__classPrivateFieldGet(this, _MenuLoader_request, "f") || src !== __classPrivateFieldGet(this, _MenuLoader_request, "f").src || !__classPrivateFieldGet(this, _MenuLoader_host, "f").body()?.firstElementChild) {
77
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_load).call(this, src, opening);
78
+ }
79
+ else if (loader?.isConnected) {
80
+ // A retry fetches the loader's own src, which a morph may have rewritten
81
+ if ((loader.getAttribute('src') ?? '').trim() === __classPrivateFieldGet(this, _MenuLoader_request, "f").src) {
82
+ loader.classList.remove('is-error');
83
+ loader.refetch();
84
+ }
85
+ else {
86
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_load).call(this, src, opening);
87
+ }
88
+ }
89
+ }
90
+ // Forgets the current request so the menu loads again at the current src:
91
+ // at once when open or preloaded, otherwise when it next opens.
92
+ refresh() {
93
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_invalidate).call(this);
94
+ this.sync();
95
+ }
96
+ // Picks up loaders other code inserts into the body (the last one wins, so
97
+ // a loader appended next to a pending one supersedes it), and loads again
98
+ // when other code empties the body, such as a morph of the server markup,
99
+ // which renders the body empty.
100
+ mutated(records) {
101
+ if (!this.deferred)
102
+ return;
103
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
104
+ if (!body)
105
+ return;
106
+ if (records.some(record => record.addedNodes.length > 0)) {
107
+ const fragments = Array.from(body.querySelectorAll(':scope > include-fragment')).filter(fragment => !__classPrivateFieldGet(this, _MenuLoader_ownedLoaders, "f").has(fragment));
108
+ const fragment = fragments.at(-1);
109
+ if (fragment)
110
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_adopt).call(this, fragment);
111
+ }
112
+ // An empty body was emptied or replaced by other code, unless it is the
113
+ // one the current loader left empty; loading again there would loop on a
114
+ // preloaded menu, so that one waits for the next open
115
+ const loader = __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader;
116
+ if (!body.firstElementChild && !(loader && __classPrivateFieldGet(this, _MenuLoader_emptyLoaders, "f").get(loader) === body))
117
+ this.sync();
118
+ this.updateReady();
119
+ }
120
+ // Form inputs are kept on the host for a pending or failed load
121
+ get keepingInputs() {
122
+ return Boolean(__classPrivateFieldGet(this, _MenuLoader_host, "f").element.querySelector(':scope > [data-list-inputs]'));
123
+ }
124
+ }
125
+ _MenuLoader_host = new WeakMap(), _MenuLoader_request = new WeakMap(), _MenuLoader_ownedLoaders = new WeakMap(), _MenuLoader_emptyLoaders = new WeakMap(), _MenuLoader_instances = new WeakSet(), _MenuLoader_preload_get = function _MenuLoader_preload_get() {
126
+ return __classPrivateFieldGet(this, _MenuLoader_host, "f").template()?.content.firstElementChild?.getAttribute('loading') === 'eager';
127
+ }, _MenuLoader_invalidate = function _MenuLoader_invalidate() {
128
+ __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader.remove();
129
+ __classPrivateFieldSet(this, _MenuLoader_request, null, "f");
130
+ this.updateReady();
131
+ }, _MenuLoader_load = function _MenuLoader_load(src, opening) {
132
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
133
+ const template = __classPrivateFieldGet(this, _MenuLoader_host, "f").template()?.content.firstElementChild;
134
+ if (!body || !template)
135
+ return;
136
+ // Imported, not cloned, so the loader is upgraded before it connects
137
+ const loader = __classPrivateFieldGet(this, _MenuLoader_host, "f").element.ownerDocument.importNode(template, true);
138
+ loader.setAttribute('src', src);
139
+ loader.setAttribute('loading', 'eager');
140
+ __classPrivateFieldSet(this, _MenuLoader_request, { loader, src }, "f");
141
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_own).call(this, loader);
142
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_keepInputs).call(this, body);
143
+ body.prepend(loader);
144
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").onLoadStart(loader, opening || __classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen());
145
+ // A focus handler may have started a newer load meanwhile
146
+ if (__classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader !== loader)
147
+ return;
148
+ for (const child of Array.from(body.childNodes)) {
149
+ if (child !== loader)
150
+ child.remove();
151
+ }
152
+ this.updateReady();
153
+ }, _MenuLoader_adopt = function _MenuLoader_adopt(fragment) {
154
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
155
+ if (!body)
156
+ return;
157
+ const src = (fragment.getAttribute('src') ?? '').trim();
158
+ __classPrivateFieldSet(this, _MenuLoader_request, { loader: fragment, src }, "f");
159
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_own).call(this, fragment);
160
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_keepInputs).call(this, body);
161
+ const open = __classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen();
162
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").onLoadStart(fragment, open);
163
+ // A focus handler may have started a newer load meanwhile
164
+ if (__classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader !== fragment)
165
+ return;
166
+ for (const child of Array.from(body.childNodes)) {
167
+ if (child !== fragment)
168
+ child.remove();
169
+ }
170
+ const element = __classPrivateFieldGet(this, _MenuLoader_host, "f").element;
171
+ if (src && src !== element.getAttribute('src')) {
172
+ element.setAttribute('src', src);
173
+ }
174
+ else if (__classPrivateFieldGet(this, _MenuLoader_host, "f").isOpen() || __classPrivateFieldGet(this, _MenuLoader_instances, "a", _MenuLoader_preload_get)) {
175
+ // An unchanged src does not run the rule, and a lazy loader inserted
176
+ // before its attributes never starts on its own
177
+ this.sync();
178
+ }
179
+ }, _MenuLoader_own = function _MenuLoader_own(loader) {
180
+ __classPrivateFieldGet(this, _MenuLoader_ownedLoaders, "f").add(loader);
181
+ loader.addEventListener('include-fragment-replace', event => __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_onReplace).call(this, loader, event));
182
+ loader.addEventListener('include-fragment-replaced', () => __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_onReplaced).call(this, loader));
183
+ }, _MenuLoader_onReplace = function _MenuLoader_onReplace(loader, event) {
184
+ if (loader !== __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader)
185
+ return;
186
+ // Other code removed the current loader before it completed, eg. by
187
+ // emptying the body, or rewrote its src, eg. by a morph: its response is
188
+ // not the one requested and must not render or mark the menu ready
189
+ const body = __classPrivateFieldGet(this, _MenuLoader_host, "f").body();
190
+ if (!body || loader.parentElement !== body || (loader.getAttribute('src') ?? '').trim() !== __classPrivateFieldGet(this, _MenuLoader_request, "f").src) {
191
+ event.preventDefault();
192
+ return;
193
+ }
194
+ // A response without menu items is discarded, so the body stays empty
195
+ // and the menu loads again when it next opens
196
+ const fragment = event instanceof CustomEvent ? event.detail?.fragment : null;
197
+ if (!(fragment instanceof DocumentFragment))
198
+ return;
199
+ if (!fragment.querySelector(__classPrivateFieldGet(this, _MenuLoader_host, "f").itemSelector)) {
200
+ fragment.replaceChildren();
201
+ __classPrivateFieldGet(this, _MenuLoader_emptyLoaders, "f").set(loader, body);
202
+ }
203
+ }, _MenuLoader_onReplaced = function _MenuLoader_onReplaced(loader) {
204
+ // A superseded loader that completed late
205
+ if (loader !== __classPrivateFieldGet(this, _MenuLoader_request, "f")?.loader)
206
+ return;
207
+ // Before the menu syncs its inputs, so they come from the response; a
208
+ // load the menu's completion work starts keeps the response's inputs
209
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_dropKeptInputs).call(this);
210
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").onLoaded();
211
+ this.updateReady();
212
+ }, _MenuLoader_keepInputs = function _MenuLoader_keepInputs(body) {
213
+ const inputs = body.querySelector('[data-list-inputs]');
214
+ if (!inputs)
215
+ return;
216
+ __classPrivateFieldGet(this, _MenuLoader_instances, "m", _MenuLoader_dropKeptInputs).call(this);
217
+ __classPrivateFieldGet(this, _MenuLoader_host, "f").element.append(inputs);
218
+ }, _MenuLoader_dropKeptInputs = function _MenuLoader_dropKeptInputs() {
219
+ for (const inputs of __classPrivateFieldGet(this, _MenuLoader_host, "f").element.querySelectorAll(':scope > [data-list-inputs]'))
220
+ inputs.remove();
221
+ };
@@ -0,0 +1,269 @@
1
+ import type {IncludeFragmentElement} from '@github/include-fragment-element'
2
+
3
+ // What MenuLoader needs from the menu it loads into. The loader owns
4
+ // requests, adoption, readiness and kept form inputs; the menu owns focus,
5
+ // popover state, the dynamic label and input sync.
6
+ export type MenuLoaderHost = {
7
+ // The <action-menu>: its `src` is the state, and it receives `data-ready`
8
+ // and the form inputs kept while a load is pending
9
+ element: HTMLElement
10
+ // The primary overlay's body, looked up on every call, since other code
11
+ // may replace the body or the overlay
12
+ body(): HTMLElement | undefined
13
+ template(): HTMLTemplateElement | undefined
14
+ // Matches the menu's items; a response without a match is empty
15
+ itemSelector: string
16
+ isOpen(): boolean
17
+ // A loader is in the body and about to replace its old content; keepOpen
18
+ // when the menu is open or opening
19
+ onLoadStart(fragment: IncludeFragmentElement, keepOpen: boolean): void
20
+ // The current loader's response is in the body
21
+ onLoaded(): void
22
+ }
23
+
24
+ type LoaderRequest = Readonly<{loader: IncludeFragmentElement; src: string}>
25
+
26
+ // Loads a deferred menu's items. The one rule (`sync`): a load starts when
27
+ // there is no current request, when the host src differs from the current
28
+ // request's, or when the body holds no element (other code emptied it, or
29
+ // the last response was empty); the current loader still in the body is
30
+ // retried. A closed lazy menu waits for the open moment (`opening`) instead.
31
+ export class MenuLoader {
32
+ #host: MenuLoaderHost
33
+
34
+ // The current request: the loader it was made with and the src it was
35
+ // made for. Never updated; a new request replaces it. The src is recorded
36
+ // because the loader's attribute is configuration a morph may rewrite while
37
+ // the request is in flight; the record is what was requested, so a
38
+ // rewritten loader cannot pass an old response off as new.
39
+ #request: LoaderRequest | null = null
40
+ // Every loader this module created or adopted, so completions of
41
+ // superseded ones can be told apart
42
+ #ownedLoaders = new WeakSet<IncludeFragmentElement>()
43
+ // Loaders whose response had no menu items, with the body they left
44
+ // empty. That body staying empty is not a reason to load again before the
45
+ // next open; a body other code puts in its place is.
46
+ #emptyLoaders = new WeakMap<IncludeFragmentElement, HTMLElement>()
47
+
48
+ constructor(host: MenuLoaderHost) {
49
+ this.#host = host
50
+ }
51
+
52
+ // A primary menu with `src` renders its loader as a template
53
+ get deferred(): boolean {
54
+ return Boolean(this.#host.template())
55
+ }
56
+
57
+ get #preload(): boolean {
58
+ return this.#host.template()?.content.firstElementChild?.getAttribute('loading') === 'eager'
59
+ }
60
+
61
+ // The current loader is still waiting in the body
62
+ get pending(): boolean {
63
+ const loader = this.#request?.loader
64
+ return Boolean(loader) && loader!.parentElement === (this.#host.body() ?? null)
65
+ }
66
+
67
+ // `data-ready` means no load is pending
68
+ updateReady() {
69
+ if (!this.deferred) return
70
+
71
+ if (this.pending) {
72
+ this.#host.element.removeAttribute('data-ready')
73
+ } else {
74
+ this.#host.element.setAttribute('data-ready', 'true')
75
+ }
76
+ }
77
+
78
+ sync(opening = false) {
79
+ const src = (this.#host.element.getAttribute('src') ?? '').trim()
80
+ if (!this.deferred || !src) return
81
+
82
+ if (!(opening || this.#host.isOpen() || this.#preload)) {
83
+ // A pending or failed loader for another src must not render meanwhile;
84
+ // a completed one is kept, so setting its src again does not reload
85
+ if (this.#request && src !== this.#request.src && this.pending) this.#invalidate()
86
+ return
87
+ }
88
+
89
+ const loader = this.#request?.loader
90
+ if (!this.#request || src !== this.#request.src || !this.#host.body()?.firstElementChild) {
91
+ this.#load(src, opening)
92
+ } else if (loader?.isConnected) {
93
+ // A retry fetches the loader's own src, which a morph may have rewritten
94
+ if ((loader.getAttribute('src') ?? '').trim() === this.#request.src) {
95
+ loader.classList.remove('is-error')
96
+ loader.refetch()
97
+ } else {
98
+ this.#load(src, opening)
99
+ }
100
+ }
101
+ }
102
+
103
+ // Forgets the current request so the menu loads again at the current src:
104
+ // at once when open or preloaded, otherwise when it next opens.
105
+ refresh() {
106
+ this.#invalidate()
107
+ this.sync()
108
+ }
109
+
110
+ // Picks up loaders other code inserts into the body (the last one wins, so
111
+ // a loader appended next to a pending one supersedes it), and loads again
112
+ // when other code empties the body, such as a morph of the server markup,
113
+ // which renders the body empty.
114
+ mutated(records: MutationRecord[]) {
115
+ if (!this.deferred) return
116
+
117
+ const body = this.#host.body()
118
+ if (!body) return
119
+
120
+ if (records.some(record => record.addedNodes.length > 0)) {
121
+ const fragments = Array.from(body.querySelectorAll<IncludeFragmentElement>(':scope > include-fragment')).filter(
122
+ fragment => !this.#ownedLoaders.has(fragment),
123
+ )
124
+ const fragment = fragments.at(-1)
125
+ if (fragment) this.#adopt(fragment)
126
+ }
127
+
128
+ // An empty body was emptied or replaced by other code, unless it is the
129
+ // one the current loader left empty; loading again there would loop on a
130
+ // preloaded menu, so that one waits for the next open
131
+ const loader = this.#request?.loader
132
+ if (!body.firstElementChild && !(loader && this.#emptyLoaders.get(loader) === body)) this.sync()
133
+
134
+ this.updateReady()
135
+ }
136
+
137
+ // Drops the current request. Its loader is removed even while the menu is
138
+ // detached, since upstream still completes the fetch there.
139
+ #invalidate() {
140
+ this.#request?.loader.remove()
141
+ this.#request = null
142
+ this.updateReady()
143
+ }
144
+
145
+ // Starts a request for src with a fresh loader from the template, which
146
+ // replaces whatever the body holds.
147
+ #load(src: string, opening: boolean) {
148
+ const body = this.#host.body()
149
+ const template = this.#host.template()?.content.firstElementChild
150
+ if (!body || !template) return
151
+
152
+ // Imported, not cloned, so the loader is upgraded before it connects
153
+ const loader = this.#host.element.ownerDocument.importNode(template, true) as IncludeFragmentElement
154
+ loader.setAttribute('src', src)
155
+ loader.setAttribute('loading', 'eager')
156
+
157
+ this.#request = {loader, src}
158
+ this.#own(loader)
159
+ this.#keepInputs(body)
160
+
161
+ body.prepend(loader)
162
+ this.#host.onLoadStart(loader, opening || this.#host.isOpen())
163
+ // A focus handler may have started a newer load meanwhile
164
+ if (this.#request?.loader !== loader) return
165
+
166
+ for (const child of Array.from(body.childNodes)) {
167
+ if (child !== loader) child.remove()
168
+ }
169
+
170
+ this.updateReady()
171
+ }
172
+
173
+ // Makes an inserted loader the current request. Its src is reflected onto
174
+ // the host, so the rule sees the request as current: an open or preloaded
175
+ // menu loads it at once (upstream's busy guard drops the duplicate for an
176
+ // eager loader), a closed lazy one when it opens, unless the loader is eager
177
+ // and has already started itself. Idiomorph inserts a loader before setting
178
+ // its attributes, but sets them synchronously, so the loader arrives here
179
+ // with its src; one that gets its src only in a later task is replaced with
180
+ // a load at the host src. The template is not touched.
181
+ #adopt(fragment: IncludeFragmentElement) {
182
+ const body = this.#host.body()
183
+ if (!body) return
184
+
185
+ const src = (fragment.getAttribute('src') ?? '').trim()
186
+
187
+ this.#request = {loader: fragment, src}
188
+ this.#own(fragment)
189
+ this.#keepInputs(body)
190
+
191
+ const open = this.#host.isOpen()
192
+ this.#host.onLoadStart(fragment, open)
193
+ // A focus handler may have started a newer load meanwhile
194
+ if (this.#request?.loader !== fragment) return
195
+
196
+ for (const child of Array.from(body.childNodes)) {
197
+ if (child !== fragment) child.remove()
198
+ }
199
+
200
+ const element = this.#host.element
201
+ if (src && src !== element.getAttribute('src')) {
202
+ element.setAttribute('src', src)
203
+ } else if (this.#host.isOpen() || this.#preload) {
204
+ // An unchanged src does not run the rule, and a lazy loader inserted
205
+ // before its attributes never starts on its own
206
+ this.sync()
207
+ }
208
+ }
209
+
210
+ // Loaders are never reused, so the listeners need no lifetime of their own
211
+ #own(loader: IncludeFragmentElement) {
212
+ this.#ownedLoaders.add(loader)
213
+ loader.addEventListener('include-fragment-replace', event => this.#onReplace(loader, event))
214
+ loader.addEventListener('include-fragment-replaced', () => this.#onReplaced(loader))
215
+ }
216
+
217
+ #onReplace(loader: IncludeFragmentElement, event: Event) {
218
+ if (loader !== this.#request?.loader) return
219
+
220
+ // Other code removed the current loader before it completed, eg. by
221
+ // emptying the body, or rewrote its src, eg. by a morph: its response is
222
+ // not the one requested and must not render or mark the menu ready
223
+ const body = this.#host.body()
224
+ if (!body || loader.parentElement !== body || (loader.getAttribute('src') ?? '').trim() !== this.#request.src) {
225
+ event.preventDefault()
226
+ return
227
+ }
228
+
229
+ // A response without menu items is discarded, so the body stays empty
230
+ // and the menu loads again when it next opens
231
+ const fragment: unknown = event instanceof CustomEvent ? event.detail?.fragment : null
232
+ if (!(fragment instanceof DocumentFragment)) return
233
+
234
+ if (!fragment.querySelector(this.#host.itemSelector)) {
235
+ fragment.replaceChildren()
236
+ this.#emptyLoaders.set(loader, body)
237
+ }
238
+ }
239
+
240
+ #onReplaced(loader: IncludeFragmentElement) {
241
+ // A superseded loader that completed late
242
+ if (loader !== this.#request?.loader) return
243
+
244
+ // Before the menu syncs its inputs, so they come from the response; a
245
+ // load the menu's completion work starts keeps the response's inputs
246
+ this.#dropKeptInputs()
247
+ this.#host.onLoaded()
248
+ this.updateReady()
249
+ }
250
+
251
+ // Keeps the current form inputs submitting while a load replaces the body,
252
+ // also when it fails: they move to the host until a response brings its own
253
+ #keepInputs(body: HTMLElement) {
254
+ const inputs = body.querySelector('[data-list-inputs]')
255
+ if (!inputs) return
256
+
257
+ this.#dropKeptInputs()
258
+ this.#host.element.append(inputs)
259
+ }
260
+
261
+ // Form inputs are kept on the host for a pending or failed load
262
+ get keepingInputs(): boolean {
263
+ return Boolean(this.#host.element.querySelector(':scope > [data-list-inputs]'))
264
+ }
265
+
266
+ #dropKeptInputs() {
267
+ for (const inputs of this.#host.element.querySelectorAll(':scope > [data-list-inputs]')) inputs.remove()
268
+ }
269
+ }
@@ -68,6 +68,15 @@ module Primer
68
68
  { data: { target: "action-menu.list" } }
69
69
  )
70
70
 
71
+ if system_arguments[:src].present?
72
+ system_arguments[:body_arguments] ||= {}
73
+
74
+ system_arguments[:body_arguments][:data] = merge_data(
75
+ system_arguments[:body_arguments],
76
+ { data: { target: "action-menu.overlayBody" } }
77
+ )
78
+ end
79
+
71
80
  super(
72
81
  anchor_align: anchor_align,
73
82
  anchor_side: anchor_side,
@@ -84,6 +93,12 @@ module Primer
84
93
  evaluate_block(button, &block)
85
94
  end
86
95
  end
96
+
97
+ private
98
+
99
+ def primary?
100
+ true
101
+ end
87
102
  end
88
103
  end
89
104
  end