@grucloud/bau-ui 0.301.0 → 0.303.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 (61) hide show
  1. package/accordion/accordion.js +36 -63
  2. package/alert/alert.js +14 -24
  3. package/alertStack/README.md +89 -0
  4. package/alertStack/alertStack.js +98 -28
  5. package/animate/animate.js +2 -0
  6. package/autocomplete/autocomplete.js +130 -164
  7. package/avatar/avatar.js +20 -25
  8. package/breadcrumbs/breadcrumbs.js +34 -30
  9. package/button/button.js +27 -41
  10. package/buttonGroup/buttonGroup.js +20 -27
  11. package/calendar/calendar.js +6 -12
  12. package/carousel/carousel.js +64 -47
  13. package/checkbox/checkbox.js +10 -13
  14. package/chip/chip.js +9 -0
  15. package/collapsible/collapsible.js +73 -47
  16. package/collapsible/index.d.ts +12 -1
  17. package/constants.d.ts +1 -0
  18. package/constants.js +4 -0
  19. package/divider/divider.js +1 -0
  20. package/drawer/drawer.js +48 -1
  21. package/drillDownMenu/drillDownMenu.js +90 -67
  22. package/dropdownMenu/dropdownMenu.js +29 -122
  23. package/fileInput/fileInput.js +4 -0
  24. package/globalStyle/globalStyle.js +75 -30
  25. package/input/input.js +35 -44
  26. package/inputSearch/inputSearch.js +26 -31
  27. package/lazy/lazy.js +8 -5
  28. package/linearProgress/linearProgress.js +12 -24
  29. package/list/list.js +2 -1
  30. package/loadingButton/loadingButton.js +29 -22
  31. package/modal/modal.js +38 -41
  32. package/multiSelect/multiSelect.js +84 -171
  33. package/package.json +13 -5
  34. package/paginationNavigation/paginationNavigation.js +8 -13
  35. package/popover/popover.js +5 -2
  36. package/radioButton/radioButton.js +10 -23
  37. package/radioButtonGroup/radioButtonGroup.js +15 -24
  38. package/resizable/resizable.js +74 -70
  39. package/select/select.js +84 -160
  40. package/selectNative/selectNative.js +2 -0
  41. package/slider/slider.js +6 -12
  42. package/spinner/spinner.js +37 -53
  43. package/stepper/stepper.js +23 -35
  44. package/switch/switch.js +28 -45
  45. package/tableOfContent/tableOfContent.js +55 -84
  46. package/tablePagination/tablePagination.js +39 -69
  47. package/tabs/tabs.js +95 -59
  48. package/textareaAutosize/textareaAutosize.js +13 -16
  49. package/themeSwitch/themeSwitch.js +27 -41
  50. package/toggle/index.d.ts +8 -1
  51. package/toggle/toggle.js +17 -9
  52. package/toggleGroup/toggleGroup.js +23 -28
  53. package/tooltip/tooltip.js +79 -94
  54. package/treeView/treeView.js +57 -25
  55. package/utils/control.js +20 -0
  56. package/utils/popupList.js +137 -0
  57. package/utils/urlChange.js +32 -0
  58. package/virtualList/index.d.ts +27 -0
  59. package/virtualList/index.js +1 -0
  60. package/virtualList/virtualList.js +111 -0
  61. package/vite.config.js +48 -0
@@ -1,22 +1,5 @@
1
- import { toPropsAndChildren } from "@grucloud/bau/bau.js";
2
1
  import collapsible from "../collapsible";
3
- import { Colors } from "../constants.js";
4
-
5
- const colorsToCss = () =>
6
- Colors.map(
7
- (color) =>
8
- `
9
- & li.plain.${color} h3::after {
10
- color: var(--color-${color});
11
- }
12
- & li.outline.${color} h3::after {
13
- color: var(--color-${color});
14
- }
15
- & h3.solid.${color}:hover {
16
- filter: brightness(var(--brightness-hover-always));
17
- }
18
- `
19
- ).join("\n");
2
+ import { ofColor } from "../constants.js";
20
3
 
21
4
  export default function (context, options = {}) {
22
5
  const { bau, css } = context;
@@ -64,69 +47,59 @@ export default function (context, options = {}) {
64
47
  }
65
48
  }
66
49
  }
67
- ${colorsToCss()}
50
+ & li:is(.plain, .outline)${ofColor} h3::after {
51
+ color: var(--accent-text);
52
+ }
53
+ & h3.solid${ofColor}:hover {
54
+ filter: brightness(var(--brightness-hover-always));
55
+ }
68
56
  `;
69
57
 
70
- return function Accordion(...args) {
71
- let [
72
- {
73
- size = options.size ?? "md",
74
- variant = options.variant ?? "plain",
75
- color = options.color ?? "neutral",
76
- data = [],
77
- ...props
78
- },
79
- ] = toPropsAndChildren(args);
80
-
81
- const itemNameState = bau.state("");
82
-
58
+ return function Accordion({
59
+ size = options.size ?? "md",
60
+ variant = options.variant ?? "plain",
61
+ color = options.color ?? "neutral",
62
+ data = [],
63
+ ...props
64
+ } = {}) {
83
65
  const Collapsible = collapsible(context, { size, variant, color });
84
66
 
85
- const onclick = (name) => (event) => {
86
- if (itemNameState.val == name) {
87
- itemNameState.val = "";
88
- } else {
89
- itemNameState.val = name;
90
- }
91
- };
67
+ // One section open at a time
68
+ const openStates = data.map(() => bau.state(false));
69
+ const onToggle =
70
+ (index) =>
71
+ ({ open }) =>
72
+ open &&
73
+ openStates.forEach((state, i) => i != index && (state.val = false));
92
74
 
93
- const AccordionItem = (item) => {
75
+ const AccordionItem = (item, index) => {
94
76
  const { Header, Content, name } = item;
77
+ const openState = openStates[index];
78
+
79
+ // The collapsible gives the button aria-expanded and aria-controls
95
80
  const AccordionHeader = () =>
96
81
  h3(
97
- {
98
- class: () => itemNameState.val == name && "active",
99
- },
100
- button(
101
- {
102
- type: "button",
103
- "aria-controls": `bau-${name}`,
104
- "aria-expanded": ({ element }) => itemNameState.val == name,
105
- },
106
- Header(item)
107
- )
82
+ { class: () => [openState.val && "active"] },
83
+ button({ type: "button" }, Header(item))
108
84
  );
109
85
 
110
86
  const AccordionContent = () =>
111
87
  div(
112
- {
113
- id: `bau-${name}`,
114
- "data-state": ({ element }) => {
115
- const open = itemNameState.val == name;
116
- return open;
117
- },
118
- },
88
+ { id: `bau-${name}`, "data-state": () => openState.val },
119
89
  Content(item)
120
90
  );
121
91
 
122
92
  return li(
123
- {
124
- class: [color, variant, size],
125
- onclick: onclick(name),
126
- },
127
- Collapsible({ Header: AccordionHeader, Content: AccordionContent })
93
+ { class: [color, variant, size] },
94
+ Collapsible({
95
+ Header: AccordionHeader,
96
+ Content: AccordionContent,
97
+ openState,
98
+ onToggle: onToggle(index),
99
+ })
128
100
  );
129
101
  };
102
+
130
103
  return div(
131
104
  {
132
105
  class: ["accordion", className, options?.class, props.class],
package/alert/alert.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { toPropsAndChildren } from "@grucloud/bau/bau.js";
2
- import { Colors } from "../constants.js";
2
+ import { ofColor } from "../constants.js";
3
3
 
4
4
  import button from "../button";
5
5
 
@@ -11,27 +11,6 @@ const severityMap = {
11
11
  neutral: "\u24D8",
12
12
  };
13
13
 
14
- const colorsToCss = () =>
15
- Colors.map(
16
- (color) =>
17
- `
18
- &.alert {
19
- &.plain.${color} {
20
- & .icon {
21
- color: var(--color-${color});
22
-
23
- }
24
- }
25
- &.outline.${color} {
26
- & .icon {
27
- color: var(--color-${color});
28
- }
29
- border: 2px solid var(--color-${color});
30
- }
31
- }
32
- `
33
- ).join("\n");
34
-
35
14
  export default function (context, options = {}) {
36
15
  const { bau, css } = context;
37
16
  const { div, span } = bau.tags;
@@ -68,8 +47,18 @@ export default function (context, options = {}) {
68
47
  }
69
48
  & .button-close {
70
49
  margin: 1rem;
50
+ /* On the color of the alert: its text color, no background of its own */
51
+ background: none;
52
+ color: inherit;
53
+ }
54
+ &.alert {
55
+ &:is(.plain, .outline)${ofColor} .icon {
56
+ color: var(--accent-text);
57
+ }
58
+ &.outline${ofColor} {
59
+ border: 1px solid var(--accent-border);
60
+ }
71
61
  }
72
- ${colorsToCss()}
73
62
  `;
74
63
  const Button = button(context);
75
64
 
@@ -110,7 +99,8 @@ export default function (context, options = {}) {
110
99
  ],
111
100
  role: "alert",
112
101
  },
113
- span({ class: "icon" }, severityMap[color]),
102
+ // The color tells the severity: the icon is not read
103
+ span({ class: "icon", "aria-hidden": "true" }, severityMap[color]),
114
104
  div({ class: "content" }, children),
115
105
  onRemove && CloseIcon({ onclick: onRemove })
116
106
  );
@@ -0,0 +1,89 @@
1
+ # AlertStack
2
+
3
+ A stack of alerts, or toasts, shown at the top right of the page. The app adds and removes
4
+ alerts with events on `document`: it does not need a reference to the stack.
5
+
6
+ ## Usage
7
+
8
+ Create the stack once, put it in the layout of the app:
9
+
10
+ ```js
11
+ import alertStack from "@grucloud/bau-ui/alertStack";
12
+ import alert from "@grucloud/bau-ui/alert";
13
+
14
+ const AlertStack = alertStack(context, { limit: 5, deleteAfterDuration: 10e3 });
15
+ const Alert = alert(context);
16
+
17
+ document.body.append(AlertStack());
18
+ ```
19
+
20
+ Add an alert from anywhere:
21
+
22
+ ```js
23
+ document.dispatchEvent(
24
+ new CustomEvent("alert.add", {
25
+ detail: {
26
+ id: "saved",
27
+ // onRemove: the Close button of the alert, for the keyboard
28
+ Component: ({ onRemove }) =>
29
+ Alert({ color: "success", onRemove }, "Saved"),
30
+ },
31
+ }),
32
+ );
33
+ ```
34
+
35
+ Remove it before its time:
36
+
37
+ ```js
38
+ document.dispatchEvent(
39
+ new CustomEvent("alert.remove", { detail: { id: "saved" } }),
40
+ );
41
+ ```
42
+
43
+ ## Options
44
+
45
+ `alertStack(context, options)`:
46
+
47
+ | Option | Default | Description |
48
+ | --------------------- | ------- | -------------------------------------------------- |
49
+ | `limit` | `10` | The most alerts shown; past it, the oldest leaves. |
50
+ | `deleteAfterDuration` | `15000` | The time, in milliseconds, an alert stays shown. |
51
+ | `class` | | A class added to every stack of the factory. |
52
+
53
+ `AlertStack(props)` takes a `class`, added to this stack.
54
+
55
+ ## Events
56
+
57
+ | Event | `detail` | Effect |
58
+ | -------------- | ------------------- | --------------------------------------------------------- |
59
+ | `alert.add` | `{ id, Component }` | Shows the alert. `id` is optional. |
60
+ | `alert.remove` | `{ id }` | Removes the alert of this id; nothing if it is not shown. |
61
+
62
+ - `Component({ onRemove })` returns the element of the alert, usually an `Alert`. `onRemove` removes
63
+ it: pass it to `Alert` for its Close button.
64
+ - `id` names the alert, to remove it with `alert.remove`. An alert added with the `id` of one
65
+ shown replaces it. Without an `id`, the stack gives one.
66
+
67
+ ## Behavior
68
+
69
+ - **Order:** the alerts are shown in the order they are added, the newest at the bottom.
70
+ - **Limit:** past `limit` alerts, the oldest one leaves. The alerts already leaving are not
71
+ counted.
72
+ - **Removal:** an alert leaves after `deleteAfterDuration`, on a click anywhere on it, on Escape
73
+ while the focus is in it, with its Close button, or on `alert.remove`.
74
+ - **Time to read:** the time stops while the pointer is on the alert or the focus is in it, and
75
+ starts again from the beginning once they leave.
76
+ - **Animations:** an alert grows in when it appears and shrinks out when it leaves. While it
77
+ leaves, it is out of reach of the keyboard and of the screen readers (`inert`). It is removed
78
+ once its animation ends; at once without an animation, like with reduced motion.
79
+ - **Several stacks:** each stack has its own alerts. Every stack on the page shows the alerts
80
+ added with `alert.add`.
81
+ - **Unmounted:** a stack out of the page listens no more, and its timers are cancelled.
82
+
83
+ ## Accessibility
84
+
85
+ - The stack is a polite live region (`aria-live="polite"`): an alert is read when it appears,
86
+ without interrupting. An `Alert` has the role `alert`: it is read at once.
87
+ - The keyboard dismisses an alert with its Close button, or with Escape while the focus is in it.
88
+ - The time stops while the focus is in an alert, so a keyboard user can read it
89
+ ([WCAG 2.2.1, Timing Adjustable](https://www.w3.org/WAI/WCAG22/Understanding/timing-adjustable)).
@@ -2,7 +2,6 @@ export default (context, options = {}) => {
2
2
  const { bau, css, keyframes } = context;
3
3
  const { limit = 10, deleteAfterDuration = 15e3 } = options;
4
4
  const { div } = bau.tags;
5
- const messagesState = bau.state([]);
6
5
 
7
6
  const animation = {
8
7
  inserting: keyframes`
@@ -17,8 +16,9 @@ export default (context, options = {}) => {
17
16
 
18
17
  const styles = {
19
18
  stack: css`
20
- min-width: 300px;
21
- max-width: 90% vw;
19
+ /* Within the screen of a phone too */
20
+ min-width: min(300px, calc(100vw - 2 * var(--global-spacing)));
21
+ max-width: calc(100vw - 2 * var(--global-spacing));
22
22
  position: fixed;
23
23
  right: var(--global-spacing);
24
24
  top: var(--global-spacing);
@@ -33,57 +33,127 @@ export default (context, options = {}) => {
33
33
  cursor: pointer;
34
34
  animation: ${animation.inserting} var(--transition-slow) ease-out;
35
35
  `,
36
- itemOut: css`
37
- animation: ${animation.removing} var(--transition-slow) ease-out;
36
+ leaving: css`
37
+ animation: ${animation.removing} var(--transition-slow) ease-out forwards;
38
38
  `,
39
39
  };
40
40
 
41
- const setStatus = ({ id, status }) => {
42
- const idx = messagesState.val.findIndex((message) => message.id === id);
43
- if (idx != -1) {
44
- messagesState.val[idx].status = status;
45
- }
46
- };
47
-
48
41
  return function AlertStack(props = {}) {
49
- const remove = ({ id }) => {
50
- setStatus({ id, status: "removing" });
42
+ // The alerts of this stack
43
+ const messagesState = bau.state([]);
44
+ // What the stack keeps of each alert, by id, from its add to its drop:
45
+ // whether it is leaving, the timer that removes it, its element once drawn
46
+ const alertMap = new Map();
47
+
48
+ // In the stack, not leaving: not for an id unknown or dropped
49
+ const isShown = ({ id }) => alertMap.get(id)?.leavingState.val === false;
50
+
51
+ const stopTimer = (id) => clearTimeout(alertMap.get(id)?.timer);
52
+ // Again from the start, unless it is leaving
53
+ const startTimer = (id) => {
54
+ if (!isShown({ id })) return;
55
+ const alert = alertMap.get(id);
56
+ clearTimeout(alert.timer);
57
+ alert.timer = setTimeout(() => remove({ id }), deleteAfterDuration);
58
+ };
59
+
60
+ // Out of the list, at once
61
+ const drop = (id) => {
62
+ stopTimer(id);
63
+ alertMap.delete(id);
51
64
  const idx = messagesState.val.findIndex((message) => message.id === id);
52
65
  if (idx != -1) {
53
66
  messagesState.val.splice(idx, 1);
54
67
  }
55
68
  };
56
69
 
57
- const add = ({ Component }) => {
70
+ // The alert leaves with an animation, out of reach of the keyboard and of
71
+ // the readers meanwhile; out of the list once it ends. Not drawn yet,
72
+ // without an animation like with reduced motion, or if it never ends: at
73
+ // once, or after a while.
74
+ const remove = ({ id }) => {
75
+ if (!isShown({ id })) return;
76
+ const alert = alertMap.get(id);
77
+ const { leavingState, element } = alert;
78
+ if (!element) return drop(id);
79
+ stopTimer(id);
80
+ leavingState.val = true;
81
+ // The same alert: not a newer one with the same id
82
+ const finish = () => alertMap.get(id) === alert && drop(id);
83
+ element.addEventListener("animationend", finish, { once: true });
84
+ setTimeout(finish, 1e3);
85
+ requestAnimationFrame(
86
+ () => element.getAnimations().length == 0 && finish()
87
+ );
88
+ };
89
+
90
+ // The id given, to remove it with alert.remove, else its own
91
+ const add = ({ id, Component }) => {
58
92
  const message = {
59
- id: Math.random().toString(10).split(".")[1],
93
+ id: id ?? Math.random().toString(10).split(".")[1],
60
94
  Component,
61
- status: "inserting",
62
95
  };
96
+ // Replaced by the alert of the same id
97
+ drop(message.id);
63
98
 
64
- if (messagesState.val.length >= limit) {
65
- remove({ id: messagesState.val[0].id });
99
+ const shown = messagesState.val.filter(isShown);
100
+ if (shown.length >= limit) {
101
+ remove({ id: shown[0].id });
66
102
  }
67
103
 
104
+ alertMap.set(message.id, { leavingState: bau.state(false) });
68
105
  messagesState.val.push(message);
69
-
70
- setTimeout(() => remove(message), deleteAfterDuration);
106
+ startTimer(message.id);
71
107
  };
72
108
 
73
- const AlertItem = (message) =>
74
- div(
109
+ // Read at its pace: the timer waits while the pointer is on the alert or
110
+ // the focus in it. Its Component gets onRemove, for a Close button.
111
+ const AlertItem = (message) => {
112
+ const { id } = message;
113
+ const alert = alertMap.get(id);
114
+ const { leavingState } = alert;
115
+ const onRemove = () => remove({ id });
116
+ return div(
75
117
  {
76
- class: styles.item,
77
- onclick: () => remove(message),
118
+ class: () => [styles.item, leavingState.val && styles.leaving],
119
+ inert: () => leavingState.val,
120
+ onclick: onRemove,
121
+ onkeydown: (event) => {
122
+ event.key == "Escape" && onRemove();
123
+ },
124
+ onmouseenter: () => stopTimer(id),
125
+ onmouseleave: () => startTimer(id),
126
+ // Unlike focus and blur, focusin and focusout bubble up from inside
127
+ // addEventListener: the DOM has no onfocusin property
128
+ // No removeEventListener: collected with the element, once dropped
129
+ bauCreated: ({ element }) => {
130
+ alert.element = element;
131
+ element.addEventListener("focusin", () => stopTimer(id));
132
+ element.addEventListener("focusout", () => startTimer(id));
133
+ },
78
134
  },
79
- message.Component()
135
+ message.Component({ onRemove })
80
136
  );
137
+ };
81
138
 
82
- document.addEventListener("alert.add", (event) => add(event.detail));
83
- document.addEventListener("alert.remove", (event) => remove(event.detail));
139
+ const onAdd = (event) => add(event.detail);
140
+ const onRemove = (event) => remove(event.detail);
84
141
 
85
142
  return div(
86
143
  {
144
+ // The alerts are read when they appear; Alert, role alert, at once
145
+ "aria-live": "polite",
146
+ // Listening from its creation, until it leaves the page
147
+ bauCreated: () => {
148
+ document.addEventListener("alert.add", onAdd);
149
+ document.addEventListener("alert.remove", onRemove);
150
+ },
151
+ bauUnmounted: () => {
152
+ document.removeEventListener("alert.add", onAdd);
153
+ document.removeEventListener("alert.remove", onRemove);
154
+ alertMap.forEach(({ timer }) => clearTimeout(timer));
155
+ alertMap.clear();
156
+ },
87
157
  class: [styles.stack, options?.class, props.class],
88
158
  },
89
159
  bau.loop(messagesState, div(), AlertItem)
@@ -19,6 +19,8 @@ export default function (context, options = {}) {
19
19
  const nodeCloned = childNode.cloneNode(true);
20
20
  window.requestAnimationFrame(() => {
21
21
  nodeCloned.setAttribute("cloned", true);
22
+ // A picture leaving: not reached by the keyboard or the readers
23
+ nodeCloned.inert = true;
22
24
  nodeCloned.style.top = 0;
23
25
  nodeCloned.style.left = 0;
24
26
  nodeCloned.style.width = childNode.getAttribute("width");