@webtides/element-library 0.3.1 → 0.5.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 (252) hide show
  1. package/CHANGELOG.md +300 -0
  2. package/README.md +72 -56
  3. package/all.js +16 -0
  4. package/catalog.js +17 -1
  5. package/custom-elements.json +15912 -10065
  6. package/package.json +1 -1
  7. package/src/components/accordion/accordion.changelog.md +11 -0
  8. package/src/components/accordion/accordion.js +15 -3
  9. package/src/components/accordion/accordion.readme.md +27 -1
  10. package/src/components/accordion/accordion.style.js +19 -0
  11. package/src/components/app-bar/app-bar.changelog.md +24 -0
  12. package/src/components/app-bar/app-bar.define.js +3 -0
  13. package/src/components/app-bar/app-bar.js +91 -0
  14. package/src/components/app-bar/app-bar.readme.md +130 -0
  15. package/src/components/app-bar/app-bar.style.js +103 -0
  16. package/src/components/app-frame/app-frame.changelog.md +59 -0
  17. package/src/components/app-frame/app-frame.define.js +3 -0
  18. package/src/components/app-frame/app-frame.events.js +6 -0
  19. package/src/components/app-frame/app-frame.js +1085 -0
  20. package/src/components/app-frame/app-frame.readme.md +425 -0
  21. package/src/components/app-frame/app-frame.style.js +534 -0
  22. package/src/components/button/button.changelog.md +21 -0
  23. package/src/components/button/button.events.js +3 -0
  24. package/src/components/button/button.js +145 -3
  25. package/src/components/button/button.readme.md +72 -4
  26. package/src/components/button/button.style.js +167 -8
  27. package/src/components/button-group/button-group.changelog.md +10 -0
  28. package/src/components/button-group/button-group.readme.md +2 -0
  29. package/src/components/button-group/button-group.style.js +6 -2
  30. package/src/components/checkbox-group-field/checkbox-group-field.changelog.md +6 -0
  31. package/src/components/checkbox-group-field/checkbox-group-field.js +7 -2
  32. package/src/components/checkbox-group-field/checkbox-group-field.readme.md +23 -1
  33. package/src/components/combobox-field/combobox-field.changelog.md +6 -0
  34. package/src/components/combobox-field/combobox-field.readme.md +8 -0
  35. package/src/components/combobox-field/combobox-field.style.js +2 -0
  36. package/src/components/date-field/date-field.changelog.md +6 -0
  37. package/src/components/date-field/date-field.readme.md +10 -0
  38. package/src/components/date-field/date-field.style.js +8 -3
  39. package/src/components/description-list/description-list.changelog.md +42 -0
  40. package/src/components/description-list/description-list.define.js +3 -0
  41. package/src/components/description-list/description-list.js +49 -0
  42. package/src/components/description-list/description-list.readme.md +161 -0
  43. package/src/components/description-list/description-list.style.js +169 -0
  44. package/src/components/dialog/dialog.changelog.md +7 -0
  45. package/src/components/dialog/dialog.readme.md +5 -0
  46. package/src/components/dialog/dialog.style.js +17 -2
  47. package/src/components/divider/divider.changelog.md +6 -0
  48. package/src/components/divider/divider.readme.md +1 -1
  49. package/src/components/divider/divider.style.js +6 -0
  50. package/src/components/drawer/drawer.changelog.md +7 -0
  51. package/src/components/drawer/drawer.readme.md +7 -6
  52. package/src/components/drawer/drawer.style.js +35 -2
  53. package/src/components/dropdown/dropdown.readme.md +6 -1
  54. package/src/components/file-field/file-field.changelog.md +8 -0
  55. package/src/components/file-field/file-field.js +135 -5
  56. package/src/components/file-field/file-field.readme.md +35 -6
  57. package/src/components/file-field/file-field.style.js +71 -0
  58. package/src/components/form-field/form-field.js +1 -1
  59. package/src/components/input-field/input-field.changelog.md +10 -0
  60. package/src/components/input-field/input-field.js +192 -14
  61. package/src/components/input-field/input-field.readme.md +88 -2
  62. package/src/components/input-field/input-field.style.js +2 -0
  63. package/src/components/list/list.changelog.md +30 -0
  64. package/src/components/list/list.define.js +3 -0
  65. package/src/components/list/list.js +68 -0
  66. package/src/components/list/list.readme.md +123 -0
  67. package/src/components/list/list.style.js +56 -0
  68. package/src/components/list-item/list-item.changelog.md +40 -0
  69. package/src/components/list-item/list-item.define.js +3 -0
  70. package/src/components/list-item/list-item.js +130 -0
  71. package/src/components/list-item/list-item.readme.md +144 -0
  72. package/src/components/list-item/list-item.style.js +246 -0
  73. package/src/components/menu/menu.changelog.md +44 -0
  74. package/src/components/menu/menu.define.js +3 -0
  75. package/src/components/menu/menu.events.js +5 -0
  76. package/src/components/menu/menu.js +793 -0
  77. package/src/components/menu/menu.readme.md +313 -0
  78. package/src/components/menu/menu.style.js +59 -0
  79. package/src/components/menu-group/menu-group.changelog.md +20 -0
  80. package/src/components/menu-group/menu-group.define.js +3 -0
  81. package/src/components/menu-group/menu-group.js +44 -0
  82. package/src/components/menu-group/menu-group.readme.md +74 -0
  83. package/src/components/menu-group/menu-group.style.js +38 -0
  84. package/src/components/menu-item/menu-item.changelog.md +32 -0
  85. package/src/components/menu-item/menu-item.define.js +3 -0
  86. package/src/components/menu-item/menu-item.js +168 -0
  87. package/src/components/menu-item/menu-item.readme.md +132 -0
  88. package/src/components/menu-item/menu-item.style.js +117 -0
  89. package/src/components/nav/nav.changelog.md +35 -0
  90. package/src/components/nav/nav.define.js +3 -0
  91. package/src/components/nav/nav.js +212 -0
  92. package/src/components/nav/nav.readme.md +248 -0
  93. package/src/components/nav/nav.style.js +269 -0
  94. package/src/components/nav-item/nav-item.changelog.md +25 -0
  95. package/src/components/nav-item/nav-item.define.js +3 -0
  96. package/src/components/nav-item/nav-item.js +75 -0
  97. package/src/components/nav-item/nav-item.readme.md +111 -0
  98. package/src/components/nav-item/nav-item.style.js +159 -0
  99. package/src/components/otp-field/otp-field.changelog.md +6 -0
  100. package/src/components/otp-field/otp-field.readme.md +8 -0
  101. package/src/components/otp-field/otp-field.style.js +8 -3
  102. package/src/components/pagination/pagination.changelog.md +8 -0
  103. package/src/components/pagination/pagination.js +123 -49
  104. package/src/components/pagination/pagination.readme.md +24 -6
  105. package/src/components/pagination/pagination.style.js +11 -1
  106. package/src/components/password-field/password-field.changelog.md +7 -0
  107. package/src/components/password-field/password-field.readme.md +11 -1
  108. package/src/components/password-field/password-field.style.js +2 -0
  109. package/src/components/popover/popover.changelog.md +6 -0
  110. package/src/components/popover/popover.style.js +1 -0
  111. package/src/components/radio-field/radio-field.changelog.md +6 -0
  112. package/src/components/radio-field/radio-field.js +7 -2
  113. package/src/components/radio-field/radio-field.readme.md +23 -1
  114. package/src/components/scroll-to-top/scroll-to-top.changelog.md +7 -5
  115. package/src/components/scroll-to-top/scroll-to-top.js +16 -12
  116. package/src/components/scroll-to-top/scroll-to-top.readme.md +2 -0
  117. package/src/components/segmented-field/segmented-field.changelog.md +27 -0
  118. package/src/components/segmented-field/segmented-field.define.js +3 -0
  119. package/src/components/segmented-field/segmented-field.js +175 -0
  120. package/src/components/segmented-field/segmented-field.readme.md +140 -0
  121. package/src/components/segmented-field/segmented-field.style.js +193 -0
  122. package/src/components/splitter/splitter.changelog.md +24 -0
  123. package/src/components/splitter/splitter.define.js +3 -0
  124. package/src/components/splitter/splitter.events.js +4 -0
  125. package/src/components/splitter/splitter.js +237 -0
  126. package/src/components/splitter/splitter.readme.md +164 -0
  127. package/src/components/splitter/splitter.style.js +100 -0
  128. package/src/components/status-bar/status-bar.changelog.md +25 -0
  129. package/src/components/status-bar/status-bar.define.js +3 -0
  130. package/src/components/status-bar/status-bar.js +74 -0
  131. package/src/components/status-bar/status-bar.readme.md +139 -0
  132. package/src/components/status-bar/status-bar.style.js +122 -0
  133. package/src/components/tab-group/tab-group.changelog.md +7 -0
  134. package/src/components/tab-group/tab-group.js +30 -9
  135. package/src/components/tab-group/tab-group.readme.md +14 -0
  136. package/src/components/textarea-field/textarea-field.changelog.md +6 -0
  137. package/src/components/textarea-field/textarea-field.js +14 -1
  138. package/src/components/textarea-field/textarea-field.readme.md +23 -1
  139. package/src/components/toast/toast.changelog.md +6 -0
  140. package/src/components/toast/toast.readme.md +5 -3
  141. package/src/components/toast/toast.style.js +22 -6
  142. package/src/components/toolbar/toolbar.changelog.md +24 -0
  143. package/src/components/toolbar/toolbar.define.js +3 -0
  144. package/src/components/toolbar/toolbar.js +233 -0
  145. package/src/components/toolbar/toolbar.readme.md +156 -0
  146. package/src/components/toolbar/toolbar.style.js +49 -0
  147. package/src/components/tooltip/tooltip.changelog.md +11 -0
  148. package/src/components/tooltip/tooltip.js +51 -4
  149. package/src/components/tooltip/tooltip.readme.md +26 -1
  150. package/src/components/tree/tree.changelog.md +23 -0
  151. package/src/components/tree/tree.define.js +3 -0
  152. package/src/components/tree/tree.js +138 -0
  153. package/src/components/tree/tree.readme.md +119 -0
  154. package/src/components/tree/tree.style.js +16 -0
  155. package/src/components/tree-item/tree-item.changelog.md +23 -0
  156. package/src/components/tree-item/tree-item.define.js +3 -0
  157. package/src/components/tree-item/tree-item.events.js +3 -0
  158. package/src/components/tree-item/tree-item.js +246 -0
  159. package/src/components/tree-item/tree-item.readme.md +128 -0
  160. package/src/components/tree-item/tree-item.style.js +226 -0
  161. package/src/styles/appearance.style.js +47 -0
  162. package/src/styles/choice-group.style.js +27 -1
  163. package/src/styles/input-control.style.js +87 -0
  164. package/src/styles/modal.style.js +14 -1
  165. package/src/styles/native.css +100 -0
  166. package/src/styles/size.style.js +41 -0
  167. package/src/utils/floating.js +15 -1
  168. package/src/utils/focus.js +69 -0
  169. package/src/utils/options.js +35 -0
  170. package/src/utils/scroll.js +24 -0
  171. package/src/utils/text-entry-attributes.js +13 -0
  172. package/types/all.d.ts +16 -0
  173. package/types/src/components/accordion/accordion.d.ts +3 -1
  174. package/types/src/components/app-bar/app-bar.d.ts +43 -0
  175. package/types/src/components/app-bar/app-bar.define.d.ts +1 -0
  176. package/types/src/components/app-bar/app-bar.style.d.ts +2 -0
  177. package/types/src/components/app-frame/app-frame.d.ts +497 -0
  178. package/types/src/components/app-frame/app-frame.define.d.ts +1 -0
  179. package/types/src/components/app-frame/app-frame.events.d.ts +7 -0
  180. package/types/src/components/app-frame/app-frame.style.d.ts +8 -0
  181. package/types/src/components/button/button.d.ts +61 -0
  182. package/types/src/components/button/button.events.d.ts +4 -0
  183. package/types/src/components/checkbox-group-field/checkbox-group-field.d.ts +5 -2
  184. package/types/src/components/combobox-field/combobox-field.d.ts +10 -0
  185. package/types/src/components/date-field/date-field.d.ts +10 -0
  186. package/types/src/components/description-list/description-list.d.ts +34 -0
  187. package/types/src/components/description-list/description-list.define.d.ts +1 -0
  188. package/types/src/components/description-list/description-list.style.d.ts +2 -0
  189. package/types/src/components/file-field/file-field.d.ts +68 -3
  190. package/types/src/components/form-field/form-field.d.ts +1 -1
  191. package/types/src/components/input-field/input-field.d.ts +99 -0
  192. package/types/src/components/list/list.d.ts +41 -0
  193. package/types/src/components/list/list.define.d.ts +1 -0
  194. package/types/src/components/list/list.style.d.ts +2 -0
  195. package/types/src/components/list-item/list-item.d.ts +64 -0
  196. package/types/src/components/list-item/list-item.define.d.ts +1 -0
  197. package/types/src/components/list-item/list-item.style.d.ts +2 -0
  198. package/types/src/components/menu/menu.d.ts +362 -0
  199. package/types/src/components/menu/menu.define.d.ts +1 -0
  200. package/types/src/components/menu/menu.events.d.ts +6 -0
  201. package/types/src/components/menu/menu.style.d.ts +2 -0
  202. package/types/src/components/menu-group/menu-group.d.ts +24 -0
  203. package/types/src/components/menu-group/menu-group.define.d.ts +1 -0
  204. package/types/src/components/menu-group/menu-group.style.d.ts +2 -0
  205. package/types/src/components/menu-item/menu-item.d.ts +80 -0
  206. package/types/src/components/menu-item/menu-item.define.d.ts +1 -0
  207. package/types/src/components/menu-item/menu-item.style.d.ts +2 -0
  208. package/types/src/components/nav/nav.d.ts +103 -0
  209. package/types/src/components/nav/nav.define.d.ts +1 -0
  210. package/types/src/components/nav/nav.style.d.ts +2 -0
  211. package/types/src/components/nav-item/nav-item.d.ts +39 -0
  212. package/types/src/components/nav-item/nav-item.define.d.ts +1 -0
  213. package/types/src/components/nav-item/nav-item.style.d.ts +2 -0
  214. package/types/src/components/otp-field/otp-field.d.ts +10 -0
  215. package/types/src/components/pagination/pagination.d.ts +31 -2
  216. package/types/src/components/password-field/password-field.d.ts +10 -0
  217. package/types/src/components/radio-field/radio-field.d.ts +5 -2
  218. package/types/src/components/scroll-to-top/scroll-to-top.d.ts +6 -5
  219. package/types/src/components/segmented-field/segmented-field.d.ts +85 -0
  220. package/types/src/components/segmented-field/segmented-field.define.d.ts +1 -0
  221. package/types/src/components/segmented-field/segmented-field.style.d.ts +2 -0
  222. package/types/src/components/splitter/splitter.d.ts +127 -0
  223. package/types/src/components/splitter/splitter.define.d.ts +1 -0
  224. package/types/src/components/splitter/splitter.events.d.ts +5 -0
  225. package/types/src/components/splitter/splitter.style.d.ts +2 -0
  226. package/types/src/components/status-bar/status-bar.d.ts +44 -0
  227. package/types/src/components/status-bar/status-bar.define.d.ts +1 -0
  228. package/types/src/components/status-bar/status-bar.style.d.ts +2 -0
  229. package/types/src/components/tab-group/tab-group.d.ts +17 -0
  230. package/types/src/components/textarea-field/textarea-field.d.ts +7 -0
  231. package/types/src/components/toolbar/toolbar.d.ts +96 -0
  232. package/types/src/components/toolbar/toolbar.define.d.ts +1 -0
  233. package/types/src/components/toolbar/toolbar.style.d.ts +2 -0
  234. package/types/src/components/tooltip/tooltip.d.ts +26 -0
  235. package/types/src/components/tree/tree.d.ts +62 -0
  236. package/types/src/components/tree/tree.define.d.ts +1 -0
  237. package/types/src/components/tree/tree.style.d.ts +2 -0
  238. package/types/src/components/tree-item/tree-item.d.ts +106 -0
  239. package/types/src/components/tree-item/tree-item.define.d.ts +1 -0
  240. package/types/src/components/tree-item/tree-item.events.d.ts +4 -0
  241. package/types/src/components/tree-item/tree-item.style.d.ts +2 -0
  242. package/types/src/styles/appearance.style.d.ts +31 -0
  243. package/types/src/styles/choice-group.style.d.ts +1 -1
  244. package/types/src/styles/input-control.style.d.ts +15 -0
  245. package/types/src/styles/modal.style.d.ts +5 -0
  246. package/types/src/styles/size.style.d.ts +24 -0
  247. package/types/src/utils/floating.d.ts +11 -0
  248. package/types/src/utils/focus.d.ts +25 -0
  249. package/types/src/utils/options.d.ts +17 -0
  250. package/types/src/utils/scroll.d.ts +11 -0
  251. package/types/src/utils/text-entry-attributes.d.ts +10 -0
  252. package/web-types.json +2935 -308
@@ -0,0 +1,793 @@
1
+ import { TemplateElement, defineElement, html } from '@webtides/element-js';
2
+ import { activeElement } from '../../utils/focus.js';
3
+ import { followAnchor, placeAtAnchor, scrollersOf } from '../../utils/floating.js';
4
+ import style from './menu.style.js';
5
+ import Events from './menu.events.js';
6
+
7
+ /**
8
+ * The menu that is open. One at a time: a menu that opens closes the one before it.
9
+ * @type {HTMLElement | undefined}
10
+ */
11
+ let openMenu;
12
+
13
+ /**
14
+ * A menu button: a button that opens a list of actions. "Edit, Duplicate, Delete" for a row of a
15
+ * table, the account in a header, "More" at the end of a toolbar. The button is what the page
16
+ * puts into the `trigger` slot, the actions are `el-menu-item` elements.
17
+ *
18
+ * To assistive technology it is a menu (`role="menu"`) of menu items, and the button says that it
19
+ * opens one and whether it is open. The keyboard moves through the entries with the arrow keys,
20
+ * `Home`, `End` and the first letter of an entry. `Escape` closes the menu and gives the focus
21
+ * back to the button, the Tab key closes it and moves on.
22
+ *
23
+ * The panel is a native popover: it is in the top layer, so nothing clips it and it moves nothing
24
+ * on the page. It is placed at the button, at the side that `placement` names, on the other side
25
+ * without room there, and never outside of the viewport. A press beside it closes it.
26
+ *
27
+ * Choosing an entry closes the menu and fires `menu-select`.
28
+ *
29
+ * One menu can serve many things, the rows of a list: `showAt()` opens it at a point of the
30
+ * viewport, for a right click or a long press, or at an element of the page, a button of a row.
31
+ * What it was opened for is the `opener`, which gets the focus back and which `menu-select`
32
+ * names.
33
+ *
34
+ * @element el-menu
35
+ *
36
+ * @fires {CustomEvent} menu-show - Fired when the menu has opened.
37
+ * @fires {CustomEvent} menu-hide - Fired when the menu has closed.
38
+ * @fires {CustomEvent<{ item: HTMLElement, value: string, opener: Element | undefined }>} menu-select -
39
+ * Fired when the user has chosen an entry: the `el-menu-item` is in `detail.item`, its `value`
40
+ * in `detail.value`, and what the menu was opened for in `detail.opener`. The menu closes after
41
+ * it.
42
+ *
43
+ * @slot - The entries: `el-menu-item` elements, in `el-menu-group` elements where they are
44
+ * divided into groups. An `el-divider` is a line between two of them.
45
+ * @slot trigger - The button that opens the menu. It gets `aria-haspopup="menu"` and
46
+ * `aria-expanded`.
47
+ *
48
+ * @csspart trigger - Forwarded onto the `trigger` slot.
49
+ * @csspart panel - The floating panel, which is the menu (`role="menu"`): background, border,
50
+ * radius, elevation, width.
51
+ *
52
+ * @cssstate open - Set while the menu is shown.
53
+ *
54
+ * @property {boolean} open - Whether the menu is shown. Reflected to the `open` attribute.
55
+ * @property {string} label - The name of the menu for assistive technology. Without one the menu
56
+ * has the name of its button.
57
+ * @property {'top'|'top-start'|'top-end'|'bottom'|'bottom-start'|'bottom-end'|'left'|'left-start'|'left-end'|'right'|'right-start'|'right-end'} placement -
58
+ * The side of the button the menu is placed at, and with `-start` or `-end` the edge of the
59
+ * button it is aligned with. Without room there, the menu goes to the opposite side. Default
60
+ * `'bottom-start'`.
61
+ * @property {number} distance - The gap between the button and the menu, in pixels. Default `4`.
62
+ */
63
+ export default class Menu extends TemplateElement {
64
+ /**
65
+ * Which entry gets the focus when the menu opens: the first one, or the last one for
66
+ * `ArrowUp` on the button.
67
+ * @private
68
+ * @type {'first'|'last'}
69
+ */
70
+ _enterAt = 'first';
71
+
72
+ /**
73
+ * Whether the focus goes back to the button when the menu closes. It does after a choice and
74
+ * after `Escape`, and not where the user has moved it on.
75
+ * @private
76
+ */
77
+ _restoreFocus = false;
78
+
79
+ /**
80
+ * Where the menu is placed while it is open somewhere else than at its own button: an element
81
+ * of the page, or a point of the viewport.
82
+ * @private
83
+ * @type {Element | { x: number, y: number } | undefined}
84
+ */
85
+ _place = undefined;
86
+
87
+ /**
88
+ * What the menu was opened for with `showAt()`.
89
+ * @private
90
+ * @type {Element | undefined}
91
+ */
92
+ _opener = undefined;
93
+
94
+ /**
95
+ * The button of the page that was told that the menu is open.
96
+ * @private
97
+ * @type {Element | undefined}
98
+ */
99
+ _expanded = undefined;
100
+
101
+ /**
102
+ * Where the menu opens next, and what for: what `showAt()` was given. It is taken when the
103
+ * menu opens, which is after the element has rendered.
104
+ * @private
105
+ * @type {{ place: Element | { x: number, y: number }, opener: Element | undefined } | undefined}
106
+ */
107
+ _next = undefined;
108
+
109
+ /**
110
+ * Whether the Tab key was pressed in the menu, and the focus is about to leave it.
111
+ * @private
112
+ */
113
+ _leaving = false;
114
+
115
+ /**
116
+ * Whether the panel is being placed, and whether it was asked to be placed again meanwhile.
117
+ * @private
118
+ */
119
+ _placing = false;
120
+
121
+ /** @private */
122
+ _placeAgain = false;
123
+
124
+ /**
125
+ * The entry that had the focus last while the menu is open. It gets the focus back when a
126
+ * press beside the entries has given it to the panel.
127
+ * @private
128
+ * @type {HTMLElement | undefined}
129
+ */
130
+ _current = undefined;
131
+
132
+ /**
133
+ * Whether the menu is open and has not put the focus on an entry yet: its entries are not
134
+ * there, or not defined. The focus goes to them once they are.
135
+ * @private
136
+ */
137
+ _entering = false;
138
+
139
+ /**
140
+ * Whether the pointer that went down last is on the element the menu is open at, the button
141
+ * of a row. The `showAt()` the page calls with the click of that press closes the menu.
142
+ * @private
143
+ */
144
+ _pressedAtPlace = false;
145
+
146
+ /**
147
+ * A pointer that goes down beside the open menu closes it. The panel is a popover the menu
148
+ * opens and closes itself (`popover="manual"`): the platform would close one of its own
149
+ * (`auto`) when the pointer that opened it is let go, the finger after a long press.
150
+ * @private
151
+ * @param {PointerEvent} event
152
+ */
153
+ _onPressOutside = (event) => {
154
+ const path = event.composedPath();
155
+ this._pressedAtPlace = false;
156
+ if (path.includes(this.$refs.panel)) return;
157
+ // The button of the menu closes it with its click.
158
+ if (!this._place && path.includes(this._trigger())) return;
159
+ // So does the element the menu is open at, the button of a row. Its click is the one of
160
+ // the page, which calls showAt() with it: closed here, the menu would be opened again
161
+ // by that call, and the button could not close what it has opened.
162
+ if (this._place instanceof Element && path.includes(this._place)) {
163
+ this._pressedAtPlace = true;
164
+ return;
165
+ }
166
+
167
+ this._restoreFocus = false;
168
+ this.open = false;
169
+ };
170
+
171
+ /**
172
+ * Whether the menu closes because the page has scrolled under it. The focus goes back to
173
+ * what the menu was opened for without scrolling to it then: the scroll of the user goes on.
174
+ * @private
175
+ */
176
+ _closedByScroll = false;
177
+
178
+ /**
179
+ * What the menu listens on for a scroll while it is open at a point.
180
+ * @private
181
+ * @type {Array<Element | Window | Document> | undefined}
182
+ */
183
+ _scrollers = undefined;
184
+
185
+ /**
186
+ * What a menu at a point was opened for scrolls away under it: the page, or a region around
187
+ * the row. The menu closes, as the menu of the system does. At an element it follows the
188
+ * element instead, and it has no point to be left at.
189
+ *
190
+ * A scroll inside of the menu is none of these: a long menu scrolls in its panel. Neither is
191
+ * one of a region that does not move what the menu was opened for, another column of the
192
+ * page. A menu that was opened for nothing closes for every scroll in the page beside it.
193
+ * @private
194
+ * @param {Event} event
195
+ */
196
+ _onScroll = (event) => {
197
+ if (event.target instanceof Node && this.contains(event.target)) return;
198
+
199
+ this._restoreFocus = false;
200
+ this._closedByScroll = true;
201
+ this.open = false;
202
+ };
203
+
204
+ /**
205
+ * The keys of a menu, for as long as it is open and wherever the focus is: an open menu that
206
+ * has lost the focus would be left with no key at all. The window hears a key before the
207
+ * page does, so `Escape` closes the menu and nothing behind it.
208
+ * @private
209
+ * @param {KeyboardEvent} event
210
+ */
211
+ _onKeyDown = (event) => {
212
+ // A key is no press of a pointer: `Enter` on the button of a row is a click as well.
213
+ this._pressedAtPlace = false;
214
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
215
+
216
+ const items = this._items();
217
+ const active = activeElement();
218
+ const index = items.findIndex((item) => item === active || item.shadowRoot?.contains(active));
219
+ const inMenu = index >= 0 || active === this.$refs.panel;
220
+
221
+ // The focus leaves the menu with this key, and may leave the page: the browser says
222
+ // where it goes only while it stays in the page. Outside of the menu the focus moves on
223
+ // from where it is, and the menu closes as well.
224
+ if (event.key === 'Tab') {
225
+ if (inMenu) {
226
+ this._leaving = true;
227
+ requestAnimationFrame(() => (this._leaving = false));
228
+ } else {
229
+ this._restoreFocus = false;
230
+ this.open = false;
231
+ }
232
+ return;
233
+ }
234
+ // The key is the one of the menu: a dialog around it stays open.
235
+ if (event.key === 'Escape') {
236
+ event.preventDefault();
237
+ event.stopPropagation();
238
+ this._restoreFocus = true;
239
+ this.open = false;
240
+ return;
241
+ }
242
+ // The focus is somewhere else in the page: the arrow keys bring it into the menu, and
243
+ // every other key is the one of the page.
244
+ if (!inMenu) {
245
+ if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') return;
246
+ event.preventDefault();
247
+ event.stopPropagation();
248
+ this._enterAt = event.key === 'ArrowUp' ? 'last' : 'first';
249
+ this._focusEntry();
250
+ return;
251
+ }
252
+
253
+ const go = (to) => {
254
+ event.preventDefault();
255
+ items[(to + items.length) % items.length]?.focus();
256
+ };
257
+
258
+ if (event.key === 'ArrowDown') go(index + 1);
259
+ else if (event.key === 'ArrowUp') go(index < 0 ? items.length - 1 : index - 1);
260
+ else if (event.key === 'Home') go(0);
261
+ else if (event.key === 'End') go(items.length - 1);
262
+ else if (event.key === ' ') {
263
+ // A button is pressed with the space bar by the browser. A link is not, and in a menu
264
+ // the key chooses every entry. On the panel it chooses nothing, and scrolls nothing.
265
+ event.preventDefault();
266
+ if (index >= 0) active.click();
267
+ } else if (event.key.length === 1 && event.key.trim()) {
268
+ // A letter: the next entry that starts with it, from the one that has the focus on.
269
+ const letter = event.key.toLocaleLowerCase();
270
+ const starts = (item) => this._textOf(item).toLocaleLowerCase().startsWith(letter);
271
+ const next = [...items.slice(index + 1), ...items.slice(0, index + 1)].find(starts);
272
+ if (next) {
273
+ event.preventDefault();
274
+ next.focus();
275
+ }
276
+ }
277
+ };
278
+
279
+ constructor() {
280
+ super({ styles: [style], shadowRender: true, propertyOptions: { open: { reflect: true } } });
281
+ }
282
+
283
+ properties() {
284
+ return {
285
+ open: false,
286
+ label: '',
287
+ placement: 'bottom-start',
288
+ distance: 4,
289
+ };
290
+ }
291
+
292
+ connected() {
293
+ this._syncTrigger();
294
+ // The watcher does not run for the first value, so a menu that is open from the start is
295
+ // shown here.
296
+ if (this.open) this._applyOpen();
297
+ }
298
+
299
+ disconnected() {
300
+ this._stopPositioning();
301
+ document.removeEventListener('pointerdown', this._onPressOutside, true);
302
+ window.removeEventListener('keydown', this._onKeyDown, true);
303
+ if (openMenu === this) openMenu = undefined;
304
+ }
305
+
306
+ afterUpdate() {
307
+ this._syncTrigger();
308
+ // A new placement or distance.
309
+ if (this.open) this._position();
310
+ }
311
+
312
+ watch() {
313
+ return {
314
+ open: (open) => {
315
+ if (open) this._applyOpen();
316
+ else this._applyClose();
317
+ },
318
+ };
319
+ }
320
+
321
+ events() {
322
+ return {
323
+ '[data-trigger]': {
324
+ pointerdown: () => {
325
+ this._openAtPointerDown = this.open;
326
+ },
327
+ click: (event) => {
328
+ // A press beside the open menu closes it, and where the menu is open for a
329
+ // row, its own button is beside it. A click that found it closed would open
330
+ // it again, so a click of a pointer goes by what was the case when the
331
+ // pointer went down. A click of the keyboard has no pointer (`detail` is 0).
332
+ const byPointer = event.detail > 0 && this._openAtPointerDown !== undefined;
333
+ const wasOpen = byPointer ? this._openAtPointerDown : this.open;
334
+ this._openAtPointerDown = undefined;
335
+ this._enterAt = 'first';
336
+ this.open = !wasOpen;
337
+ },
338
+ keydown: (event) => this._onTriggerKeyDown(event),
339
+ slotchange: () => this._syncTrigger(),
340
+ },
341
+ "[part~='panel']": {
342
+ click: (event) => this._onClick(event),
343
+ focusin: (event) => this._onFocusIn(event),
344
+ focusout: (event) => this._onFocusOut(event),
345
+ // The element does not render again when an entry comes or goes: the slot tells
346
+ // of it. An entry that has come renders with the next frame.
347
+ slotchange: () => requestAnimationFrame(() => this._entriesChanged()),
348
+ },
349
+ };
350
+ }
351
+
352
+ /**
353
+ * Opens the menu, with the focus on its first entry.
354
+ * @returns {void}
355
+ */
356
+ show() {
357
+ this.open = true;
358
+ }
359
+
360
+ /**
361
+ * Opens the menu somewhere else than at its own button: one menu for the rows of a list.
362
+ *
363
+ * At a point of the viewport, from a `contextmenu` event or a long press: the corner of the
364
+ * menu is at the point, and the menu stays inside of the viewport. Or at an element of the
365
+ * page, the button of a row: the menu is placed at it as at its own button.
366
+ *
367
+ * `opener` is what the menu is opened for. It gets the focus back when the menu closes, and
368
+ * `menu-select` names it (`detail.opener`). Without one it is the element the menu is placed
369
+ * at.
370
+ *
371
+ * Called with the click of a press on the element the menu is open at, it closes the menu:
372
+ * the button of a row closes what it has opened, with the listener that opens it.
373
+ * @param {Element | { x: number, y: number }} place - The element, or the point in the
374
+ * coordinates of the viewport (`clientX`, `clientY`).
375
+ * @param {Element} [opener] - The row or the button the menu is opened for.
376
+ * @returns {void}
377
+ */
378
+ showAt(place, opener) {
379
+ const next = { place, opener: opener ?? (place instanceof Element ? place : undefined) };
380
+ this._enterAt = 'first';
381
+
382
+ // Open already, for something else: the panel stays where the platform has it, in the
383
+ // top layer, and moves. The page is told of both, the row it has left and the row it is
384
+ // open for now.
385
+ if (this.open && this.$refs.panel?.matches(':popover-open')) {
386
+ // The pointer is on what the menu is open at, and the page calls this with the click
387
+ // of that press: a second press on a button closes what the first one has opened.
388
+ if (this._pressedAtPlace && next.place === this._place) {
389
+ this.open = false;
390
+ return;
391
+ }
392
+
393
+ this.dispatch(Events.HIDE);
394
+ this._place = next.place;
395
+ this._opener = next.opener;
396
+ this._current = undefined;
397
+ this._pressedAtPlace = false;
398
+ this._syncTrigger();
399
+ this._startPositioning();
400
+ this._focusEntry();
401
+ this.dispatch(Events.SHOW);
402
+ return;
403
+ }
404
+
405
+ // The property tells of its change after the element has rendered: the place waits.
406
+ this._next = next;
407
+ this.open = true;
408
+ }
409
+
410
+ /**
411
+ * Closes the menu.
412
+ * @returns {void}
413
+ */
414
+ hide() {
415
+ this.open = false;
416
+ }
417
+
418
+ /**
419
+ * What the menu is open for: the row or the button it was opened for with `showAt()`, or its
420
+ * own button. Nothing while it is closed.
421
+ * @returns {Element | undefined}
422
+ */
423
+ get opener() {
424
+ return this.open ? (this._opener ?? this._trigger()) : undefined;
425
+ }
426
+
427
+ /**
428
+ * Opens the menu when it is closed, and closes it when it is open.
429
+ * @returns {void}
430
+ */
431
+ toggle() {
432
+ this.open = !this.open;
433
+ }
434
+
435
+ /**
436
+ * The button of the menu: what is in the `trigger` slot.
437
+ * @private
438
+ * @returns {HTMLElement | undefined}
439
+ */
440
+ _trigger() {
441
+ return this.$refs.trigger?.assignedElements({ flatten: true })[0];
442
+ }
443
+
444
+ /**
445
+ * The entries the user can reach, in the order of the page. An entry that is disabled is one
446
+ * of them: the keyboard gets to it, so a screen reader finds it, and it cannot be chosen.
447
+ * @private
448
+ * @returns {HTMLElement[]}
449
+ */
450
+ _items() {
451
+ return [...this.querySelectorAll('el-menu-item')].filter(
452
+ (item) => item.closest('el-menu') === this && !item.closest('[hidden]'),
453
+ );
454
+ }
455
+
456
+ /**
457
+ * The text of an entry, without its icon: what a letter of the keyboard is compared with.
458
+ * @private
459
+ * @param {Element} item
460
+ * @returns {string}
461
+ */
462
+ _textOf(item) {
463
+ return [...item.childNodes]
464
+ .filter((node) => node.nodeType === Node.TEXT_NODE || !node.getAttribute?.('slot'))
465
+ .map((node) => node.textContent)
466
+ .join(' ')
467
+ .replace(/\s+/g, ' ')
468
+ .trim();
469
+ }
470
+
471
+ /**
472
+ * The button says that it opens a menu, and whether the menu is open. An `el-button` passes
473
+ * both on to the control inside of it.
474
+ * @private
475
+ */
476
+ _syncTrigger() {
477
+ const trigger = this._trigger();
478
+ // Open for something else, the menu is not the one of its own button for that time.
479
+ const own = this.open && !this._place;
480
+ if (trigger) {
481
+ if (trigger.getAttribute('aria-haspopup') !== 'menu') trigger.setAttribute('aria-haspopup', 'menu');
482
+ const expanded = own ? 'true' : 'false';
483
+ if (trigger.getAttribute('aria-expanded') !== expanded) trigger.setAttribute('aria-expanded', expanded);
484
+ }
485
+
486
+ // A button of the page that opens the menu says that it does (`aria-haspopup`): the page
487
+ // writes that, the menu does not know its buttons. Such a button is told whether the
488
+ // menu is open. A row is no control, and has nothing to say.
489
+ const button = this.open && this._opener?.matches('[aria-haspopup]') ? this._opener : undefined;
490
+ if (this._expanded && this._expanded !== button) this._expanded.setAttribute('aria-expanded', 'false');
491
+ if (button) button.setAttribute('aria-expanded', 'true');
492
+ this._expanded = button;
493
+
494
+ // Without a name of its own the menu has the one of the button it is open for.
495
+ const panel = this.$refs.panel;
496
+ if (panel && 'ariaLabelledByElements' in panel) {
497
+ const named = this._place ? button : trigger;
498
+ panel.ariaLabelledByElements = this.label || !named ? null : [named];
499
+ }
500
+ }
501
+
502
+ /**
503
+ * Whether the focus is on an entry of the menu.
504
+ * @private
505
+ * @returns {boolean}
506
+ */
507
+ _hasFocusOnEntry() {
508
+ const active = activeElement();
509
+ return this._items().some((item) => item === active || item.shadowRoot?.contains(active));
510
+ }
511
+
512
+ /**
513
+ * Whether the focus is in the menu: on an entry, or on the panel around the entries.
514
+ * @private
515
+ * @returns {boolean}
516
+ */
517
+ _hasFocusInPanel() {
518
+ return activeElement() === this.$refs.panel || this._hasFocusOnEntry();
519
+ }
520
+
521
+ /**
522
+ * The arrow keys on the button open the menu: down at its first entry, up at its last one.
523
+ * `Enter` and `Space` are a click of the button.
524
+ * @private
525
+ * @param {KeyboardEvent} event
526
+ */
527
+ _onTriggerKeyDown(event) {
528
+ if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') return;
529
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
530
+ event.preventDefault();
531
+
532
+ this._enterAt = event.key === 'ArrowUp' ? 'last' : 'first';
533
+ if (this.open) this._focusEntry();
534
+ else this.open = true;
535
+ }
536
+
537
+ /**
538
+ * A click in the panel: the user has chosen the entry it is on, unless that one is disabled.
539
+ * @private
540
+ * @param {MouseEvent} event
541
+ */
542
+ _onClick(event) {
543
+ const item = event.composedPath().find((node) => node instanceof Element && node.localName === 'el-menu-item');
544
+ if (!item || item.closest('el-menu') !== this || item.disabled) return;
545
+
546
+ this._restoreFocus = true;
547
+ this.dispatch(Events.SELECT, { item, value: item.value, opener: this.opener });
548
+ this.open = false;
549
+ }
550
+
551
+ /**
552
+ * The focus has left the menu with the Tab key, which closes it and moves on. The focus is
553
+ * where the user has put it, and stays there.
554
+ *
555
+ * Only the key closes it this way. A pointer that goes down beside the menu closes it by
556
+ * itself, and the finger of a long press gives the focus to the row it is on when it is
557
+ * lifted: the menu it has just opened stays open.
558
+ * @private
559
+ * @param {FocusEvent} event
560
+ */
561
+ _onFocusOut(event) {
562
+ if (!this.open || !this._leaving) return;
563
+ if (event.relatedTarget?.closest('el-menu-item') && this.contains(event.relatedTarget)) return;
564
+
565
+ this._restoreFocus = false;
566
+ this.open = false;
567
+ }
568
+
569
+ /**
570
+ * The focus has come into the menu. On an entry: the menu remembers it. On the panel itself:
571
+ * the user has pressed beside the entries, on the heading of a group, a line or the padding,
572
+ * and the entry that had the focus gets it back. The keyboard goes on from there.
573
+ * @private
574
+ * @param {FocusEvent} event
575
+ */
576
+ _onFocusIn(event) {
577
+ if (event.target !== this.$refs.panel) {
578
+ const path = event.composedPath();
579
+ this._current = this._items().find((item) => path.includes(item)) ?? this._current;
580
+ return;
581
+ }
582
+ if (this._current && this._items().includes(this._current)) this._current.focus();
583
+ }
584
+
585
+ /**
586
+ * Puts the focus on the entry the menu is entered at. An entry that is not there yet, or
587
+ * whose element is not defined yet, cannot take it: the menu waits for it.
588
+ * @private
589
+ */
590
+ _focusEntry() {
591
+ const items = this._items();
592
+ (this._enterAt === 'last' ? items.at(-1) : items[0])?.focus();
593
+ if (this._hasFocusOnEntry()) {
594
+ this._entering = false;
595
+ this._enterAt = 'first';
596
+ return;
597
+ }
598
+ if (this._entering) return;
599
+
600
+ // The page renders the entries when the menu is asked for, or an autoloader defines the
601
+ // element of an entry when it first sees one. An element that is defined renders with
602
+ // the next frame.
603
+ this._entering = true;
604
+ customElements.whenDefined('el-menu-item').then(() => requestAnimationFrame(() => this._entriesChanged()));
605
+ }
606
+
607
+ /**
608
+ * Other entries in the open menu, or entries that have rendered by now. The panel has
609
+ * another size, and a menu at a point is placed once. And the focus goes to the entry the
610
+ * menu is entered at, where none could take it when the menu opened and nothing in the menu
611
+ * has it by now.
612
+ * @private
613
+ */
614
+ _entriesChanged() {
615
+ if (!this.open) return;
616
+ this._position();
617
+ if (this._entering && !this._hasFocusInPanel()) this._focusEntry();
618
+ }
619
+
620
+ /** @private */
621
+ _applyOpen() {
622
+ const panel = this.$refs.panel;
623
+ if (!panel || !this.isConnected) return;
624
+
625
+ // What showAt() was given, or nothing: the menu of its own button.
626
+ this._place = this._next?.place;
627
+ this._opener = this._next?.opener;
628
+ this._next = undefined;
629
+
630
+ // One menu at a time.
631
+ if (openMenu && openMenu !== this) openMenu.open = false;
632
+ openMenu = this;
633
+
634
+ this._restoreFocus = false;
635
+ this._current = undefined;
636
+ this._entering = false;
637
+ if (!panel.matches(':popover-open')) panel.showPopover();
638
+ document.addEventListener('pointerdown', this._onPressOutside, true);
639
+ window.addEventListener('keydown', this._onKeyDown, true);
640
+ this._internals.states.add('open');
641
+ this._syncTrigger();
642
+ this._startPositioning();
643
+ this._focusEntry();
644
+ this.dispatch(Events.SHOW);
645
+ }
646
+
647
+ /** @private */
648
+ _applyClose() {
649
+ const panel = this.$refs.panel;
650
+ if (!panel) return;
651
+
652
+ // Asked before the panel is hidden: an entry that has the focus loses it then.
653
+ const restore = this._restoreFocus || this._hasFocusInPanel();
654
+ this._restoreFocus = false;
655
+
656
+ // What the menu was open for gets the focus back: the row, the button, its own button.
657
+ const opener = this._opener ?? this._trigger();
658
+ this._place = undefined;
659
+ this._opener = undefined;
660
+ this._current = undefined;
661
+ this._entering = false;
662
+ this._pressedAtPlace = false;
663
+
664
+ if (openMenu === this) openMenu = undefined;
665
+ document.removeEventListener('pointerdown', this._onPressOutside, true);
666
+ window.removeEventListener('keydown', this._onKeyDown, true);
667
+ this._stopPositioning();
668
+ if (panel.matches(':popover-open')) panel.hidePopover();
669
+ this._internals.states.delete('open');
670
+ this._syncTrigger();
671
+ if (restore) opener?.focus?.({ preventScroll: this._closedByScroll });
672
+ this._closedByScroll = false;
673
+ this.dispatch(Events.HIDE);
674
+ }
675
+
676
+ /**
677
+ * What the panel is placed at: its own button, the element of `showAt()`, or its point. A
678
+ * point is an element for the placing that has no size and does not move.
679
+ * @private
680
+ * @returns {Element | { getBoundingClientRect: () => DOMRect }}
681
+ */
682
+ _anchor() {
683
+ const place = this._place;
684
+ if (!place) return this._trigger() ?? this;
685
+ if (place instanceof Element) return place;
686
+ return { getBoundingClientRect: () => new DOMRect(Number(place.x) || 0, Number(place.y) || 0, 0, 0) };
687
+ }
688
+
689
+ /**
690
+ * Places the panel now, and again whenever the button moves: on scroll, on resize, when the
691
+ * button or the panel changes its size.
692
+ * @private
693
+ */
694
+ _startPositioning() {
695
+ this._stopPositioning();
696
+ const anchor = this._anchor();
697
+ // A point does not move: the menu is placed at it once, and closes when what it was
698
+ // opened for scrolls away under it.
699
+ if (anchor instanceof Element) {
700
+ this._stopAutoUpdate = followAnchor(anchor, this.$refs.panel, () => this._position());
701
+ } else {
702
+ this._position();
703
+ this._listenForScroll();
704
+ }
705
+ }
706
+
707
+ /**
708
+ * A scroll does not bubble and does not leave a shadow root, so the menu listens where it
709
+ * happens: on every region around what it was opened for, and on the window for the page.
710
+ * Opened for nothing, it hears every scroll of the page on its way down from the document.
711
+ * @private
712
+ */
713
+ _listenForScroll() {
714
+ const capture = !this._opener;
715
+ this._scrollers = this._opener ? scrollersOf(this._opener) : [document];
716
+ for (const scroller of this._scrollers) {
717
+ scroller.addEventListener('scroll', this._onScroll, { capture, passive: true });
718
+ }
719
+ }
720
+
721
+ /** @private */
722
+ _stopPositioning() {
723
+ this._stopAutoUpdate?.();
724
+ this._stopAutoUpdate = undefined;
725
+ for (const scroller of this._scrollers ?? []) {
726
+ scroller.removeEventListener('scroll', this._onScroll, false);
727
+ scroller.removeEventListener('scroll', this._onScroll, true);
728
+ }
729
+ this._scrollers = undefined;
730
+ }
731
+
732
+ /**
733
+ * Places the panel at what the menu is open at.
734
+ *
735
+ * Not before the panel is shown: the element renders when `open` is set, before the menu has
736
+ * taken its place from `showAt()`, and would be placed at its own element. And one placement
737
+ * at a time: a placement takes a few turns, more where the panel has to try the other side,
738
+ * so one that was asked for first can be done last. A menu whose own element is out of view,
739
+ * below a long list, was taken there that way. What is asked for while the panel is being
740
+ * placed is placed after it, at what the menu is open at by then.
741
+ * @private
742
+ */
743
+ async _position() {
744
+ const panel = this.$refs.panel;
745
+ if (!panel || !this.open || !panel.matches(':popover-open')) return;
746
+ if (this._placing) {
747
+ this._placeAgain = true;
748
+ return;
749
+ }
750
+
751
+ this._placing = true;
752
+ try {
753
+ do {
754
+ this._placeAgain = false;
755
+ const atPoint = this._place && !(this._place instanceof Element);
756
+ await placeAtAnchor(this._anchor(), panel, {
757
+ // At a point the corner of the menu is at the pointer, as the menu of the
758
+ // system is.
759
+ placement: atPoint ? 'bottom-start' : this.placement,
760
+ distance: atPoint ? 0 : this.distance,
761
+ });
762
+ } while (this._placeAgain && this.open);
763
+ } finally {
764
+ this._placing = false;
765
+ }
766
+ }
767
+
768
+ template() {
769
+ // A plain binding leaves the name out while there is none. The panel takes the focus of a
770
+ // press beside its entries, and is no stop of the Tab key: without it the focus of such
771
+ // a press would be nowhere in the page.
772
+ return html`
773
+ <slot part="trigger" name="trigger" ref="trigger" data-trigger></slot>
774
+ <div
775
+ part="panel"
776
+ ref="panel"
777
+ class="panel"
778
+ popover="manual"
779
+ role="menu"
780
+ tabindex="-1"
781
+ aria-label="${this.label || null}"
782
+ >
783
+ <slot></slot>
784
+ </div>
785
+ `;
786
+ }
787
+ }
788
+
789
+ export function define() {
790
+ defineElement('el-menu', Menu);
791
+ }
792
+
793
+ export { Events };