@inglorious/ui 8.5.0 → 9.0.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 (40) hide show
  1. package/package.json +1 -1
  2. package/src/controls/button/button.stories.js +22 -0
  3. package/src/controls/button/template.js +39 -6
  4. package/src/controls/combobox/style.css +14 -7
  5. package/src/controls/combobox/template.js +12 -2
  6. package/src/controls/file-upload/style.css +4 -0
  7. package/src/controls/rating/style.css +26 -4
  8. package/src/controls/rating/template.js +29 -12
  9. package/src/controls/switch/template.js +3 -1
  10. package/src/data-display/tooltip/style.css +22 -14
  11. package/src/examples/dashboard/primitive-content.js +6 -6
  12. package/src/examples/dashboard/style.css +11 -5
  13. package/src/feedback/dialog/dialog.stories.js +9 -0
  14. package/src/feedback/dialog/style.css +13 -12
  15. package/src/feedback/dialog/template.js +63 -18
  16. package/src/feedback/progress/style.css +47 -23
  17. package/src/feedback/progress/template.js +18 -11
  18. package/src/feedback/snackbar/style.css +13 -0
  19. package/src/feedback/snackbar/template.js +7 -0
  20. package/src/navigation/drawer/drawer.stories.js +3 -7
  21. package/src/navigation/drawer/style.css +59 -18
  22. package/src/navigation/drawer/template.js +137 -80
  23. package/src/navigation/menu/menu.stories.js +15 -0
  24. package/src/navigation/menu/style.css +19 -0
  25. package/src/navigation/menu/template.js +59 -6
  26. package/src/navigation/speed-dial/style.css +32 -5
  27. package/src/navigation/speed-dial/template.js +41 -12
  28. package/src/navigation/tabs/template.js +52 -4
  29. package/src/shared/anchor.js +11 -0
  30. package/src/shared/top-layer.js +83 -0
  31. package/src/surfaces/accordion/style.css +14 -7
  32. package/src/surfaces/accordion/template.js +27 -20
  33. package/types/controls/button.d.ts +7 -1
  34. package/types/controls/rating.d.ts +4 -0
  35. package/types/feedback/dialog.d.ts +3 -1
  36. package/types/feedback/progress.d.ts +2 -0
  37. package/types/navigation/drawer.d.ts +8 -3
  38. package/types/navigation/menu.d.ts +17 -0
  39. package/types/navigation/speed-dial.d.ts +5 -0
  40. package/types/surfaces/accordion.d.ts +2 -0
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The CSS anchor name that ties a popover to the element it opens from.
3
+ * Set as `anchor-name` on the trigger and `position-anchor` on the popover,
4
+ * so the browser positions the popover against it, flipping when it runs out
5
+ * of room. Built from the popover's id, which is unique on the page.
6
+ * @param {string} id
7
+ * @returns {string}
8
+ */
9
+ export function anchorName(id) {
10
+ return `--iw-anchor-${String(id).replace(/[^\w-]/g, "-")}`
11
+ }
@@ -0,0 +1,83 @@
1
+ /**
2
+ * Native `<dialog>` and `popover` elements keep their own open state, while the
3
+ * components that use them are driven by an `isOpen` prop from the store. These
4
+ * helpers run from a `ref` on every render and keep the two in step: the prop
5
+ * opens and closes the element, and a change the browser makes on its own
6
+ * (Esc, a light dismiss, a `method="dialog"` form, a `commandfor` invoker) is
7
+ * reported through `onClose` or `onOpen` so the store can catch up.
8
+ *
9
+ * The browser fires `close` and `toggle` after the fact, so the handler tells
10
+ * the two apart by the latest props: a change the props asked for finds
11
+ * `isOpen` already matching, and only one they did not ask for is reported.
12
+ */
13
+
14
+ /**
15
+ * Latest props per element. The listener is added once, and reads the props
16
+ * from here so it never calls an `onClose` captured by an earlier render.
17
+ * @type {WeakMap<Element, { isOpen: boolean, isModal?: boolean, onOpen?: () => void, onClose?: () => void }>}
18
+ */
19
+ const latestProps = new WeakMap()
20
+
21
+ /**
22
+ * Opens or closes a `<dialog>` to match `isOpen`.
23
+ * @param {HTMLDialogElement | undefined} element
24
+ * @param {{ isOpen: boolean, isModal?: boolean, onClose?: () => void }} props
25
+ */
26
+ export function syncDialog(element, props) {
27
+ if (!element) return
28
+ track(element, props, "close", () => false)
29
+ whenConnected(element, () => {
30
+ const { isOpen, isModal = true } = latestProps.get(element)
31
+ if (isOpen && !element.open) {
32
+ if (isModal) element.showModal()
33
+ else element.show()
34
+ }
35
+ if (!isOpen && element.open) element.close()
36
+ })
37
+ }
38
+
39
+ /**
40
+ * Shows or hides a `popover` element to match `isOpen`. Showing an open
41
+ * popover and hiding a closed one are no-ops, so no state check is needed.
42
+ * @param {HTMLElement | undefined} element
43
+ * @param {{ isOpen: boolean, onOpen?: () => void, onClose?: () => void }} props
44
+ */
45
+ export function syncPopover(element, props) {
46
+ if (!element) return
47
+ track(element, props, "toggle", (event) => event.newState === "open")
48
+ whenConnected(element, () => {
49
+ if (!element.popover) return
50
+ if (latestProps.get(element).isOpen) element.showPopover()
51
+ else element.hidePopover()
52
+ })
53
+ }
54
+
55
+ /**
56
+ * @param {Element} element
57
+ * @param {object} props
58
+ * @param {string} eventName
59
+ * @param {(event: Event) => boolean} isOpenAfter whether the event left it open
60
+ */
61
+ function track(element, props, eventName, isOpenAfter) {
62
+ const isTracked = latestProps.has(element)
63
+ latestProps.set(element, props)
64
+ if (isTracked) return
65
+
66
+ element.addEventListener(eventName, (event) => {
67
+ const latest = latestProps.get(element)
68
+ const isOpen = isOpenAfter(event)
69
+ if (isOpen === latest.isOpen) return
70
+ if (isOpen) latest.onOpen?.()
71
+ else latest.onClose?.()
72
+ })
73
+ }
74
+
75
+ /**
76
+ * A ref runs while a new template is still in its fragment, and neither
77
+ * `showModal()` nor `showPopover()` works on an element outside the document.
78
+ * The fragment is inserted synchronously after, so a microtask is late enough.
79
+ */
80
+ function whenConnected(element, callback) {
81
+ if (element.isConnected) callback()
82
+ else queueMicrotask(() => element.isConnected && callback())
83
+ }
@@ -11,20 +11,27 @@
11
11
  border-top: 1px solid var(--iw-color-border);
12
12
  }
13
13
 
14
+ /* A <summary>: flex drops the browser's disclosure marker, and the caret
15
+ stands in for it. */
14
16
  .iw-accordion-trigger {
15
- width: 100%;
16
- border: 0 none;
17
- background: transparent;
18
- color: inherit;
19
17
  padding: var(--iw-space-3) var(--iw-space-4);
20
18
  display: flex;
21
19
  align-items: center;
22
20
  gap: var(--iw-space-2);
23
- text-align: left;
21
+ text-align: start;
24
22
  cursor: pointer;
23
+
24
+ &::-webkit-details-marker {
25
+ display: none;
26
+ }
27
+ }
28
+
29
+ .iw-accordion-item-disabled .iw-accordion-trigger {
30
+ cursor: default;
25
31
  }
26
32
 
27
- .iw-accordion-trigger:hover:enabled {
33
+ .iw-accordion-item:not(.iw-accordion-item-disabled)
34
+ .iw-accordion-trigger:hover {
28
35
  background: color-mix(
29
36
  in srgb,
30
37
  var(--iw-color-surface-hover) 75%,
@@ -50,7 +57,7 @@
50
57
  color: var(--iw-color-text-muted, var(--iw-color-text));
51
58
  }
52
59
 
53
- .iw-accordion-item-expanded .iw-accordion-caret {
60
+ .iw-accordion-item[open] .iw-accordion-caret {
54
61
  transform: rotate(180deg);
55
62
  }
56
63
 
@@ -4,7 +4,7 @@
4
4
  * @typedef {import('@inglorious/web').TemplateResult} TemplateResult
5
5
  */
6
6
 
7
- import { html } from "@inglorious/web"
7
+ import { html, nothing } from "@inglorious/web"
8
8
  import { classMap } from "@inglorious/web/directives/class-map"
9
9
  import { ref } from "@inglorious/web/directives/ref"
10
10
 
@@ -30,8 +30,10 @@ export const Accordion = {
30
30
  renderAccordion(props) {
31
31
  const {
32
32
  type, // eslint-disable-line no-unused-vars
33
+ name, // eslint-disable-line no-unused-vars
33
34
  items = [],
34
35
  className = "",
36
+ onItemToggle, // eslint-disable-line no-unused-vars
35
37
  ...rest
36
38
  } = props
37
39
 
@@ -54,8 +56,11 @@ export const Accordion = {
54
56
  },
55
57
 
56
58
  /**
57
- * Renders a single accordion item with trigger and panel content.
58
- * Handles expand/collapse behavior and item-level callbacks.
59
+ * Renders a single accordion item as a native `<details>`, whose `<summary>`
60
+ * is the trigger. The browser opens and closes it, keeps the panel's
61
+ * content findable (find-in-page opens the item that holds a match), and,
62
+ * when the accordion has a `name`, closes the other items as one opens.
63
+ * Each change is reported so the store can follow.
59
64
  * @param {AccordionItem} item
60
65
  * @param {number} index
61
66
  * @param {AccordionProps} props
@@ -71,33 +76,35 @@ export const Accordion = {
71
76
  icon,
72
77
  } = item
73
78
 
74
- return html`<section
79
+ return html`<details
75
80
  class=${classMap({
76
81
  "iw-accordion-item": true,
77
82
  "iw-accordion-item-expanded": isExpanded,
78
83
  "iw-accordion-item-disabled": isDisabled,
79
84
  })}
85
+ name=${props.name || nothing}
86
+ .open=${isExpanded}
87
+ @toggle=${(event) => {
88
+ const isOpen = event.newState === "open"
89
+ // Also fires when isExpanded opened it, which needs no report.
90
+ if (isOpen === isExpanded) return
91
+ onToggle?.(item, index, isOpen)
92
+ props.onItemToggle?.(item, index, isOpen)
93
+ }}
80
94
  >
81
- <button
95
+ <summary
82
96
  class="iw-accordion-trigger"
83
- type="button"
84
- aria-expanded=${isExpanded}
85
- ?disabled=${isDisabled}
86
- @click=${() => {
87
- if (isDisabled) return
88
- onToggle?.(item, index, !isExpanded)
89
- props.onItemToggle?.(item, index, !isExpanded)
97
+ aria-disabled=${isDisabled ? "true" : nothing}
98
+ @click=${(event) => {
99
+ // A summary cannot be disabled, so its toggle is refused instead.
100
+ if (isDisabled) event.preventDefault()
90
101
  }}
91
102
  >
92
103
  ${icon ? html`<span class="iw-accordion-icon">${icon}</span>` : null}
93
104
  <span class="iw-accordion-title">${title}</span>
94
- <span class="iw-accordion-caret">▾</span>
95
- </button>
96
- ${
97
- isExpanded
98
- ? html`<div class="iw-accordion-panel">${content}</div>`
99
- : null
100
- }
101
- </section>`
105
+ <span class="iw-accordion-caret" aria-hidden="true">▾</span>
106
+ </summary>
107
+ <div class="iw-accordion-panel">${content}</div>
108
+ </details>`
102
109
  },
103
110
  }
@@ -22,7 +22,13 @@ export interface ButtonProps {
22
22
  ariaLabel?: string
23
23
  isAriaPressed?: boolean
24
24
  className?: string
25
- onClick?: () => void
25
+ /** File types the picker offers, as in the native `accept` attribute */
26
+ accept?: string
27
+ /** Lets the picker select more than one file */
28
+ isMultiple?: boolean
29
+ onClick?: (event: MouseEvent) => void
30
+ /** Turns the button into a file picker; called with the chosen files */
31
+ onFileSelect?: (files: FileList) => void
26
32
  [key: string]: unknown
27
33
  }
28
34
 
@@ -2,6 +2,8 @@ import type { TemplateResult } from "@inglorious/web"
2
2
 
3
3
  export interface RatingProps {
4
4
  id?: string
5
+ /** Groups the radios and names the value in a form. Falls back to `id`. */
6
+ name?: string
5
7
  value?: number
6
8
  max?: number
7
9
  isDisabled?: boolean
@@ -10,6 +12,8 @@ export interface RatingProps {
10
12
  emptySymbol?: string
11
13
  onChange?: (value: number) => void
12
14
  size?: "sm" | "md" | "lg"
15
+ /** Names the group for assistive tech. Defaults to "Rating". */
16
+ ariaLabel?: string
13
17
  }
14
18
 
15
19
  export type RatingType = {
@@ -4,6 +4,8 @@ export interface DialogProps {
4
4
  id?: string
5
5
  type?: string
6
6
  isOpen?: boolean
7
+ /** Modal by default; `false` shows the dialog in place, with the page still usable. */
8
+ isModal?: boolean
7
9
  title?: TemplateResult | string | number
8
10
  description?: TemplateResult | string | number
9
11
  children?: TemplateResult | string | number
@@ -15,7 +17,7 @@ export interface DialogProps {
15
17
  }
16
18
 
17
19
  export type DialogType = {
18
- render(props: DialogProps): TemplateResult | null
20
+ render(props: DialogProps): TemplateResult
19
21
  }
20
22
 
21
23
  export declare const Dialog: DialogType
@@ -15,6 +15,8 @@ export interface ProgressProps {
15
15
  | "info"
16
16
  size?: number
17
17
  thickness?: number
18
+ /** Names the indicator for assistive tech, e.g. "Uploading photo". */
19
+ ariaLabel?: string
18
20
  className?: string
19
21
  [key: string]: unknown
20
22
  }
@@ -5,19 +5,24 @@ export interface DrawerProps {
5
5
  type?: string
6
6
  isOpen?: boolean
7
7
  anchor?: "left" | "right" | "top" | "bottom"
8
- variant?: "temporary" | "persistent" | "permanent" | "responsive"
9
- breakpoint?: "sm" | "md" | "lg" | "xl"
8
+ /**
9
+ * When the drawer overlays the page as a popover: `responsive` (default)
10
+ * below 64rem and docked above, `temporary` at every width, `permanent` never.
11
+ */
12
+ variant?: "responsive" | "temporary" | "permanent"
13
+ isHidden?: boolean
10
14
  isCollapsed?: boolean
11
15
  title?: TemplateResult | string | number
12
16
  children?: TemplateResult | string | number
13
17
  hasBackdrop?: boolean
14
18
  className?: string
15
19
  onClose?: () => void
20
+ onCollapseToggle?: () => void
16
21
  [key: string]: unknown
17
22
  }
18
23
 
19
24
  export type DrawerType = {
20
- render(props: DrawerProps): TemplateResult | null
25
+ render(props: DrawerProps): TemplateResult
21
26
  }
22
27
 
23
28
  export declare const Drawer: DrawerType
@@ -1,5 +1,7 @@
1
1
  import type { TemplateResult } from "@inglorious/web"
2
2
 
3
+ import type { ButtonProps } from "../controls/button"
4
+
3
5
  export interface MenuItem {
4
6
  id?: string | number
5
7
  value?: string | number
@@ -14,9 +16,23 @@ export interface MenuItem {
14
16
  }
15
17
 
16
18
  export interface MenuProps {
19
+ /** Required with `trigger`: it links the button to the menu. */
17
20
  id?: string
18
21
  type?: string
19
22
  isOpen?: boolean
23
+ /**
24
+ * Content of a button that opens the menu. With it, the menu is a popover
25
+ * anchored to the button; without it, the menu renders in place while open.
26
+ */
27
+ trigger?: TemplateResult | string | number
28
+ /** Extra props for the trigger button, such as `variant` or `ariaLabel`. */
29
+ triggerProps?: ButtonProps
30
+ /**
31
+ * Called with the new state when the browser opens or closes an anchored
32
+ * menu. Without it, the browser alone keeps the open state and `isOpen` is
33
+ * not applied.
34
+ */
35
+ onToggle?: (isOpen: boolean) => void
20
36
  items?: MenuItem[]
21
37
  isDense?: boolean
22
38
  className?: string
@@ -26,6 +42,7 @@ export interface MenuProps {
26
42
 
27
43
  export type MenuType = {
28
44
  render(props: MenuProps): TemplateResult | null
45
+ renderTrigger(props: MenuProps): TemplateResult
29
46
  renderMenu(props: MenuProps): TemplateResult | null
30
47
  renderItem(item: MenuItem, index: number, props: MenuProps): TemplateResult
31
48
  }
@@ -18,6 +18,11 @@ export interface SpeedDialProps {
18
18
  ariaLabel?: string
19
19
  direction?: "up" | "down" | "left" | "right"
20
20
  actions?: SpeedDialAction[]
21
+ /**
22
+ * Called with the new state when the browser opens or closes the actions.
23
+ * Without it, the browser alone keeps the open state and `isOpen` is not
24
+ * applied.
25
+ */
21
26
  onToggle?: (isOpen: boolean) => void
22
27
  onAction?: (value: string | number, action: SpeedDialAction) => void
23
28
  [key: string]: unknown
@@ -15,6 +15,8 @@ export interface AccordionProps {
15
15
  id?: string
16
16
  type?: string
17
17
  className?: string
18
+ /** Items sharing a name close each other: at most one is open at a time. */
19
+ name?: string
18
20
  items?: AccordionItem[]
19
21
  onItemToggle?: (item: AccordionItem, index: number, expanded: boolean) => void
20
22
  [key: string]: unknown