@webtides/element-library 0.3.0 → 0.4.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 (199) hide show
  1. package/CHANGELOG.md +208 -0
  2. package/README.md +72 -56
  3. package/all.js +16 -0
  4. package/catalog.js +17 -1
  5. package/custom-elements.json +14731 -10193
  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 +45 -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 +1073 -0
  20. package/src/components/app-frame/app-frame.readme.md +416 -0
  21. package/src/components/app-frame/app-frame.style.js +527 -0
  22. package/src/components/button/button.changelog.md +25 -0
  23. package/src/components/button/button.events.js +3 -0
  24. package/src/components/button/button.js +209 -14
  25. package/src/components/button/button.readme.md +82 -6
  26. package/src/components/button/button.style.js +161 -7
  27. package/src/components/checkbox-field/checkbox-field.changelog.md +6 -0
  28. package/src/components/checkbox-field/checkbox-field.js +7 -1
  29. package/src/components/description-list/description-list.changelog.md +25 -0
  30. package/src/components/description-list/description-list.define.js +3 -0
  31. package/src/components/description-list/description-list.js +44 -0
  32. package/src/components/description-list/description-list.readme.md +135 -0
  33. package/src/components/description-list/description-list.style.js +140 -0
  34. package/src/components/dialog/dialog.changelog.md +7 -0
  35. package/src/components/dialog/dialog.readme.md +5 -0
  36. package/src/components/dialog/dialog.style.js +17 -2
  37. package/src/components/drawer/drawer.changelog.md +7 -0
  38. package/src/components/drawer/drawer.readme.md +7 -6
  39. package/src/components/drawer/drawer.style.js +35 -2
  40. package/src/components/dropdown/dropdown.readme.md +6 -1
  41. package/src/components/list/list.changelog.md +24 -0
  42. package/src/components/list/list.define.js +3 -0
  43. package/src/components/list/list.js +66 -0
  44. package/src/components/list/list.readme.md +122 -0
  45. package/src/components/list/list.style.js +56 -0
  46. package/src/components/list-item/list-item.changelog.md +24 -0
  47. package/src/components/list-item/list-item.define.js +3 -0
  48. package/src/components/list-item/list-item.js +128 -0
  49. package/src/components/list-item/list-item.readme.md +126 -0
  50. package/src/components/list-item/list-item.style.js +234 -0
  51. package/src/components/menu/menu.changelog.md +27 -0
  52. package/src/components/menu/menu.define.js +3 -0
  53. package/src/components/menu/menu.events.js +5 -0
  54. package/src/components/menu/menu.js +570 -0
  55. package/src/components/menu/menu.readme.md +272 -0
  56. package/src/components/menu/menu.style.js +55 -0
  57. package/src/components/menu-group/menu-group.changelog.md +20 -0
  58. package/src/components/menu-group/menu-group.define.js +3 -0
  59. package/src/components/menu-group/menu-group.js +44 -0
  60. package/src/components/menu-group/menu-group.readme.md +74 -0
  61. package/src/components/menu-group/menu-group.style.js +38 -0
  62. package/src/components/menu-item/menu-item.changelog.md +24 -0
  63. package/src/components/menu-item/menu-item.define.js +3 -0
  64. package/src/components/menu-item/menu-item.js +130 -0
  65. package/src/components/menu-item/menu-item.readme.md +94 -0
  66. package/src/components/menu-item/menu-item.style.js +87 -0
  67. package/src/components/nav/nav.changelog.md +35 -0
  68. package/src/components/nav/nav.define.js +3 -0
  69. package/src/components/nav/nav.js +212 -0
  70. package/src/components/nav/nav.readme.md +248 -0
  71. package/src/components/nav/nav.style.js +269 -0
  72. package/src/components/nav-item/nav-item.changelog.md +25 -0
  73. package/src/components/nav-item/nav-item.define.js +3 -0
  74. package/src/components/nav-item/nav-item.js +75 -0
  75. package/src/components/nav-item/nav-item.readme.md +111 -0
  76. package/src/components/nav-item/nav-item.style.js +159 -0
  77. package/src/components/pagination/pagination.changelog.md +8 -0
  78. package/src/components/pagination/pagination.js +123 -49
  79. package/src/components/pagination/pagination.readme.md +24 -6
  80. package/src/components/pagination/pagination.style.js +11 -1
  81. package/src/components/scroll-to-top/scroll-to-top.changelog.md +7 -5
  82. package/src/components/scroll-to-top/scroll-to-top.js +16 -12
  83. package/src/components/scroll-to-top/scroll-to-top.readme.md +2 -0
  84. package/src/components/segmented-field/segmented-field.changelog.md +27 -0
  85. package/src/components/segmented-field/segmented-field.define.js +3 -0
  86. package/src/components/segmented-field/segmented-field.js +175 -0
  87. package/src/components/segmented-field/segmented-field.readme.md +140 -0
  88. package/src/components/segmented-field/segmented-field.style.js +193 -0
  89. package/src/components/splitter/splitter.changelog.md +24 -0
  90. package/src/components/splitter/splitter.define.js +3 -0
  91. package/src/components/splitter/splitter.events.js +4 -0
  92. package/src/components/splitter/splitter.js +237 -0
  93. package/src/components/splitter/splitter.readme.md +164 -0
  94. package/src/components/splitter/splitter.style.js +100 -0
  95. package/src/components/status-bar/status-bar.changelog.md +25 -0
  96. package/src/components/status-bar/status-bar.define.js +3 -0
  97. package/src/components/status-bar/status-bar.js +74 -0
  98. package/src/components/status-bar/status-bar.readme.md +139 -0
  99. package/src/components/status-bar/status-bar.style.js +122 -0
  100. package/src/components/switch-field/switch-field.changelog.md +6 -0
  101. package/src/components/switch-field/switch-field.js +7 -1
  102. package/src/components/tab-group/tab-group.changelog.md +7 -0
  103. package/src/components/tab-group/tab-group.js +30 -9
  104. package/src/components/tab-group/tab-group.readme.md +14 -0
  105. package/src/components/toast/toast.changelog.md +6 -0
  106. package/src/components/toast/toast.readme.md +5 -3
  107. package/src/components/toast/toast.style.js +22 -6
  108. package/src/components/toolbar/toolbar.changelog.md +24 -0
  109. package/src/components/toolbar/toolbar.define.js +3 -0
  110. package/src/components/toolbar/toolbar.js +233 -0
  111. package/src/components/toolbar/toolbar.readme.md +156 -0
  112. package/src/components/toolbar/toolbar.style.js +49 -0
  113. package/src/components/tooltip/tooltip.changelog.md +11 -0
  114. package/src/components/tooltip/tooltip.js +51 -4
  115. package/src/components/tooltip/tooltip.readme.md +26 -1
  116. package/src/components/tree/tree.changelog.md +23 -0
  117. package/src/components/tree/tree.define.js +3 -0
  118. package/src/components/tree/tree.js +138 -0
  119. package/src/components/tree/tree.readme.md +119 -0
  120. package/src/components/tree/tree.style.js +16 -0
  121. package/src/components/tree-item/tree-item.changelog.md +23 -0
  122. package/src/components/tree-item/tree-item.define.js +3 -0
  123. package/src/components/tree-item/tree-item.events.js +3 -0
  124. package/src/components/tree-item/tree-item.js +246 -0
  125. package/src/components/tree-item/tree-item.readme.md +128 -0
  126. package/src/components/tree-item/tree-item.style.js +226 -0
  127. package/src/styles/appearance.style.js +47 -0
  128. package/src/styles/choice-group.style.js +1 -1
  129. package/src/styles/modal.style.js +14 -1
  130. package/src/styles/size.style.js +41 -0
  131. package/src/utils/focus.js +69 -0
  132. package/src/utils/scroll.js +24 -0
  133. package/types/all.d.ts +16 -0
  134. package/types/src/components/accordion/accordion.d.ts +3 -1
  135. package/types/src/components/app-bar/app-bar.d.ts +43 -0
  136. package/types/src/components/app-bar/app-bar.define.d.ts +1 -0
  137. package/types/src/components/app-bar/app-bar.style.d.ts +2 -0
  138. package/types/src/components/app-frame/app-frame.d.ts +491 -0
  139. package/types/src/components/app-frame/app-frame.define.d.ts +1 -0
  140. package/types/src/components/app-frame/app-frame.events.d.ts +7 -0
  141. package/types/src/components/app-frame/app-frame.style.d.ts +8 -0
  142. package/types/src/components/button/button.d.ts +97 -7
  143. package/types/src/components/button/button.events.d.ts +4 -0
  144. package/types/src/components/description-list/description-list.d.ts +29 -0
  145. package/types/src/components/description-list/description-list.define.d.ts +1 -0
  146. package/types/src/components/description-list/description-list.style.d.ts +2 -0
  147. package/types/src/components/list/list.d.ts +39 -0
  148. package/types/src/components/list/list.define.d.ts +1 -0
  149. package/types/src/components/list/list.style.d.ts +2 -0
  150. package/types/src/components/list-item/list-item.d.ts +62 -0
  151. package/types/src/components/list-item/list-item.define.d.ts +1 -0
  152. package/types/src/components/list-item/list-item.style.d.ts +2 -0
  153. package/types/src/components/menu/menu.d.ts +266 -0
  154. package/types/src/components/menu/menu.define.d.ts +1 -0
  155. package/types/src/components/menu/menu.events.d.ts +6 -0
  156. package/types/src/components/menu/menu.style.d.ts +2 -0
  157. package/types/src/components/menu-group/menu-group.d.ts +24 -0
  158. package/types/src/components/menu-group/menu-group.define.d.ts +1 -0
  159. package/types/src/components/menu-group/menu-group.style.d.ts +2 -0
  160. package/types/src/components/menu-item/menu-item.d.ts +59 -0
  161. package/types/src/components/menu-item/menu-item.define.d.ts +1 -0
  162. package/types/src/components/menu-item/menu-item.style.d.ts +2 -0
  163. package/types/src/components/nav/nav.d.ts +103 -0
  164. package/types/src/components/nav/nav.define.d.ts +1 -0
  165. package/types/src/components/nav/nav.style.d.ts +2 -0
  166. package/types/src/components/nav-item/nav-item.d.ts +39 -0
  167. package/types/src/components/nav-item/nav-item.define.d.ts +1 -0
  168. package/types/src/components/nav-item/nav-item.style.d.ts +2 -0
  169. package/types/src/components/pagination/pagination.d.ts +31 -2
  170. package/types/src/components/scroll-to-top/scroll-to-top.d.ts +6 -5
  171. package/types/src/components/segmented-field/segmented-field.d.ts +85 -0
  172. package/types/src/components/segmented-field/segmented-field.define.d.ts +1 -0
  173. package/types/src/components/segmented-field/segmented-field.style.d.ts +2 -0
  174. package/types/src/components/splitter/splitter.d.ts +127 -0
  175. package/types/src/components/splitter/splitter.define.d.ts +1 -0
  176. package/types/src/components/splitter/splitter.events.d.ts +5 -0
  177. package/types/src/components/splitter/splitter.style.d.ts +2 -0
  178. package/types/src/components/status-bar/status-bar.d.ts +44 -0
  179. package/types/src/components/status-bar/status-bar.define.d.ts +1 -0
  180. package/types/src/components/status-bar/status-bar.style.d.ts +2 -0
  181. package/types/src/components/tab-group/tab-group.d.ts +17 -0
  182. package/types/src/components/toolbar/toolbar.d.ts +96 -0
  183. package/types/src/components/toolbar/toolbar.define.d.ts +1 -0
  184. package/types/src/components/toolbar/toolbar.style.d.ts +2 -0
  185. package/types/src/components/tooltip/tooltip.d.ts +26 -0
  186. package/types/src/components/tree/tree.d.ts +62 -0
  187. package/types/src/components/tree/tree.define.d.ts +1 -0
  188. package/types/src/components/tree/tree.style.d.ts +2 -0
  189. package/types/src/components/tree-item/tree-item.d.ts +106 -0
  190. package/types/src/components/tree-item/tree-item.define.d.ts +1 -0
  191. package/types/src/components/tree-item/tree-item.events.d.ts +4 -0
  192. package/types/src/components/tree-item/tree-item.style.d.ts +2 -0
  193. package/types/src/styles/appearance.style.d.ts +31 -0
  194. package/types/src/styles/choice-group.style.d.ts +1 -1
  195. package/types/src/styles/modal.style.d.ts +5 -0
  196. package/types/src/styles/size.style.d.ts +24 -0
  197. package/types/src/utils/focus.d.ts +25 -0
  198. package/types/src/utils/scroll.d.ts +11 -0
  199. package/web-types.json +2530 -393
@@ -1,5 +1,6 @@
1
1
  import { TemplateElement, defineElement, html, classMap, spreadAttributes } from '@webtides/element-js';
2
2
  import style from './button.style.js';
3
+ import Events from './button.events.js';
3
4
 
4
5
  /**
5
6
  * The ARIA attributes of the element that its control takes over. The control is what has the
@@ -44,15 +45,28 @@ const SUBMITTER_ATTRIBUTES = ['form', 'formaction', 'formenctype', 'formmethod',
44
45
  * `aria-controls` as references to the elements they name.
45
46
  *
46
47
  * The element is form-associated: a `<fieldset disabled>` around it disables it, and it is listed in
47
- * the `elements` of its form. A button of the type `submit` keeps a hidden native
48
- * `<button type="submit">` in its content, which is what the form is submitted with: it is the
49
- * `submitter` of the `submit` event, its `name` and `value` are in the data of the form, and Enter in
50
- * a field of the form finds it. The attributes `form`, `formaction`, `formenctype`, `formmethod`,
51
- * `formnovalidate` and `formtarget` of the element do what they do on a native button. A `click`
52
- * listener of the page that calls `preventDefault()` keeps the button from submitting or resetting.
48
+ * the `elements` of its form. A button of the type `submit` submits its form with a hidden native
49
+ * `<button type="submit">` in its content: it is the `submitter` of the `submit` event, its `name`
50
+ * and `value` are in the data of the form, and Enter in a field of the form finds it. The element
51
+ * makes that button when the form is first submitted with it, by a click or by Enter in a field.
52
+ * Until then its content is what the page wrote, so a framework can hydrate a server-rendered
53
+ * button, also when the element has started before the framework does. The attributes `form`,
54
+ * `formaction`, `formenctype`, `formmethod`, `formnovalidate` and `formtarget` of the element do
55
+ * what they do on a native button. A `click` listener of the page that calls `preventDefault()`
56
+ * keeps the button from submitting or resetting.
57
+ *
58
+ * In a container that gives its controls a size and an appearance, an `el-toolbar`, the button
59
+ * takes them from it, while the page has not set `size` or `appearance` on the button itself.
60
+ *
61
+ * With `toggle` the button stays pressed: a click flips `pressed`, the control says so to assistive
62
+ * technology (`aria-pressed`), and the button shows it. A `click` listener of the page that calls
63
+ * `preventDefault()` keeps the button from flipping, as it keeps it from submitting.
53
64
  *
54
65
  * @element el-button
55
66
  *
67
+ * @fires {CustomEvent<{ pressed: boolean }>} button-toggle - Fired when a click has flipped a
68
+ * toggle button. `detail.pressed` is the new state. Not fired when `pressed` is set from a script.
69
+ *
56
70
  * @slot - The button's label.
57
71
  * @slot prefix - A presentational prefix icon or similar element.
58
72
  * @slot suffix - A presentational suffix icon or similar element.
@@ -77,6 +91,10 @@ const SUBMITTER_ATTRIBUTES = ['form', 'formaction', 'formenctype', 'formmethod',
77
91
  * @property {boolean} loading - Shows a spinner and blocks interaction.
78
92
  * @property {boolean} disabled - Disables the button. Reflected to the `disabled` attribute, which is
79
93
  * there while the button is disabled and gone while it is not.
94
+ * @property {boolean} toggle - The button is a toggle button: it stays pressed. A click flips
95
+ * `pressed`, and the control has `aria-pressed`. Ignored when `href` is set. Default `false`.
96
+ * @property {boolean} pressed - Whether a toggle button is pressed. Reflected to the `pressed`
97
+ * attribute of a toggle button. Without `toggle` it does nothing. Default `false`.
80
98
  * @property {'button'|'submit'|'reset'} type - Behaviour when activated. `submit` / `reset` act on
81
99
  * the form of the button: the nearest ancestor `<form>`, or the one its `form` attribute names.
82
100
  * Ignored when `href` is set. Default `'button'`.
@@ -96,7 +114,14 @@ export default class Button extends TemplateElement {
96
114
  static formAssociated = true;
97
115
 
98
116
  static get observedAttributes() {
99
- return [...ARIA_ATTRIBUTES, ...Object.keys(ARIA_RELATIONS), ...SUBMITTER_ATTRIBUTES, 'disabled'];
117
+ return [
118
+ ...ARIA_ATTRIBUTES,
119
+ ...Object.keys(ARIA_RELATIONS),
120
+ ...SUBMITTER_ATTRIBUTES,
121
+ 'disabled',
122
+ 'size',
123
+ 'appearance',
124
+ ];
100
125
  }
101
126
 
102
127
  /**
@@ -106,12 +131,32 @@ export default class Button extends TemplateElement {
106
131
  */
107
132
  _submitter = undefined;
108
133
 
134
+ /**
135
+ * Where the element listens for Enter in a field of its form, see `afterUpdate()`.
136
+ * @private
137
+ * @type {Node | undefined}
138
+ */
139
+ _keydownRoot = undefined;
140
+
141
+ /**
142
+ * Which of `size` and `appearance` the page has set on the button itself, see `_isOwn()`.
143
+ * @private
144
+ * @type {Set<string> | undefined}
145
+ */
146
+ _ownLook = undefined;
147
+
148
+ /**
149
+ * Whether the button is writing a property to its attribute right now, see `reflectProperty()`.
150
+ * @private
151
+ */
152
+ _reflecting = false;
153
+
109
154
  constructor() {
110
155
  super({
111
156
  styles: [style],
112
157
  shadowRender: true,
113
- // The native submit button is in the content of the element. When the page replaces
114
- // the content, the next render puts it back.
158
+ // A native submit button that the page writes into the content of the element is
159
+ // taken with the next render, see `_syncSubmitter()`.
115
160
  mutationObserverOptions: { childList: true },
116
161
  // The shared style modules select the host by these attributes.
117
162
  propertyOptions: {
@@ -120,6 +165,8 @@ export default class Button extends TemplateElement {
120
165
  size: { reflect: true },
121
166
  // For the platform the attribute is what disables the element, see `reflectProperty()`.
122
167
  disabled: { reflect: true },
168
+ // The attribute of a toggle button says whether it is pressed, see `reflectProperty()`.
169
+ pressed: { reflect: true },
123
170
  // What a form submits is text. element-js parses every attribute, so that text which
124
171
  // reads as a number arrives as one: "1.50" as 1.5.
125
172
  name: { parse: false },
@@ -138,6 +185,8 @@ export default class Button extends TemplateElement {
138
185
  caret: false,
139
186
  loading: false,
140
187
  disabled: false,
188
+ toggle: false,
189
+ pressed: false,
141
190
  type: 'button',
142
191
  name: '',
143
192
  value: '',
@@ -148,11 +197,58 @@ export default class Button extends TemplateElement {
148
197
  };
149
198
  }
150
199
 
200
+ connectedCallback() {
201
+ // Before element-js writes the defaults onto the element: after that, every button has a
202
+ // `size` and an `appearance` attribute, and nothing says who wrote them. A framework may
203
+ // have set them as properties before the element was connected.
204
+ this._ownLook ??= new Set(
205
+ ['size', 'appearance'].filter((name) => this.hasAttribute(name) || Object.hasOwn(this, name)),
206
+ );
207
+ super.connectedCallback();
208
+ }
209
+
210
+ /**
211
+ * Whether the page has given the button a `size` or an `appearance` of its own. A button
212
+ * without one takes it from a container that gives one to its controls, a toolbar. The
213
+ * template marks the control with the answer, and the stylesheet reads the mark.
214
+ *
215
+ * On the server no attribute has been written by the element: the ones that are there are
216
+ * the ones of the page.
217
+ * @private
218
+ * @param {'size' | 'appearance'} name
219
+ * @returns {boolean}
220
+ */
221
+ _isOwn(name) {
222
+ if (!this._ownLook) return !!this.hasAttribute?.(name);
223
+ // A value that is not the default was set by someone: the watcher has not run yet when the
224
+ // button renders with a value it has just got from a script.
225
+ return this._ownLook.has(name) || this[name] !== this.properties()[name];
226
+ }
227
+
151
228
  /** Whether the button renders as a link rather than a native button. */
152
229
  get isLink() {
153
230
  return !!this.href;
154
231
  }
155
232
 
233
+ /**
234
+ * Whether the button is a toggle button: one that stays pressed. A link leads somewhere, it
235
+ * has no state to stay in.
236
+ */
237
+ get isToggle() {
238
+ return !!this.toggle && !this.isLink;
239
+ }
240
+
241
+ watch() {
242
+ return {
243
+ // A button that becomes a toggle gets the attribute of its state.
244
+ toggle: () => this.reflectProperty({ property: 'pressed', newValue: this.pressed }),
245
+ // A size or an appearance that is set later, as an attribute or from a script, is the
246
+ // button's own from then on.
247
+ size: () => this._ownLook?.add('size'),
248
+ appearance: () => this._ownLook?.add('appearance'),
249
+ };
250
+ }
251
+
156
252
  /**
157
253
  * An ARIA attribute of the element, or one it shares with a native submit button, was set,
158
254
  * changed or removed. They are no properties of the component: the template and the submit
@@ -169,6 +265,16 @@ export default class Button extends TemplateElement {
169
265
  if (newValue === 'false' || newValue === '') this.removeAttribute('disabled');
170
266
  return;
171
267
  }
268
+ if (name === 'size' || name === 'appearance') {
269
+ // The page has written the attribute, after the button was connected: the size or the
270
+ // appearance is the button's own from now on, also when it is the default. The button
271
+ // writes the two itself as well, which says nothing.
272
+ if (this._ownLook && !this._reflecting && newValue !== null && !this._ownLook.has(name)) {
273
+ this._ownLook.add(name);
274
+ if (this.$refs?.control) this.requestUpdate();
275
+ }
276
+ return;
277
+ }
172
278
  if (oldValue !== newValue && this.$refs?.control) this.requestUpdate();
173
279
  }
174
280
 
@@ -186,8 +292,18 @@ export default class Button extends TemplateElement {
186
292
  * @param {{ property: string, newValue: unknown }} options
187
293
  */
188
294
  reflectProperty(options) {
295
+ if (options.property === 'pressed') {
296
+ this._reflectPressed(options.newValue);
297
+ return;
298
+ }
189
299
  if (options.property !== 'disabled') {
190
- super.reflectProperty(options);
300
+ // `attributeChangedCallback()` runs before `setAttribute()` returns.
301
+ this._reflecting = true;
302
+ try {
303
+ super.reflectProperty(options);
304
+ } finally {
305
+ this._reflecting = false;
306
+ }
191
307
  return;
192
308
  }
193
309
  if (!options.newValue) this.removeAttribute('disabled');
@@ -196,11 +312,60 @@ export default class Button extends TemplateElement {
196
312
  if (options.newValue === null) this.disabled = false;
197
313
  }
198
314
 
315
+ /**
316
+ * `pressed` is the attribute of a toggle button: `"true"` or `"false"`, so that the page can
317
+ * read the state and select by it. On a button that is no toggle the attribute says nothing,
318
+ * and the element does not write it there.
319
+ * @private
320
+ * @param {unknown} pressed
321
+ */
322
+ _reflectPressed(pressed) {
323
+ // element-js reads an attribute that is gone as `null`. The property stays a boolean.
324
+ if (pressed === null || pressed === undefined || pressed === '') {
325
+ this.pressed = false;
326
+ return;
327
+ }
328
+ if (!this.isToggle) return;
329
+ const text = pressed ? 'true' : 'false';
330
+ if (this.getAttribute('pressed') !== text) this.setAttribute('pressed', text);
331
+ }
332
+
199
333
  afterUpdate() {
200
334
  this._syncAriaRelations();
201
335
  this._syncSubmitter();
336
+
337
+ // `keydown` passes the root of its tree on the way to the field, the document or a shadow
338
+ // root: there the element hears Enter before the browser looks for the submit button.
339
+ if (!this._keydownRoot) {
340
+ this._keydownRoot = this.getRootNode();
341
+ this._keydownRoot.addEventListener('keydown', this._onRootKeydown, true);
342
+ }
343
+ }
344
+
345
+ disconnected() {
346
+ this._keydownRoot?.removeEventListener('keydown', this._onRootKeydown, true);
347
+ this._keydownRoot = undefined;
202
348
  }
203
349
 
350
+ /**
351
+ * Enter in a field of the form submits the form with its first submit button, and without one
352
+ * only a form with a single field. The browser looks for that button once every listener has
353
+ * had the key, so the native submit button of the element is made here, if it is not there yet.
354
+ *
355
+ * Only for a key the browser may submit with: Enter in an `<input>` or a `<select>` of the
356
+ * form, and not while a text is being composed. Enter in a `<textarea>` is a line break, and
357
+ * leaves the content of the element as it is.
358
+ * @private
359
+ * @param {KeyboardEvent} event
360
+ */
361
+ _onRootKeydown = (event) => {
362
+ if (event.key !== 'Enter' || event.isComposing || this.isLink || this.type !== 'submit') return;
363
+ const field = event.target;
364
+ const form = this._internals.form;
365
+ if (!form || field?.form !== form || !field.matches('input, select')) return;
366
+ this._syncSubmitter(true);
367
+ };
368
+
204
369
  /** Focuses the underlying control. */
205
370
  focus(options) {
206
371
  this.$refs.control?.focus(options);
@@ -229,12 +394,20 @@ export default class Button extends TemplateElement {
229
394
  return;
230
395
  }
231
396
  if (this.isLink) return;
397
+ // A toggle flips once the page has had the click, and not when a listener has
398
+ // prevented it: as a native checkbox takes its check mark back then.
399
+ if (this.isToggle) {
400
+ this._afterListeners(event, () => {
401
+ this.pressed = !this.pressed;
402
+ this.dispatch(Events.TOGGLE, { pressed: this.pressed });
403
+ });
404
+ }
232
405
  // The control is in the shadow root, where it has no form. A submit goes through
233
406
  // the native submit button of the element, a reset to the form of the element.
234
407
  // Both are taken here, while the button is not disabled, and act once the page
235
408
  // has had the click.
236
409
  if (this.type === 'submit') {
237
- const submitter = this._syncSubmitter();
410
+ const submitter = this._syncSubmitter(true);
238
411
  this._afterListeners(event, () => submitter?.form?.requestSubmit(submitter));
239
412
  } else if (this.type === 'reset') {
240
413
  const form = this._internals.form;
@@ -294,10 +467,17 @@ export default class Button extends TemplateElement {
294
467
  * button has. It is disabled while the element is disabled or loading, which keeps Enter from
295
468
  * submitting as well. One that is in the content already is taken: written by the page, for a
296
469
  * form that is submitted with Enter before the script has run, or copied with the element.
470
+ *
471
+ * The element makes the button only when the form is about to be submitted with it (`create`):
472
+ * with a click, or with Enter in a field. A render does not. The content of the element is what
473
+ * the page wrote until then: a framework that hydrates a server-rendered page after the element
474
+ * has started finds the children it rendered, and no other.
297
475
  * @private
298
- * @returns {HTMLButtonElement | null} The submit button, or `null` for a button of another type.
476
+ * @param {boolean} [create] - Whether to make the button when there is none.
477
+ * @returns {HTMLButtonElement | null} The submit button, or `null`: for a button of another
478
+ * type, and while there is none and none was asked for.
299
479
  */
300
- _syncSubmitter() {
480
+ _syncSubmitter(create = false) {
301
481
  const ours = this._submitter?.parentNode === this ? this._submitter : null;
302
482
  let submitter = ours ?? this.querySelector(':scope > button[type="submit"][hidden]');
303
483
 
@@ -307,6 +487,11 @@ export default class Button extends TemplateElement {
307
487
  return null;
308
488
  }
309
489
 
490
+ if (!submitter && !create) {
491
+ this._submitter = undefined;
492
+ return null;
493
+ }
494
+
310
495
  if (!submitter) {
311
496
  submitter = document.createElement('button');
312
497
  submitter.type = 'submit';
@@ -338,7 +523,13 @@ export default class Button extends TemplateElement {
338
523
  * @returns {Object<string, string | undefined>}
339
524
  */
340
525
  _ariaAttributes() {
341
- return Object.fromEntries(ARIA_ATTRIBUTES.map((name) => [name, this.getAttribute(name) || undefined]));
526
+ const attributes = Object.fromEntries(
527
+ ARIA_ATTRIBUTES.map((name) => [name, this.getAttribute(name) || undefined]),
528
+ );
529
+ // The state of a toggle button is its own. Without `toggle` the `aria-pressed` of the page
530
+ // goes through as it is, and a button that has neither has no `aria-pressed`.
531
+ if (this.isToggle) attributes['aria-pressed'] = this.pressed ? 'true' : 'false';
532
+ return attributes;
342
533
  }
343
534
 
344
535
  /**
@@ -379,6 +570,8 @@ export default class Button extends TemplateElement {
379
570
  'button--circle': !!this.circle,
380
571
  'button--loading': !!this.loading,
381
572
  'button--caret': !!this.caret,
573
+ 'button--inherit-size': !this._isOwn('size'),
574
+ 'button--inherit-appearance': !this._isOwn('appearance'),
382
575
  };
383
576
  const disabled = this._isDisabled();
384
577
 
@@ -428,3 +621,5 @@ export default class Button extends TemplateElement {
428
621
  export function define() {
429
622
  defineElement('el-button', Button);
430
623
  }
624
+
625
+ export { Events };
@@ -51,6 +51,8 @@ import '@webtides/element-library/button/define';
51
51
  | `caret` | `boolean` | `false` | Appends a dropdown caret after the label. |
52
52
  | `loading` | `boolean` | `false` | Shows a spinner and blocks interaction. |
53
53
  | `disabled` | `boolean` | `false` | Disables the button. Reflected: the attribute is there while the button is disabled, see [In a form](#in-a-form). |
54
+ | `toggle` | `boolean` | `false` | The button is a toggle button: a click flips `pressed`, see [A toggle button](#a-toggle-button). Button-only. |
55
+ | `pressed` | `boolean` | `false` | Whether a toggle button is pressed. Reflected on a toggle button (`pressed="true"` / `"false"`). Without `toggle` it does nothing. |
54
56
  | `type` | `string` | `'button'` | `button`, `submit` or `reset`. `submit` / `reset` act on the form of the button, see [In a form](#in-a-form). Button-only. |
55
57
  | `name` | `string` | `''` | Name submitted with the form, when the form is submitted with this button. Button-only. |
56
58
  | `value` | `string` | `''` | Value submitted under that name. Button-only. |
@@ -71,7 +73,11 @@ The attributes `form`, `formaction`, `formenctype`, `formmethod`, `formnovalidat
71
73
 
72
74
  #### Events
73
75
 
74
- _None custom — native `click`, `focus` and `blur` behave as usual._
76
+ | Name | Detail | Description |
77
+ | --- | --- | --- |
78
+ | `button-toggle` | `{ pressed: boolean }` | Fired when a click has flipped a toggle button. `detail.pressed` is the new state. Not fired when `pressed` is set from a script. |
79
+
80
+ Native `click`, `focus` and `blur` behave as usual.
75
81
 
76
82
  #### Slots
77
83
 
@@ -122,6 +128,68 @@ The three properties are independent of each other, and every combination works.
122
128
 
123
129
  The `hidden` attribute hides the button.
124
130
 
131
+ ## In a toolbar
132
+
133
+ An [`el-toolbar`](../toolbar/toolbar.readme.md) gives its controls one size and one appearance. A button takes them from the toolbar while the page has not set `size` or `appearance` on the button itself:
134
+
135
+ ```html
136
+ <el-toolbar label="Message" size="small" appearance="outlined">
137
+ <el-button>Reply</el-button>
138
+ <!-- small, outlined -->
139
+ <el-button size="large">Archive</el-button>
140
+ <!-- large, outlined -->
141
+ <el-button variant="brand" appearance="filled">Send</el-button>
142
+ <!-- small, filled -->
143
+ </el-toolbar>
144
+ ```
145
+
146
+ - A button that names its own keeps it, also when that is the default: `appearance="filled"`, `size="medium"`. It counts as named when the page wrote the attribute, when a script set the property before the button was connected, or when either changes later.
147
+ - The toolbar does not write to the button. It says its size and its appearance in CSS properties that are inherited, and the button reads them in its stylesheet. The `size` and `appearance` attributes of the button keep their own values, `medium` and `filled` for a button that names none.
148
+ - It works at any depth: for a button in an `el-button-group` of the toolbar, and for the anchor of an `el-popover`.
149
+ - Outside of a toolbar nothing changes.
150
+
151
+ ## In the frame of an application
152
+
153
+ A button with `data-navigation-toggle` opens the navigation of an [`el-app-frame`](../app-frame/app-frame.readme.md) where the frame is narrow and the navigation is a drawer. Where the navigation is a column beside the content, there is nothing to open, and the button is not there. The frame says so in a CSS property that is inherited, and the button reads it in its own stylesheet: that holds before any script has run. Outside of a frame the attribute changes nothing.
154
+
155
+ ```html
156
+ <el-button data-navigation-toggle circle="true" aria-label="Navigation"><svg aria-hidden="true">…</svg></el-button>
157
+ ```
158
+
159
+ In an [`el-status-bar`](../status-bar/status-bar.readme.md) a button is as high as the bar and plain, and has its small text. Its focus ring is inside of it there: around the button there is the edge of the bar, and under the bar the edge of the screen. The bar says all of that in CSS properties that are inherited.
160
+
161
+ ## A toggle button
162
+
163
+ With `toggle` the button stays pressed: "Bold" in the toolbar of an editor, "Mute", a filter above a list, the star of a favourite.
164
+
165
+ ```html
166
+ <el-button toggle="true" appearance="outlined">Bold</el-button>
167
+ <el-button toggle="true" pressed="true" appearance="outlined">Italic</el-button>
168
+ <el-button toggle="true" appearance="plain" circle="true" aria-label="Favourite">★</el-button>
169
+ ```
170
+
171
+ ```js
172
+ button.addEventListener('button-toggle', (event) => {
173
+ editor.bold = event.detail.pressed;
174
+ });
175
+ ```
176
+
177
+ - **The state** is `pressed`, a property and an attribute (`pressed="true"`, `pressed="false"`). A toggle button writes the attribute itself, so a stylesheet can select it: `el-button[pressed='true']`.
178
+ - **A click flips it**, and so do the Space and the Enter key. Then the button fires `button-toggle`. A `click` listener of the page that calls `preventDefault()` keeps the button from flipping. Setting `pressed` from a script changes the state and fires nothing.
179
+ - **A screen reader** announces a toggle button with its state: the control has `aria-pressed="true"` or `"false"`. A button without `toggle` has no `aria-pressed`.
180
+ - **A pressed button shows it** with a ring inside of its edge, in the colour of its label, in every variant and appearance. An `outlined` and a `plain` button get a tint of their text colour as well; a fill is not changed, the label on it would lose contrast. The ring is a shape and a border: it tells the button apart without a colour, and stays where the user forces colours, a little wider. It is the `::before` of the control (`::part(base)::before`).
181
+ - **In an `el-button-group`** a pressed button keeps its joined corners and the line to its neighbour; the ring has the corners of the button.
182
+ - **In server-rendered markup** `aria-pressed` and the ring are there before the script has run. The click needs the script.
183
+
184
+ `aria-pressed` that the page sets itself, on a button without `toggle`, goes to the control as before, and no click changes it. Such a button shows the ring as well.
185
+
186
+ #### What it is not
187
+
188
+ - **Not a value of a form.** A toggle button submits nothing. A setting that is saved with a form is an [`el-switch-field`](../switch-field/switch-field.readme.md) or an [`el-checkbox-field`](../checkbox-field/checkbox-field.readme.md).
189
+ - **Not a button whose name changes with its state.** A button that reads "Mute" and then "Unmute" says its state with its name, and is no toggle: with `aria-pressed` on top a screen reader would announce "Unmute, pressed". Give a toggle button one name that stays ("Mute"), and let the state say the rest.
190
+ - **Not one choice of several.** A toggle button knows nothing of its neighbours: in a row of them, any number can be pressed. A row of which one is chosen at a time is an [`el-segmented-field`](../segmented-field/segmented-field.readme.md).
191
+ - **Not a link.** `toggle` is ignored on a button with `href`.
192
+
125
193
  ## In a form
126
194
 
127
195
  An `el-button` of the type `submit` submits its form as a native submit button does, and one of the type `reset` resets it.
@@ -147,11 +215,19 @@ An `el-button` of the type `submit` submits its form as a native submit button d
147
215
 
148
216
  #### The native submit button
149
217
 
150
- A form is submitted with a submit button that belongs to it. The control of `el-button` is in a shadow root and has no form, so a button of the type `submit` keeps a hidden native `<button type="submit">` as a child of the element. That button carries the `name`, the `value` and the attributes above.
218
+ A form is submitted with a submit button that belongs to it. The control of `el-button` is in a shadow root and has no form, so a button of the type `submit` submits with a hidden native `<button type="submit">` that is a child of the element. That button carries the `name`, the `value` and the attributes above.
151
219
 
220
+ - The element makes it when the form is first submitted with the button: with a click on the `el-button`, or with Enter in an `<input>` or a `<select>` of the form. Until then the children of the `el-button` are what the page wrote. After that it stays, as the last child.
152
221
  - It is the `submitter` of the `submit` event: `event.submitter.closest('el-button')` is the `el-button`.
153
222
  - Enter in a field clicks it, so a `click` listener on the `el-button` hears that click, with the native button as its target. A native submit button hears the same click.
154
- - When the page replaces the content of the `el-button`, the element puts it back.
223
+ - When the page replaces the content of the `el-button`, the element makes it again with the next submit.
224
+ - `form.querySelector('[type="submit"]')` does not find it before the first submit. `form.elements` lists the `el-button` from the start.
225
+
226
+ #### On a page that a framework hydrates
227
+
228
+ A framework that hydrates a server-rendered page expects the children it rendered, and no other. The element adds its native submit button for a submit only, so a server-rendered `el-button` hydrates also when the element has started before the framework does (Svelte 5: no `hydration_mismatch`). The page has nothing to write for that.
229
+
230
+ One case is left: a submit before the hydration that does not leave the page, such as a click on the button of a form that is not valid. The native button is there afterwards, and the framework finds a child it did not render. A page that writes the native submit button itself, as in [Before the script has run](#before-the-script-has-run), does not have that case: the framework has rendered the button and expects it.
155
231
 
156
232
  #### The `disabled` attribute
157
233
 
@@ -176,8 +252,7 @@ The element itself has no role. What takes the focus and what a screen reader an
176
252
  <!-- An icon button gets its name -->
177
253
  <el-button circle="true" aria-label="Add">+</el-button>
178
254
 
179
- <!-- A toggle, a button that opens something -->
180
- <el-button aria-pressed="true">Bold</el-button>
255
+ <!-- A button that opens something -->
181
256
  <el-button aria-expanded="false" aria-controls="filters">Filters</el-button>
182
257
 
183
258
  <!-- Described by a text elsewhere in the page -->
@@ -190,7 +265,8 @@ The element itself has no role. What takes the focus and what a screen reader an
190
265
  | `aria-label`, `aria-description`, `aria-expanded`, `aria-haspopup`, `aria-pressed`, `aria-current` | As the same attribute, with every change. |
191
266
  | `aria-labelledby`, `aria-describedby`, `aria-controls` | As references to the elements they name. An id does not reach out of the shadow root the control is in. |
192
267
 
193
- - The attributes stay on the element as well, where a stylesheet can select them: `el-button[aria-pressed='true']`.
268
+ - The attributes stay on the element as well, where a stylesheet can select them: `el-button[aria-expanded='true']`.
269
+ - `aria-pressed` is the state of a button that stays pressed. With [`toggle`](#a-toggle-button) the button keeps that state itself, and an `aria-pressed` of the page gives way to it.
194
270
  - An empty attribute counts as not set.
195
271
  - `aria-disabled` and `aria-busy` are not passed on. The button sets them itself, from `disabled` and `loading`.
196
272
  - The elements behind an id are looked up in the document, or in the shadow root the button is used in, when the button renders. An element that comes into the page later is found once the attribute is set again.