@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
@@ -0,0 +1,138 @@
1
+ import { TemplateElement, defineElement, html, spreadAttributes } from '@webtides/element-js';
2
+ import { activeElement } from '../../utils/focus.js';
3
+ import style from './tree.style.js';
4
+
5
+ /**
6
+ * A tree: entries that open and close, for folders, categories and a library beside the content.
7
+ * The tree is a list; every `el-tree-item` in it is an entry, and an entry holds the entries
8
+ * below it. An entry is a link, and one with entries below it has a button that opens and closes
9
+ * them.
10
+ *
11
+ * To assistive technology it is what it is in the markup: nested lists of links, each branch with
12
+ * a button that says whether it is open. It has no `role="tree"`: the links stay links, and they
13
+ * work before the script has run. The script adds the arrow keys of a tree.
14
+ *
15
+ * @element el-tree
16
+ *
17
+ * @slot - The entries of the first level: `el-tree-item` elements.
18
+ *
19
+ * @csspart base - The list of the first level (`role="list"`).
20
+ *
21
+ * @cssproperty --el-tree-indent - How far the entries of a level stand in from the ones of the
22
+ * level above. Unset, it is `--el-space-4`, and `1rem` without a theme. Read by the entries, so
23
+ * it can be set on the tree or for the page.
24
+ *
25
+ * @property {string} label - The name of the tree for assistive technology. Unset, the tree has
26
+ * none.
27
+ */
28
+ export default class Tree extends TemplateElement {
29
+ constructor() {
30
+ super({
31
+ styles: [style],
32
+ shadowRender: true,
33
+ });
34
+ }
35
+
36
+ properties() {
37
+ return {
38
+ label: '',
39
+ };
40
+ }
41
+
42
+ events() {
43
+ return {
44
+ this: {
45
+ keydown: (event) => this._onKeydown(event),
46
+ },
47
+ };
48
+ }
49
+
50
+ /**
51
+ * The entries that are shown, in their order: every entry that is in no closed branch. An
52
+ * entry is asked for the control the focus goes to, see `focusTarget` of `el-tree-item`; one
53
+ * that has none (a name without a link and without entries below it) is passed.
54
+ * @private
55
+ * @returns {HTMLElement[]}
56
+ */
57
+ _shownItems() {
58
+ const isShown = (item) => {
59
+ for (let parent = this._parentOf(item); parent; parent = this._parentOf(parent)) {
60
+ if (!parent.open) return false;
61
+ }
62
+ return true;
63
+ };
64
+ return [...this.querySelectorAll('el-tree-item')].filter((item) => item.focusTarget && isShown(item));
65
+ }
66
+
67
+ /**
68
+ * The entry an entry is below, or `null` for one of the first level.
69
+ * @private
70
+ * @param {Element} item
71
+ * @returns {Element | null}
72
+ */
73
+ _parentOf(item) {
74
+ const parent = item.parentElement?.closest('el-tree-item');
75
+ return parent && this.contains(parent) ? parent : null;
76
+ }
77
+
78
+ /**
79
+ * The keys of the WAI-ARIA tree view pattern. Up and down go to the entry before and after
80
+ * among the ones that are shown, Home and End to the first and the last. Right opens a branch,
81
+ * and goes to its first entry when it is open; left closes it, and goes to the entry above
82
+ * when it is closed or has nothing below it. In a text that runs from right to left, the two
83
+ * are the other way round.
84
+ * @private
85
+ * @param {KeyboardEvent} event
86
+ */
87
+ _onKeydown(event) {
88
+ if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey || event.isComposing) return;
89
+
90
+ // The entry the focus is in: on its link, or on the button that opens it.
91
+ const focused = activeElement();
92
+ const current = event
93
+ .composedPath()
94
+ .find((node) => node instanceof Element && node.localName === 'el-tree-item' && this.contains(node));
95
+ if (!current || !current.shadowRoot?.contains(focused)) return;
96
+
97
+ const items = this._shownItems();
98
+ const index = items.indexOf(current);
99
+ const rtl = getComputedStyle(this).direction === 'rtl';
100
+ const [inwards, outwards] = rtl ? ['ArrowLeft', 'ArrowRight'] : ['ArrowRight', 'ArrowLeft'];
101
+ const focus = (item) => item?.focusTarget?.focus();
102
+
103
+ const keys = {
104
+ ArrowDown: () => focus(items[index + 1]),
105
+ ArrowUp: () => focus(items[index - 1]),
106
+ Home: () => focus(items[0]),
107
+ End: () => focus(items[items.length - 1]),
108
+ [inwards]: () => {
109
+ if (!current.hasItems) return;
110
+ if (!current.open) current.toggle(true);
111
+ else focus(items[index + 1]);
112
+ },
113
+ [outwards]: () => {
114
+ if (current.hasItems && current.open) current.toggle(false);
115
+ else focus(this._parentOf(current));
116
+ },
117
+ };
118
+ if (!Object.hasOwn(keys, event.key)) return;
119
+
120
+ // The keys would scroll the page.
121
+ event.preventDefault();
122
+ keys[event.key]();
123
+ }
124
+
125
+ template() {
126
+ // The role is inside of the shadow root, not on the element: it is part of the
127
+ // server-rendered markup that way, and needs no script.
128
+ return html`
129
+ <div part="base" class="tree" role="list" ${spreadAttributes({ 'aria-label': this.label || undefined })}>
130
+ <slot></slot>
131
+ </div>
132
+ `;
133
+ }
134
+ }
135
+
136
+ export function define() {
137
+ defineElement('el-tree', Tree);
138
+ }
@@ -0,0 +1,119 @@
1
+ # Tree
2
+
3
+ `tree` shows entries that open and close: folders, categories, the library beside the content. The tree is a list; every [`el-tree-item`](../tree-item/tree-item.readme.md) in it is an entry, and an entry holds the entries below it. An entry is a link, and one with entries below it has a button that opens and closes them.
4
+
5
+ ## How to use
6
+
7
+ #### Install
8
+
9
+ ```sh
10
+ npm i --save @webtides/element-library
11
+ ```
12
+
13
+ #### Use
14
+
15
+ ```js
16
+ import '@webtides/element-library/tree/define';
17
+ import '@webtides/element-library/tree-item/define';
18
+ ```
19
+
20
+ ```html
21
+ <el-tree label="Folders">
22
+ <el-tree-item label="Inbox" href="/inbox" open="true">
23
+ <span slot="end">12</span>
24
+ <el-tree-item label="Work" href="/inbox/work" current="true"></el-tree-item>
25
+ <el-tree-item label="Private" href="/inbox/private"></el-tree-item>
26
+ </el-tree-item>
27
+ <el-tree-item label="Drafts" href="/drafts"></el-tree-item>
28
+ </el-tree>
29
+ ```
30
+
31
+ > Boolean attributes are passed as strings (`open="true"`), matching the rest of the library.
32
+
33
+ ## API
34
+
35
+ #### Properties/Attributes
36
+
37
+ | Name | Type | Default | Description |
38
+ | ------- | -------- | ------- | ------------------------------------------------------------------------ |
39
+ | `label` | `string` | `''` | The name of the tree for assistive technology. Unset, the tree has none. |
40
+
41
+ #### Methods
42
+
43
+ _None._
44
+
45
+ #### Events
46
+
47
+ _None of its own. `tree-item-toggle` of an entry bubbles through the tree: listen for it there to keep the open branches._
48
+
49
+ #### Slots
50
+
51
+ | Name | Description |
52
+ | --------- | -------------------------------------------------------- |
53
+ | (default) | The entries of the first level: `el-tree-item` elements. |
54
+
55
+ #### CSS Parts
56
+
57
+ | Name | Description |
58
+ | ------ | -------------------------------------------- |
59
+ | `base` | The list of the first level (`role="list"`). |
60
+
61
+ #### CSS Custom Properties
62
+
63
+ | Name | Default | Description |
64
+ | --- | --- | --- |
65
+ | `--el-tree-indent` | `--el-space-4` | How far the entries of a level stand in from the ones of the level above. Read by the entries, so it can be set on the tree or for the page. Without a theme it is `1rem`. |
66
+
67
+ ## Open branches
68
+
69
+ Which branches are open is said by the `open` attribute of the entries, and by nothing else: the tree keeps no state of its own. So the page can render it, on a server as well, and keep it:
70
+
71
+ ```js
72
+ tree.addEventListener('tree-item-toggle', (event) => {
73
+ // The entry itself, not one further up: the event bubbles.
74
+ remember(event.target.id, event.detail.open);
75
+ });
76
+ ```
77
+
78
+ A branch that holds the current entry is not opened by the tree. Render `open="true"` on the entries above the current one.
79
+
80
+ ## What it is, and what it is not
81
+
82
+ To assistive technology the tree is what its markup says: a list of links, with a list below each entry that has entries, and a button per branch that says whether the branch is open (`aria-expanded`). It has no `role="tree"`.
83
+
84
+ That is a decision. `role="tree"` makes a widget of the list: a screen reader stops reading it as links, and its user has to know the keys of a tree and has nothing before the script has run. The WAI-ARIA Authoring Practices advise against it for a navigation. As nested lists the entries stay links: they are in the list of links of a screen reader, they say "current page" and "visited", and they work without JavaScript.
85
+
86
+ What the tree takes from the tree view pattern is its keyboard, on top of the Tab key.
87
+
88
+ ## Keyboard
89
+
90
+ Every link and every button of a branch is a stop of the Tab key, as on any page. With the focus on an entry, the script adds the keys of the WAI-ARIA tree view pattern:
91
+
92
+ | Key | What it does |
93
+ | --- | --- |
94
+ | Down arrow, up arrow | Moves the focus to the next and the previous entry that is shown. It stops at the ends. |
95
+ | Right arrow | Opens a closed branch. On an open one, moves the focus to its first entry. |
96
+ | Left arrow | Closes an open branch. On a closed one, or an entry without entries below, moves the focus to the entry above. |
97
+ | Home, End | Moves the focus to the first and the last entry that is shown. |
98
+ | Enter | Follows the link, as on any link. |
99
+
100
+ In a text that runs from right to left, the left arrow opens and goes in, and the right arrow closes and goes out.
101
+
102
+ ## Accessibility
103
+
104
+ - The tree has `role="list"`, every entry `role="listitem"`, and the entries below an entry are a list of their own: a screen reader says how many entries a level has.
105
+ - `label` names the tree. A tree that is the navigation of a page belongs into a `<nav>` of the page, which carries the name then.
106
+ - The entry of the page the user is on has `aria-current="page"`, see [`el-tree-item`](../tree-item/tree-item.readme.md#the-current-entry).
107
+ - The button of a branch is named by the name of its entry and says whether the branch is open.
108
+ - On a touch screen (`@media (any-pointer: coarse)`) a row is at least `--el-touch-target` high, and the button of a branch is that wide and high.
109
+
110
+ ## Server-side rendering
111
+
112
+ The lists, the links, the buttons and the open branches are part of the server-rendered markup (Declarative Shadow DOM): the stylesheet of an entry reads its `open` attribute. A server-rendered tree shows its open branches before the elements take over and looks the same after, and its links work without JavaScript. The script adds the opening and closing and the arrow keys.
113
+
114
+ The `hidden` attribute hides the tree, and an entry.
115
+
116
+ ## What it does not do
117
+
118
+ - No moving of entries by dragging, no renaming in place, no checkbox per entry.
119
+ - No loading of the entries below when a branch opens: the entries are in the page.
@@ -0,0 +1,16 @@
1
+ import hostStyle from '../../styles/host.style.js';
2
+
3
+ const css = String.raw;
4
+
5
+ const style = css`
6
+ :host {
7
+ display: block;
8
+ }
9
+
10
+ .tree {
11
+ font-family: var(--el-font-family, inherit);
12
+ line-height: var(--el-line-height, inherit);
13
+ }
14
+ `;
15
+
16
+ export default [hostStyle(), style].join('\n');
@@ -0,0 +1,23 @@
1
+ # Change Log
2
+
3
+ All notable changes to this component live here. Versions refer to the parent `@webtides/element-library` release (see root [`CHANGELOG.md`](../../../CHANGELOG.md)); components no longer version independently.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/).
6
+
7
+ <!--
8
+ When your PR touches this component, add an entry under Unreleased and
9
+ mirror it (prefixed with the component name) in root CHANGELOG.md.
10
+ Uncomment headings as needed.
11
+ -->
12
+
13
+ ## Unreleased
14
+
15
+ ## [0.4.0] - 2026-11-27
16
+
17
+ ### Added
18
+
19
+ - Initial `el-tree-item`: one entry of an `el-tree`, an item of its list (`role="listitem"`), with the entries below it in a list of their own. It has an icon at the start (`start` slot), its name (`label`, or the `label` slot) and a count at the end (`end`). ([#193](https://github.com/webtides/element-library/issues/193))
20
+ - With `href` the name is a native link whose target is the whole row. An entry that also has entries below it gets a button that opens and closes them, named by the name of the entry, with `aria-expanded`. Without `href`, an entry with entries below it is that button as a whole.
21
+ - `open` is the state of a branch, reflected to the attribute. The stylesheet reads the attribute: a server-rendered entry shows its entries, or hides them, from the first paint. The button, the arrow keys and `toggle()` change it and fire `tree-item-toggle`; setting `open` from a script fires nothing.
22
+ - `current` marks the entry of the page the user is on: `aria-current="page"`, a tint, a bar at the start in the accent colour and a heavier name. The bar is a border, so it stays where the user forces colours.
23
+ - A row is as wide as the tree on every level, and what stands in it is moved in by `--el-tree-indent` per level. A long name breaks, inside of a word where it has to. On a touch screen a row is at least `--el-touch-target` high, and the button of a branch that wide and high.
@@ -0,0 +1,3 @@
1
+ import { define } from './tree-item.js';
2
+
3
+ define();
@@ -0,0 +1,3 @@
1
+ export default {
2
+ TOGGLE: 'tree-item-toggle',
3
+ };
@@ -0,0 +1,246 @@
1
+ import { TemplateElement, defineElement, html, classMap, spreadAttributes } from '@webtides/element-js';
2
+ import { hasSlotted } from '../../utils/slots.js';
3
+ import style from './tree-item.style.js';
4
+ import Events from './tree-item.events.js';
5
+
6
+ /**
7
+ * One entry of an `el-tree`, and the entries below it. It has the parts of a row of a list: an
8
+ * icon at the start, its name, a count at the end. With `href` the name is a link, and the whole
9
+ * row is its target. An entry with entries below it has a button that opens and closes them; its
10
+ * state is the `open` attribute, which the page can render.
11
+ *
12
+ * The entries below it are the `el-tree-item` elements the page writes into it.
13
+ *
14
+ * @element el-tree-item
15
+ *
16
+ * @fires {CustomEvent<{ open: boolean }>} tree-item-toggle - Fired when the user has opened or
17
+ * closed the entry, with its button or with the arrow keys. `detail.open` is the new state. Not
18
+ * fired when `open` is set from a script. It bubbles: an entry further up hears it as well, so
19
+ * look at `event.target`.
20
+ *
21
+ * @slot - The entries below this one: `el-tree-item` elements.
22
+ * @slot label - The name of the entry. Takes precedence over the `label` attribute.
23
+ * @slot start - What stands in front of the name: an icon.
24
+ * @slot end - What stands at the end of the row: a count. In an entry that is a link it is part
25
+ * of the target of the link.
26
+ *
27
+ * @csspart base - The entry: an item of the list it is in.
28
+ * @csspart row - The row of the entry: its button, its start, its name and its end.
29
+ * @csspart toggle - The button that opens and closes the entries below, present only with such
30
+ * entries.
31
+ * @csspart start - The region of the `start` slot.
32
+ * @csspart label - The name: an `<a>` with `href`; without, a `<button>` that opens and closes
33
+ * the entry when it has entries below it, and a `<span>` when it has none.
34
+ * @csspart end - The region of the `end` slot.
35
+ * @csspart children - The list of the entries below (`role="list"`).
36
+ *
37
+ * @cssproperty --el-tree-indent - How far the entries below stand in from this one. Unset, it is
38
+ * `--el-space-4`, and `1rem` without a theme.
39
+ *
40
+ * @property {string} label - The name of the entry.
41
+ * @property {string} href - The address the entry leads to. Unset, the entry is no link.
42
+ * @property {string} target - Where to open the address (only with `href`).
43
+ * @property {string} rel - `rel` attribute for the link, written when `target` is set. Default
44
+ * `'noreferrer noopener'`.
45
+ * @property {boolean} current - The entry is the one of the page the user is on: it gets
46
+ * `aria-current="page"` (`"true"` without `href`), a bar at its start and a heavier name.
47
+ * Default `false`.
48
+ * @property {boolean} open - Whether the entries below this one are shown. Reflected to the
49
+ * `open` attribute, which the stylesheet reads: an entry without `open="true"` is closed, before
50
+ * the element starts as well. Default `false`.
51
+ */
52
+ export default class TreeItem extends TemplateElement {
53
+ constructor() {
54
+ super({
55
+ styles: [style],
56
+ shadowRender: true,
57
+ // The stylesheet selects the host by this attribute.
58
+ propertyOptions: { open: { reflect: true } },
59
+ });
60
+ }
61
+
62
+ properties() {
63
+ return {
64
+ label: '',
65
+ href: '',
66
+ target: '',
67
+ rel: 'noreferrer noopener',
68
+ current: false,
69
+ open: false,
70
+ };
71
+ }
72
+
73
+ events() {
74
+ return {
75
+ '[data-toggle]': {
76
+ click: () => this.toggle(),
77
+ },
78
+ // A template element does not render again when a child comes, goes or changes its
79
+ // `slot` attribute. The slot tells of all three, and the event bubbles to the base.
80
+ "[part~='base']": {
81
+ slotchange: () => this.requestUpdate(),
82
+ },
83
+ };
84
+ }
85
+
86
+ /**
87
+ * Whether the entry has entries below it. The children of the host are asked, not the slot:
88
+ * a template is rendered on a server too, where there are no slots to ask.
89
+ * @returns {boolean}
90
+ */
91
+ get hasItems() {
92
+ return [...(this.children ?? [])].some((child) => !child.getAttribute('slot'));
93
+ }
94
+
95
+ /**
96
+ * The control of the entry that the focus goes to: its link, or the button its name is when it
97
+ * has entries below it and no address. `null` for a name alone.
98
+ * @returns {HTMLElement | null}
99
+ */
100
+ get focusTarget() {
101
+ return this.shadowRoot?.querySelector('a[part~="label"], button[part~="label"]') ?? null;
102
+ }
103
+
104
+ /**
105
+ * Opens or closes the entry, as its button does, and says so with `tree-item-toggle`. An entry
106
+ * without entries below it stays as it is.
107
+ * @param {boolean} [open] - The state to go to. Left out, the entry goes to the other one.
108
+ * @returns {void}
109
+ */
110
+ toggle(open = !this.open) {
111
+ if (!this.hasItems || !!open === !!this.open) return;
112
+ this.open = !!open;
113
+ this.dispatch(Events.TOGGLE, { open: this.open });
114
+ }
115
+
116
+ /** @private */
117
+ labelContent() {
118
+ return html`<slot name="label">${this.label}</slot>`;
119
+ }
120
+
121
+ /**
122
+ * The name of the entry, and what it is: a link, the button of a branch that leads nowhere, or
123
+ * a text.
124
+ * @private
125
+ */
126
+ labelTemplate(hasItems) {
127
+ if (this.href) {
128
+ return html`
129
+ <a
130
+ part="label"
131
+ id="label"
132
+ class="label"
133
+ href="${this.href}"
134
+ ${spreadAttributes({
135
+ target: this.target || undefined,
136
+ rel: this.target ? this.rel : undefined,
137
+ 'aria-current': this.current ? 'page' : undefined,
138
+ })}
139
+ >${this.labelContent()}</a
140
+ >
141
+ `;
142
+ }
143
+
144
+ const current = { 'aria-current': this.current ? 'true' : undefined };
145
+ if (hasItems) {
146
+ return html`
147
+ <button
148
+ part="label"
149
+ id="label"
150
+ class="label"
151
+ type="button"
152
+ aria-expanded="${this.open ? 'true' : 'false'}"
153
+ aria-controls="children"
154
+ data-toggle
155
+ ${spreadAttributes(current)}
156
+ >
157
+ ${this.labelContent()}
158
+ </button>
159
+ `;
160
+ }
161
+
162
+ return html`<span part="label" id="label" class="label" ${spreadAttributes(current)}
163
+ >${this.labelContent()}</span
164
+ >`;
165
+ }
166
+
167
+ /**
168
+ * The button that opens and closes the entries below: for an entry that is a link, which
169
+ * cannot do both. It is named by the name of the entry, and says whether the entry is open.
170
+ * @private
171
+ */
172
+ toggleTemplate() {
173
+ return html`
174
+ <button
175
+ part="toggle"
176
+ class="toggle"
177
+ type="button"
178
+ aria-labelledby="label"
179
+ aria-expanded="${this.open ? 'true' : 'false'}"
180
+ aria-controls="children"
181
+ data-toggle
182
+ >
183
+ ${this.chevronTemplate()}
184
+ </button>
185
+ `;
186
+ }
187
+
188
+ /** @private */
189
+ chevronTemplate() {
190
+ return html`
191
+ <svg
192
+ class="chevron"
193
+ viewBox="0 0 24 24"
194
+ fill="none"
195
+ stroke="currentColor"
196
+ stroke-width="2"
197
+ stroke-linecap="round"
198
+ stroke-linejoin="round"
199
+ aria-hidden="true"
200
+ >
201
+ <path d="m9 6 6 6-6 6"></path>
202
+ </svg>
203
+ `;
204
+ }
205
+
206
+ template() {
207
+ const hasItems = this.hasItems;
208
+ const isLink = !!this.href;
209
+ const classes = {
210
+ row: true,
211
+ 'row--link': isLink,
212
+ 'row--branch': hasItems,
213
+ 'row--current': !!this.current,
214
+ };
215
+
216
+ // The roles are inside of the shadow root, not on the element: they are part of the
217
+ // server-rendered markup that way, and need no script.
218
+ return html`
219
+ <div part="base" class="item" role="listitem">
220
+ <div part="row" class="${classMap(classes)}">
221
+ ${
222
+ hasItems && isLink
223
+ ? this.toggleTemplate()
224
+ : html`<span class="toggle-place" aria-hidden="true"
225
+ >${hasItems ? this.chevronTemplate() : ''}</span
226
+ >`
227
+ }
228
+ <span part="start" class="start" ?hidden="${!hasSlotted(this, 'start')}"
229
+ ><slot name="start"></slot
230
+ ></span>
231
+ ${this.labelTemplate(hasItems)}
232
+ <span part="end" class="end" ?hidden="${!hasSlotted(this, 'end')}"><slot name="end"></slot></span>
233
+ </div>
234
+ <div part="children" id="children" class="children" role="list" ?hidden="${!hasItems}">
235
+ <slot></slot>
236
+ </div>
237
+ </div>
238
+ `;
239
+ }
240
+ }
241
+
242
+ export function define() {
243
+ defineElement('el-tree-item', TreeItem);
244
+ }
245
+
246
+ export { Events };