@diegopluna/narvi 0.4.1 → 0.5.1

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 (180) hide show
  1. package/README.md +3 -1
  2. package/dist/base.css +3 -0
  3. package/dist/components/app-shell/AppShellPanel.svelte +3 -2
  4. package/dist/components/button/Button.svelte +29 -17
  5. package/dist/components/checkbox/Checkbox.svelte +106 -54
  6. package/dist/components/checkbox/Checkbox.svelte.d.ts +22 -8
  7. package/dist/components/choice-cards/ChoiceCards.svelte +68 -39
  8. package/dist/components/choice-cards/ChoiceCards.svelte.d.ts +12 -8
  9. package/dist/components/collapsible/CollapsibleContent.svelte +71 -17
  10. package/dist/components/collapsible/CollapsibleContent.svelte.d.ts +14 -10
  11. package/dist/components/collapsible/CollapsibleRoot.svelte +13 -9
  12. package/dist/components/collapsible/CollapsibleRoot.svelte.d.ts +8 -9
  13. package/dist/components/collapsible/CollapsibleTrigger.svelte +24 -15
  14. package/dist/components/collapsible/CollapsibleTrigger.svelte.d.ts +10 -14
  15. package/dist/components/color-field/ColorField.svelte +13 -7
  16. package/dist/components/command/CommandDialog.svelte +30 -20
  17. package/dist/components/command/CommandDialog.svelte.d.ts +4 -3
  18. package/dist/components/command/CommandEmpty.svelte +11 -9
  19. package/dist/components/command/CommandEmpty.svelte.d.ts +6 -17
  20. package/dist/components/command/CommandGroup.svelte +32 -11
  21. package/dist/components/command/CommandGroup.svelte.d.ts +7 -11
  22. package/dist/components/command/CommandInput.svelte +25 -10
  23. package/dist/components/command/CommandInput.svelte.d.ts +8 -12
  24. package/dist/components/command/CommandItem.svelte +67 -29
  25. package/dist/components/command/CommandItem.svelte.d.ts +6 -32
  26. package/dist/components/command/CommandList.svelte +8 -10
  27. package/dist/components/command/CommandList.svelte.d.ts +6 -8
  28. package/dist/components/command/CommandRoot.svelte +247 -57
  29. package/dist/components/command/CommandRoot.svelte.d.ts +14 -28
  30. package/dist/components/command/CommandSeparator.svelte +18 -0
  31. package/dist/components/command/CommandSeparator.svelte.d.ts +7 -0
  32. package/dist/components/command/context.d.ts +27 -0
  33. package/dist/components/command/context.js +16 -0
  34. package/dist/components/command/index.d.ts +6 -4
  35. package/dist/components/command/index.js +5 -4
  36. package/dist/components/command/score.d.ts +27 -0
  37. package/dist/components/command/score.js +77 -0
  38. package/dist/components/command/styles.d.ts +32 -9
  39. package/dist/components/command/styles.js +42 -14
  40. package/dist/components/composer/ComposerModelPicker.svelte +350 -346
  41. package/dist/components/composer/ComposerModelPicker.svelte.d.ts +6 -3
  42. package/dist/components/composer/ComposerRoot.svelte.d.ts +1 -1
  43. package/dist/components/dialog/ConfirmDialog.svelte +73 -53
  44. package/dist/components/dialog/DialogClose.svelte +17 -0
  45. package/dist/components/dialog/DialogClose.svelte.d.ts +10 -0
  46. package/dist/components/dialog/DialogContent.svelte +35 -33
  47. package/dist/components/dialog/DialogContent.svelte.d.ts +6 -20
  48. package/dist/components/dialog/DialogDescription.svelte +9 -12
  49. package/dist/components/dialog/DialogDescription.svelte.d.ts +6 -8
  50. package/dist/components/dialog/DialogRoot.svelte +20 -0
  51. package/dist/components/dialog/DialogRoot.svelte.d.ts +7 -0
  52. package/dist/components/dialog/DialogTitle.svelte +12 -8
  53. package/dist/components/dialog/DialogTitle.svelte.d.ts +8 -8
  54. package/dist/components/dialog/DialogTrigger.svelte +17 -0
  55. package/dist/components/dialog/DialogTrigger.svelte.d.ts +10 -0
  56. package/dist/components/dialog/index.d.ts +6 -5
  57. package/dist/components/dialog/index.js +3 -6
  58. package/dist/components/dialog/styles.d.ts +1 -2
  59. package/dist/components/dialog/styles.js +11 -8
  60. package/dist/components/hover-card/HoverCardContent.svelte +35 -29
  61. package/dist/components/hover-card/HoverCardContent.svelte.d.ts +8 -12
  62. package/dist/components/hover-card/HoverCardRoot.svelte +7 -7
  63. package/dist/components/hover-card/HoverCardRoot.svelte.d.ts +5 -4
  64. package/dist/components/hover-card/HoverCardTrigger.svelte +18 -0
  65. package/dist/components/hover-card/HoverCardTrigger.svelte.d.ts +10 -0
  66. package/dist/components/hover-card/index.d.ts +2 -3
  67. package/dist/components/hover-card/index.js +1 -5
  68. package/dist/components/input/Input.svelte.d.ts +1 -1
  69. package/dist/components/markdown/MarkdownBlock.svelte +6 -3
  70. package/dist/components/markdown/MarkdownTable.svelte +15 -7
  71. package/dist/components/menu/ContextMenuContent.svelte +15 -15
  72. package/dist/components/menu/ContextMenuContent.svelte.d.ts +6 -9
  73. package/dist/components/menu/ContextMenuTrigger.svelte +14 -16
  74. package/dist/components/menu/ContextMenuTrigger.svelte.d.ts +8 -14
  75. package/dist/components/menu/MenuCheckboxItem.svelte +22 -11
  76. package/dist/components/menu/MenuCheckboxItem.svelte.d.ts +8 -22
  77. package/dist/components/menu/MenuContent.svelte +16 -18
  78. package/dist/components/menu/MenuContent.svelte.d.ts +6 -18
  79. package/dist/components/menu/MenuGroup.svelte +4 -4
  80. package/dist/components/menu/MenuGroup.svelte.d.ts +5 -4
  81. package/dist/components/menu/MenuItem.svelte +25 -12
  82. package/dist/components/menu/MenuItem.svelte.d.ts +6 -21
  83. package/dist/components/menu/MenuLabel.svelte +7 -11
  84. package/dist/components/menu/MenuLabel.svelte.d.ts +6 -8
  85. package/dist/components/menu/MenuRadioGroup.svelte +4 -4
  86. package/dist/components/menu/MenuRadioGroup.svelte.d.ts +5 -4
  87. package/dist/components/menu/MenuRadioItem.svelte +16 -14
  88. package/dist/components/menu/MenuRadioItem.svelte.d.ts +6 -19
  89. package/dist/components/menu/MenuSeparator.svelte +6 -10
  90. package/dist/components/menu/MenuSeparator.svelte.d.ts +6 -7
  91. package/dist/components/menu/MenuSubContent.svelte +22 -19
  92. package/dist/components/menu/MenuSubContent.svelte.d.ts +8 -15
  93. package/dist/components/menu/MenuSubTrigger.svelte +7 -11
  94. package/dist/components/menu/MenuSubTrigger.svelte.d.ts +6 -15
  95. package/dist/components/menu/MenuTrigger.svelte +17 -0
  96. package/dist/components/menu/MenuTrigger.svelte.d.ts +10 -0
  97. package/dist/components/menu/context-menu.d.ts +2 -2
  98. package/dist/components/menu/context-menu.js +3 -2
  99. package/dist/components/menu/group.js +1 -1
  100. package/dist/components/menu/index.d.ts +5 -4
  101. package/dist/components/menu/index.js +5 -5
  102. package/dist/components/menu/row.d.ts +20 -0
  103. package/dist/components/menu/row.js +12 -0
  104. package/dist/components/popover/PopoverClose.svelte +17 -0
  105. package/dist/components/popover/PopoverClose.svelte.d.ts +10 -0
  106. package/dist/components/popover/PopoverContent.svelte +30 -29
  107. package/dist/components/popover/PopoverContent.svelte.d.ts +6 -17
  108. package/dist/components/popover/PopoverTrigger.svelte +17 -0
  109. package/dist/components/popover/PopoverTrigger.svelte.d.ts +10 -0
  110. package/dist/components/popover/index.d.ts +6 -4
  111. package/dist/components/popover/index.js +6 -5
  112. package/dist/components/progress/Progress.svelte +16 -22
  113. package/dist/components/progress/Progress.svelte.d.ts +6 -5
  114. package/dist/components/reasoning/ReasoningRoot.svelte.d.ts +1 -1
  115. package/dist/components/resize-handle/ResizeHandle.svelte +80 -14
  116. package/dist/components/resize-handle/ResizeHandle.svelte.d.ts +15 -1
  117. package/dist/components/segmented/SegmentedControl.svelte +100 -91
  118. package/dist/components/segmented/SegmentedControl.svelte.d.ts +6 -4
  119. package/dist/components/select/Select.svelte +97 -59
  120. package/dist/components/select/Select.svelte.d.ts +3 -2
  121. package/dist/components/sheet/SheetContent.svelte +31 -38
  122. package/dist/components/sheet/SheetContent.svelte.d.ts +7 -22
  123. package/dist/components/sheet/index.d.ts +3 -5
  124. package/dist/components/sheet/index.js +3 -5
  125. package/dist/components/sheet/styles.js +26 -5
  126. package/dist/components/sidebar/SidebarItem.svelte +2 -3
  127. package/dist/components/slider/Slider.svelte +86 -65
  128. package/dist/components/slider/Slider.svelte.d.ts +9 -8
  129. package/dist/components/split-button/SplitButton.svelte +20 -3
  130. package/dist/components/split-button/SplitButton.svelte.d.ts +1 -1
  131. package/dist/components/switch/Switch.svelte +77 -49
  132. package/dist/components/switch/Switch.svelte.d.ts +15 -8
  133. package/dist/components/tab-strip/TabStrip.svelte +87 -68
  134. package/dist/components/tabs/Tabs.svelte +129 -99
  135. package/dist/components/tabs/Tabs.svelte.d.ts +6 -4
  136. package/dist/components/textarea/Textarea.svelte.d.ts +1 -1
  137. package/dist/components/toast/Toaster.svelte +132 -115
  138. package/dist/components/toast/Toaster.svelte.d.ts +5 -0
  139. package/dist/components/toast/toasts.svelte.d.ts +10 -23
  140. package/dist/components/toast/toasts.svelte.js +62 -40
  141. package/dist/components/toggle/Toggle.svelte +15 -9
  142. package/dist/components/toggle/Toggle.svelte.d.ts +8 -17
  143. package/dist/components/toggle-group/ToggleGroupItem.svelte +20 -9
  144. package/dist/components/toggle-group/ToggleGroupItem.svelte.d.ts +10 -10
  145. package/dist/components/toggle-group/ToggleGroupRoot.svelte +33 -18
  146. package/dist/components/toggle-group/ToggleGroupRoot.svelte.d.ts +4 -4
  147. package/dist/components/tool/ToolRoot.svelte.d.ts +1 -1
  148. package/dist/components/tooltip/TooltipContent.svelte +38 -28
  149. package/dist/components/tooltip/TooltipContent.svelte.d.ts +6 -15
  150. package/dist/components/tooltip/TooltipProvider.svelte +34 -10
  151. package/dist/components/tooltip/TooltipProvider.svelte.d.ts +18 -4
  152. package/dist/components/tooltip/TooltipRoot.svelte +52 -23
  153. package/dist/components/tooltip/TooltipRoot.svelte.d.ts +10 -10
  154. package/dist/components/tooltip/TooltipTrigger.svelte +14 -17
  155. package/dist/components/tooltip/TooltipTrigger.svelte.d.ts +10 -4
  156. package/dist/components/tooltip/index.d.ts +1 -1
  157. package/dist/components/tooltip/index.js +1 -1
  158. package/dist/components/work-fold/WorkFold.svelte +14 -5
  159. package/dist/index.d.ts +5 -5
  160. package/dist/index.js +1 -1
  161. package/dist/internal/TooltipScope.svelte +4 -4
  162. package/dist/internal/layer.d.ts +11 -9
  163. package/dist/internal/layer.js +21 -15
  164. package/dist/internal/loose.d.ts +30 -5
  165. package/dist/internal/loose.js +1 -1
  166. package/dist/internal/markers.stylex.d.ts +0 -4
  167. package/dist/internal/markers.stylex.js +0 -2
  168. package/dist/internal/toggle.d.ts +3 -0
  169. package/dist/internal/toggle.js +6 -2
  170. package/dist/internal/tooltip-trigger.d.ts +7 -6
  171. package/dist/internal/tooltip-trigger.js +9 -12
  172. package/dist/vite.d.ts +7 -1
  173. package/dist/vite.js +20 -5
  174. package/package.json +3 -2
  175. package/skills/building-ui/SKILL.md +22 -22
  176. package/skills/building-ui/references/components.md +30 -4
  177. package/skills/markdown/SKILL.md +1 -1
  178. package/skills/setup/SKILL.md +4 -8
  179. package/skills/styling/SKILL.md +1 -1
  180. package/skills/theming/SKILL.md +3 -7
@@ -1,41 +1,62 @@
1
1
  <!--
2
2
  Where toasts appear. Place one near the root of the app.
3
3
 
4
- It sits in the top layer, so toasts stay above dialogs and can still be
5
- clicked while one is open. Each toast waits while the pointer or focus is on
6
- the stack, and while the window is hidden, so none goes unread.
4
+ Built on ui-sdk's toast region. It sits in the top layer, so toasts stay above
5
+ dialogs, and ui-sdk keeps them usable while a modal dialog is open. Each
6
+ toast waits while the pointer or focus is on the stack, while the window is
7
+ hidden or in the background, so none goes unread.
7
8
  -->
8
9
  <script lang="ts" module>
9
10
  import * as stylex from '@stylexjs/stylex';
11
+ import {
12
+ Toast,
13
+ ToastClose,
14
+ ToastDescription,
15
+ ToastRegion,
16
+ ToastTitle
17
+ } from '@ui-sdk/svelte/toast';
10
18
  import { colors, density, font, motion, radius, space } from '../../tokens.stylex.js';
11
19
  import { media } from '../../media.stylex.js';
12
20
  import Glyph from '../../internal/Glyph.svelte';
21
+ import { shared } from '../../internal/shared.js';
13
22
  import Button from '../button/Button.svelte';
14
23
  import Spinner from '../spinner/Spinner.svelte';
15
- import { finish, toasts, type ToastEnd, type ToastEntry } from './toasts.svelte.js';
24
+ import { answer, toaster, type ToastData } from './toasts.svelte.js';
16
25
 
17
26
  export type ToasterProps = {
18
27
  /** Distance from the top of the window, to clear an app's own header. */
19
28
  offset?: string | undefined;
20
29
  /** Accessible name of the region. */
21
30
  label?: string | undefined;
31
+ /**
32
+ * The key that moves focus to the newest toast, and back again: `F6` by default, or
33
+ * `alt+t`. `null` leaves the key to the app, for one that cycles its own areas with F6.
34
+ */
35
+ shortcut?: string | null | undefined;
22
36
  };
23
37
 
24
38
  /** A length at the window's density (`density` in the tokens). */
25
39
  const d = (rem: string) => `calc(${rem} * ${density.factor})`;
26
40
 
41
+ // ui-sdk's state attributes on a toast: its first frame, while it leaves, while it is swiped
42
+ const STARTING = ':is([data-starting-style])';
43
+ const ENDING = ':is([data-ending-style])';
44
+ const SWIPING = ':is([data-swiping])';
45
+ // Where a swipe has taken it, which it leaves from
46
+ const SWIPE = 'var(--toast-swipe-x, 0px) var(--toast-swipe-y, 0px)';
47
+
27
48
  const styles = stylex.create({
28
- // The top layer brings the browser's popover styles; these undo them
49
+ // The top layer brings the browser's popover styles; these undo them. ui-sdk measures the
50
+ // toasts, newest first, and the stack is as tall as they are with the gaps between them.
29
51
  region: {
30
52
  position: 'fixed',
31
53
  inset: 'auto',
32
54
  top: 'var(--narvi-toaster-offset)',
33
55
  insetInlineEnd: space.x4,
34
- display: 'flex',
35
- flexDirection: 'column',
36
- gap: space.x2,
56
+ display: 'block',
37
57
  width: 'min(22rem, calc(100vw - 2rem))',
38
- height: 'auto',
58
+ maxWidth: 'none',
59
+ height: `max(0px, calc(var(--toast-stack-height, 0px) + (var(--toast-count, 1) - 1) * ${space.x2}))`,
39
60
  margin: 0,
40
61
  padding: 0,
41
62
  overflow: 'visible',
@@ -43,7 +64,22 @@
43
64
  backgroundColor: 'transparent',
44
65
  pointerEvents: 'none'
45
66
  },
67
+ // More toasts than the window holds: the stack stops at the window's foot and scrolls, so
68
+ // the oldest can still be read and put away. Only then, as it clips what spills out of it,
69
+ // a shadow or a toast being swiped away.
70
+ crowded: {
71
+ maxHeight: `calc(100dvh - var(--narvi-toaster-offset) - ${space.x4})`,
72
+ overflowY: 'auto',
73
+ overscrollBehavior: 'contain',
74
+ pointerEvents: 'auto'
75
+ },
76
+ // Each toast below the newer ones. One that leaves keeps its place while it fades, and
77
+ // the rest close up behind it.
46
78
  toast: {
79
+ position: 'absolute',
80
+ insetInline: 0,
81
+ top: `calc(var(--toast-offset, 0px) + var(--toast-index, 0) * ${space.x2})`,
82
+ boxSizing: 'border-box',
47
83
  display: 'grid',
48
84
  gridTemplateColumns: 'auto minmax(0, 1fr) auto',
49
85
  alignItems: 'start',
@@ -59,20 +95,24 @@
59
95
  fontSize: font.sizeBody,
60
96
  lineHeight: font.leadingSnug,
61
97
  pointerEvents: 'auto',
62
- opacity: { default: 1, '@starting-style': 0 },
98
+ opacity: { default: 1, [STARTING]: 0, [ENDING]: 0 },
99
+ // A swipe follows the pointer, with or without motion
63
100
  translate: {
64
- default: null,
65
- [media.motion]: { default: '0 0', '@starting-style': '0 -0.5rem' }
101
+ default: SWIPE,
102
+ [media.motion]: {
103
+ default: SWIPE,
104
+ [STARTING]: '0 -0.5rem',
105
+ [ENDING]: 'calc(var(--toast-swipe-x, 0px) + 0.5rem) var(--toast-swipe-y, 0px)'
106
+ }
66
107
  },
67
- transitionProperty: { default: 'opacity', [media.motion]: 'opacity, translate' },
68
- transitionDuration: motion.slow,
108
+ transitionProperty: {
109
+ default: 'opacity',
110
+ [media.motion]: 'opacity, translate, top',
111
+ [SWIPING]: 'none'
112
+ },
113
+ transitionDuration: { default: motion.slow, [ENDING]: motion.fast },
69
114
  transitionTimingFunction: motion.easeOut
70
115
  },
71
- leaving: {
72
- opacity: 0,
73
- translate: { default: null, [media.motion]: '0.5rem 0' },
74
- transitionDuration: motion.fast
75
- },
76
116
  icon: { display: 'grid', placeItems: 'center', width: '1rem', height: '1.25rem' },
77
117
  dot: {
78
118
  width: '0.375rem',
@@ -90,7 +130,9 @@
90
130
  flexDirection: 'column',
91
131
  gap: '0.125rem',
92
132
  minWidth: 0,
93
- paddingTop: '1px'
133
+ paddingTop: '1px',
134
+ // A branch or a hash with nowhere to break breaks anywhere, rather than running out
135
+ overflowWrap: 'anywhere'
94
136
  },
95
137
  title: { fontWeight: font.weightMedium },
96
138
  // What went wrong can be selected and copied
@@ -100,6 +142,14 @@
100
142
  userSelect: 'text',
101
143
  overflowWrap: 'anywhere'
102
144
  },
145
+ // An action's label past 10rem is cut short, so the title keeps its room beside it
146
+ action: { maxWidth: '10rem', minWidth: 0 },
147
+ actionLabel: {
148
+ minWidth: 0,
149
+ overflow: 'hidden',
150
+ textOverflow: 'ellipsis',
151
+ whiteSpace: 'nowrap'
152
+ },
103
153
  // Its small buttons are centred on the title's line, about 20px, at any density
104
154
  actions: {
105
155
  display: 'flex',
@@ -111,131 +161,98 @@
111
161
  </script>
112
162
 
113
163
  <script lang="ts">
114
- let { offset = '1rem', label = 'Notifications' }: ToasterProps = $props();
164
+ let { offset = '1rem', label = 'Notifications', shortcut }: ToasterProps = $props();
115
165
 
116
- let region = $state<HTMLElement | null>(null);
117
- let held = $state(false);
118
- let hidden = $state(false);
119
- // Toasts on their way out, by revision, kept a moment so they can fade. A toast replaced
120
- // while it fades comes back at once.
121
- let leaving = $state<number[]>([]);
166
+ const id = $props.id();
167
+ let crowded = $state(false);
122
168
 
169
+ // Whether the stack runs past the window's foot. Measured as the stack or the window changes
170
+ // size; while crowded the region keeps one height, and grows again only once the stack fits.
123
171
  $effect(() => {
124
- region?.showPopover?.();
125
- const visibility = () => (hidden = document.hidden);
126
- document.addEventListener('visibilitychange', visibility);
127
- return () => document.removeEventListener('visibilitychange', visibility);
128
- });
129
-
130
- function close(entry: ToastEntry, how: ToastEnd) {
131
- const { id, revision } = entry;
132
- if (leaving.includes(revision)) return;
133
- leaving = [...leaving, revision];
134
- setTimeout(() => {
135
- finish(id, how, revision);
136
- leaving = leaving.filter((each) => each !== revision);
137
- }, 140);
138
- }
139
-
140
- // Each toast's own timer, which runs only while nothing holds the stack
141
- function timed(element: HTMLElement, entry: ToastEntry) {
142
- let remaining = entry.duration ?? Infinity;
143
- let started = 0;
144
- let timer: ReturnType<typeof setTimeout> | undefined;
145
- const run = () => {
146
- if (!Number.isFinite(remaining)) return;
147
- started = performance.now();
148
- timer = setTimeout(() => close(entry, 'auto'), remaining);
172
+ const region = document.querySelector<HTMLElement>(`[data-narvi-toaster="${id}"]`);
173
+ if (!region) return;
174
+ const measure = () => {
175
+ // The room `crowded` leaves it: down to 1rem above the window's foot
176
+ const room = window.innerHeight - region.getBoundingClientRect().top - 16;
177
+ crowded = region.scrollHeight > room + 1;
149
178
  };
150
- const pause = () => {
151
- if (timer === undefined) return;
152
- clearTimeout(timer);
153
- timer = undefined;
154
- remaining -= performance.now() - started;
179
+ const observer = new ResizeObserver(measure);
180
+ observer.observe(region);
181
+ window.addEventListener('resize', measure);
182
+ return () => {
183
+ observer.disconnect();
184
+ window.removeEventListener('resize', measure);
155
185
  };
156
- return { run, pause, stop: () => clearTimeout(timer), element };
157
- }
186
+ });
158
187
  </script>
159
188
 
160
- <section
161
- bind:this={region}
162
- popover="manual"
189
+ <!-- Every toast shows at once, so nothing waits unseen behind the stack -->
190
+ <ToastRegion
191
+ {toaster}
192
+ side="top"
193
+ align="end"
194
+ maxVisible={Infinity}
195
+ expanded
196
+ {shortcut}
163
197
  aria-label={label}
164
- data-slot="toaster"
165
- style:--narvi-toaster-offset={offset}
166
- {...stylex.attrs(styles.region)}
167
- onpointerenter={() => (held = true)}
168
- onpointerleave={() => (held = false)}
169
- onfocusin={() => (held = true)}
170
- onfocusout={(event) => {
171
- if (!region?.contains(event.relatedTarget as Node | null)) held = false;
172
- }}
198
+ style="--narvi-toaster-offset: {offset}"
199
+ data-narvi-toaster={id}
200
+ {...stylex.attrs(styles.region, crowded && styles.crowded)}
173
201
  >
174
- {#each toasts.list as entry (entry.id)}
175
- {@const clock = timed(region!, entry)}
176
- <div
177
- role={entry.tone === 'error' ? 'alert' : 'status'}
178
- data-tone={entry.tone}
179
- {...stylex.attrs(styles.toast, leaving.includes(entry.revision) && styles.leaving)}
180
- {@attach () => {
181
- // Restarts when the toast changes, as a loading toast turns into a result
182
- void entry.revision;
183
- if (!held && !hidden) clock.run();
184
- return () => clock.pause();
185
- }}
186
- >
202
+ {#snippet children(item, place)}
203
+ {@const data = item.data as ToastData}
204
+ <!-- One on its way out can no longer be pressed, nor be read a second time -->
205
+ <Toast inert={place.leaving || undefined} {...stylex.attrs(shared.focusable, styles.toast)}>
187
206
  <span
188
207
  {...stylex.attrs(
189
208
  styles.icon,
190
- entry.tone === 'info' && styles.info,
191
- entry.tone === 'success' && styles.success,
192
- entry.tone === 'warning' && styles.warning,
193
- entry.tone === 'error' && styles.error,
194
- (entry.tone === 'default' || entry.tone === 'loading') && styles.muted
209
+ data.tone === 'info' && styles.info,
210
+ data.tone === 'success' && styles.success,
211
+ data.tone === 'warning' && styles.warning,
212
+ data.tone === 'error' && styles.error,
213
+ (data.tone === 'default' || data.tone === 'loading') && styles.muted
195
214
  )}
196
215
  >
197
- {#if entry.tone === 'loading'}
216
+ {#if data.tone === 'loading'}
198
217
  <Spinner size={14} label={null} />
199
- {:else if entry.tone === 'success'}
218
+ {:else if data.tone === 'success'}
200
219
  <Glyph name="check" stroke={1.75} />
201
- {:else if entry.tone === 'error' || entry.tone === 'warning'}
220
+ {:else if data.tone === 'error' || data.tone === 'warning'}
202
221
  <Glyph name="alert" />
203
- {:else if entry.tone === 'info'}
222
+ {:else if data.tone === 'info'}
204
223
  <Glyph name="info" />
205
224
  {:else}
206
225
  <span {...stylex.attrs(styles.dot)}></span>
207
226
  {/if}
208
227
  </span>
209
- <span {...stylex.attrs(styles.text)}>
210
- <span {...stylex.attrs(styles.title)}>{entry.title}</span>
211
- {#if entry.description}
212
- <span {...stylex.attrs(styles.description)}>{entry.description}</span>
213
- {/if}
214
- </span>
228
+ <div {...stylex.attrs(styles.text)}>
229
+ <ToastTitle {...stylex.attrs(styles.title)} />
230
+ <ToastDescription {...stylex.attrs(styles.description)} />
231
+ </div>
215
232
  <span {...stylex.attrs(styles.actions)}>
216
- {#if entry.action}
233
+ {#if data.action}
234
+ {@const action = data.action}
217
235
  <Button
218
236
  size="sm"
219
237
  variant="secondary"
238
+ sx={styles.action}
220
239
  onclick={(event) => {
221
- entry.action?.onclick(event);
222
- if (!event.defaultPrevented) close(entry, 'action');
240
+ action.onclick(event);
241
+ if (!event.defaultPrevented) answer(data.id);
223
242
  }}
224
243
  >
225
- {entry.action.label}
244
+ <span {...stylex.attrs(styles.actionLabel)}>{action.label}</span>
226
245
  </Button>
227
246
  {/if}
228
247
  <!-- Every toast can be put away, a lasting one most of all; work under way goes on -->
229
- <Button
230
- size="sm"
231
- variant="ghost"
232
- iconOnly
233
- aria-label="Dismiss"
234
- onclick={() => close(entry, 'dismiss')}
235
- >
236
- <Glyph name="close" size={14} />
237
- </Button>
248
+ <ToastClose>
249
+ {#snippet render({ props })}
250
+ <Button {...props} size="sm" variant="ghost" iconOnly aria-label="Dismiss">
251
+ <Glyph name="close" size={14} />
252
+ </Button>
253
+ {/snippet}
254
+ </ToastClose>
238
255
  </span>
239
- </div>
240
- {/each}
241
- </section>
256
+ </Toast>
257
+ {/snippet}
258
+ </ToastRegion>
@@ -3,6 +3,11 @@ export type ToasterProps = {
3
3
  offset?: string | undefined;
4
4
  /** Accessible name of the region. */
5
5
  label?: string | undefined;
6
+ /**
7
+ * The key that moves focus to the newest toast, and back again: `F6` by default, or
8
+ * `alt+t`. `null` leaves the key to the app, for one that cycles its own areas with F6.
9
+ */
10
+ shortcut?: string | null | undefined;
6
11
  };
7
12
  declare const Toaster: import("svelte").Component<ToasterProps, {}, "">;
8
13
  type Toaster = ReturnType<typeof Toaster>;
@@ -1,7 +1,3 @@
1
- /**
2
- * Toasts: short notes about something that happened away from where the person
3
- * is looking. Call `toast()` from anywhere; one <Toaster /> shows them.
4
- */
5
1
  export type ToastTone = 'default' | 'info' | 'success' | 'warning' | 'error' | 'loading';
6
2
  /** A toast's id: the one `toast()` returns, or one of your own given as `id`. */
7
3
  export type ToastId = string | number;
@@ -30,33 +26,24 @@ export type ToastOptions = {
30
26
  /** Called when it goes by itself, as its time runs out. */
31
27
  onAutoClose?: ((id: ToastId) => void) | undefined;
32
28
  };
33
- /** How a toast went: closed by the person or by code, by its own clock, or by its action. */
34
- export type ToastEnd = 'dismiss' | 'auto' | 'action';
35
- export type ToastEntry = {
29
+ /** What the Toaster reads from a toast's `data`: its own id, its tone and its action. */
30
+ export type ToastData = {
36
31
  id: ToastId;
37
- title: string;
38
32
  tone: ToastTone;
39
- description?: string | undefined;
40
33
  action?: ToastAction | undefined;
41
- duration: number | null;
42
- onDismiss?: ((id: ToastId) => void) | undefined;
43
- onAutoClose?: ((id: ToastId) => void) | undefined;
44
- /** Changes each time the toast is replaced or updated, so its clock starts again. */
45
- revision: number;
46
34
  };
47
- declare class Toasts {
48
- list: ToastEntry[];
49
- }
50
- export declare const toasts: Toasts;
35
+ /**
36
+ * Narvi's queue, on ui-sdk's toaster: its own, so an app's own ui-sdk toasts don't mix with
37
+ * these. The Toaster shows it. ui-sdk keeps each toast's place, and its timer, which starts
38
+ * again whenever the toast changes.
39
+ */
40
+ export declare const toaster: import("@ui-sdk/core/toast").Toaster<string>;
51
41
  declare function show(title: string, options?: ToastOptions): ToastId;
52
42
  declare function update(id: ToastId, patch: ToastOptions & {
53
43
  title?: string | undefined;
54
44
  }): void;
55
- /**
56
- * Takes a toast away and says so to whoever showed it. The Toaster calls it as a toast
57
- * leaves; `revision` is the one it closed, so a toast replaced meanwhile stays.
58
- */
59
- export declare function finish(id: ToastId, how: ToastEnd, revision?: number): void;
45
+ /** Closes a toast its action answered. The Toaster calls it. */
46
+ export declare function answer(id: ToastId): void;
60
47
  /** Closes one toast, or every toast when given no id. Each one's `onDismiss` is called. */
61
48
  declare function dismiss(id?: ToastId): void;
62
49
  type Message<T> = string | ((value: T) => string);
@@ -2,65 +2,87 @@
2
2
  * Toasts: short notes about something that happened away from where the person
3
3
  * is looking. Call `toast()` from anywhere; one <Toaster /> shows them.
4
4
  */
5
+ import { createToaster } from '@ui-sdk/svelte/toast';
5
6
  const DURATION = 5000;
6
7
  let next = 1;
7
- let revisions = 1;
8
- class Toasts {
9
- list = $state([]);
10
- }
11
- export const toasts = new Toasts();
8
+ /**
9
+ * Narvi's queue, on ui-sdk's toaster: its own, so an app's own ui-sdk toasts don't mix with
10
+ * these. The Toaster shows it. ui-sdk keeps each toast's place, and its timer, which starts
11
+ * again whenever the toast changes.
12
+ */
13
+ export const toaster = createToaster({ duration: DURATION });
14
+ // Each toast as it was last shown or updated, by ui-sdk's id for it
15
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity -- read as toasts end, never by the page
16
+ const entries = new Map();
17
+ // Toasts their own action closed: an answer of its own, which calls neither callback
18
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity -- read as toasts end, never by the page
19
+ const answered = new Set();
20
+ // ui-sdk's ids are strings; Narvi's may be numbers too, and 1 and '1' stay apart
21
+ const key = (id) => (typeof id === 'number' ? `n${id}` : `s${id}`);
12
22
  // Errors and work in progress stay until they are dealt with
13
23
  const lasting = (tone) => tone === 'error' || tone === 'loading';
14
- function entryFrom(id, title, options) {
24
+ const durationFor = (tone, duration) => duration !== undefined ? duration : lasting(tone) ? null : DURATION;
25
+ function ended(at, details) {
26
+ const entry = entries.get(at);
27
+ entries.delete(at);
28
+ if (answered.delete(at) || !entry)
29
+ return;
30
+ if (details.reason === 'timeout')
31
+ entry.onAutoClose?.(entry.id);
32
+ else if (details.reason !== 'action')
33
+ entry.onDismiss?.(entry.id);
34
+ }
35
+ // Every field is given each time, so a toast shown again under its id is replaced whole
36
+ function put(entry) {
37
+ const at = key(entry.id);
38
+ entries.set(at, entry);
39
+ toaster({
40
+ id: at,
41
+ title: entry.title,
42
+ description: entry.description,
43
+ type: entry.tone,
44
+ duration: entry.duration ?? Infinity,
45
+ data: { id: entry.id, tone: entry.tone, action: entry.action },
46
+ onDismiss: (details) => ended(at, details)
47
+ });
48
+ }
49
+ function show(title, options = {}) {
50
+ const id = options.id ?? next++;
15
51
  const tone = options.tone ?? 'default';
16
- return {
52
+ put({
17
53
  id,
18
54
  title,
19
55
  tone,
20
56
  description: options.description,
21
57
  action: options.action,
22
- duration: options.duration !== undefined ? options.duration : lasting(tone) ? null : DURATION,
58
+ duration: durationFor(tone, options.duration),
23
59
  onDismiss: options.onDismiss,
24
- onAutoClose: options.onAutoClose,
25
- revision: revisions++
26
- };
27
- }
28
- function show(title, options = {}) {
29
- const id = options.id ?? next++;
30
- const entry = entryFrom(id, title, options);
31
- const at = toasts.list.findIndex((existing) => existing.id === id);
32
- toasts.list = at === -1 ? [...toasts.list, entry] : toasts.list.with(at, entry);
60
+ onAutoClose: options.onAutoClose
61
+ });
33
62
  return id;
34
63
  }
35
64
  function update(id, patch) {
36
- toasts.list = toasts.list.map((entry) => {
37
- if (entry.id !== id)
38
- return entry;
39
- const tone = patch.tone ?? entry.tone;
40
- const duration = patch.duration !== undefined ? patch.duration : lasting(tone) ? null : DURATION;
41
- const defined = Object.fromEntries(Object.entries(patch).filter(([, value]) => value !== undefined));
42
- return { ...entry, ...defined, id, tone, duration, revision: revisions++ };
43
- });
65
+ const entry = entries.get(key(id));
66
+ if (!entry)
67
+ return;
68
+ const tone = patch.tone ?? entry.tone;
69
+ const defined = Object.fromEntries(Object.entries(patch).filter(([, value]) => value !== undefined));
70
+ put({ ...entry, ...defined, id: entry.id, tone, duration: durationFor(tone, patch.duration) });
44
71
  }
45
- /**
46
- * Takes a toast away and says so to whoever showed it. The Toaster calls it as a toast
47
- * leaves; `revision` is the one it closed, so a toast replaced meanwhile stays.
48
- */
49
- export function finish(id, how, revision) {
50
- const entry = toasts.list.find((existing) => existing.id === id);
51
- if (!entry || (revision !== undefined && entry.revision !== revision))
72
+ /** Closes a toast its action answered. The Toaster calls it. */
73
+ export function answer(id) {
74
+ const at = key(id);
75
+ if (!entries.has(at))
52
76
  return;
53
- toasts.list = toasts.list.filter((existing) => existing !== entry);
54
- if (how === 'dismiss')
55
- entry.onDismiss?.(id);
56
- else if (how === 'auto')
57
- entry.onAutoClose?.(id);
77
+ answered.add(at);
78
+ toaster.dismiss(at);
58
79
  }
59
80
  /** Closes one toast, or every toast when given no id. Each one's `onDismiss` is called. */
60
81
  function dismiss(id) {
61
- const ids = id === undefined ? toasts.list.map((entry) => entry.id) : [id];
62
- for (const each of ids)
63
- finish(each, 'dismiss');
82
+ if (id === undefined)
83
+ toaster.dismiss();
84
+ else
85
+ toaster.dismiss(key(id));
64
86
  }
65
87
  /**
66
88
  * Shows a toast and returns its id.
@@ -1,12 +1,12 @@
1
1
  <script lang="ts" module>
2
- import type { Loose } from '../../internal/loose.js';
3
- import { Toggle } from 'bits-ui';
2
+ import type { Loose, PartProps } from '../../internal/loose.js';
3
+ import { Toggle as UiToggle } from '@ui-sdk/svelte/toggle';
4
4
  import type { Snippet } from 'svelte';
5
5
  import { attrs, type Sx } from '../../internal/sx.js';
6
6
  import { toggle, type ToggleSize, type ToggleTone } from '../../internal/toggle.js';
7
7
 
8
- export type ToggleProps = Loose<
9
- Omit<Toggle.RootProps, 'children' | 'child'> & {
8
+ export interface ToggleProps extends Loose<
9
+ Omit<PartProps<typeof UiToggle>, 'children' | 'render' | 'value'> & {
10
10
  size?: ToggleSize | undefined;
11
11
  /**
12
12
  * How it shows it is pressed: lit in the `accent`, or `neutral`, in the fill of what is
@@ -15,11 +15,13 @@
15
15
  tone?: ToggleTone | undefined;
16
16
  /** Square, for a single icon. Give it an `aria-label`. */
17
17
  iconOnly?: boolean | undefined;
18
+ /** The button element. Bindable. */
19
+ ref?: HTMLButtonElement | null | undefined;
18
20
  sx?: Sx | undefined;
19
21
  /** An icon, a short label, or both. */
20
22
  children?: Snippet | undefined;
21
23
  }
22
- >;
24
+ > {}
23
25
  </script>
24
26
 
25
27
  <script lang="ts">
@@ -38,16 +40,20 @@
38
40
  const square = { sm: toggle.iconSm, md: toggle.iconMd };
39
41
  </script>
40
42
 
41
- <Toggle.Root
43
+ <!-- An icon put in it takes its size (`data-icons`), as in a Button -->
44
+ <UiToggle
42
45
  bind:pressed
43
- bind:ref
44
- data-slot="toggle"
45
46
  data-tone={tone}
47
+ data-icons
46
48
  {...rest}
47
49
  {...attrs(
48
50
  [toggle.base, tone === 'neutral' && toggle.neutral, toggle[size], iconOnly && square[size], sx],
49
51
  className
50
52
  )}
53
+ {@attach (element: HTMLButtonElement) => {
54
+ ref = element;
55
+ return () => (ref = null);
56
+ }}
51
57
  >
52
58
  {@render children?.()}
53
- </Toggle.Root>
59
+ </UiToggle>
@@ -1,9 +1,9 @@
1
- import type { Loose } from '../../internal/loose.js';
2
- import { Toggle } from 'bits-ui';
1
+ import type { Loose, PartProps } from '../../internal/loose.js';
2
+ import { Toggle as UiToggle } from '@ui-sdk/svelte/toggle';
3
3
  import type { Snippet } from 'svelte';
4
4
  import { type Sx } from '../../internal/sx.js';
5
5
  import { type ToggleSize, type ToggleTone } from '../../internal/toggle.js';
6
- export type ToggleProps = Loose<Omit<Toggle.RootProps, 'children' | 'child'> & {
6
+ export interface ToggleProps extends Loose<Omit<PartProps<typeof UiToggle>, 'children' | 'render' | 'value'> & {
7
7
  size?: ToggleSize | undefined;
8
8
  /**
9
9
  * How it shows it is pressed: lit in the `accent`, or `neutral`, in the fill of what is
@@ -12,22 +12,13 @@ export type ToggleProps = Loose<Omit<Toggle.RootProps, 'children' | 'child'> & {
12
12
  tone?: ToggleTone | undefined;
13
13
  /** Square, for a single icon. Give it an `aria-label`. */
14
14
  iconOnly?: boolean | undefined;
15
+ /** The button element. Bindable. */
16
+ ref?: HTMLButtonElement | null | undefined;
15
17
  sx?: Sx | undefined;
16
18
  /** An icon, a short label, or both. */
17
19
  children?: Snippet | undefined;
18
- }>;
19
- declare const Toggle: import("svelte").Component<Loose<Omit<Toggle.RootProps, "children" | "child"> & {
20
- size?: ToggleSize | undefined;
21
- /**
22
- * How it shows it is pressed: lit in the `accent`, or `neutral`, in the fill of what is
23
- * chosen, for a toolbar's icons that show or hide something.
24
- */
25
- tone?: ToggleTone | undefined;
26
- /** Square, for a single icon. Give it an `aria-label`. */
27
- iconOnly?: boolean | undefined;
28
- sx?: Sx | undefined;
29
- /** An icon, a short label, or both. */
30
- children?: Snippet | undefined;
31
- }>, {}, "ref" | "pressed">;
20
+ }> {
21
+ }
22
+ declare const Toggle: import("svelte").Component<ToggleProps, {}, "ref" | "pressed">;
32
23
  type Toggle = ReturnType<typeof Toggle>;
33
24
  export default Toggle;