@prunellejs/ui 0.0.0-stage → 0.7.2

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 (84) hide show
  1. package/LICENSE +21 -0
  2. package/llms.txt +78 -0
  3. package/package.json +32 -4
  4. package/src/CommandPalette.tsx +262 -0
  5. package/src/DataTable.tsx +244 -0
  6. package/src/Dialog.tsx +181 -0
  7. package/src/Drawer.tsx +4 -0
  8. package/src/Menu.tsx +117 -0
  9. package/src/Select.tsx +125 -0
  10. package/src/Tabs.tsx +64 -0
  11. package/src/Toaster.tsx +105 -0
  12. package/src/behaviors.ts +385 -0
  13. package/src/field-context.ts +21 -0
  14. package/src/fold.ts +7 -0
  15. package/src/index.ts +72 -0
  16. package/src/labels/dialog.ts +3 -0
  17. package/src/labels/fr/dialog.ts +3 -0
  18. package/src/labels/fr/palette.ts +8 -0
  19. package/src/labels/fr/select.ts +1 -0
  20. package/src/labels/fr/table.ts +13 -0
  21. package/src/labels/fr/toaster.ts +1 -0
  22. package/src/labels/palette.ts +8 -0
  23. package/src/labels/select.ts +1 -0
  24. package/src/labels/table.ts +13 -0
  25. package/src/labels/toaster.ts +1 -0
  26. package/src/platform-fallback.ts +36 -0
  27. package/src/plugin.ts +188 -0
  28. package/src/primitives.tsx +238 -0
  29. package/src/static.tsx +324 -0
  30. package/src/styles/bare.css +25 -0
  31. package/src/styles/base.css +61 -0
  32. package/src/styles/components/accordion.css +10 -0
  33. package/src/styles/components/badge.css +6 -0
  34. package/src/styles/components/button.css +11 -0
  35. package/src/styles/components/card.css +5 -0
  36. package/src/styles/components/checkbox.css +11 -0
  37. package/src/styles/components/command.css +25 -0
  38. package/src/styles/components/consent.css +38 -0
  39. package/src/styles/components/datatable.css +9 -0
  40. package/src/styles/components/dialog.css +16 -0
  41. package/src/styles/components/drawer.css +29 -0
  42. package/src/styles/components/field.css +8 -0
  43. package/src/styles/components/layout.css +69 -0
  44. package/src/styles/components/menu.css +21 -0
  45. package/src/styles/components/notice.css +5 -0
  46. package/src/styles/components/popover.css +8 -0
  47. package/src/styles/components/progress.css +14 -0
  48. package/src/styles/components/radio-group.css +11 -0
  49. package/src/styles/components/select.css +25 -0
  50. package/src/styles/components/switch.css +42 -0
  51. package/src/styles/components/tabs.css +4 -0
  52. package/src/styles/components/toast.css +28 -0
  53. package/src/styles/grammars/console.css +179 -0
  54. package/src/styles/grammars/editorial.css +164 -0
  55. package/src/styles/grammars/orbit.css +269 -0
  56. package/src/styles/grammars/prunelle.css +504 -0
  57. package/src/styles/grammars/soft.css +160 -0
  58. package/src/styles/grammars/workshop.css +172 -0
  59. package/src/styles/scan.ts +144 -0
  60. package/src/styles/split.ts +429 -0
  61. package/src/styles/theme.css +22 -0
  62. package/src/styles/themed/accordion.css +20 -0
  63. package/src/styles/themed/badge.css +20 -0
  64. package/src/styles/themed/button.css +24 -0
  65. package/src/styles/themed/card.css +23 -0
  66. package/src/styles/themed/checkbox.css +4 -0
  67. package/src/styles/themed/command.css +33 -0
  68. package/src/styles/themed/consent.css +33 -0
  69. package/src/styles/themed/datatable.css +28 -0
  70. package/src/styles/themed/dialog.css +13 -0
  71. package/src/styles/themed/drawer.css +13 -0
  72. package/src/styles/themed/field.css +22 -0
  73. package/src/styles/themed/menu.css +21 -0
  74. package/src/styles/themed/notice.css +17 -0
  75. package/src/styles/themed/popover.css +11 -0
  76. package/src/styles/themed/radio-group.css +12 -0
  77. package/src/styles/themed/select.css +32 -0
  78. package/src/styles/themed/switch.css +23 -0
  79. package/src/styles/themed/tabs.css +20 -0
  80. package/src/styles/themed/toast.css +18 -0
  81. package/src/table-state.ts +163 -0
  82. package/src/tabs-state.ts +39 -0
  83. package/src/toast.ts +110 -0
  84. package/README.md +0 -3
package/src/Dialog.tsx ADDED
@@ -0,0 +1,181 @@
1
+ /** @jsxImportSource prunelle */
2
+ // Native `<dialog>` primitives shared by Dialog and Drawer: top layer, Esc and backdrop close, ::backdrop and focus return come from the browser.
3
+ // `Dialog` and `Drawer` open from their trigger's `command`, with no JS; `ControlledDialog` and `ControlledDrawer` also follow an `open` signal, as an island.
4
+
5
+ import type { PrunelleChild, Signal } from 'prunelle'
6
+ import type { ButtonVariant } from './static'
7
+ import { effect } from 'prunelle'
8
+ import { nextUiId, syncDialogOpen } from './behaviors'
9
+ import * as DIALOG_LABELS from './labels/dialog'
10
+
11
+ export interface DialogProps {
12
+ id?: string | undefined
13
+ title?: string | undefined
14
+ ariaLabel?: string | undefined
15
+ description?: string | undefined
16
+ triggerLabel?: string | undefined
17
+ triggerVariant?: ButtonVariant | undefined
18
+ closeLabel?: string | undefined
19
+ /** A Dialog ships no JS: to open or close it from code, use `ControlledDialog` (or `ControlledDrawer`) with this signal. */
20
+ open?: undefined
21
+ children?: PrunelleChild | undefined
22
+ }
23
+
24
+ export interface DrawerProps extends DialogProps {
25
+ side?: 'right' | 'left' | 'bottom' | undefined
26
+ }
27
+
28
+ export interface ControlledDialogProps extends Omit<DialogProps, 'open'> {
29
+ /** Opens and closes the dialog; the browser closing it (Esc, backdrop, close button) sets it to `false`. */
30
+ open: Signal<boolean>
31
+ }
32
+
33
+ export interface ControlledDrawerProps extends ControlledDialogProps {
34
+ side?: 'right' | 'left' | 'bottom' | undefined
35
+ }
36
+
37
+ type Kind = 'dialog' | 'drawer'
38
+ type Shared = Omit<DrawerProps, 'open'>
39
+
40
+ interface DialogIds {
41
+ baseId: string
42
+ titleId: string
43
+ descId: string
44
+ }
45
+
46
+ function dialogIds(props: Shared, kind: Kind): DialogIds {
47
+ const baseId = props.id ?? nextUiId(`kui-${kind}`)
48
+ return { baseId, titleId: `${baseId}-title`, descId: `${baseId}-desc` }
49
+ }
50
+
51
+ function dialogAttrs(props: Shared, kind: Kind, ids: DialogIds) {
52
+ return {
53
+ 'id': ids.baseId,
54
+ 'closedby': 'any' as const,
55
+ 'data-ui': kind,
56
+ 'data-side': kind === 'drawer' ? (props.side ?? 'right') : undefined,
57
+ 'data-part': 'content',
58
+ 'aria-labelledby': props.title ? ids.titleId : undefined,
59
+ 'aria-label': !props.title ? (props.ariaLabel ?? (kind === 'drawer' ? DIALOG_LABELS.drawer : DIALOG_LABELS.dialog)) : undefined,
60
+ 'aria-describedby': props.description ? ids.descId : undefined,
61
+ }
62
+ }
63
+
64
+ function closeButtonAttrs(dialog: Shared, ids: DialogIds) {
65
+ return {
66
+ 'data-ui': 'button',
67
+ 'data-variant': 'ghost',
68
+ 'data-size': 'sm',
69
+ 'data-part': 'close',
70
+ 'type': 'button' as const,
71
+ 'commandfor': ids.baseId,
72
+ 'command': 'close' as const,
73
+ 'aria-label': dialog.closeLabel ?? DIALOG_LABELS.close,
74
+ }
75
+ }
76
+
77
+ // Takes `props` without spreading so lazy `get children()` getters evaluate strictly inside `<div data-part="body">`.
78
+ function DialogPanel(props: { dialog: Shared, ids: DialogIds }) {
79
+ const { dialog, ids } = props
80
+ return (
81
+ <div data-part="panel">
82
+ <header data-part="header" data-layout="split">
83
+ {dialog.title && <h2 id={ids.titleId} data-part="title">{dialog.title}</h2>}
84
+ <button {...closeButtonAttrs(dialog, ids)}>✕</button>
85
+ </header>
86
+ {dialog.description && <p id={ids.descId} data-part="description">{dialog.description}</p>}
87
+ <div data-part="body">{dialog.children}</div>
88
+ </div>
89
+ )
90
+ }
91
+
92
+ function ControlledDialogPanel({ dialog, ids, onClose }: { dialog: Shared, ids: DialogIds, onClose: () => void }) {
93
+ return (
94
+ <div data-part="panel">
95
+ <header data-part="header" data-layout="split">
96
+ {dialog.title && <h2 id={ids.titleId} data-part="title">{dialog.title}</h2>}
97
+ <button {...closeButtonAttrs(dialog, ids)} onClick={onClose}>✕</button>
98
+ </header>
99
+ {dialog.description && <p id={ids.descId} data-part="description">{dialog.description}</p>}
100
+ <div data-part="body">{dialog.children}</div>
101
+ </div>
102
+ )
103
+ }
104
+
105
+ function renderStaticDialog(props: DrawerProps, kind: Kind) {
106
+ const ids = dialogIds(props, kind)
107
+ return (
108
+ <div data-ui={`${kind}-root`}>
109
+ {props.triggerLabel && (
110
+ <button
111
+ data-ui="button"
112
+ data-variant={props.triggerVariant ?? 'outline'}
113
+ data-part="trigger"
114
+ type="button"
115
+ aria-haspopup="dialog"
116
+ commandfor={ids.baseId}
117
+ command="show-modal"
118
+ >
119
+ {props.triggerLabel}
120
+ </button>
121
+ )}
122
+ <dialog {...dialogAttrs(props, kind, ids)}>
123
+ <DialogPanel dialog={props} ids={ids} />
124
+ </dialog>
125
+ </div>
126
+ )
127
+ }
128
+
129
+ function renderControlledDialog(props: ControlledDrawerProps, kind: Kind) {
130
+ const ids = dialogIds(props, kind)
131
+ const isOpen = props.open
132
+ let dialogEl: HTMLDialogElement | undefined
133
+
134
+ effect(() => {
135
+ syncDialogOpen(dialogEl, isOpen, isOpen.value)
136
+ })
137
+
138
+ return (
139
+ <div data-ui={`${kind}-root`} data-state={isOpen.value ? 'open' : 'closed'}>
140
+ {props.triggerLabel && (
141
+ <button
142
+ data-ui="button"
143
+ data-variant={props.triggerVariant ?? 'outline'}
144
+ data-part="trigger"
145
+ type="button"
146
+ aria-haspopup="dialog"
147
+ aria-expanded={isOpen.value ? 'true' : 'false'}
148
+ onClick={() => { isOpen.value = true }}
149
+ >
150
+ {props.triggerLabel}
151
+ </button>
152
+ )}
153
+ <dialog
154
+ {...dialogAttrs(props, kind, ids)}
155
+ ref={(el) => {
156
+ dialogEl = el
157
+ syncDialogOpen(dialogEl, isOpen, isOpen.peek())
158
+ }}
159
+ onClose={() => { isOpen.value = false }}
160
+ >
161
+ <ControlledDialogPanel dialog={props} ids={ids} onClose={() => { isOpen.value = false }} />
162
+ </dialog>
163
+ </div>
164
+ )
165
+ }
166
+
167
+ export function Dialog(props: DialogProps) {
168
+ return renderStaticDialog(props, 'dialog')
169
+ }
170
+
171
+ export function Drawer(props: DrawerProps) {
172
+ return renderStaticDialog(props, 'drawer')
173
+ }
174
+
175
+ export function ControlledDialog(props: ControlledDialogProps) {
176
+ return renderControlledDialog(props, 'dialog')
177
+ }
178
+
179
+ export function ControlledDrawer(props: ControlledDrawerProps) {
180
+ return renderControlledDialog(props, 'drawer')
181
+ }
package/src/Drawer.tsx ADDED
@@ -0,0 +1,4 @@
1
+ // Accessible native `<dialog>` side/bottom Drawer primitive (re-exported from shared Dialog/ModalDialogSurface module).
2
+
3
+ export { ControlledDrawer, Drawer } from './Dialog'
4
+ export type { ControlledDrawerProps, DrawerProps } from './Dialog'
package/src/Menu.tsx ADDED
@@ -0,0 +1,117 @@
1
+ /** @jsxImportSource prunelle */
2
+ // Accessible dropdown Menu primitive with roving menuitem focus, link items, ArrowDown/Up trigger open, Escape/Tab close & click-outside dismiss.
3
+
4
+ import type { ButtonVariant } from './static'
5
+ import { createListPopup, navigateSafeHref, nextUiId, safeHref } from './behaviors'
6
+
7
+ export interface MenuItemDef {
8
+ id: string
9
+ label: string
10
+ href?: string | undefined
11
+ disabled?: boolean | undefined
12
+ onSelect?: (() => void) | undefined
13
+ }
14
+
15
+ export interface MenuProps {
16
+ id?: string | undefined
17
+ label: string
18
+ items: MenuItemDef[]
19
+ variant?: ButtonVariant | undefined
20
+ onSelect?: ((id: string) => void) | undefined
21
+ }
22
+
23
+ export function Menu(props: MenuProps) {
24
+ const baseId = props.id ?? nextUiId('kui-menu')
25
+ const triggerId = `${baseId}-trigger`
26
+ const menuId = `${baseId}-menu`
27
+ const itemId = (idx: number) => `${baseId}-item-${idx}`
28
+ const variant = props.variant ?? 'outline'
29
+ const label = props.label
30
+
31
+ const activate = (item: MenuItemDef, navigateHref: boolean) => {
32
+ if (item.disabled)
33
+ return
34
+ item.onSelect?.()
35
+ props.onSelect?.(item.id)
36
+ const href = safeHref(item.href)
37
+ popup.close(href === undefined)
38
+ if (navigateHref && href !== undefined)
39
+ navigateSafeHref(href)
40
+ }
41
+
42
+ const popup = createListPopup<MenuItemDef>({
43
+ items: () => props.items,
44
+ itemId,
45
+ isDisabled: it => Boolean(it.disabled),
46
+ onSelect: active => activate(active, true),
47
+ focusActive: true,
48
+ })
49
+
50
+ return (
51
+ <div
52
+ data-ui="menu"
53
+ data-state={popup.open.value ? 'open' : 'closed'}
54
+ ref={(el) => { popup.setRoot(el) }}
55
+ >
56
+ <button
57
+ id={triggerId}
58
+ data-ui="button"
59
+ data-variant={variant}
60
+ data-part="trigger"
61
+ type="button"
62
+ aria-haspopup="menu"
63
+ aria-expanded={popup.open.value ? 'true' : 'false'}
64
+ aria-controls={popup.open.value ? menuId : undefined}
65
+ onClick={e => popup.toggle(e)}
66
+ onKeyDown={e => popup.onTriggerKeyDown(e)}
67
+ >
68
+ {label}
69
+ </button>
70
+ {popup.open.value && (
71
+ <div
72
+ id={menuId}
73
+ data-part="content"
74
+ role="menu"
75
+ aria-labelledby={triggerId}
76
+ popover="auto"
77
+ ref={popup.mountPopup}
78
+ >
79
+ {props.items.map((item, idx) => {
80
+ const href = safeHref(item.href)
81
+ return href !== undefined && !item.disabled
82
+ ? (
83
+ <a
84
+ key={item.id}
85
+ id={itemId(idx)}
86
+ data-part="item"
87
+ data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
88
+ role="menuitem"
89
+ href={href}
90
+ tabindex={-1}
91
+ onClick={() => activate(item, false)}
92
+ >
93
+ {item.label}
94
+ </a>
95
+ )
96
+ : (
97
+ <button
98
+ key={item.id}
99
+ id={itemId(idx)}
100
+ data-part="item"
101
+ data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
102
+ role="menuitem"
103
+ type="button"
104
+ tabindex={-1}
105
+ disabled={item.disabled}
106
+ aria-disabled={item.disabled ? 'true' : undefined}
107
+ onClick={() => activate(item, false)}
108
+ >
109
+ {item.label}
110
+ </button>
111
+ )
112
+ })}
113
+ </div>
114
+ )}
115
+ </div>
116
+ )
117
+ }
package/src/Select.tsx ADDED
@@ -0,0 +1,125 @@
1
+ /** @jsxImportSource prunelle */
2
+ // Accessible custom Select (listbox) primitive with aria-activedescendant navigation + hidden input for native <form> submission.
3
+
4
+ import type { Signal } from 'prunelle'
5
+ import { onMount, signal } from 'prunelle'
6
+ import { createListPopup, nextUiId, useFieldContext } from './behaviors'
7
+ import * as SELECT_LABELS from './labels/select'
8
+
9
+ export interface SelectOption {
10
+ value: string
11
+ label: string
12
+ disabled?: boolean | undefined
13
+ }
14
+
15
+ export interface SelectProps {
16
+ id?: string | undefined
17
+ options: SelectOption[]
18
+ value?: Signal<string> | undefined
19
+ defaultValue?: string | undefined
20
+ name?: string | undefined
21
+ placeholder?: string | undefined
22
+ ariaLabel?: string | undefined
23
+ onChange?: ((value: string) => void) | undefined
24
+ }
25
+
26
+ export function Select(props: SelectProps) {
27
+ const field = useFieldContext()
28
+ const baseId = props.id ?? field?.id ?? nextUiId('kui-select')
29
+ const triggerId = signal(props.id ?? field?.id ?? `${baseId}-trigger`)
30
+ const listboxId = `${baseId}-listbox`
31
+ const optionId = (idx: number) => `${baseId}-opt-${idx}`
32
+ const name = props.name
33
+ const placeholder = props.placeholder ?? SELECT_LABELS.placeholder
34
+ const ariaLabel = props.ariaLabel
35
+
36
+ const selected = props.value ?? signal(props.defaultValue ?? '')
37
+ const currentLabel = () => props.options.find(o => o.value === selected.value)?.label ?? placeholder
38
+
39
+ const pick = (opt: SelectOption) => {
40
+ if (opt.disabled)
41
+ return
42
+ selected.value = opt.value
43
+ props.onChange?.(opt.value)
44
+ popup.close(true)
45
+ }
46
+
47
+ const popup = createListPopup<SelectOption>({
48
+ items: () => props.options,
49
+ itemId: optionId,
50
+ isDisabled: opt => Boolean(opt.disabled),
51
+ onSelect: pick,
52
+ initialIndex: () => props.options.findIndex(o => o.value === selected.value && !o.disabled),
53
+ })
54
+
55
+ let rootEl: HTMLElement | undefined
56
+ const syncFieldLabel = (el: HTMLElement | undefined) => {
57
+ if (!el || props.id || field)
58
+ return
59
+ const labelFor = el.closest('[data-ui="field"]')?.querySelector('label')?.htmlFor
60
+ if (labelFor)
61
+ triggerId.value = labelFor
62
+ }
63
+ onMount(() => { syncFieldLabel(rootEl) })
64
+
65
+ return (
66
+ <div
67
+ data-ui="select"
68
+ data-state={popup.open.value ? 'open' : 'closed'}
69
+ ref={(el) => {
70
+ rootEl = el
71
+ popup.setRoot(el)
72
+ syncFieldLabel(el)
73
+ }}
74
+ >
75
+ {name && <input type="hidden" name={name} value={selected.value} />}
76
+ <button
77
+ id={triggerId.value}
78
+ data-part="trigger"
79
+ type="button"
80
+ role="combobox"
81
+ aria-label={ariaLabel}
82
+ aria-haspopup="listbox"
83
+ aria-expanded={popup.open.value ? 'true' : 'false'}
84
+ aria-controls={popup.open.value ? listboxId : undefined}
85
+ aria-activedescendant={popup.activeDescendantId()}
86
+ onClick={e => popup.toggle(e)}
87
+ onKeyDown={e => popup.onTriggerKeyDown(e)}
88
+ >
89
+ <span data-part="value">{currentLabel()}</span>
90
+ <span aria-hidden="true">▾</span>
91
+ </button>
92
+ {popup.open.value && (
93
+ <div
94
+ id={listboxId}
95
+ data-part="listbox"
96
+ role="listbox"
97
+ aria-label={ariaLabel}
98
+ aria-labelledby={ariaLabel ? undefined : triggerId.value}
99
+ popover="auto"
100
+ ref={popup.mountPopup}
101
+ >
102
+ {props.options.map((opt, idx) => (
103
+ <button
104
+ key={opt.value}
105
+ id={optionId(idx)}
106
+ data-part="option"
107
+ data-selected={selected.value === opt.value ? 'true' : undefined}
108
+ data-highlighted={popup.highlighted.value === idx ? 'true' : undefined}
109
+ role="option"
110
+ type="button"
111
+ tabindex={-1}
112
+ disabled={opt.disabled}
113
+ aria-disabled={opt.disabled ? 'true' : undefined}
114
+ aria-selected={selected.value === opt.value ? 'true' : 'false'}
115
+ onClick={() => pick(opt)}
116
+ >
117
+ <span>{opt.label}</span>
118
+ <span aria-hidden="true">{selected.value === opt.value ? '✓' : ''}</span>
119
+ </button>
120
+ ))}
121
+ </div>
122
+ )}
123
+ </div>
124
+ )
125
+ }
package/src/Tabs.tsx ADDED
@@ -0,0 +1,64 @@
1
+ /** @jsxImportSource prunelle */
2
+ // Accessible Tabs primitive with roving arrow-key + Home/End keyboard navigation.
3
+
4
+ import type { PrunelleChild } from 'prunelle'
5
+ import { nextUiId } from './behaviors'
6
+ import { createTabs } from './tabs-state'
7
+
8
+ export interface TabDef {
9
+ id: string
10
+ label: string
11
+ content: PrunelleChild
12
+ }
13
+
14
+ export interface TabsProps {
15
+ id?: string | undefined
16
+ items: TabDef[]
17
+ defaultValue?: string | undefined
18
+ label?: string | undefined
19
+ class?: string | undefined
20
+ }
21
+
22
+ export function Tabs({ id, items, defaultValue, label, class: cls }: TabsProps) {
23
+ const baseId = id ?? nextUiId('kui-tabs')
24
+ const ids = items.map(i => i.id)
25
+ const tabs = createTabs(defaultValue ?? items[0]?.id ?? '')
26
+
27
+ return (
28
+ <div data-ui="tabs" class={cls}>
29
+ <div data-part="list" role="tablist" aria-label={label}>
30
+ {items.map(item => (
31
+ <button
32
+ key={item.id}
33
+ id={`${baseId}-tab-${item.id}`}
34
+ data-part="trigger"
35
+ data-state={tabs.isActive(item.id) ? 'active' : 'inactive'}
36
+ role="tab"
37
+ type="button"
38
+ aria-selected={tabs.isActive(item.id) ? 'true' : 'false'}
39
+ aria-controls={`${baseId}-panel-${item.id}`}
40
+ tabIndex={tabs.isActive(item.id) ? 0 : -1}
41
+ onClick={() => tabs.select(item.id)}
42
+ onKeyDown={e => tabs.onKeyDown(e, ids)}
43
+ >
44
+ {item.label}
45
+ </button>
46
+ ))}
47
+ </div>
48
+ {items.map(item => (
49
+ <div
50
+ key={item.id}
51
+ id={`${baseId}-panel-${item.id}`}
52
+ data-part="panel"
53
+ data-state={tabs.isActive(item.id) ? 'active' : 'inactive'}
54
+ role="tabpanel"
55
+ aria-labelledby={`${baseId}-tab-${item.id}`}
56
+ tabIndex={0}
57
+ hidden={!tabs.isActive(item.id)}
58
+ >
59
+ {item.content}
60
+ </div>
61
+ ))}
62
+ </div>
63
+ )
64
+ }
@@ -0,0 +1,105 @@
1
+ /** @jsxImportSource prunelle */
2
+ // Live-region Toast stack driven by the reactive `toast` store.
3
+
4
+ import { effect } from 'prunelle'
5
+ import { dismiss } from './labels/toaster'
6
+ import { pauseToasts, resumeToasts, toast } from './toast'
7
+
8
+ export interface ToasterProps {
9
+ dismissLabel?: string | undefined
10
+ }
11
+
12
+ export function Toaster({ dismissLabel = dismiss }: ToasterProps = {}) {
13
+ let hovered = false
14
+ let focused = false
15
+ // Where focus was before it entered the toaster, given back when the last toast closes.
16
+ let returnTo: HTMLElement | null = null
17
+
18
+ const resume = () => {
19
+ if (!hovered && !focused)
20
+ resumeToasts()
21
+ }
22
+
23
+ // A toast pushed while the stack is hovered or focused waits too.
24
+ effect(() => {
25
+ if (toast.items.value.length > 0 && (hovered || focused))
26
+ pauseToasts()
27
+ })
28
+
29
+ // Closing the focused toast moves focus to the next one, else the previous one, else back where it came from.
30
+ const close = (item: HTMLElement) => {
31
+ const root = item.closest<HTMLElement>('[data-ui="toaster"]')
32
+ const hadFocus = item.contains(document.activeElement)
33
+ const sibling = (item.nextElementSibling ?? item.previousElementSibling)?.querySelector<HTMLElement>('[data-part="close"]')
34
+ toast.dismiss(item.getAttribute('data-toast-id') ?? '')
35
+ if (!hadFocus)
36
+ return
37
+ const target = sibling ?? returnTo
38
+ if (target?.isConnected)
39
+ target.focus()
40
+ if (root !== null && (!root.contains(document.activeElement) || item.contains(document.activeElement))) {
41
+ focused = false
42
+ returnTo = null
43
+ resume()
44
+ }
45
+ }
46
+
47
+ return (
48
+ <div
49
+ data-ui="toaster"
50
+ onMouseEnter={() => {
51
+ hovered = true
52
+ pauseToasts()
53
+ }}
54
+ onMouseLeave={() => {
55
+ hovered = false
56
+ resume()
57
+ }}
58
+ onFocusIn={(e) => {
59
+ const from = e.relatedTarget
60
+ if (from instanceof HTMLElement && !e.currentTarget.contains(from))
61
+ returnTo = from
62
+ focused = true
63
+ pauseToasts()
64
+ }}
65
+ onFocusOut={(e) => {
66
+ const next = e.relatedTarget as Node | null
67
+ if (next === null || !e.currentTarget.contains(next)) {
68
+ focused = false
69
+ returnTo = null
70
+ resume()
71
+ }
72
+ }}
73
+ >
74
+ {toast.items.value.map(item => (
75
+ <div
76
+ key={item.id}
77
+ data-ui="toast"
78
+ data-toast-id={item.id}
79
+ data-tone={item.tone}
80
+ role={item.tone === 'danger' ? 'alert' : 'status'}
81
+ aria-atomic="true"
82
+ onKeyDown={(e) => {
83
+ if (e.key !== 'Escape' || e.defaultPrevented)
84
+ return
85
+ e.preventDefault()
86
+ close(e.currentTarget)
87
+ }}
88
+ >
89
+ <div>
90
+ <strong data-part="title">{item.title}</strong>
91
+ {item.description && <p data-part="description">{item.description}</p>}
92
+ </div>
93
+ <button
94
+ data-part="close"
95
+ type="button"
96
+ aria-label={dismissLabel}
97
+ onClick={e => close(e.currentTarget.closest<HTMLElement>('[data-ui="toast"]')!)}
98
+ >
99
+ ✕
100
+ </button>
101
+ </div>
102
+ ))}
103
+ </div>
104
+ )
105
+ }