rift-ds 1.22.0 → 1.23.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 (104) hide show
  1. package/README.md +3 -3
  2. package/behaviors/chat-card.css +135 -0
  3. package/chat/AssistantTurn.js +9 -3
  4. package/chat/ChatLauncher.js +3 -5
  5. package/chat/ChatProvider.d.ts +10 -1
  6. package/chat/ChatProvider.js +3 -2
  7. package/chat/ChatWidget.css +12 -0
  8. package/chat/ChatWidget.d.ts +27 -4
  9. package/chat/ChatWidget.js +73 -37
  10. package/chat/DemoWelcome.js +2 -2
  11. package/chat/actions.d.ts +105 -0
  12. package/chat/actions.js +47 -0
  13. package/chat/demo-card-components.d.ts +3 -4
  14. package/chat/demo-script.js +15 -15
  15. package/chat/generatedStarters.d.ts +19 -0
  16. package/chat/generatedStarters.js +49 -0
  17. package/chat/index.d.ts +7 -3
  18. package/chat/index.js +25 -2
  19. package/chat/placement.d.ts +9 -0
  20. package/chat/placement.js +7 -0
  21. package/chat/queries.d.ts +6 -0
  22. package/chat/queries.js +25 -0
  23. package/chat/questions.d.ts +1 -1
  24. package/chat/questions.js +1 -1
  25. package/chat/registry.json.d.ts +4 -2
  26. package/chat/registry.json.js +1 -1
  27. package/chat/selection.css +8 -0
  28. package/chat/selection.d.ts +40 -0
  29. package/chat/selection.js +140 -0
  30. package/chat/selectionContext.d.ts +70 -0
  31. package/chat/selectionContext.js +60 -0
  32. package/chat/server.d.ts +54 -1
  33. package/chat/server.js +101 -5
  34. package/chat/suggestions.d.ts +12 -8
  35. package/chat/suggestions.js +1 -1
  36. package/chat/types.d.ts +2 -0
  37. package/chat/useChat.js +5 -3
  38. package/components/ActionCard/ActionCard.css +147 -0
  39. package/components/ActionCard/ActionCard.d.ts +54 -0
  40. package/components/ActionCard/ActionCard.js +75 -0
  41. package/components/ActionReadout/ActionReadout.css +47 -0
  42. package/components/ActionReadout/ActionReadout.d.ts +29 -0
  43. package/components/ActionReadout/ActionReadout.js +22 -0
  44. package/components/AgentPlan/AgentPlan.css +18 -8
  45. package/components/AgentPlan/AgentPlan.js +1 -0
  46. package/components/AppSidebar/AppSidebar.css +11 -5
  47. package/components/AppSidebar/AppSidebar.d.ts +2 -0
  48. package/components/AppSidebar/AppSidebar.js +13 -1
  49. package/components/ArtifactCard/ArtifactCard.css +11 -10
  50. package/components/ArtifactCard/ArtifactCard.js +3 -1
  51. package/components/ChatThread/ChatThread.css +6 -3
  52. package/components/Chip/Chip.css +9 -1
  53. package/components/CodeBlock/CodeBlock.css +2 -0
  54. package/components/CodeDiff/CodeDiff.css +18 -8
  55. package/components/CodeDiff/CodeDiff.js +1 -0
  56. package/components/Composer/Composer.css +52 -13
  57. package/components/Composer/Composer.d.ts +9 -1
  58. package/components/Composer/Composer.js +19 -1
  59. package/components/DataTable/FilterEditor.js +4 -2
  60. package/components/DataTable/filterModel.d.ts +8 -0
  61. package/components/DataTable/filterModel.js +4 -0
  62. package/components/DataTable/views.js +2 -2
  63. package/components/DocumentChip/DocumentChip.css +27 -3
  64. package/components/EntitySwitcher/EntitySwitcher.css +333 -0
  65. package/components/EntitySwitcher/EntitySwitcher.d.ts +74 -0
  66. package/components/EntitySwitcher/EntitySwitcher.js +291 -0
  67. package/components/InterruptCard/InterruptCard.css +96 -34
  68. package/components/InterruptCard/InterruptCard.d.ts +20 -3
  69. package/components/InterruptCard/InterruptCard.js +23 -8
  70. package/components/MessageCard/MessageCard.css +27 -18
  71. package/components/MessageCard/MessageCard.js +1 -0
  72. package/components/PromptSuggestions/PromptSuggestions.css +3 -0
  73. package/components/Prose/Prose.css +2 -0
  74. package/components/QuestionCard/QuestionCard.css +9 -5
  75. package/components/QuestionCard/QuestionCard.js +1 -0
  76. package/components/SourceTrail/SourceTrail.css +33 -11
  77. package/components/SourceTrail/SourceTrail.js +3 -2
  78. package/components/TableToolbar/TableToolbar.css +4 -1
  79. package/components/TableToolbar/TableToolbar.js +1 -1
  80. package/components/ToolCall/ToolCall.css +89 -22
  81. package/components/ToolCall/ToolCall.js +2 -1
  82. package/components/UsageCard/UsageCard.css +29 -2
  83. package/components/UsageCard/UsageCard.js +1 -0
  84. package/components/registry.json +24 -0
  85. package/components/registry.json.d.ts +24 -0
  86. package/components/registry.json.js +1 -1
  87. package/fonts/presets/bricolage-grotesque-300-800-latin-ext.woff2 +0 -0
  88. package/fonts/presets/bricolage-grotesque-300-800-latin.woff2 +0 -0
  89. package/fonts/presets/figtree-300-800-latin-ext.woff2 +0 -0
  90. package/fonts/presets/figtree-300-800-latin.woff2 +0 -0
  91. package/index.d.ts +3 -0
  92. package/index.js +6 -0
  93. package/package.json +5 -1
  94. package/tokens/motion.d.ts +2 -0
  95. package/tokens/motion.js +2 -0
  96. package/tokens/presets/plum.css +419 -0
  97. package/tokens/presets/presets.css +1 -0
  98. package/tokens/registry.json +2 -0
  99. package/tokens/registry.json.d.ts +2 -0
  100. package/tokens/registry.json.js +1 -1
  101. package/tokens/tokens-dark.css +4 -0
  102. package/tokens/tokens-light.css +4 -0
  103. package/tokens/tokens-typography.css +8 -2
  104. package/tokens/values.json.js +1 -1
@@ -0,0 +1,333 @@
1
+ /* ============================================
2
+ ENTITY SWITCHER
3
+ An entity's mark, name and description over a panel to switch it. The
4
+ panel rides Popover's menu surface; its rows are overlay rows (no fill
5
+ at rest, the passive wash on hover and focus at --radius-200).
6
+ ============================================ */
7
+
8
+ .ds-entity-switcher {
9
+ display: flex;
10
+ width: 100%;
11
+ min-width: 0;
12
+ }
13
+
14
+ .ds-entity-switcher > .ds-popover__trigger {
15
+ display: flex;
16
+ width: 100%;
17
+ min-width: 0;
18
+ }
19
+
20
+ /* The panel takes the trigger's width, measured on open, so it hangs square
21
+ under it. */
22
+ .ds-entity-switcher > .ds-popover__panel {
23
+ box-sizing: border-box;
24
+ width: var(--ds-entity-switcher-width, auto);
25
+ min-width: 0;
26
+ }
27
+
28
+ /* ============================================
29
+ TRIGGER
30
+ A card that is a control: the container fill and hairline, the passive
31
+ wash on hover. The edge is an inset shadow rather than a border so it
32
+ takes no room, and the mark starts one --padding-300 in: inside a rail
33
+ held at the nav rows' inset, that seats it on the collapsed rail's axis.
34
+ ============================================ */
35
+
36
+ .ds-entity-switcher__trigger {
37
+ display: flex;
38
+ align-items: center;
39
+ gap: var(--gap-300);
40
+ width: 100%;
41
+ min-width: 0;
42
+ padding: var(--padding-200) var(--padding-200) var(--padding-200) var(--padding-300);
43
+ border: 0;
44
+ border-radius: var(--radius-300);
45
+ box-shadow: inset 0 0 0 var(--border-025) var(--color-bg-container-border);
46
+ background-color: var(--color-bg-container-primary);
47
+ color: var(--color-text-primary);
48
+ font: inherit;
49
+ text-align: start;
50
+ cursor: pointer;
51
+ transition:
52
+ background-color var(--motion-duration-base) var(--motion-ease-standard),
53
+ box-shadow var(--motion-duration-base) var(--motion-ease-standard);
54
+ }
55
+
56
+ .ds-entity-switcher__trigger:hover,
57
+ .ds-entity-switcher__trigger[aria-expanded='true'] {
58
+ background-color: var(--color-action-passive-bg-hover);
59
+ }
60
+
61
+ .ds-entity-switcher__trigger:focus-visible {
62
+ outline: var(--border-050) solid var(--color-action-primary-bg);
63
+ outline-offset: var(--border-050);
64
+ }
65
+
66
+ .ds-entity-switcher__trigger .ds-entity-switcher__text,
67
+ .ds-entity-switcher__chevron {
68
+ transition: opacity var(--motion-duration-base) var(--motion-ease-standard);
69
+ }
70
+
71
+ .ds-entity-switcher__chevron {
72
+ flex-shrink: 0;
73
+ color: var(--color-text-secondary);
74
+ --icon-size: var(--icon-size-500);
75
+ }
76
+
77
+ /* ============================================
78
+ COLLAPSED
79
+ The mark alone: the box and the words fade while the mark holds its
80
+ seat. Set by `collapsed`, or by a host rail marked
81
+ data-expanded="false" unless `collapsed={false}` opts out.
82
+ ============================================ */
83
+
84
+ .ds-entity-switcher--collapsed .ds-entity-switcher__trigger,
85
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__trigger {
86
+ background-color: transparent;
87
+ box-shadow: none;
88
+ }
89
+
90
+ .ds-entity-switcher--collapsed .ds-entity-switcher__trigger .ds-entity-switcher__text,
91
+ .ds-entity-switcher--collapsed .ds-entity-switcher__chevron,
92
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__trigger .ds-entity-switcher__text,
93
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__chevron {
94
+ opacity: 0;
95
+ }
96
+
97
+ /* Collapsed, the mark's own tile carries hover and the focus ring. */
98
+ .ds-entity-switcher--collapsed .ds-entity-switcher__trigger:hover .ds-entity-switcher__mark,
99
+ .ds-entity-switcher--collapsed .ds-entity-switcher__trigger[aria-expanded='true'] .ds-entity-switcher__mark,
100
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__trigger:hover .ds-entity-switcher__mark,
101
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__trigger[aria-expanded='true'] .ds-entity-switcher__mark {
102
+ background-color: var(--color-action-passive-bg-hover);
103
+ }
104
+
105
+ .ds-entity-switcher--collapsed .ds-entity-switcher__trigger:focus-visible,
106
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__trigger:focus-visible {
107
+ outline: none;
108
+ }
109
+
110
+ .ds-entity-switcher--collapsed .ds-entity-switcher__trigger:focus-visible .ds-entity-switcher__mark,
111
+ :where([data-expanded='false']) .ds-entity-switcher:not(.ds-entity-switcher--expanded) .ds-entity-switcher__trigger:focus-visible .ds-entity-switcher__mark {
112
+ outline: var(--border-050) solid var(--color-action-primary-bg);
113
+ outline-offset: var(--border-050);
114
+ }
115
+
116
+ /* Opened beside the mark, the panel takes an expanded rail's width. */
117
+ .ds-entity-switcher--beside > .ds-popover__panel {
118
+ width: calc(var(--control-height-default) * 6.4);
119
+ }
120
+
121
+ /* ============================================
122
+ MARK
123
+ A tile, the inner step of the trigger's corner. A logo fills it, an
124
+ icon or an inline mark sits inside it, initials stand in for both.
125
+ ============================================ */
126
+
127
+ .ds-entity-switcher__mark {
128
+ display: grid;
129
+ place-items: center;
130
+ flex-shrink: 0;
131
+ width: var(--icon-size-600);
132
+ height: var(--icon-size-600);
133
+ overflow: hidden;
134
+ border-radius: var(--radius-200);
135
+ background-color: var(--color-bg-container-secondary);
136
+ color: var(--color-text-secondary);
137
+ transition: background-color var(--motion-duration-base) var(--motion-ease-standard);
138
+ }
139
+
140
+ /* An entity's `fill`: one of Avatar's six, the same token pair, on the initials tile only. */
141
+ .ds-entity-switcher__mark--fill-1 {
142
+ background-color: var(--color-avatar-bg-1);
143
+ color: var(--color-avatar-text-1);
144
+ }
145
+
146
+ .ds-entity-switcher__mark--fill-2 {
147
+ background-color: var(--color-avatar-bg-2);
148
+ color: var(--color-avatar-text-2);
149
+ }
150
+
151
+ .ds-entity-switcher__mark--fill-3 {
152
+ background-color: var(--color-avatar-bg-3);
153
+ color: var(--color-avatar-text-3);
154
+ }
155
+
156
+ .ds-entity-switcher__mark--fill-4 {
157
+ background-color: var(--color-avatar-bg-4);
158
+ color: var(--color-avatar-text-4);
159
+ }
160
+
161
+ .ds-entity-switcher__mark--fill-5 {
162
+ background-color: var(--color-avatar-bg-5);
163
+ color: var(--color-avatar-text-5);
164
+ }
165
+
166
+ .ds-entity-switcher__mark--fill-6 {
167
+ background-color: var(--color-avatar-bg-6);
168
+ color: var(--color-avatar-text-6);
169
+ }
170
+
171
+ .ds-entity-switcher__mark--large {
172
+ width: var(--icon-size-800);
173
+ height: var(--icon-size-800);
174
+ }
175
+
176
+ .ds-entity-switcher__mark-image {
177
+ display: block;
178
+ width: 100%;
179
+ height: 100%;
180
+ object-fit: cover;
181
+ }
182
+
183
+ .ds-entity-switcher__mark-icon {
184
+ --icon-size: var(--icon-size-500);
185
+ }
186
+
187
+ /* An inline mark (an SVG logo) sits at most the icon step inside the tile. */
188
+ .ds-entity-switcher__mark-node {
189
+ display: grid;
190
+ place-items: center;
191
+ width: 62%;
192
+ height: 62%;
193
+ }
194
+
195
+ .ds-entity-switcher__mark-node > svg,
196
+ .ds-entity-switcher__mark-node > img {
197
+ width: 100%;
198
+ height: 100%;
199
+ object-fit: contain;
200
+ }
201
+
202
+ .ds-entity-switcher__mark-initials {
203
+ font-family: var(--font-caption-family);
204
+ font-size: var(--font-caption-size);
205
+ font-weight: var(--font-paragraph-sm-emphasis-weight);
206
+ line-height: 1;
207
+ }
208
+
209
+ /* ============================================
210
+ TEXT
211
+ One line each, truncated, never wrapped.
212
+ ============================================ */
213
+
214
+ .ds-entity-switcher__text {
215
+ display: flex;
216
+ flex: 1;
217
+ flex-direction: column;
218
+ min-width: 0;
219
+ }
220
+
221
+ .ds-entity-switcher__title,
222
+ .ds-entity-switcher__name {
223
+ overflow: hidden;
224
+ text-overflow: ellipsis;
225
+ white-space: nowrap;
226
+ font-family: var(--font-paragraph-sm-emphasis-family);
227
+ font-size: var(--font-paragraph-sm-emphasis-size);
228
+ font-weight: var(--font-paragraph-sm-emphasis-weight);
229
+ line-height: var(--font-paragraph-sm-emphasis-line-height);
230
+ }
231
+
232
+ .ds-entity-switcher__description {
233
+ overflow: hidden;
234
+ text-overflow: ellipsis;
235
+ white-space: nowrap;
236
+ color: var(--color-text-secondary);
237
+ font-family: var(--font-caption-family);
238
+ font-size: var(--font-caption-size);
239
+ line-height: var(--font-caption-line-height);
240
+ }
241
+
242
+ /* ============================================
243
+ PANEL
244
+ ============================================ */
245
+
246
+ .ds-entity-switcher__panel {
247
+ display: flex;
248
+ flex-direction: column;
249
+ gap: var(--gap-100);
250
+ min-width: 0;
251
+ }
252
+
253
+ .ds-entity-switcher__search {
254
+ min-width: 0;
255
+ padding: var(--padding-100);
256
+ }
257
+
258
+ /* About eight rows before the list scrolls. */
259
+ .ds-entity-switcher__list {
260
+ display: flex;
261
+ flex-direction: column;
262
+ max-height: calc(var(--control-height-default) * 9);
263
+ overflow-y: auto;
264
+ }
265
+
266
+ .ds-entity-switcher__row {
267
+ display: flex;
268
+ align-items: center;
269
+ gap: var(--gap-300);
270
+ width: 100%;
271
+ min-width: 0;
272
+ padding: var(--padding-150) var(--padding-200);
273
+ border: 0;
274
+ border-radius: var(--radius-200);
275
+ background: transparent;
276
+ color: var(--color-text-primary);
277
+ font: inherit;
278
+ text-align: start;
279
+ cursor: pointer;
280
+ transition: background-color var(--motion-duration-fast) var(--motion-ease-standard);
281
+ }
282
+
283
+ .ds-entity-switcher__row:hover,
284
+ .ds-entity-switcher__row:focus-visible {
285
+ outline: none;
286
+ background-color: var(--color-action-passive-bg-hover);
287
+ }
288
+
289
+ .ds-entity-switcher__row--disabled {
290
+ opacity: 0.4;
291
+ cursor: not-allowed;
292
+ }
293
+
294
+ .ds-entity-switcher__row--disabled:hover {
295
+ background-color: transparent;
296
+ }
297
+
298
+ .ds-entity-switcher__trailing {
299
+ flex-shrink: 0;
300
+ color: var(--color-text-secondary);
301
+ --icon-size: var(--icon-size-500);
302
+ }
303
+
304
+ .ds-entity-switcher__row--back {
305
+ color: var(--color-text-secondary);
306
+ }
307
+
308
+ .ds-entity-switcher__row--back .ds-entity-switcher__name {
309
+ font-weight: var(--font-paragraph-sm-weight);
310
+ }
311
+
312
+ .ds-entity-switcher__back-icon {
313
+ display: grid;
314
+ place-items: center;
315
+ flex-shrink: 0;
316
+ width: var(--icon-size-600);
317
+ --icon-size: var(--icon-size-500);
318
+ }
319
+
320
+ .ds-entity-switcher__separator {
321
+ height: var(--border-025);
322
+ margin: var(--padding-100) var(--padding-200);
323
+ background-color: var(--color-bg-container-border);
324
+ }
325
+
326
+ .ds-entity-switcher__empty {
327
+ margin: 0;
328
+ padding: var(--padding-300) var(--padding-200);
329
+ color: var(--color-text-secondary);
330
+ font-family: var(--font-paragraph-sm-family);
331
+ font-size: var(--font-paragraph-sm-size);
332
+ line-height: var(--font-paragraph-sm-line-height);
333
+ }
@@ -0,0 +1,74 @@
1
+ import { default as React } from 'react';
2
+ import { AvatarFill } from '../Avatar/avatarFill';
3
+ /** One entity in the tree: a company, a business unit, a team, an advertiser, an account. */
4
+ export interface EntitySwitcherItem {
5
+ /** Unique across the whole tree; the value `value` and `onValueChange` carry. */
6
+ id: string;
7
+ /** The entity's name: the trigger's title and the row's label. */
8
+ name: string;
9
+ /** One line under the name. The trigger falls back to the entity's path when it is omitted. */
10
+ description?: string;
11
+ /** A logo or photo, by image URL, filling the mark's tile. Wins over `icon`. Decorative: the name carries the entity. */
12
+ logo?: string;
13
+ /** The mark when there is no `logo`: a Material Symbol name (string) or any element (ReactNode), such as an inline SVG logo. Initials stand in when both are omitted. */
14
+ icon?: string | React.ReactNode;
15
+ /** Which of Avatar's six fills the initials tile takes, so entities read apart at a glance. Only initials take it; a logo or icon keeps the plain tile. Plain when omitted. */
16
+ fill?: AvatarFill;
17
+ /** The entities under this one. Any child anywhere in the tree makes the list drill in. */
18
+ children?: EntitySwitcherItem[];
19
+ /** Whether the entity is shown but cannot be picked or opened. */
20
+ disabled?: boolean;
21
+ }
22
+ /** Props owned by EntitySwitcher itself; everything else falls through to the root `<div>`. */
23
+ type EntitySwitcherOwnProps = {
24
+ /** The top level of the tree. Its shape decides the list: all leaves is one flat list, any children make it drill in. */
25
+ items: EntitySwitcherItem[];
26
+ /** The selected entity's id (controlled). */
27
+ value?: string;
28
+ /** The entity selected at first (uncontrolled). The first entity when omitted. */
29
+ defaultValue?: string;
30
+ /** Called with the id and the entity when one is picked. */
31
+ onValueChange?: (id: string, item: EntitySwitcherItem) => void;
32
+ /** Whether the panel is open (controlled). */
33
+ open?: boolean;
34
+ /** Whether the panel starts open (uncontrolled). */
35
+ defaultOpen?: boolean;
36
+ /** Called when the panel opens or closes. */
37
+ onOpenChange?: (open: boolean) => void;
38
+ /**
39
+ * The mark alone, the rest of the trigger faded out, and the panel opening
40
+ * beside it rather than below: the form for a collapsed rail. The mark
41
+ * never moves between the two forms. Left unset, it follows a host rail
42
+ * that marks itself `data-expanded="false"` (AppSidebar's
43
+ * `persistentTopSlot`).
44
+ */
45
+ collapsed?: boolean;
46
+ /** How many entities, across every level, before the search field appears. */
47
+ searchThreshold?: number;
48
+ /** Accessible name of the trigger and the panel; the selected entity's name follows it on the trigger. */
49
+ switcherLabel?: string;
50
+ /** Placeholder and accessible name of the search field. */
51
+ searchLabel?: string;
52
+ /** The row that climbs out of a level. `{name}` is the level above. */
53
+ backLabel?: string;
54
+ /** Stands in for `{name}` in `backLabel` when the level above is the top. */
55
+ topLevelLabel?: string;
56
+ /** The description of the row that picks a whole branch, leading its level. `{name}` is the branch. */
57
+ wholeBranchLabel?: string;
58
+ /** Shown when a search finds nothing. */
59
+ emptyLabel?: string;
60
+ /** Additional CSS classes on the root. */
61
+ className?: string;
62
+ };
63
+ export interface EntitySwitcherProps extends EntitySwitcherOwnProps, Omit<React.ComponentPropsWithRef<'div'>, keyof EntitySwitcherOwnProps | 'children'> {
64
+ }
65
+ /**
66
+ * EntitySwitcher: the "which one am I looking at" control, an entity's mark,
67
+ * name and description over a panel to switch it. The data shapes the panel:
68
+ * a list of leaves is one flat list, and any nesting makes it drill in, each
69
+ * level led by its parent so a whole branch can be picked, with a back row
70
+ * to climb out and a search across every level once the tree is large.
71
+ * Forwards a ref to the root div and spreads native attributes onto it.
72
+ */
73
+ export declare function EntitySwitcher({ items, value, defaultValue, onValueChange, open: openProp, defaultOpen, onOpenChange, collapsed: collapsedProp, searchThreshold, switcherLabel, searchLabel, backLabel, topLevelLabel, wholeBranchLabel, emptyLabel, className, style, ref, ...rest }: EntitySwitcherProps): React.JSX.Element | null;
74
+ export {};
@@ -0,0 +1,291 @@
1
+ "use client";
2
+ import { jsxs, jsx, Fragment } from "react/jsx-runtime";
3
+ import React from "react";
4
+ import { Popover } from "../Popover/Popover.js";
5
+ import { Input } from "../Input/Input.js";
6
+ import { ChosenMark } from "../Dropdown/Dropdown.js";
7
+ import { logicalArrowKey, mirrorClass } from "../../behaviors/direction.js";
8
+ import "./EntitySwitcher.css";
9
+ import "../../behaviors/direction.css";
10
+ import "../../fonts/material-symbols.css";
11
+ function flatten(items, path = []) {
12
+ return items.flatMap((item) => [
13
+ { item, path },
14
+ ...flatten(item.children ?? [], [...path, item])
15
+ ]);
16
+ }
17
+ function initials(name) {
18
+ const words = name.trim().split(/\s+/);
19
+ const letters = words.length > 1 ? words[0][0] + words[1][0] : name.trim().slice(0, 2);
20
+ return letters.toUpperCase();
21
+ }
22
+ function Mark({ item, large = false }) {
23
+ const filled = item.fill && !item.logo && !item.icon;
24
+ const className = [
25
+ "ds-entity-switcher__mark",
26
+ large ? "ds-entity-switcher__mark--large" : "",
27
+ filled ? `ds-entity-switcher__mark--fill-${item.fill}` : ""
28
+ ].filter(Boolean).join(" ");
29
+ let face;
30
+ if (item.logo) {
31
+ face = /* @__PURE__ */ jsx("img", { className: "ds-entity-switcher__mark-image", src: item.logo, alt: "" });
32
+ } else if (typeof item.icon === "string") {
33
+ face = /* @__PURE__ */ jsx(
34
+ "span",
35
+ {
36
+ className: ["material-symbols-rounded", mirrorClass(item.icon), "ds-entity-switcher__mark-icon"].filter(Boolean).join(" "),
37
+ children: item.icon
38
+ }
39
+ );
40
+ } else if (item.icon) {
41
+ face = /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__mark-node", children: item.icon });
42
+ } else {
43
+ face = /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__mark-initials", children: initials(item.name) });
44
+ }
45
+ return /* @__PURE__ */ jsx("span", { className, "aria-hidden": "true", children: face });
46
+ }
47
+ const fill = (template, name) => template.replace("{name}", name);
48
+ function EntitySwitcher({
49
+ items,
50
+ value,
51
+ defaultValue,
52
+ onValueChange,
53
+ open: openProp,
54
+ defaultOpen = false,
55
+ onOpenChange,
56
+ collapsed: collapsedProp,
57
+ searchThreshold = 8,
58
+ switcherLabel = "Switch entity",
59
+ searchLabel = "Search",
60
+ backLabel = "Back to {name}",
61
+ topLevelLabel = "all",
62
+ wholeBranchLabel = "All of {name}",
63
+ emptyLabel = "Nothing matches",
64
+ className = "",
65
+ style,
66
+ ref,
67
+ ...rest
68
+ }) {
69
+ const all = React.useMemo(() => flatten(items), [items]);
70
+ const byId = React.useMemo(() => new Map(all.map((l) => [l.item.id, l])), [all]);
71
+ const searchable = all.length > searchThreshold;
72
+ const [ownValue, setOwnValue] = React.useState(defaultValue);
73
+ const selectedId = value ?? ownValue;
74
+ const selected = (selectedId ? byId.get(selectedId) : void 0) ?? all[0];
75
+ const [ownOpen, setOwnOpen] = React.useState(defaultOpen);
76
+ const open = openProp ?? ownOpen;
77
+ const [query, setQuery] = React.useState("");
78
+ const [trail, setTrail] = React.useState([]);
79
+ const [width, setWidth] = React.useState();
80
+ const [beside, setBeside] = React.useState(false);
81
+ const rootRef = React.useRef(null);
82
+ const listRef = React.useRef(null);
83
+ const setRootRef = (node) => {
84
+ rootRef.current = node;
85
+ if (typeof ref === "function") ref(node);
86
+ else if (ref) ref.current = node;
87
+ };
88
+ const setOpen = (next) => {
89
+ if (next) {
90
+ const s = selected;
91
+ setTrail(s ? s.item.children?.length ? [...s.path, s.item] : s.path : []);
92
+ setQuery("");
93
+ const isCollapsed = collapsedProp ?? !!rootRef.current?.closest('[data-expanded="false"]');
94
+ setBeside(isCollapsed);
95
+ setWidth(isCollapsed ? void 0 : rootRef.current?.getBoundingClientRect().width);
96
+ }
97
+ if (openProp === void 0) setOwnOpen(next);
98
+ onOpenChange?.(next);
99
+ };
100
+ const pick = (item) => {
101
+ if (value === void 0) setOwnValue(item.id);
102
+ onValueChange?.(item.id, item);
103
+ setOpen(false);
104
+ };
105
+ const focusFirstRow = () => requestAnimationFrame(
106
+ () => listRef.current?.querySelector('[data-row]:not([aria-disabled="true"])')?.focus()
107
+ );
108
+ const drill = (next) => {
109
+ setTrail(next);
110
+ focusFirstRow();
111
+ };
112
+ const term = query.trim().toLowerCase();
113
+ const level = trail[trail.length - 1];
114
+ const rows = term ? all.filter((l) => l.item.name.toLowerCase().includes(term)) : (level?.children ?? items).map((item) => ({ item, path: trail }));
115
+ const onListKeyDown = (event) => {
116
+ const rowEls = Array.from(
117
+ listRef.current?.querySelectorAll('[data-row]:not([aria-disabled="true"])') ?? []
118
+ );
119
+ const at = rowEls.indexOf(document.activeElement);
120
+ const key = logicalArrowKey(event.key, event.currentTarget);
121
+ if (key === "ArrowDown" || key === "ArrowUp") {
122
+ event.preventDefault();
123
+ const step = key === "ArrowDown" ? 1 : -1;
124
+ rowEls[(at + step + rowEls.length) % rowEls.length]?.focus();
125
+ } else if (key === "Home" || key === "End") {
126
+ event.preventDefault();
127
+ rowEls[key === "Home" ? 0 : rowEls.length - 1]?.focus();
128
+ } else if (key === "ArrowLeft" && trail.length && !term) {
129
+ event.preventDefault();
130
+ drill(trail.slice(0, -1));
131
+ } else if (key === "ArrowRight" && at >= 0) {
132
+ const target = byId.get(rowEls[at].dataset.drill ?? "");
133
+ if (target) {
134
+ event.preventDefault();
135
+ drill([...target.path, target.item]);
136
+ }
137
+ }
138
+ };
139
+ const shownIds = [
140
+ ...!term && level ? [`lead-${level.id}`] : [],
141
+ ...rows.map((l) => l.item.id)
142
+ ];
143
+ const enabledShown = shownIds.filter((key) => {
144
+ const id = key.replace(/^lead-/, "");
145
+ return !byId.get(id)?.item.disabled;
146
+ });
147
+ const tabStop = enabledShown.find((key) => key.replace(/^lead-/, "") === selected?.item.id) ?? enabledShown[0] ?? (!term && level ? "back" : void 0);
148
+ const row = ({ item, path }, lead = false) => {
149
+ const opens = !lead && !term && !!item.children?.length;
150
+ const isSelected = item.id === selected?.item.id;
151
+ const sub = term ? path.map((p) => p.name).join(" / ") || item.description : lead ? fill(wholeBranchLabel, item.name) : item.description;
152
+ const rowClass = [
153
+ "ds-entity-switcher__row",
154
+ item.disabled ? "ds-entity-switcher__row--disabled" : ""
155
+ ].filter(Boolean).join(" ");
156
+ return /* @__PURE__ */ jsxs(
157
+ "button",
158
+ {
159
+ type: "button",
160
+ role: opens ? "menuitem" : "menuitemradio",
161
+ "aria-checked": opens ? void 0 : isSelected,
162
+ "aria-haspopup": opens ? "menu" : void 0,
163
+ "aria-disabled": item.disabled || void 0,
164
+ tabIndex: tabStop === (lead ? "lead-" : "") + item.id ? 0 : -1,
165
+ "data-row": "",
166
+ "data-drill": opens && !item.disabled ? item.id : void 0,
167
+ className: rowClass,
168
+ onClick: () => {
169
+ if (item.disabled) return;
170
+ if (opens) drill([...path, item]);
171
+ else pick(item);
172
+ },
173
+ children: [
174
+ /* @__PURE__ */ jsx(Mark, { item }),
175
+ /* @__PURE__ */ jsxs("span", { className: "ds-entity-switcher__text", children: [
176
+ /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__name", children: item.name }),
177
+ sub && /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__description", children: sub })
178
+ ] }),
179
+ opens ? /* @__PURE__ */ jsx(
180
+ "span",
181
+ {
182
+ className: "material-symbols-rounded ds-mirror-rtl ds-entity-switcher__trailing",
183
+ "aria-hidden": "true",
184
+ children: "chevron_right"
185
+ }
186
+ ) : isSelected ? /* @__PURE__ */ jsx(ChosenMark, {}) : null
187
+ ]
188
+ },
189
+ (lead ? "lead-" : "") + item.id
190
+ );
191
+ };
192
+ const content = /* @__PURE__ */ jsxs("div", { className: "ds-entity-switcher__panel", children: [
193
+ searchable && /* @__PURE__ */ jsx("div", { className: "ds-entity-switcher__search", children: /* @__PURE__ */ jsx(
194
+ Input,
195
+ {
196
+ type: "search",
197
+ size: "compact",
198
+ iconLeft: "search",
199
+ value: query,
200
+ onValueChange: setQuery,
201
+ placeholder: searchLabel,
202
+ "aria-label": searchLabel,
203
+ onKeyDown: (event) => {
204
+ if (event.key === "ArrowDown") {
205
+ event.preventDefault();
206
+ focusFirstRow();
207
+ }
208
+ }
209
+ }
210
+ ) }),
211
+ /* @__PURE__ */ jsxs(
212
+ "div",
213
+ {
214
+ ref: listRef,
215
+ role: "menu",
216
+ "aria-label": switcherLabel,
217
+ className: "ds-entity-switcher__list",
218
+ onKeyDown: onListKeyDown,
219
+ children: [
220
+ !term && level && /* @__PURE__ */ jsxs(Fragment, { children: [
221
+ /* @__PURE__ */ jsxs(
222
+ "button",
223
+ {
224
+ type: "button",
225
+ role: "menuitem",
226
+ tabIndex: tabStop === "back" ? 0 : -1,
227
+ "data-row": "",
228
+ className: "ds-entity-switcher__row ds-entity-switcher__row--back",
229
+ onClick: () => drill(trail.slice(0, -1)),
230
+ children: [
231
+ /* @__PURE__ */ jsx(
232
+ "span",
233
+ {
234
+ className: "material-symbols-rounded ds-mirror-rtl ds-entity-switcher__back-icon",
235
+ "aria-hidden": "true",
236
+ children: "arrow_back"
237
+ }
238
+ ),
239
+ /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__name", children: fill(backLabel, trail[trail.length - 2]?.name ?? topLevelLabel) })
240
+ ]
241
+ }
242
+ ),
243
+ row({ item: level, path: trail.slice(0, -1) }, true),
244
+ /* @__PURE__ */ jsx("div", { className: "ds-entity-switcher__separator", role: "separator" })
245
+ ] }),
246
+ rows.length ? rows.map((l) => row(l)) : /* @__PURE__ */ jsx("p", { className: "ds-entity-switcher__empty", children: emptyLabel })
247
+ ]
248
+ }
249
+ )
250
+ ] });
251
+ React.useEffect(() => {
252
+ if (open && !searchable) focusFirstRow();
253
+ }, [open, searchable]);
254
+ if (!selected) return null;
255
+ const triggerSub = selected.item.description ?? (selected.path.map((p) => p.name).join(" / ") || void 0);
256
+ const rootClass = [
257
+ "ds-entity-switcher",
258
+ collapsedProp === true ? "ds-entity-switcher--collapsed" : "",
259
+ // An explicit `false` opts out of following a collapsed host rail.
260
+ collapsedProp === false ? "ds-entity-switcher--expanded" : "",
261
+ beside ? "ds-entity-switcher--beside" : "",
262
+ className
263
+ ].filter(Boolean).join(" ");
264
+ return /* @__PURE__ */ jsx(
265
+ Popover,
266
+ {
267
+ ...rest,
268
+ ref: setRootRef,
269
+ className: rootClass,
270
+ style: width ? { ...style, "--ds-entity-switcher-width": `${width}px` } : style,
271
+ content,
272
+ surface: "menu",
273
+ position: beside ? "end" : "bottom",
274
+ align: "start",
275
+ open,
276
+ onOpenChange: setOpen,
277
+ ariaLabel: `${switcherLabel}: ${selected.item.name}`,
278
+ children: /* @__PURE__ */ jsxs("button", { type: "button", className: "ds-entity-switcher__trigger", children: [
279
+ /* @__PURE__ */ jsx(Mark, { item: selected.item, large: true }),
280
+ /* @__PURE__ */ jsxs("span", { className: "ds-entity-switcher__text", children: [
281
+ /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__title", children: selected.item.name }),
282
+ triggerSub && /* @__PURE__ */ jsx("span", { className: "ds-entity-switcher__description", children: triggerSub })
283
+ ] }),
284
+ /* @__PURE__ */ jsx("span", { className: "material-symbols-rounded ds-entity-switcher__chevron", "aria-hidden": "true", children: "unfold_more" })
285
+ ] })
286
+ }
287
+ );
288
+ }
289
+ export {
290
+ EntitySwitcher
291
+ };