@lion/ui 0.17.0 → 0.18.0-menu-system.1

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 (218) hide show
  1. package/collapsible.js +1 -0
  2. package/components/button/src/LionButton.js +4 -0
  3. package/components/button/test-suites/LionButton.suite.js +1 -0
  4. package/components/calendar/src/LionCalendar.js +2 -0
  5. package/components/collapsible/src/DisclosureMixin.js +313 -0
  6. package/components/collapsible/types/DisclosureMixinTypes.ts +48 -0
  7. package/components/combobox/test/lion-combobox.test.js +1 -0
  8. package/components/form-core/src/form-group/FormGroupMixin.js +7 -0
  9. package/components/form-core/src/registration/FormRegistrarMixin.js +11 -0
  10. package/components/form-core/test/FormControlMixin.test.js +1 -0
  11. package/components/form-core/test/validate/Validator.test.js +2 -0
  12. package/components/form-core/test-suites/ValidateMixin.suite.js +2 -0
  13. package/components/form-core/test-suites/ValidateMixinFeedbackPart.suite.js +1 -0
  14. package/components/input-datepicker/src/localizeNamespaceLoader.js +1 -0
  15. package/components/input-datepicker/test/lion-input-datepicker.test.js +5 -6
  16. package/components/localize/test/date/formatDate.test.js +1 -0
  17. package/components/menu/CHANGELOG.md +37 -0
  18. package/components/menu/README.md +335 -0
  19. package/components/menu/index.js +2 -0
  20. package/components/menu/lion-menu-overlay.js +3 -0
  21. package/components/menu/lion-menu.js +3 -0
  22. package/components/menu/lion-spinbutton.js +3 -0
  23. package/components/menu/lion-toolbar.js +3 -0
  24. package/components/menu/lion-tree.js +3 -0
  25. package/components/menu/package.json +46 -0
  26. package/components/menu/requirements.temp.md +12 -0
  27. package/components/menu/src/InteractiveListMixin.js +805 -0
  28. package/components/menu/src/LionMenu.js +250 -0
  29. package/components/menu/src/LionMenuHybrid.js +46 -0
  30. package/components/menu/src/LionMenuOverlay.js +155 -0
  31. package/components/menu/src/LionSpinbutton.js +213 -0
  32. package/components/menu/src/LionToolbar.js +94 -0
  33. package/components/menu/src/LionTree.js +52 -0
  34. package/components/menu/src/MoreButtonMenuMixin.js +297 -0
  35. package/components/menu/src/MultiLevelListMixin.js +274 -0
  36. package/components/menu/src/utils/isInView.js +26 -0
  37. package/components/menu/src/utils/listItemInteractions.js +104 -0
  38. package/components/menu/src/utils/uuid.js +3 -0
  39. package/components/menu/test/lion-menu.test.js +3 -0
  40. package/components/menu/test-suites/InteractiveListMixin.suite.js +142 -0
  41. package/components/menu/types/InteractiveListMixinTypes.d.ts +82 -0
  42. package/components/navigation-bar/README.md +1 -0
  43. package/components/navigation-bar/src/LionNavigationBar.js +439 -0
  44. package/components/navigation-bar/src/types.js +41 -0
  45. package/components/overlays/NEXT.md +49 -0
  46. package/components/overlays/src/OverlayController.js +244 -111
  47. package/components/overlays/src/OverlayMixin.js +58 -11
  48. package/components/overlays/src/OverlaysManager.js +2 -2
  49. package/components/overlays/src/utils/adopt-styles.js +3 -2
  50. package/components/overlays/src/utils/get-focusable-elements.js +4 -0
  51. package/components/overlays/src/utils/is-equal-config.js +10 -6
  52. package/components/overlays/test/OverlayController.test.js +5 -3
  53. package/components/overlays/test/utils-tests/is-equal-config.test.js +1 -1
  54. package/components/overlays/test-suites/OverlayMixin.suite.js +8 -8
  55. package/components/overlays/types/OverlayConfig.ts +16 -5
  56. package/components/select-rich/test/lion-select-rich.test.js +3 -0
  57. package/custom-elements.json +32144 -19531
  58. package/dist-types/components/button/src/LionButton.d.ts +1 -0
  59. package/dist-types/components/button/src/LionButton.d.ts.map +1 -1
  60. package/dist-types/components/button/test-suites/LionButton.suite.d.ts.map +1 -1
  61. package/dist-types/components/calendar/src/LionCalendar.d.ts.map +1 -1
  62. package/dist-types/components/collapsible/src/DisclosureMixin.d.ts +3 -0
  63. package/dist-types/components/collapsible/src/DisclosureMixin.d.ts.map +1 -0
  64. package/dist-types/components/collapsible/types/DisclosureMixinTypes.d.ts +24 -0
  65. package/dist-types/components/collapsible/types/DisclosureMixinTypes.d.ts.map +1 -0
  66. package/dist-types/components/dialog/src/LionDialog.d.ts +3 -1
  67. package/dist-types/components/dialog/src/LionDialog.d.ts.map +1 -1
  68. package/dist-types/components/form-core/src/form-group/FormGroupMixin.d.ts.map +1 -1
  69. package/dist-types/components/form-core/src/registration/FormRegistrarMixin.d.ts.map +1 -1
  70. package/dist-types/components/form-core/test-suites/ValidateMixin.suite.d.ts.map +1 -1
  71. package/dist-types/components/form-core/test-suites/ValidateMixinFeedbackPart.suite.d.ts.map +1 -1
  72. package/dist-types/components/input-datepicker/src/localizeNamespaceLoader.d.ts.map +1 -1
  73. package/dist-types/components/menu/index.d.ts +3 -0
  74. package/dist-types/components/menu/index.d.ts.map +1 -0
  75. package/dist-types/components/menu/lion-menu-overlay.d.ts +2 -0
  76. package/dist-types/components/menu/lion-menu-overlay.d.ts.map +1 -0
  77. package/dist-types/components/menu/lion-menu.d.ts +2 -0
  78. package/dist-types/components/menu/lion-menu.d.ts.map +1 -0
  79. package/dist-types/components/menu/lion-spinbutton.d.ts +2 -0
  80. package/dist-types/components/menu/lion-spinbutton.d.ts.map +1 -0
  81. package/dist-types/components/menu/lion-toolbar.d.ts +2 -0
  82. package/dist-types/components/menu/lion-toolbar.d.ts.map +1 -0
  83. package/dist-types/components/menu/lion-tree.d.ts +2 -0
  84. package/dist-types/components/menu/lion-tree.d.ts.map +1 -0
  85. package/dist-types/components/menu/src/InteractiveListMixin.d.ts +611 -0
  86. package/dist-types/components/menu/src/InteractiveListMixin.d.ts.map +1 -0
  87. package/dist-types/components/menu/src/LionMenu.d.ts +563 -0
  88. package/dist-types/components/menu/src/LionMenu.d.ts.map +1 -0
  89. package/dist-types/components/menu/src/LionMenuHybrid.d.ts +19 -0
  90. package/dist-types/components/menu/src/LionMenuHybrid.d.ts.map +1 -0
  91. package/dist-types/components/menu/src/LionMenuOverlay.d.ts +1007 -0
  92. package/dist-types/components/menu/src/LionMenuOverlay.d.ts.map +1 -0
  93. package/dist-types/components/menu/src/LionSpinbutton.d.ts +58 -0
  94. package/dist-types/components/menu/src/LionSpinbutton.d.ts.map +1 -0
  95. package/dist-types/components/menu/src/LionToolbar.d.ts +8 -0
  96. package/dist-types/components/menu/src/LionToolbar.d.ts.map +1 -0
  97. package/dist-types/components/menu/src/LionTree.d.ts +445 -0
  98. package/dist-types/components/menu/src/LionTree.d.ts.map +1 -0
  99. package/dist-types/components/menu/src/MoreButtonMenuMixin.d.ts +31 -0
  100. package/dist-types/components/menu/src/MoreButtonMenuMixin.d.ts.map +1 -0
  101. package/dist-types/components/menu/src/MultiLevelListMixin.d.ts +493 -0
  102. package/dist-types/components/menu/src/MultiLevelListMixin.d.ts.map +1 -0
  103. package/dist-types/components/menu/src/utils/isInView.d.ts +9 -0
  104. package/dist-types/components/menu/src/utils/isInView.d.ts.map +1 -0
  105. package/dist-types/components/menu/src/utils/listItemInteractions.d.ts +42 -0
  106. package/dist-types/components/menu/src/utils/listItemInteractions.d.ts.map +1 -0
  107. package/dist-types/components/menu/src/utils/uuid.d.ts +2 -0
  108. package/dist-types/components/menu/src/utils/uuid.d.ts.map +1 -0
  109. package/dist-types/components/menu/test/lion-menu.test.d.ts +2 -0
  110. package/dist-types/components/menu/test/lion-menu.test.d.ts.map +1 -0
  111. package/dist-types/components/menu/test-suites/InteractiveListMixin.suite.d.ts +13 -0
  112. package/dist-types/components/menu/test-suites/InteractiveListMixin.suite.d.ts.map +1 -0
  113. package/dist-types/components/navigation-bar/src/LionNavigationBar.d.ts +238 -0
  114. package/dist-types/components/navigation-bar/src/LionNavigationBar.d.ts.map +1 -0
  115. package/dist-types/components/navigation-bar/src/types.d.ts +85 -0
  116. package/dist-types/components/navigation-bar/src/types.d.ts.map +1 -0
  117. package/dist-types/components/overlays/src/OverlayController.d.ts +9 -13
  118. package/dist-types/components/overlays/src/OverlayController.d.ts.map +1 -1
  119. package/dist-types/components/overlays/src/OverlayMixin.d.ts.map +1 -1
  120. package/dist-types/components/overlays/src/utils/get-focusable-elements.d.ts.map +1 -1
  121. package/dist-types/components/overlays/src/utils/is-equal-config.d.ts.map +1 -1
  122. package/dist-types/components/overlays/types/OverlayConfig.d.ts +10 -2
  123. package/dist-types/components/overlays/types/OverlayConfig.d.ts.map +1 -1
  124. package/dist-types/components/select-rich/src/LionSelectRich.d.ts +8 -2
  125. package/dist-types/components/select-rich/src/LionSelectRich.d.ts.map +1 -1
  126. package/dist-types/exports/define/lion-item.d.ts +2 -0
  127. package/dist-types/exports/define/lion-item.d.ts.map +1 -0
  128. package/dist-types/exports/define/lion-menu-overlay.d.ts +2 -0
  129. package/dist-types/exports/define/lion-menu-overlay.d.ts.map +1 -0
  130. package/dist-types/exports/define/lion-menu.d.ts +2 -0
  131. package/dist-types/exports/define/lion-menu.d.ts.map +1 -0
  132. package/dist-types/exports/define/lion-navigation-bar.d.ts +2 -0
  133. package/dist-types/exports/define/lion-navigation-bar.d.ts.map +1 -0
  134. package/dist-types/exports/define/lion-toolbar.d.ts +2 -0
  135. package/dist-types/exports/define/lion-toolbar.d.ts.map +1 -0
  136. package/dist-types/exports/define/lion-tree.d.ts +2 -0
  137. package/dist-types/exports/define/lion-tree.d.ts.map +1 -0
  138. package/dist-types/exports/menu.d.ts +7 -0
  139. package/dist-types/exports/menu.d.ts.map +1 -0
  140. package/dist-types/exports/navigation-bar.d.ts +2 -0
  141. package/dist-types/exports/navigation-bar.d.ts.map +1 -0
  142. package/dist-types/exports/overlays.d.ts +3 -0
  143. package/dist-types/exports/overlays.d.ts.map +1 -1
  144. package/dist-types/tsconfig.tsbuildinfo +1 -1
  145. package/docs/components/checkbox-group/overview.md +2 -2
  146. package/docs/components/collapsible/overview.md +1 -1
  147. package/docs/components/combobox/api-table.md +21 -8
  148. package/docs/components/combobox/overview.md +1 -1
  149. package/docs/components/combobox/use-cases.md +1 -1
  150. package/docs/components/dialog/api-table.md +31 -6
  151. package/docs/components/dialog/overview.md +1 -1
  152. package/docs/components/fieldset/overview.md +5 -5
  153. package/docs/components/form/overview.md +3 -3
  154. package/docs/components/form/use-cases.md +2 -2
  155. package/docs/components/icon/use-cases.md +1 -1
  156. package/docs/components/input/overview.md +1 -1
  157. package/docs/components/input-amount/overview.md +3 -3
  158. package/docs/components/input-date/overview.md +3 -3
  159. package/docs/components/input-datepicker/api-table.md +21 -4
  160. package/docs/components/input-datepicker/overview.md +5 -5
  161. package/docs/components/input-email/overview.md +2 -2
  162. package/docs/components/input-file/overview.md +1 -1
  163. package/docs/components/input-iban/overview.md +2 -2
  164. package/docs/components/input-range/overview.md +2 -2
  165. package/docs/components/input-stepper/overview.md +1 -1
  166. package/docs/components/input-tel/overview.md +1 -1
  167. package/docs/components/input-tel/use-cases.md +6 -6
  168. package/docs/components/input-tel-dropdown/overview.md +1 -1
  169. package/docs/components/listbox/use-cases.md +1 -1
  170. package/docs/components/menu/api-table.md +450 -0
  171. package/docs/components/menu/applyMenuDemoStyles.js +173 -0
  172. package/docs/components/menu/extensions.md +235 -0
  173. package/docs/components/menu/index.md +13 -0
  174. package/docs/components/menu/overview.md +422 -0
  175. package/docs/components/radio-group/overview.md +3 -3
  176. package/docs/components/select/use-cases.md +1 -1
  177. package/docs/components/select-rich/api-table.md +21 -5
  178. package/docs/components/select-rich/use-cases.md +2 -2
  179. package/docs/components/tabs/examples.md +1 -1
  180. package/docs/components/tooltip/api-table.md +36 -11
  181. package/docs/fundamentals/index.md +4 -4
  182. package/docs/fundamentals/node-tools/publish-docs/overview.md +3 -3
  183. package/docs/fundamentals/node-tools/rocket-preset-extend-lion-docs/overview.md +2 -2
  184. package/docs/fundamentals/rationales/accessibility.md +1 -1
  185. package/docs/fundamentals/systems/core/SlotMixin.md +1 -1
  186. package/docs/fundamentals/systems/core/overview.md +1 -1
  187. package/docs/fundamentals/systems/form/formatting-and-parsing.md +4 -4
  188. package/docs/fundamentals/systems/form/model-value.md +1 -1
  189. package/docs/fundamentals/systems/form/overview.md +28 -28
  190. package/docs/fundamentals/systems/form/validate.md +2 -2
  191. package/docs/fundamentals/systems/localize/overview.md +4 -4
  192. package/docs/fundamentals/systems/overlays/api-table.md +34 -34
  193. package/docs/fundamentals/systems/overlays/configuration.md +2 -2
  194. package/docs/fundamentals/systems/overlays/overview.md +5 -5
  195. package/docs/fundamentals/systems/overlays/use-cases.md +2 -2
  196. package/docs/fundamentals/tools/helpers/overview.md +1 -1
  197. package/docs/fundamentals/tools/singleton-manager/overview.md +4 -4
  198. package/docs/guides/how-to/create-a-custom-field.md +5 -5
  199. package/docs/guides/how-to/create-a-form.md +4 -4
  200. package/docs/guides/how-to/extend-a-native-input.md +2 -2
  201. package/docs/guides/how-to/index.md +1 -1
  202. package/docs/guides/index.md +8 -8
  203. package/docs/guides/principles/index.md +5 -5
  204. package/docs/guides/principles/styling.md +1 -1
  205. package/exports/collapsible.js +1 -0
  206. package/exports/define/lion-item.js +3 -0
  207. package/exports/define/lion-menu-overlay.js +3 -0
  208. package/exports/define/lion-menu.js +3 -0
  209. package/exports/define/lion-navigation-bar.js +3 -0
  210. package/exports/define/lion-toolbar.js +3 -0
  211. package/exports/define/lion-tree.js +3 -0
  212. package/exports/menu.js +7 -0
  213. package/exports/navigation-bar.js +1 -0
  214. package/exports/overlays.js +6 -0
  215. package/menu.js +7 -0
  216. package/navigation-bar.js +1 -0
  217. package/overlays.js +6 -0
  218. package/package.json +1 -1
@@ -0,0 +1,805 @@
1
+ /* eslint-disable max-classes-per-file */
2
+ /* eslint-disable import/no-extraneous-dependencies */
3
+ import { html, css, LitElement } from 'lit';
4
+ import { SlotMixin, DisabledMixin } from '@lion/ui/core.js';
5
+ import { dedupeMixin } from '@open-wc/dedupe-mixin';
6
+ import { MoreButtonMenuMixin } from './MoreButtonMenuMixin.js';
7
+ import { uuid } from './utils/uuid.js';
8
+ import { isInView } from './utils/isInView.js';
9
+ import {
10
+ isDisabled,
11
+ isChecked,
12
+ setChecked,
13
+ toggleChecked,
14
+ isActive,
15
+ setActive,
16
+ getValue,
17
+ } from './utils/listItemInteractions.js';
18
+
19
+ /**
20
+ * @typedef {import('../types/InteractiveListMixinTypes.js').InteractiveListItemRole} InteractiveListItemRole
21
+ */
22
+
23
+ // TODO: consider renaming to FocusGroupMixin
24
+
25
+ // TODO: make all available in controller/directve (same logic with an elegent prop-to-host-mapping)
26
+
27
+ /**
28
+ * @param {Element} potentialFocusable
29
+ * @returns {Element|null}
30
+ */
31
+ function isFocusableElement(potentialFocusable) {
32
+ // @ts-ignore - returns Element or falsy value
33
+ return (
34
+ potentialFocusable &&
35
+ (potentialFocusable.hasAttribute('tabindex') ||
36
+ ['BUTTON', 'A'].includes(potentialFocusable.tagName))
37
+ );
38
+ }
39
+
40
+ /**
41
+ * Lightweight component supporting InteractiveListItemRole as a type.
42
+ *
43
+ * Supports:
44
+ * - 1. menu item
45
+ * <lion-item> Item label </lion-item>
46
+ * - 2. sub menu
47
+ * <lion-item>
48
+ * <button slot="invoker"> Item label </button>
49
+ * <lion-menu slot="subitems"> ... </lion-menu>
50
+ * </lion-item>
51
+ *
52
+ * Sets the role property on the right element. For case 1, this would be the item itself,
53
+ * for case 2 it would be the [slot=invoker].
54
+ */
55
+ export class LionItem extends LitElement {
56
+ /** @type {any} */
57
+ static get properties() {
58
+ return { type: String };
59
+ }
60
+
61
+ get _invokerNode() {
62
+ return Array.from(this.children).find(child => child.slot === 'invoker');
63
+ }
64
+
65
+ constructor() {
66
+ super();
67
+ /** @type {InteractiveListItemRole |''} */
68
+ this.type = '';
69
+ }
70
+
71
+ connectedCallback() {
72
+ super.connectedCallback();
73
+
74
+ if (!this._computedType) {
75
+ this._computedType = this.type || 'listitem';
76
+ }
77
+ if (this._invokerNode) {
78
+ this._invokerNode.setAttribute('role', this._computedType);
79
+ } else {
80
+ this.setAttribute('role', this._computedType);
81
+ }
82
+ }
83
+
84
+ static styles = [
85
+ css`
86
+ :host {
87
+ display: block;
88
+ }
89
+ `,
90
+ ];
91
+
92
+ render() {
93
+ return html`
94
+ <slot name="invoker"></slot>
95
+ <slot></slot>
96
+ `;
97
+ }
98
+ }
99
+
100
+ export class LionMenuitem extends LionItem {
101
+ connectedCallback() {
102
+ this._computedType = `menuitem${this.type}`;
103
+ super.connectedCallback();
104
+ }
105
+ }
106
+ customElements.define('lion-menuitem', LionMenuitem);
107
+
108
+ /**
109
+ * Handles keyboard interaction plus bookkeeping for active and selected states.
110
+ * Can be used as a fundament for:
111
+ * - [role="menu|menubar"] having children with [role="menuitem|menuitemcheckbox|menuitemradio"]
112
+ * - [role="listbox"] having children with [role="option"]
113
+ * - [role="toolbar"] having children with [role="radiogroup|radio|group|checkbox|spinbutton"]
114
+ * - [role="tree"] having children with [role="group|treeitem"]
115
+ *
116
+ * An interactive list accepts two types of items:
117
+ * - 'smart' items: responsible for registering themselves and reflecting accessibility properties,
118
+ * for example LionOption
119
+ * - 'regular' items with a [role="menuitem|menuitemcheckbox|menuitemradio|option|radio|checkbox|treeitem|listitem]
120
+ *
121
+ * Features:
122
+ * - keyboard navigation according to [aria specs](https://www.w3.org/TR/wai-aria-practices-1.1) for role
123
+ * - types
124
+ *
125
+ * @param {import('@open-wc/dedupe-mixin').Constructor<import('lit').LitElement>} superclass
126
+ */
127
+ const InteractiveListMixinImplementation = superclass =>
128
+ // @ts-ignore https://github.com/microsoft/TypeScript/issues/36821#issuecomment-588375051
129
+ class InteractiveListMixin extends DisabledMixin(SlotMixin(MoreButtonMenuMixin(superclass))) {
130
+ /** @type {any} */
131
+ static styles = [
132
+ css`
133
+ :host {
134
+ display: block;
135
+ }
136
+
137
+ :host([hidden]) {
138
+ display: none;
139
+ }
140
+
141
+ :host([disabled]) {
142
+ color: #adadad;
143
+ }
144
+
145
+ :host([orientation='horizontal']) ::slotted([slot='list']) {
146
+ display: flex;
147
+ }
148
+ `,
149
+ ];
150
+
151
+ render() {
152
+ return html`
153
+ <slot name="list"></slot>
154
+ <slot></slot>
155
+ `;
156
+ }
157
+
158
+ static get properties() {
159
+ return {
160
+ // TODO: align with name of open-ui => "inline|block" in focusgroup
161
+ orientation: { type: String, reflect: true },
162
+ // TODO: align with name of platform => ("multiple" in select, selection not part of focusgroup)
163
+ multipleChoice: { type: Boolean, attribute: 'multiple-choice' },
164
+ // TODO: align with name of open-ui
165
+ selectionFollowsFocus: { type: Boolean, attribute: 'selection-follows-focus' },
166
+ // TODO: align with name of open-ui => "wrap" in focsugroup
167
+ rotateKeyboardNavigation: { type: Boolean, attribute: 'rotate-keyboard-navigation' },
168
+ // TODO: align with name of open-ui
169
+ noPreselect: { type: Boolean, attribute: 'no-preselect' },
170
+
171
+ // TODO: implement, for now we start with only more-menu. See instructions in code about more menu.
172
+ // Values:
173
+ // - 'more-menu'
174
+ // - 'scroll-buttons' (think of those material design tab bars)
175
+ // - 'scroll-bar' (puts overflow: scroll on it) (think of tab bars, tables etc.)
176
+ itemWrap: { type: Boolean, attribute: 'item-wrap' },
177
+
178
+ _activateOnTypedChars: Boolean,
179
+ _hasSmartListItems: Boolean,
180
+ _activeMode: String,
181
+ };
182
+ }
183
+
184
+ get slots() {
185
+ return {
186
+ ...super.slots,
187
+ list: () => document.createElement('div'),
188
+ };
189
+ }
190
+
191
+ /**
192
+ * This is the (usually focusable, unless combobox) element that will contain [aria-activedescendant]
193
+ * Depending on the children, role will be "listbox|menu|menubar|tree|toolbar"
194
+ * @type {HTMLElement}
195
+ */
196
+ get _listNode() {
197
+ return /** @type {HTMLElement} */ (Array.from(this.children).find(c => c.slot === 'list'));
198
+ }
199
+
200
+ get _scrollTargetNode() {
201
+ return this;
202
+ }
203
+
204
+ /**
205
+ * Place in the template that contains children with ._childrenRoles
206
+ * @type {HTMLSlotElement}
207
+ */
208
+ get _listItemsSlot() {
209
+ const slots = Array.from(
210
+ /** @type {ShadowRoot} */ (this.shadowRoot).querySelectorAll('slot'),
211
+ );
212
+ return /** @type {HTMLSlotElement} */ (slots.find(s => !s.name));
213
+ }
214
+
215
+ /**
216
+ * When used with FormRegistrarMixin, children will register themselves. Otherwise,
217
+ * __listItems are gathered via a slotchange listener
218
+ * @type {HTMLElement[]}
219
+ */
220
+ get listItems() {
221
+ // @ts-ignore
222
+ return this.__listItems || this.formElements;
223
+ }
224
+
225
+ /**
226
+ * The roles to look for when children are registered to listItems
227
+ * @overridable
228
+ * @type {InteractiveListItemRole[]}
229
+ */
230
+ // eslint-disable-next-line class-methods-use-this
231
+ get _childrenRoles() {
232
+ // @ts-ignore - string type in array of InteractiveListItemRole
233
+ return [...this._interactiveChildrenRoles, 'listitem', 'group'];
234
+ }
235
+
236
+ // eslint-disable-next-line class-methods-use-this
237
+ get _interactiveChildrenRoles() {
238
+ return ['menuitem', 'menuitemcheckbox', 'menuitemradio', 'option', 'treeitem'];
239
+ }
240
+
241
+ get activeIndex() {
242
+ return this.listItems.findIndex(el => isActive(el));
243
+ }
244
+
245
+ set activeIndex(index) {
246
+ const activeItem = this.listItems[index];
247
+
248
+ if (!activeItem) return;
249
+
250
+ const prevActiveEl = this.activeItem;
251
+ // This can be a focusable element or a wrapper thereof
252
+ const el = this.listItems[index];
253
+
254
+ // Unset siblings
255
+ this.listItems.forEach(item => {
256
+ setActive(item, true);
257
+ });
258
+ setActive(el);
259
+
260
+ // Determine focusableEl and prevFocusableEl
261
+
262
+ let focusableEl;
263
+ let prevFocusableEl;
264
+
265
+ if (this._activeMode === 'activedescendant') {
266
+ focusableEl = el;
267
+ prevFocusableEl = prevActiveEl;
268
+ }
269
+ // activeMode is 'roving-tabindex' or 'tabbable-disclosure'
270
+ else {
271
+ // are we a focusable element or a wrapper thereof?
272
+ focusableEl = isFocusableElement(el) ? el : el.firstElementChild;
273
+ prevFocusableEl =
274
+ prevActiveEl &&
275
+ (isFocusableElement(prevActiveEl) ? prevActiveEl : prevActiveEl.firstElementChild);
276
+ }
277
+
278
+ if (!focusableEl) return;
279
+
280
+ // Update 'active mode'
281
+ if (this._activeMode === 'activedescendant') {
282
+ this._listNode.setAttribute('aria-activedescendant', focusableEl.id);
283
+ } else if (
284
+ this._activeMode === 'roving-tabindex' ||
285
+ this._activeMode === 'tabbable-disclosure'
286
+ ) {
287
+ // @ts-ignore - Element extends HTMLElement for focus
288
+ focusableEl.focus();
289
+ }
290
+
291
+ // @ts-ignore - focusableEl is safely used as HTMLElement here
292
+ if (!isInView(this._scrollTargetNode, focusableEl)) {
293
+ focusableEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
294
+ }
295
+
296
+ if (this._activeMode === 'roving-tabindex') {
297
+ if (prevFocusableEl) {
298
+ prevFocusableEl.setAttribute('tabindex', '-1');
299
+ }
300
+ focusableEl.setAttribute('tabindex', '0');
301
+ }
302
+ }
303
+
304
+ get activeItem() {
305
+ return this.listItems[this.activeIndex];
306
+ }
307
+
308
+ /**
309
+ * @type {number | number[]}
310
+ */
311
+ get checkedIndex() {
312
+ if (!this.multipleChoice) {
313
+ return this.listItems.findIndex(o => isChecked(o));
314
+ }
315
+ return this.listItems.filter(o => isChecked(o)).map(o => this.listItems.indexOf(o));
316
+ }
317
+
318
+ /**
319
+ * @deprecated
320
+ * This setter exists for backwards compatibility of single choice groups.
321
+ * A setter api would be confusing for a multipleChoice group. Use `setCheckedIndex` instead.
322
+ * @param {number} index
323
+ */
324
+ set checkedIndex(index) {
325
+ this.setCheckedIndex(index);
326
+ }
327
+
328
+ /**
329
+ * When `multipleChoice` is false, will toggle, else will check provided index
330
+ * @param {Number} index
331
+ */
332
+ setCheckedIndex(index) {
333
+ const item = this.listItems[index];
334
+ if (!item) return;
335
+
336
+ if (!this.multipleChoice) {
337
+ // Uncheck all
338
+ this.listItems.forEach(listItem => {
339
+ setChecked(listItem, false);
340
+ });
341
+ setChecked(this.listItems[index]);
342
+ } else {
343
+ toggleChecked(this.listItems[index]);
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Since we cannot check instanceof with mixins, use this getter instead.
349
+ * This way, the outside world can confirm the type of an element
350
+ */
351
+ // eslint-disable-next-line class-methods-use-this
352
+ get isInteractiveList() {
353
+ return true;
354
+ }
355
+
356
+ constructor() {
357
+ super();
358
+
359
+ // TODO: copy descriptions from ListboxMixin and ChoiceGroupMixin to here
360
+ this.orientation = 'vertical';
361
+ this.multipleChoice = false;
362
+ this.selectionFollowsFocus = false;
363
+ this.rotateKeyboardNavigation = false;
364
+
365
+ /**
366
+ * By default, checkedIndex is set to 0. When noPreselect is true,
367
+ * checkedIndex will not be set
368
+ * @type {boolean}
369
+ */
370
+ this.noPreselect = false;
371
+
372
+ /**
373
+ * Whenever possible (for [role="listbox|menu"], usually with one level) we use an
374
+ * [active descendant](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_focus_activedescendant).
375
+ * For [role=toolbar], we use a [roving tabindex](https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_roving_tabindex)
376
+ * Other examples:
377
+ * - [role="menubar"](https://www.w3.org/TR/wai-aria-practices-1.1/examples/menubar/menubar-1/menubar-1.html)
378
+ * - [role="tree"](https://www.w3.org/TR/wai-aria-practices-1.1/examples/treeview/treeview-1/treeview-1b.html)
379
+ * For disclosure menus, we need keyboard navigation, but treat every list item
380
+ * (more precisely its anchor child) as a tab stop. Also we apply aria-current
381
+ * @type {'activedescendant'|'roving-tabindex'|'tabbable-disclosure'|'none'}
382
+ */
383
+ this._activeMode = 'activedescendant';
384
+
385
+ /**
386
+ * When children are 'smart' (like LionOption), checked and active states will be set
387
+ * to them and they are responsible for setting [aria-selected].
388
+ * When false, `get listItems()` needs to be configured to point to the list items.
389
+ * @type {boolean}
390
+ */
391
+ this._hasSmartChildren = false;
392
+
393
+ /**
394
+ * The role that will be put on _listNode
395
+ * @type {'menu'|'menubar'|'listbox'|'tree'|'toolbar'}
396
+ */
397
+ this._listRole = 'menu';
398
+
399
+ /**
400
+ * @type {HTMLElement[]}
401
+ */
402
+ this.__listItems = [];
403
+
404
+ /**
405
+ * By default, a typed character (or a sequence thereof) will be matched against
406
+ * the values of list items. The first and closest match, becomes active
407
+ * (focused or activedescendant depending on _matchMode)
408
+ * @type {boolean}
409
+ */
410
+ this._activateOnTypedChars = false;
411
+
412
+ /**
413
+ * The pending char sequence that will set active list item
414
+ * @type {number}
415
+ */
416
+ this._activateOnCharTimeout = 2000;
417
+
418
+ /**
419
+ * @type {string[]}
420
+ */
421
+ this.__typedChars = [];
422
+
423
+ this._onListClick = this._onListClick.bind(this);
424
+ this._onListKeyUp = this._onListKeyUp.bind(this);
425
+ this._onListKeyDown = this._onListKeyDown.bind(this);
426
+ this._onListFocusIn = this._onListFocusIn.bind(this);
427
+ }
428
+
429
+ /**
430
+ * @param {import('lit').PropertyValues } changedProperties
431
+ */
432
+ firstUpdated(changedProperties) {
433
+ super.firstUpdated(changedProperties);
434
+ this._setupList();
435
+ }
436
+
437
+ /**
438
+ * @param {Node[]} nodes
439
+ */
440
+ #identifyNewItemsAndInitListItems(nodes) {
441
+ /**
442
+ *
443
+ * @param {HTMLElement} node
444
+ * @param {{ newItems: HTMLElement[], level?: number }} opts
445
+ * @returns
446
+ */
447
+ const handleInteractiveListAdditionLevel = (node, { newItems, level = 1 }) => {
448
+ // Move list item to element with aria-activedescendant ([role="listbox|menu|menubar"])
449
+ if (level === 1) {
450
+ this._listNode.appendChild(node);
451
+ if (this._hasSmartChildren || !(node instanceof Element)) {
452
+ return;
453
+ }
454
+ }
455
+ /**
456
+ * When [role=group], we can have a grouped set within a level, like multiple
457
+ * menuitemradios or -checkboxes.
458
+ * When no role at all, we might deal with a wrapped node (for instance triggering a
459
+ * submenu)
460
+ * Alternatively, when _activeMode is ['disclosure'](https://www.w3.org/TR/wai-aria-practices-1.1/examples/disclosure/disclosure-navigation.html),
461
+ * we search for a[href]
462
+ */
463
+ if (node.getAttribute('role') === 'group') {
464
+ // console.debug('checking children of', node, node.children);
465
+ // @ts-ignore - HTMLCollection is iterable
466
+ for (const child of node.children) {
467
+ // @ts-ignore
468
+ handleInteractiveListAdditionLevel(child, { newItems, level: level + 1 });
469
+ }
470
+ return;
471
+ }
472
+ // Also support scenarios like these: https://adrianroselli.com/2019/06/link-disclosure-widget-navigation.html
473
+ // TODO: For now, we only allow direct children. Should we consider deeper nesting?
474
+ const focusableChildren = Array.from(node.children).filter(c => isFocusableElement(c));
475
+ if (focusableChildren.length) {
476
+ for (const focusableChild of focusableChildren) {
477
+ // @ts-ignore - focusableChild is HTMLElement
478
+ newItems.push(focusableChild);
479
+ }
480
+ } else {
481
+ // Then we must be focusable (either via activedescendant, tabindex)
482
+ newItems.push(node);
483
+ }
484
+ };
485
+
486
+ /** @type {HTMLElement[]} */
487
+ const newItems = [];
488
+
489
+ // Move items to _listNode and add interactive node references (with [role]) to listItems
490
+ nodes.forEach(node => {
491
+ // @ts-expect-error - node type compatibility
492
+ handleInteractiveListAdditionLevel(node, { newItems });
493
+ });
494
+
495
+ // @ts-expect-error
496
+ if (this.itemWrap && nodes.length) {
497
+ // @ts-expect-error
498
+ this._initMoreButtonMenu();
499
+ }
500
+
501
+ newItems.forEach(item => {
502
+ if (!this.__listItems.includes(item)) {
503
+ this.__listItems.push(item);
504
+ }
505
+ });
506
+
507
+ this._initListItems(newItems);
508
+ }
509
+
510
+ _setupList() {
511
+ // /**
512
+ // * @param {HTMLElement} node
513
+ // */
514
+ // const shouldAddItem = node => {
515
+ // const role = /** @type {InteractiveListItemRole|'group'} */ (node.getAttribute('role'));
516
+ // return (
517
+ // (role && this._interactiveChildrenRoles.includes(role)) || node.hasAttribute('data-item')
518
+ // );
519
+ // };
520
+
521
+ this._listItemsSlot.addEventListener('slotchange', () => {
522
+ // N.B. we do not use `this._listItemsSlot.assignedNodes()`, as it omits comment nodes.
523
+ // Comment nodes are really important for lit-html)
524
+ const nodes = Array.from(
525
+ (this._listItemsSlot.assignedSlot || this).childNodes || [],
526
+ // @ts-expect-error
527
+ ).filter(n => !n.slot);
528
+ this.#identifyNewItemsAndInitListItems(nodes);
529
+ });
530
+
531
+ this._listNode.setAttribute('role', this._listRole);
532
+ this._listNode.setAttribute(
533
+ 'tabindex',
534
+ this._activeMode === 'tabbable-disclosure' ? '-1' : '0',
535
+ );
536
+ this._listNode.addEventListener('click', this._onListClick);
537
+ this._listNode.addEventListener('keyup', this._onListKeyUp);
538
+ this._listNode.addEventListener('keydown', this._onListKeyDown);
539
+ this._listNode.addEventListener('focusin', this._onListFocusIn);
540
+
541
+ // TODO: add "more menu" functionality here... when itemWrap is set. We only support "more-menu" (for now):
542
+ // 1. we can measure the width (if orientation is horizontal) of this._listNode
543
+ // and see how its children fit, moving them to an overlayController
544
+ // having hideVisually as hide mechanism (opening on focus, closing on blur).
545
+ // N.B. this._listNode has `display:flex`. This means we need to add `text-wrap: nowrap;`
546
+ // to avoid elems going over two lines, or give them a max-height based on measuring the height of one item.
547
+ // 2. N.B. hideVisually needs to be implementd in OverlayController.
548
+ // 3. when the more menu opens a next level, MultiLevelListMixin will be applied to the host as well.
549
+ // That means it knows when a child menu is open. If _subListMap of one of the items is open, we should hide more menu like this:
550
+ // https://jsfiddle.net/a4ocktfp/19/
551
+ }
552
+
553
+ /**
554
+ * When _listItemsSlot receives new items, they are initialized via this method
555
+ * @param {LionItem[]|HTMLElement[]} newItems the newly added items needing initialization
556
+ */
557
+ _initListItems(newItems) {
558
+ if (this._activeMode === 'roving-tabindex') {
559
+ // Make item focusable, but not part of tab sequence
560
+ newItems.forEach(item => item.setAttribute?.('tabindex', '-1'));
561
+ } else if (this._activeMode === 'activedescendant') {
562
+ newItems.forEach(item => {
563
+ // eslint-disable-next-line no-param-reassign
564
+ item.id = item.id || `${this.localName}-item-${uuid()}`;
565
+ });
566
+ } // for 'tabbable-disclosure', we assume items are already focusable and part of tab sequence
567
+
568
+ if (!this.noPreselect && this.checkedIndex === -1) {
569
+ this.checkedIndex = 0;
570
+ }
571
+ }
572
+
573
+ /**
574
+ * Handles various keyboard controls
575
+ * @param {KeyboardEvent} ev - the keydown event object
576
+ */
577
+ _onListKeyDown(ev) {
578
+ if (isDisabled(this)) {
579
+ return;
580
+ }
581
+
582
+ const charHandled = this.__onKeydownActivateTypedChars(ev);
583
+ if (charHandled) {
584
+ return;
585
+ }
586
+
587
+ const { key } = ev;
588
+
589
+ switch (key) {
590
+ case 'Enter':
591
+ case ' ':
592
+ ev.preventDefault();
593
+ // ev.stopPropagation(); // prevent nested menus from conflicting
594
+ this.setCheckedIndex(this.activeIndex);
595
+ break;
596
+ case 'ArrowUp':
597
+ ev.preventDefault();
598
+ if (this.orientation === 'vertical' || this._activeMode === 'tabbable-disclosure') {
599
+ this.activeIndex = this._getPreviousEnabledOption(this.activeIndex);
600
+ }
601
+ break;
602
+ case 'ArrowLeft':
603
+ if (this.orientation === 'horizontal' || this._activeMode === 'tabbable-disclosure') {
604
+ this.activeIndex = this._getPreviousEnabledOption(this.activeIndex);
605
+ }
606
+ break;
607
+ case 'ArrowDown':
608
+ ev.preventDefault();
609
+ if (this.orientation === 'vertical' || this._activeMode === 'tabbable-disclosure') {
610
+ this.activeIndex = this._getNextEnabledOption(this.activeIndex);
611
+ }
612
+ break;
613
+ case 'ArrowRight':
614
+ if (this.orientation === 'horizontal' || this._activeMode === 'tabbable-disclosure') {
615
+ this.activeIndex = this._getNextEnabledOption(this.activeIndex);
616
+ }
617
+ break;
618
+ case 'Home':
619
+ ev.preventDefault();
620
+ this.activeIndex = this._getNextEnabledOption(0, 0);
621
+ break;
622
+ case 'End':
623
+ ev.preventDefault();
624
+ this.activeIndex = this._getPreviousEnabledOption(this.listItems.length - 1, 0);
625
+ break;
626
+ /* no default */
627
+ }
628
+
629
+ const keys = ['ArrowUp', 'ArrowDown', 'Home', 'End'];
630
+ if (keys.includes(key) && this.selectionFollowsFocus && !this.multipleChoice) {
631
+ this.setCheckedIndex(this.activeIndex);
632
+ }
633
+ }
634
+
635
+ /**
636
+ * @param {KeyboardEvent} ev - the keydown event object
637
+ * @returns {boolean} whether a character key or a number was typed
638
+ */
639
+ __onKeydownActivateTypedChars(ev) {
640
+ if (!this._activateOnTypedChars) {
641
+ return false;
642
+ }
643
+
644
+ const { key, code } = ev;
645
+
646
+ if (code.startsWith('Key') || code.startsWith('Digit') || code.startsWith('Numpad')) {
647
+ this.__typedChars.push(key);
648
+ // TODO: schedule in updated loop for perf
649
+ const chars = this.__typedChars.join('');
650
+ const matchItem = this.listItems.find(item =>
651
+ this._matchCharsAgainstItem({ chars: [chars], item }),
652
+ );
653
+ // @ts-ignore - matchItem might be undefined
654
+ this.activeIndex = this.listItems.indexOf(matchItem);
655
+
656
+ // @ts-ignore - __clearCharTimeout can be a function
657
+ if (typeof this.__clearCharTimeout === 'function') {
658
+ // @ts-ignore
659
+ this.__clearCharTimeout();
660
+ }
661
+ this.__clearCharTimeout = setTimeout(() => {
662
+ this.__typedChars = [];
663
+ }, this._activateOnCharTimeout);
664
+
665
+ return true;
666
+ }
667
+ return false;
668
+ }
669
+
670
+ /**
671
+ * @overridable
672
+ * @param {{chars: string[], item: HTMLElement}} config
673
+ */
674
+ // eslint-disable-next-line class-methods-use-this
675
+ _matchCharsAgainstItem({ chars, item }) {
676
+ const value = getValue(item).toLowerCase();
677
+ return value.startsWith(chars);
678
+ }
679
+
680
+ /**
681
+ * @param {KeyboardEvent} ev
682
+ */
683
+ __preventScrollingWithArrowKeys(ev) {
684
+ if (this.disabled) {
685
+ return;
686
+ }
687
+ const { key } = ev;
688
+ switch (key) {
689
+ case 'ArrowUp':
690
+ case 'ArrowDown':
691
+ case 'Home':
692
+ case 'End':
693
+ ev.preventDefault();
694
+ /* no default */
695
+ }
696
+ }
697
+
698
+ /**
699
+ * @overridable
700
+ * @param {FocusEvent} ev
701
+ */
702
+ _onListFocusIn(ev) {
703
+ /** @type {HTMLElement} */
704
+ // @ts-ignore - target is EventTarget
705
+ const { target } = ev;
706
+ // const item = /** @type {HTMLElement} */ (target.closest('[role]'));
707
+ // @ts-ignore - target is HTMLElement
708
+ const foundIndex = this.listItems.indexOf(target);
709
+ // console.debug('focusin', target, foundIndex, this.listItems);
710
+ const previousActiveIndex = this.activeIndex;
711
+ if (foundIndex > -1 && foundIndex !== this.activeIndex) {
712
+ this.activeIndex = foundIndex;
713
+ if (this._activeMode === 'tabbable-disclosure') {
714
+ const previousActiveItem = this.listItems[previousActiveIndex];
715
+ previousActiveItem?.removeAttribute('aria-current');
716
+ const activeItem = this.listItems[this.activeIndex];
717
+ activeItem.setAttribute('aria-current', 'page');
718
+ }
719
+ }
720
+ }
721
+
722
+ /**
723
+ * @overridable
724
+ * @param {MouseEvent} ev
725
+ */
726
+ // eslint-disable-next-line class-methods-use-this, no-unused-vars
727
+ _onListClick(ev) {
728
+ /** @type {HTMLElement} */
729
+ // @ts-ignore - target is EventTarget
730
+ const { target } = ev;
731
+ const item = /** @type {HTMLElement} */ (target.closest('[role]')); // TODO: we might need a more specific contract
732
+ const foundIndex = this.listItems.indexOf(item);
733
+ if (foundIndex > -1) {
734
+ this.activeIndex = foundIndex;
735
+ this.setCheckedIndex(foundIndex);
736
+ }
737
+ }
738
+
739
+ /**
740
+ * @overridable
741
+ * @param {KeyboardEvent} ev
742
+ */
743
+ // eslint-disable-next-line class-methods-use-this, no-unused-vars
744
+ _onListKeyUp(ev) {
745
+ if (this.disabled) {
746
+ return;
747
+ }
748
+ const { key } = ev;
749
+ // eslint-disable-next-line default-case
750
+ switch (key) {
751
+ case 'ArrowUp':
752
+ case 'ArrowDown':
753
+ case 'Home':
754
+ case 'End':
755
+ case ' ':
756
+ case 'Enter':
757
+ ev.preventDefault();
758
+ }
759
+ }
760
+
761
+ /**
762
+ * @param {number} currentIndex
763
+ * @param {number} offset
764
+ */
765
+ __getNextOption(currentIndex, offset) {
766
+ /**
767
+ * @param {number} i
768
+ */
769
+ const until = i => (offset === 1 ? i < this.listItems.length : i >= 0);
770
+
771
+ for (let i = currentIndex + offset; until(i); i += offset) {
772
+ if (this.listItems[i] && !isDisabled(this.listItems[i])) {
773
+ return i;
774
+ }
775
+ }
776
+
777
+ if (this.rotateKeyboardNavigation) {
778
+ const startIndex = offset === -1 ? this.listItems.length - 1 : 0;
779
+ for (let i = startIndex; until(i); i += 1) {
780
+ if (this.listItems[i] && !isDisabled(this.listItems[i])) {
781
+ return i;
782
+ }
783
+ }
784
+ }
785
+ return currentIndex;
786
+ }
787
+
788
+ /**
789
+ * @param {number} currentIndex
790
+ * @param {number} [offset=1]
791
+ */
792
+ _getNextEnabledOption(currentIndex, offset = 1) {
793
+ return this.__getNextOption(currentIndex, offset);
794
+ }
795
+
796
+ /**
797
+ * @param {number} currentIndex
798
+ * @param {number} [offset=-1]
799
+ */
800
+ _getPreviousEnabledOption(currentIndex, offset = -1) {
801
+ return this.__getNextOption(currentIndex, offset);
802
+ }
803
+ };
804
+ // @ts-ignore
805
+ export const InteractiveListMixin = dedupeMixin(InteractiveListMixinImplementation);