@swisper.ai/web-chat-ui 0.0.9 → 0.2.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 (87) hide show
  1. package/README.md +2 -5
  2. package/dist/components/chat-card/chat-card.stories.d.ts.map +1 -1
  3. package/dist/components/chat-card/components/chat-card-header.d.ts.map +1 -1
  4. package/dist/components/chat-card-container/chat-card-container.stories.d.ts.map +1 -1
  5. package/dist/components/chat-thinking-process/chat-thinking-process.d.ts.map +1 -1
  6. package/dist/components/rich-text-field/rich-text-field.stories.d.ts.map +1 -1
  7. package/dist/components/rich-text-field/utils.d.ts +38 -38
  8. package/dist/index.css +1 -1
  9. package/dist/index.d.ts +0 -1
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.es.js +1571 -1813
  12. package/dist/index.es.js.map +1 -1
  13. package/package.json +5 -5
  14. package/src/components/chat-card/chat-card.json +6 -6
  15. package/src/components/chat-card/chat-card.stories.tsx +6 -14
  16. package/src/components/chat-card/components/chat-card-body.tsx +1 -1
  17. package/src/components/chat-card/components/chat-card-header.tsx +3 -7
  18. package/src/components/chat-card-container/chat-card-container.json +6 -6
  19. package/src/components/chat-card-container/chat-card-container.stories.tsx +10 -18
  20. package/src/components/chat-container/chat-container.json +1 -1
  21. package/src/components/chat-thinking-process/chat-thinking-process.tsx +4 -12
  22. package/src/components/rich-text-area/__tests__/rich-text-area.spec.tsx +12 -2
  23. package/src/components/rich-text-field/rich-text-field.stories.tsx +3 -1
  24. package/src/index.ts +0 -1
  25. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.d.ts +0 -97
  26. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.d.ts.map +0 -1
  27. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.stories.d.ts +0 -52
  28. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.stories.d.ts.map +0 -1
  29. package/dist/layouts/chat-card-list-layout/index.d.ts +0 -3
  30. package/dist/layouts/chat-card-list-layout/index.d.ts.map +0 -1
  31. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.d.ts +0 -84
  32. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.d.ts.map +0 -1
  33. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.sizing.d.ts +0 -45
  34. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.sizing.d.ts.map +0 -1
  35. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.stories.d.ts +0 -62
  36. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.stories.d.ts.map +0 -1
  37. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.variants.d.ts +0 -81
  38. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.variants.d.ts.map +0 -1
  39. package/dist/layouts/chat-card-three-column-layout/index.d.ts +0 -5
  40. package/dist/layouts/chat-card-three-column-layout/index.d.ts.map +0 -1
  41. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.d.ts +0 -37
  42. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.d.ts.map +0 -1
  43. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.sizing.d.ts +0 -12
  44. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.sizing.d.ts.map +0 -1
  45. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.stories.d.ts +0 -40
  46. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.stories.d.ts.map +0 -1
  47. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.variants.d.ts +0 -35
  48. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.variants.d.ts.map +0 -1
  49. package/dist/layouts/chat-card-two-column-layout/index.d.ts +0 -4
  50. package/dist/layouts/chat-card-two-column-layout/index.d.ts.map +0 -1
  51. package/dist/layouts/index.d.ts +0 -4
  52. package/dist/layouts/index.d.ts.map +0 -1
  53. package/src/layouts/chat-card-list-layout/__stories__/fixtures.ts +0 -112
  54. package/src/layouts/chat-card-list-layout/__stories__/result-card.tsx +0 -36
  55. package/src/layouts/chat-card-list-layout/__stories__/types.ts +0 -31
  56. package/src/layouts/chat-card-list-layout/__tests__/chat-card-list-layout.spec.tsx +0 -204
  57. package/src/layouts/chat-card-list-layout/chat-card-list-layout.json +0 -129
  58. package/src/layouts/chat-card-list-layout/chat-card-list-layout.stories.tsx +0 -422
  59. package/src/layouts/chat-card-list-layout/chat-card-list-layout.tsx +0 -254
  60. package/src/layouts/chat-card-list-layout/index.ts +0 -11
  61. package/src/layouts/chat-card-three-column-layout/__stories__/fixtures.ts +0 -117
  62. package/src/layouts/chat-card-three-column-layout/__stories__/resizable-chat-window.tsx +0 -18
  63. package/src/layouts/chat-card-three-column-layout/__stories__/stay-details.tsx +0 -85
  64. package/src/layouts/chat-card-three-column-layout/__stories__/stay-list.tsx +0 -50
  65. package/src/layouts/chat-card-three-column-layout/__stories__/stay-map.tsx +0 -44
  66. package/src/layouts/chat-card-three-column-layout/__stories__/stay-rooms.tsx +0 -61
  67. package/src/layouts/chat-card-three-column-layout/__stories__/types.ts +0 -35
  68. package/src/layouts/chat-card-three-column-layout/__tests__/chat-card-three-column-layout.sizing.test.ts +0 -73
  69. package/src/layouts/chat-card-three-column-layout/__tests__/chat-card-three-column-layout.spec.tsx +0 -282
  70. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.json +0 -147
  71. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.sizing.ts +0 -78
  72. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.stories.tsx +0 -694
  73. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.tsx +0 -291
  74. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.variants.ts +0 -135
  75. package/src/layouts/chat-card-three-column-layout/index.ts +0 -24
  76. package/src/layouts/chat-card-two-column-layout/__stories__/connection-itinerary.tsx +0 -51
  77. package/src/layouts/chat-card-two-column-layout/__stories__/fixtures.ts +0 -83
  78. package/src/layouts/chat-card-two-column-layout/__stories__/route-map.tsx +0 -24
  79. package/src/layouts/chat-card-two-column-layout/__stories__/types.ts +0 -15
  80. package/src/layouts/chat-card-two-column-layout/__tests__/chat-card-two-column-layout.spec.tsx +0 -109
  81. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.json +0 -109
  82. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.sizing.ts +0 -21
  83. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.stories.tsx +0 -439
  84. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.tsx +0 -141
  85. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.variants.ts +0 -65
  86. package/src/layouts/chat-card-two-column-layout/index.ts +0 -17
  87. package/src/layouts/index.ts +0 -3
@@ -1,422 +0,0 @@
1
- import type { Meta, StoryObj } from '@storybook/react-vite';
2
- import { expect, fn } from 'storybook/test';
3
-
4
- import type { ButtonProps } from '@swisper.ai/ui';
5
-
6
- import { ParentBlock } from '../../__storybook__/parent-block';
7
- import { ChatCardContainer } from '../../components/chat-card-container';
8
- import { ChatContainer } from '../../components/chat-container';
9
- import { ChatMessage, ChatMessageContent } from '../../components/chat-message';
10
-
11
- import { CALENDAR_EVENTS, EMAILS, TRAIN_CONNECTIONS } from './__stories__/fixtures';
12
- import { ResultCard } from './__stories__/result-card';
13
- import {
14
- ChatCardListLayout,
15
- ChatCardListLayoutItems,
16
- ChatCardListLayoutShowMore,
17
- useChatCardListLayoutContext,
18
- } from './chat-card-list-layout';
19
- import type { ChatCardListLayoutShowMoreProps } from './chat-card-list-layout';
20
-
21
- const meta: Meta<typeof ChatCardListLayout> = {
22
- title: 'Chat/ChatCardListLayout',
23
- component: ChatCardListLayout,
24
- tags: ['autodocs'],
25
- parameters: { layout: 'padded' },
26
- decorators: [
27
- (Story) => (
28
- <ChatContainer className="h-auto py-16">
29
- <Story />
30
- </ChatContainer>
31
- ),
32
- ],
33
- argTypes: {
34
- visibleCount: {
35
- control: { type: 'number', min: 0 },
36
- description:
37
- 'How many items `ChatCardListLayoutItems` renders while collapsed. Negative values are clamped to `0`.',
38
- table: { defaultValue: { summary: '3' } },
39
- },
40
- defaultExpanded: {
41
- control: 'boolean',
42
- description:
43
- 'Initial expanded state when uncontrolled, e.g. for a message restored from history that the reader had already expanded.',
44
- table: { defaultValue: { summary: 'false' } },
45
- },
46
- expanded: {
47
- control: 'boolean',
48
- description:
49
- 'Controlled expanded state. Only pass it when something outside owns the state; setting it back to `false` re-collapses the list.',
50
- },
51
- onExpandedChange: {
52
- description: 'Called with `true` when the show-more button is clicked.',
53
- },
54
- asChild: {
55
- control: false,
56
- description: 'Renders the single child element as the layout root instead of a `<div>`.',
57
- table: { defaultValue: { summary: 'false' } },
58
- },
59
- children: {
60
- control: false,
61
- description:
62
- 'A `ChatCardListLayoutItems` with the cards, then a `ChatCardListLayoutShowMore`.',
63
- },
64
- className: {
65
- control: 'text',
66
- description:
67
- 'Direction, gap and alignment of the list, merged last. Without it the cards stack in a left-aligned column with an 8px gap.',
68
- },
69
- },
70
- args: {
71
- visibleCount: 3,
72
- defaultExpanded: false,
73
- asChild: false,
74
- onExpandedChange: fn(),
75
- },
76
- };
77
-
78
- export default meta;
79
- type Story = StoryObj<typeof ChatCardListLayout>;
80
-
81
- /**
82
- * Six emails, three shown. The other three are not in the DOM until the button is clicked, and
83
- * the button removes itself once they are.
84
- */
85
- export const Default: Story = {
86
- render: (args) => (
87
- <ChatCardContainer variant="fixed">
88
- <ChatCardListLayout {...args}>
89
- <ChatCardListLayoutItems>
90
- {EMAILS.map((email) => (
91
- <ResultCard
92
- key={email.id}
93
- title={email.subject}
94
- subTitle={`${email.from}, ${email.date}`}
95
- >
96
- {email.preview}
97
- </ResultCard>
98
- ))}
99
- </ChatCardListLayoutItems>
100
- <ChatCardListLayoutShowMore>Show more</ChatCardListLayoutShowMore>
101
- </ChatCardListLayout>
102
- </ChatCardContainer>
103
- ),
104
- };
105
-
106
- /** Checks that only the visible slice mounts, and that the button reveals the rest from the keyboard and then removes itself. */
107
- export const ShowMoreRevealsCards: Story = {
108
- ...Default,
109
- tags: ['e2e', '!autodocs'],
110
- play: async ({ args, canvas, userEvent, step }) => {
111
- const showMore = canvas.getByRole('button', { name: 'Show more' });
112
- // TODO(echo-ds): the root is a plain div with no role or name, so it is found by its slot.
113
- const root = showMore.closest<HTMLElement>('[data-slot="chat-card-list-layout"]');
114
-
115
- await step('Mounts only the first three cards', async () => {
116
- await expect(canvas.getByText(EMAILS[2].subject)).toBeVisible();
117
- await expect(canvas.queryByText(EMAILS[3].subject)).not.toBeInTheDocument();
118
- await expect(root).toHaveAttribute('data-expanded', 'false');
119
- await expect(showMore).toHaveAttribute('data-slot', 'chat-card-list-layout-show-more');
120
- });
121
-
122
- await step('Reveals the rest with Enter and removes the button', async () => {
123
- showMore.focus();
124
- await userEvent.keyboard('{Enter}');
125
-
126
- await expect(canvas.getByText(EMAILS[5].subject)).toBeVisible();
127
- await expect(canvas.queryByRole('button', { name: 'Show more' })).not.toBeInTheDocument();
128
- await expect(root).toHaveAttribute('data-expanded', 'true');
129
- await expect(args.onExpandedChange).toHaveBeenCalledWith(true);
130
- });
131
- },
132
- };
133
-
134
- /**
135
- * Two events under the default limit of three. The button is never rendered, so a short list
136
- * carries no dead affordance.
137
- */
138
- export const ShorterThanLimit: Story = {
139
- parameters: { controls: { disable: true } },
140
- render: () => (
141
- <ChatCardContainer>
142
- <ChatCardListLayout>
143
- <ChatCardListLayoutItems>
144
- {CALENDAR_EVENTS.map((event) => (
145
- <ResultCard key={event.id} title={event.title} subTitle={event.time}>
146
- {event.location}
147
- </ResultCard>
148
- ))}
149
- </ChatCardListLayoutItems>
150
- <ChatCardListLayoutShowMore>Show more</ChatCardListLayoutShowMore>
151
- </ChatCardListLayout>
152
- </ChatCardContainer>
153
- ),
154
- };
155
-
156
- /** Checks that a list shorter than the limit mounts every card and no button. */
157
- export const ShorterThanLimitHasNoButton: Story = {
158
- ...ShorterThanLimit,
159
- tags: ['e2e', '!autodocs'],
160
- play: async ({ canvas }) => {
161
- await expect(canvas.getByText(CALENDAR_EVENTS[0].title)).toBeVisible();
162
- await expect(canvas.getByText(CALENDAR_EVENTS[1].title)).toBeVisible();
163
- await expect(canvas.queryByRole('button', { name: 'Show more' })).not.toBeInTheDocument();
164
- },
165
- };
166
-
167
- /**
168
- * A list that starts expanded, as in a message restored from history that the reader had already
169
- * expanded: every card is mounted and there is no button.
170
- */
171
- export const Expanded: Story = {
172
- ...Default,
173
- args: { defaultExpanded: true },
174
- };
175
-
176
- /** A show-more trigger that reads how many cards are hidden and says so in its label. */
177
- function ShowRemaining(props: Omit<ChatCardListLayoutShowMoreProps, 'children'>) {
178
- const { hiddenCount } = useChatCardListLayoutContext();
179
-
180
- return (
181
- <ChatCardListLayoutShowMore {...props}>{`Show ${hiddenCount} more`}</ChatCardListLayoutShowMore>
182
- );
183
- }
184
-
185
- /**
186
- * The label is a required child rather than a built-in string, so it can be translated and can
187
- * carry the count: `useChatCardListLayoutContext` publishes `hiddenCount` for exactly this.
188
- */
189
- export const CountInLabel: Story = {
190
- parameters: { controls: { disable: true } },
191
- render: () => (
192
- <ChatCardContainer variant="full">
193
- <ChatCardListLayout>
194
- <ChatCardListLayoutItems>
195
- {TRAIN_CONNECTIONS.map((connection) => (
196
- <ResultCard
197
- key={connection.id}
198
- title={`${connection.line}, ${connection.route}`}
199
- subTitle={`${connection.departure} to ${connection.arrival}, ${connection.duration}`}
200
- >
201
- {connection.transfers === 0 ? 'Direct' : `${connection.transfers} transfer`}
202
- </ResultCard>
203
- ))}
204
- </ChatCardListLayoutItems>
205
- <ShowRemaining />
206
- </ChatCardListLayout>
207
- </ChatCardContainer>
208
- ),
209
- };
210
-
211
- /** Checks that the label counts the hidden cards and that a click reveals them. */
212
- export const CountInLabelCountsHiddenCards: Story = {
213
- ...CountInLabel,
214
- tags: ['e2e', '!autodocs'],
215
- play: async ({ canvas, userEvent, step }) => {
216
- await step('Counts the two connections past the limit', async () => {
217
- await expect(canvas.getByRole('button', { name: 'Show 2 more' })).toBeVisible();
218
- await expect(canvas.queryByText(/^IR 15.*Geneva$/)).toBeInTheDocument();
219
- await expect(canvas.queryByText('09:32 to 12:16, 2h 44m')).not.toBeInTheDocument();
220
- });
221
-
222
- await step('Reveals them on click', async () => {
223
- await userEvent.click(canvas.getByRole('button', { name: 'Show 2 more' }));
224
-
225
- await expect(canvas.getByText('09:32 to 12:16, 2h 44m')).toBeVisible();
226
- await expect(canvas.queryByRole('button', { name: /^Show/ })).not.toBeInTheDocument();
227
- });
228
- },
229
- };
230
-
231
- /** The list in the parent it's allowed in, then in a chat thread and in an assistant message. */
232
- export const Examples: Story = {
233
- parameters: { controls: { disable: true } },
234
- render: () => (
235
- <div className="flex flex-col gap-24">
236
- <ParentBlock parent="web-chat-ui/chat-card-container">
237
- <ChatCardContainer variant="full">
238
- <ChatCardListLayout>
239
- <ChatCardListLayoutItems>
240
- {TRAIN_CONNECTIONS.map((connection) => (
241
- <ResultCard
242
- key={connection.id}
243
- title={`${connection.line}, ${connection.route}`}
244
- subTitle={`${connection.departure} to ${connection.arrival}, ${connection.duration}`}
245
- >
246
- {connection.transfers === 0 ? 'Direct' : `${connection.transfers} transfer`}
247
- </ResultCard>
248
- ))}
249
- </ChatCardListLayoutItems>
250
- <ChatCardListLayoutShowMore>Show more</ChatCardListLayoutShowMore>
251
- </ChatCardListLayout>
252
- </ChatCardContainer>
253
- </ParentBlock>
254
-
255
- <ParentBlock parent="chat thread">
256
- <section aria-label="Chat thread" className="flex flex-col gap-16">
257
- <ChatMessage isQuestion className="px-message">
258
- <ChatMessageContent>Which emails came in since Friday?</ChatMessageContent>
259
- </ChatMessage>
260
- <ChatCardContainer variant="fixed">
261
- <ChatCardListLayout>
262
- <ChatCardListLayoutItems>
263
- {EMAILS.map((email) => (
264
- <ResultCard
265
- key={email.id}
266
- title={email.subject}
267
- subTitle={`${email.from}, ${email.date}`}
268
- >
269
- {email.preview}
270
- </ResultCard>
271
- ))}
272
- </ChatCardListLayoutItems>
273
- <ChatCardListLayoutShowMore>Show more</ChatCardListLayoutShowMore>
274
- </ChatCardListLayout>
275
- </ChatCardContainer>
276
- </section>
277
- </ParentBlock>
278
-
279
- <ParentBlock parent="assistant message">
280
- <div role="group" aria-label="Assistant message" className="flex flex-col gap-8">
281
- <p className="px-message text-text-secondary">
282
- You have two meetings tomorrow. Neither overlaps with your train to Geneva.
283
- </p>
284
- <ChatCardContainer inline>
285
- <ChatCardListLayout>
286
- <ChatCardListLayoutItems>
287
- {CALENDAR_EVENTS.map((event) => (
288
- <ResultCard key={event.id} title={event.title} subTitle={event.time}>
289
- {event.location}
290
- </ResultCard>
291
- ))}
292
- </ChatCardListLayoutItems>
293
- <ChatCardListLayoutShowMore>Show more</ChatCardListLayoutShowMore>
294
- </ChatCardListLayout>
295
- </ChatCardContainer>
296
- </div>
297
- </ParentBlock>
298
- </div>
299
- ),
300
- };
301
-
302
- interface ChatCardListLayoutPlaygroundProps {
303
- visibleCount: number;
304
- defaultExpanded: boolean;
305
- expanded?: boolean;
306
- asChild: boolean;
307
- className: string;
308
- itemCount: number;
309
- label: string;
310
- variant: ButtonProps['variant'];
311
- size: ButtonProps['size'];
312
- hasCountInLabel: boolean;
313
- onExpandedChange: (expanded: boolean) => void;
314
- onClick: () => void;
315
- }
316
-
317
- function ChatCardListLayoutPlayground({
318
- visibleCount,
319
- defaultExpanded,
320
- expanded,
321
- asChild,
322
- className,
323
- itemCount,
324
- label,
325
- variant,
326
- size,
327
- hasCountInLabel,
328
- onExpandedChange,
329
- onClick,
330
- }: ChatCardListLayoutPlaygroundProps) {
331
- const parts = (
332
- <>
333
- <ChatCardListLayoutItems>
334
- {EMAILS.slice(0, itemCount).map((email) => (
335
- <ResultCard
336
- key={email.id}
337
- title={email.subject}
338
- subTitle={`${email.from}, ${email.date}`}
339
- >
340
- {email.preview}
341
- </ResultCard>
342
- ))}
343
- </ChatCardListLayoutItems>
344
- {hasCountInLabel ? (
345
- <ShowRemaining variant={variant} size={size} onClick={onClick} />
346
- ) : (
347
- <ChatCardListLayoutShowMore variant={variant} size={size} onClick={onClick}>
348
- {label}
349
- </ChatCardListLayoutShowMore>
350
- )}
351
- </>
352
- );
353
-
354
- return (
355
- <ChatCardContainer variant="fixed">
356
- {/* `defaultExpanded` is read once, so the key remounts the layout when the control flips it. */}
357
- <ChatCardListLayout
358
- key={String(defaultExpanded)}
359
- visibleCount={visibleCount}
360
- defaultExpanded={defaultExpanded}
361
- expanded={expanded}
362
- asChild={asChild}
363
- className={className}
364
- onExpandedChange={onExpandedChange}
365
- >
366
- {asChild ? <section aria-label="Emails">{parts}</section> : parts}
367
- </ChatCardListLayout>
368
- </ChatCardContainer>
369
- );
370
- }
371
-
372
- /** Every part of the list at once, with each prop, the card count and both kinds of trigger on the Controls panel. */
373
- export const Playground: StoryObj<typeof ChatCardListLayoutPlayground> = {
374
- tags: ['!autodocs'],
375
- args: {
376
- visibleCount: 3,
377
- defaultExpanded: false,
378
- asChild: false,
379
- className: '',
380
- itemCount: EMAILS.length,
381
- label: 'Show more',
382
- variant: 'tertiary',
383
- size: 'sm',
384
- hasCountInLabel: true,
385
- onExpandedChange: fn(),
386
- onClick: fn(),
387
- },
388
- argTypes: {
389
- asChild: {
390
- control: 'boolean',
391
- description:
392
- '`asChild` on the root: the layout renders as the `<section>` the story passes it instead of a `<div>`.',
393
- },
394
- itemCount: {
395
- control: { type: 'number', min: 0, max: EMAILS.length },
396
- description: 'Number of cards in `ChatCardListLayoutItems`.',
397
- },
398
- label: {
399
- control: 'text',
400
- description: 'Text of `ChatCardListLayoutShowMore`, used when `hasCountInLabel` is off.',
401
- },
402
- variant: {
403
- control: 'select',
404
- options: ['primary', 'prominent', 'secondary', 'tertiary', 'danger', 'danger-subtle'],
405
- description: '`variant` on `ChatCardListLayoutShowMore`, forwarded to the button.',
406
- },
407
- size: {
408
- control: 'select',
409
- options: ['sm', 'md', 'lg'],
410
- description: '`size` on `ChatCardListLayoutShowMore`, forwarded to the button.',
411
- },
412
- hasCountInLabel: {
413
- control: 'boolean',
414
- description:
415
- 'Swaps the button for a custom trigger that reads `hiddenCount` from `useChatCardListLayoutContext`.',
416
- },
417
- onClick: {
418
- description: '`onClick` on `ChatCardListLayoutShowMore`; runs as well as expanding.',
419
- },
420
- },
421
- render: (args) => <ChatCardListLayoutPlayground {...args} />,
422
- };
@@ -1,254 +0,0 @@
1
- import { Slot } from '@radix-ui/react-slot';
2
- import type { ComponentProps, MouseEvent, ReactNode } from 'react';
3
- import {
4
- Children,
5
- createContext,
6
- forwardRef,
7
- useCallback,
8
- useContext,
9
- useLayoutEffect,
10
- useMemo,
11
- useState,
12
- } from 'react';
13
-
14
- import { Button } from '@swisper.ai/ui';
15
- import type { ButtonProps } from '@swisper.ai/ui';
16
- import { cn } from '@swisper.ai/web-core';
17
-
18
- /**
19
- * How many items are rendered before the reveal.
20
- *
21
- * Three, because the collapsed list has to read as "there is more here" at a glance.
22
- * One or two look like the whole answer and the button gets ignored; from five up the
23
- * collapsed state is already taller than a phone viewport, which is the thing this
24
- * layout exists to prevent.
25
- */
26
- const DEFAULT_VISIBLE_COUNT = 3;
27
-
28
- interface ChatCardListLayoutContextValue {
29
- visibleCount: number;
30
- isExpanded: boolean;
31
- expand: () => void;
32
- hiddenCount: number;
33
- setHiddenCount: (count: number) => void;
34
- }
35
-
36
- const ChatCardListLayoutContext = createContext<ChatCardListLayoutContextValue>(null!);
37
-
38
- /**
39
- * Reads the layout's state from any descendant.
40
- *
41
- * Exported so a consumer can put the hidden count in their own label ("Show 5 more") or
42
- * build a trigger that isn't a `Button`. `hiddenCount` is `0` on the first render and
43
- * correct from the first layout effect onwards — see `ChatCardListLayoutItems`.
44
- */
45
- export function useChatCardListLayoutContext() {
46
- const context = useContext(ChatCardListLayoutContext);
47
-
48
- if (!context) {
49
- throw new Error(
50
- '[web-chat-ui] "useChatCardListLayoutContext" must be used within a ChatCardListLayout. ' +
51
- 'ChatCardListLayoutItems and ChatCardListLayoutShowMore both read it, so both must be ' +
52
- 'rendered inside the ChatCardListLayout element, not as siblings of it.',
53
- );
54
- }
55
-
56
- return context;
57
- }
58
-
59
- export interface ChatCardListLayoutProps extends ComponentProps<'div'> {
60
- children: ReactNode;
61
- /**
62
- * How many items `ChatCardListLayoutItems` renders while collapsed.
63
- *
64
- * Clamped at zero: a negative value would reach `Array.slice` as a count from the end
65
- * and quietly render the *last* n items, which is the opposite of what the caller
66
- * asked for.
67
- */
68
- visibleCount?: number;
69
- /**
70
- * Expanded state for the uncontrolled case. Set it when the surrounding message is
71
- * restored from history and the reader had already expanded this list.
72
- */
73
- defaultExpanded?: boolean;
74
- /**
75
- * Controlled expanded state. Pass it only if something outside owns the state —
76
- * driving it back to `false` re-collapses the list and brings the button back, which
77
- * the uncontrolled component deliberately cannot do.
78
- */
79
- expanded?: boolean;
80
- onExpandedChange?: (expanded: boolean) => void;
81
- asChild?: boolean;
82
- }
83
-
84
- /**
85
- * Holds a list of chat cards and reveals the overflow behind a one-way "show more".
86
- *
87
- * Structure and state only. By default the root stacks its children in a left-aligned
88
- * column with an 8px gap; `className` is merged last, so the consumer's direction, gap and
89
- * alignment replace those and the same layout serves a stacked card list and a horizontal
90
- * strip without a variant prop. The `group/chat-card-list-layout` marker paints nothing
91
- * and exists so descendants can react to the expanded state with
92
- * `group-data-[expanded=true]/chat-card-list-layout:…`.
93
- *
94
- * The three parts exist because the pieces of information live in different places: only
95
- * `ChatCardListLayoutItems` knows how many children there are, only
96
- * `ChatCardListLayoutShowMore` needs to know whether any are hidden, and this root owns
97
- * the state that joins them. Slicing here instead would mean telling item children apart
98
- * from the button by inspecting `child.type`, which breaks the moment either is wrapped.
99
- */
100
- export const ChatCardListLayout = forwardRef<HTMLDivElement, ChatCardListLayoutProps>(
101
- function ChatCardListLayout(
102
- {
103
- children,
104
- className,
105
- visibleCount = DEFAULT_VISIBLE_COUNT,
106
- defaultExpanded = false,
107
- expanded,
108
- onExpandedChange,
109
- asChild = false,
110
- ...rest
111
- },
112
- ref,
113
- ) {
114
- const [internalExpanded, setInternalExpanded] = useState(defaultExpanded);
115
- const [hiddenCount, setHiddenCount] = useState(0);
116
-
117
- const isExpanded = expanded ?? internalExpanded;
118
-
119
- const expand = useCallback(() => {
120
- if (expanded === undefined) {
121
- setInternalExpanded(true);
122
- }
123
-
124
- onExpandedChange?.(true);
125
- }, [expanded, onExpandedChange]);
126
-
127
- const contextValue = useMemo(
128
- () => ({
129
- visibleCount: Math.max(0, visibleCount),
130
- isExpanded,
131
- expand,
132
- hiddenCount,
133
- setHiddenCount,
134
- }),
135
- [visibleCount, isExpanded, expand, hiddenCount],
136
- );
137
-
138
- const Component = asChild ? Slot : 'div';
139
-
140
- return (
141
- <ChatCardListLayoutContext.Provider value={contextValue}>
142
- <Component
143
- ref={ref}
144
- data-slot="chat-card-list-layout"
145
- data-testid="chat-card-list-layout"
146
- data-expanded={isExpanded}
147
- className={cn(
148
- 'group/chat-card-list-layout',
149
- 'flex flex-col items-start gap-[8px]',
150
- className,
151
- )}
152
- {...rest}
153
- >
154
- {children}
155
- </Component>
156
- </ChatCardListLayoutContext.Provider>
157
- );
158
- },
159
- );
160
-
161
- ChatCardListLayout.displayName = 'ChatCardListLayout';
162
-
163
- export interface ChatCardListLayoutItemsProps {
164
- children: ReactNode;
165
- }
166
-
167
- /**
168
- * Renders the visible slice of the list, and nothing else — no wrapper element.
169
- *
170
- * A `div` here would sit between the root's `className` and the cards, so a consumer's
171
- * `flex`/`grid` on the root would apply to this one wrapper instead of to the items and
172
- * the layout would silently collapse to a single column. There is no `className` prop
173
- * for the same reason: there is no node to hang it on.
174
- *
175
- * Hidden items are sliced away rather than hidden with CSS, so they never mount: no DOM,
176
- * no image or data fetches from cards nobody has asked for, and nothing invisible in the
177
- * tab order.
178
- *
179
- * Only one of these per layout. Two would race on `hiddenCount` and the last effect
180
- * would win; summing across instances is buildable but nothing needs it yet.
181
- */
182
- export function ChatCardListLayoutItems({ children }: ChatCardListLayoutItemsProps) {
183
- const { visibleCount, isExpanded, setHiddenCount } = useChatCardListLayoutContext();
184
-
185
- // `Children.toArray` flattens fragments and drops `null`/`undefined`/booleans, so a
186
- // conditionally rendered card counts only when it actually renders. Reading
187
- // `Children.count` instead would count the holes and the button would offer to reveal
188
- // items that do not exist.
189
- const items = Children.toArray(children);
190
- const hiddenCount = isExpanded ? 0 : Math.max(0, items.length - visibleCount);
191
-
192
- // A layout effect, not `useEffect`: `hiddenCount` starts at 0, so the button renders
193
- // `null` on the first pass and this correction has to land before the browser paints.
194
- // With `useEffect` the button would visibly pop in after the list.
195
- useLayoutEffect(() => {
196
- setHiddenCount(hiddenCount);
197
- }, [hiddenCount, setHiddenCount]);
198
-
199
- return <>{isExpanded ? items : items.slice(0, visibleCount)}</>;
200
- }
201
-
202
- export interface ChatCardListLayoutShowMoreProps extends Omit<ButtonProps, 'children'> {
203
- /**
204
- * The label. Required on purpose: a default `'Show more'` baked into the package would
205
- * be untranslatable by the app that ships it, and this is the one string in the layout
206
- * a reader actually sees.
207
- */
208
- children: ReactNode;
209
- }
210
-
211
- /**
212
- * The reveal trigger. Renders nothing once there is nothing left to reveal — either the
213
- * list is expanded, or it never overflowed — which is what makes the button a one-shot
214
- * affordance rather than a toggle.
215
- *
216
- * By default it is a small tertiary `Button` centred under the cards. Every `Button` prop
217
- * is forwarded after those defaults and `className` is merged last, so a consumer's
218
- * `variant`, `size` or classes win.
219
- */
220
- export const ChatCardListLayoutShowMore = forwardRef<
221
- // The same element union `Button` forwards: it renders a `span` when `isSemantic` is
222
- // false, and narrowing to `HTMLButtonElement` here would make a plain object ref fail
223
- // to type-check against it.
224
- HTMLButtonElement | HTMLSpanElement,
225
- ChatCardListLayoutShowMoreProps
226
- >(function ChatCardListLayoutShowMore({ children, onClick, className, ...rest }, ref) {
227
- const { isExpanded, hiddenCount, expand } = useChatCardListLayoutContext();
228
-
229
- function handleClick(event: MouseEvent<HTMLButtonElement>) {
230
- expand();
231
- onClick?.(event);
232
- }
233
-
234
- if (isExpanded || hiddenCount === 0) {
235
- return null;
236
- }
237
-
238
- return (
239
- <Button
240
- ref={ref}
241
- variant="tertiary"
242
- size="sm"
243
- data-slot="chat-card-list-layout-show-more"
244
- data-testid="chat-card-list-layout-show-more"
245
- onClick={handleClick}
246
- className={cn('mx-auto', className)}
247
- {...rest}
248
- >
249
- {children}
250
- </Button>
251
- );
252
- });
253
-
254
- ChatCardListLayoutShowMore.displayName = 'ChatCardListLayoutShowMore';
@@ -1,11 +0,0 @@
1
- export {
2
- ChatCardListLayout,
3
- ChatCardListLayoutItems,
4
- ChatCardListLayoutShowMore,
5
- useChatCardListLayoutContext,
6
- } from './chat-card-list-layout';
7
- export type {
8
- ChatCardListLayoutProps,
9
- ChatCardListLayoutItemsProps,
10
- ChatCardListLayoutShowMoreProps,
11
- } from './chat-card-list-layout';