@swisper.ai/web-chat-ui 0.1.0 → 0.2.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 (83) 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-container/chat-card-container.stories.d.ts.map +1 -1
  4. package/dist/components/rich-text-field/rich-text-field.stories.d.ts.map +1 -1
  5. package/dist/components/rich-text-field/utils.d.ts +36 -36
  6. package/dist/index.css +1 -1
  7. package/dist/index.d.ts +0 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.es.js +1566 -1810
  10. package/dist/index.es.js.map +1 -1
  11. package/package.json +4 -4
  12. package/src/components/chat-card/chat-card.json +6 -6
  13. package/src/components/chat-card/chat-card.stories.tsx +6 -14
  14. package/src/components/chat-card/components/chat-card-body.tsx +1 -1
  15. package/src/components/chat-card-container/chat-card-container.json +6 -6
  16. package/src/components/chat-card-container/chat-card-container.stories.tsx +10 -18
  17. package/src/components/chat-container/chat-container.json +1 -1
  18. package/src/components/chat-list/chat-list-items.tsx +3 -3
  19. package/src/components/rich-text-field/rich-text-field.stories.tsx +3 -1
  20. package/src/index.ts +0 -1
  21. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.d.ts +0 -97
  22. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.d.ts.map +0 -1
  23. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.stories.d.ts +0 -52
  24. package/dist/layouts/chat-card-list-layout/chat-card-list-layout.stories.d.ts.map +0 -1
  25. package/dist/layouts/chat-card-list-layout/index.d.ts +0 -3
  26. package/dist/layouts/chat-card-list-layout/index.d.ts.map +0 -1
  27. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.d.ts +0 -89
  28. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.d.ts.map +0 -1
  29. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.sizing.d.ts +0 -44
  30. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.sizing.d.ts.map +0 -1
  31. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.stories.d.ts +0 -62
  32. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.stories.d.ts.map +0 -1
  33. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.variants.d.ts +0 -81
  34. package/dist/layouts/chat-card-three-column-layout/chat-card-three-column-layout.variants.d.ts.map +0 -1
  35. package/dist/layouts/chat-card-three-column-layout/index.d.ts +0 -5
  36. package/dist/layouts/chat-card-three-column-layout/index.d.ts.map +0 -1
  37. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.d.ts +0 -37
  38. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.d.ts.map +0 -1
  39. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.sizing.d.ts +0 -12
  40. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.sizing.d.ts.map +0 -1
  41. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.stories.d.ts +0 -40
  42. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.stories.d.ts.map +0 -1
  43. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.variants.d.ts +0 -35
  44. package/dist/layouts/chat-card-two-column-layout/chat-card-two-column-layout.variants.d.ts.map +0 -1
  45. package/dist/layouts/chat-card-two-column-layout/index.d.ts +0 -4
  46. package/dist/layouts/chat-card-two-column-layout/index.d.ts.map +0 -1
  47. package/dist/layouts/index.d.ts +0 -4
  48. package/dist/layouts/index.d.ts.map +0 -1
  49. package/src/layouts/chat-card-list-layout/__stories__/fixtures.ts +0 -112
  50. package/src/layouts/chat-card-list-layout/__stories__/result-card.tsx +0 -36
  51. package/src/layouts/chat-card-list-layout/__stories__/types.ts +0 -31
  52. package/src/layouts/chat-card-list-layout/__tests__/chat-card-list-layout.spec.tsx +0 -204
  53. package/src/layouts/chat-card-list-layout/chat-card-list-layout.json +0 -129
  54. package/src/layouts/chat-card-list-layout/chat-card-list-layout.stories.tsx +0 -422
  55. package/src/layouts/chat-card-list-layout/chat-card-list-layout.tsx +0 -254
  56. package/src/layouts/chat-card-list-layout/index.ts +0 -11
  57. package/src/layouts/chat-card-three-column-layout/__stories__/fixtures.ts +0 -117
  58. package/src/layouts/chat-card-three-column-layout/__stories__/resizable-chat-window.tsx +0 -18
  59. package/src/layouts/chat-card-three-column-layout/__stories__/stay-details.tsx +0 -85
  60. package/src/layouts/chat-card-three-column-layout/__stories__/stay-list.tsx +0 -47
  61. package/src/layouts/chat-card-three-column-layout/__stories__/stay-map.tsx +0 -44
  62. package/src/layouts/chat-card-three-column-layout/__stories__/stay-rooms.tsx +0 -61
  63. package/src/layouts/chat-card-three-column-layout/__stories__/types.ts +0 -35
  64. package/src/layouts/chat-card-three-column-layout/__tests__/chat-card-three-column-layout.sizing.test.ts +0 -71
  65. package/src/layouts/chat-card-three-column-layout/__tests__/chat-card-three-column-layout.spec.tsx +0 -327
  66. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.json +0 -153
  67. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.sizing.ts +0 -76
  68. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.stories.tsx +0 -658
  69. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.tsx +0 -293
  70. package/src/layouts/chat-card-three-column-layout/chat-card-three-column-layout.variants.ts +0 -146
  71. package/src/layouts/chat-card-three-column-layout/index.ts +0 -24
  72. package/src/layouts/chat-card-two-column-layout/__stories__/connection-itinerary.tsx +0 -51
  73. package/src/layouts/chat-card-two-column-layout/__stories__/fixtures.ts +0 -83
  74. package/src/layouts/chat-card-two-column-layout/__stories__/route-map.tsx +0 -24
  75. package/src/layouts/chat-card-two-column-layout/__stories__/types.ts +0 -15
  76. package/src/layouts/chat-card-two-column-layout/__tests__/chat-card-two-column-layout.spec.tsx +0 -109
  77. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.json +0 -109
  78. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.sizing.ts +0 -21
  79. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.stories.tsx +0 -439
  80. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.tsx +0 -141
  81. package/src/layouts/chat-card-two-column-layout/chat-card-two-column-layout.variants.ts +0 -65
  82. package/src/layouts/chat-card-two-column-layout/index.ts +0 -17
  83. package/src/layouts/index.ts +0 -3
@@ -1,129 +0,0 @@
1
- {
2
- "$schema": "../../../../int-eslint-config/schemas/component.schema.json",
3
- "component": "chat-card-list-layout",
4
- "purpose": "Holds a list of chat cards in a message and shows only the first few, revealing the rest behind a one-way \"show more\" button. Parts: ChatCardListLayout (root and state), ChatCardListLayoutItems (the visible slice), ChatCardListLayoutShowMore (the reveal button), plus useChatCardListLayoutContext for custom triggers.",
5
- "anatomy": [
6
- "import {",
7
- " ChatCardListLayout,",
8
- " ChatCardListLayoutItems,",
9
- " ChatCardListLayoutShowMore,",
10
- "} from '@swisper.ai/web-chat-ui';",
11
- "",
12
- "<ChatCardListLayout>",
13
- " <ChatCardListLayoutItems />",
14
- " <ChatCardListLayoutShowMore />",
15
- "</ChatCardListLayout>"
16
- ],
17
- "useWhen": [
18
- "An assistant message returns more cards than fit at a glance (flight options, search results, places) and the rest should stay one click away",
19
- "A long list of cards would push the reply taller than a phone viewport",
20
- "Cards that are not yet revealed should not mount at all (no DOM, no image or data fetches, no tab stops)"
21
- ],
22
- "avoidWhen": [
23
- "Showing one card — render web-chat-ui/chat-card in web-chat-ui/chat-card-container directly",
24
- "Rows of plain data rather than cards — use ui/list or ui/table instead",
25
- "The reader needs to collapse the list again — the uncontrolled layout only expands; own the state with expanded/onExpandedChange, or use ui/collapsible",
26
- "Paging through cards side by side — use ui/carousel instead"
27
- ],
28
- "uxPrinciples": [
29
- "The collapsed list must read as \"there is more here\": three items is the default because one or two look like the whole answer, and five or more is already taller than a phone viewport",
30
- "Show more is a one-shot affordance, not a toggle: once everything is visible the button disappears",
31
- "A list that never overflows carries no dead button"
32
- ],
33
- "accessibility": {
34
- "required": [
35
- "Give ChatCardListLayoutShowMore a visible text label as its children — it has no built-in label and is the only string in the layout a reader sees",
36
- "A custom trigger built on useChatCardListLayoutContext must be a real button (or ui/button) so it is keyboard operable"
37
- ],
38
- "keyboard": [
39
- "ChatCardListLayoutShowMore is a ui/button: Tab reaches it, Enter or Space reveals the hidden cards",
40
- "Hidden cards are not mounted, so they are not in the tab order until revealed"
41
- ],
42
- "aria": [
43
- "The root sets no role; it renders a div (or the asChild element) with data-expanded",
44
- "The show-more button sets no aria-expanded or aria-controls, because it removes itself once the list is expanded"
45
- ],
46
- "focus": [
47
- "The show-more button unmounts after it is clicked and the layout does not move focus; if focus placement matters, move it to the first revealed card in onExpandedChange or the button's onClick"
48
- ]
49
- },
50
- "behaviour": {
51
- "states": [
52
- "collapsed (overflowing): visibleCount items rendered, show-more button shown",
53
- "collapsed (not overflowing): every item rendered, no button",
54
- "expanded: every item rendered, no button"
55
- ],
56
- "rules": [
57
- "visibleCount is how many items ChatCardListLayoutItems renders while collapsed; a negative value is clamped to 0, which renders no items and keeps the button",
58
- "Hidden items are sliced out, not hidden with CSS: they never mount until the list expands",
59
- "Only children that actually render count: ChatCardListLayoutItems flattens fragments and drops null, undefined and booleans, so a conditionally rendered card is counted only when it renders",
60
- "ChatCardListLayoutItems renders no wrapper element, so the root's className (flex, grid, gap) applies to the cards themselves; it takes no className",
61
- "Use only one ChatCardListLayoutItems per layout — two would overwrite each other's hidden count",
62
- "ChatCardListLayoutShowMore renders nothing when the list is expanded or nothing is hidden; clicking it expands the list and also runs the consumer onClick",
63
- "Uncontrolled (defaultExpanded) the list only ever expands; defaultExpanded=true starts it expanded, e.g. for a message restored from history the reader had already expanded",
64
- "Controlled (expanded) the layout does not expand on its own: clicking reports onExpandedChange(true) and the owner sets expanded; setting expanded back to false re-collapses the list and brings the button back",
65
- "onExpandedChange is called with true on every show-more click, controlled or not",
66
- "By default the root stacks its children in a column, left-aligned, with an 8px gap; a className on the root replaces direction, gap and alignment (merged with tailwind-merge), so the same layout serves a horizontal strip",
67
- "The root carries data-slot=chat-card-list-layout, data-expanded and the group/chat-card-list-layout marker, so descendants can style on group-data-[expanded=true]/chat-card-list-layout",
68
- "ChatCardListLayoutShowMore carries data-slot=chat-card-list-layout-show-more on the button it renders, in place of the button's own data-slot=button",
69
- "ChatCardListLayoutShowMore forwards every ui/button prop; by default it is a small tertiary button centred under the cards, and variant, size or className passed to it win",
70
- "useChatCardListLayoutContext returns visibleCount, isExpanded, expand and hiddenCount; hiddenCount is 0 on the first render and correct before the first paint",
71
- "Every part and useChatCardListLayoutContext throw \"[web-chat-ui] … must be used within a ChatCardListLayout\" when rendered outside the root",
72
- "asChild renders the single child element as the root instead of a div"
73
- ]
74
- },
75
- "composition": {
76
- "allowedParents": ["web-chat-ui/chat-card-container"],
77
- "allowedChildren": [
78
- "web-chat-ui/ChatCardListLayoutItems",
79
- "web-chat-ui/ChatCardListLayoutShowMore",
80
- "web-chat-ui/chat-card",
81
- "custom show-more trigger (reads useChatCardListLayoutContext)"
82
- ],
83
- "avoidCombinations": [
84
- "ChatCardListLayoutItems or ChatCardListLayoutShowMore as siblings of ChatCardListLayout instead of children — they throw",
85
- "Wrapping the cards in an extra element inside ChatCardListLayoutItems — the wrapper counts as one item and nothing is ever hidden",
86
- "Two ChatCardListLayoutItems in one layout — their hidden counts race and the button can be wrong",
87
- "Hiding cards with CSS instead of letting the layout slice them — they would still mount, fetch and take tab stops"
88
- ],
89
- "contexts": ["chat thread", "assistant message"]
90
- },
91
- "content": {
92
- "labelRules": [
93
- "The show-more label is required and supplied by the app, so it can be translated",
94
- "Prefer a label that carries the count (\"Show 5 more\") using hiddenCount from useChatCardListLayoutContext; \"Show more\" is acceptable when the count is not useful"
95
- ],
96
- "helperTextRules": [],
97
- "errorRules": []
98
- },
99
- "aiDecisionRules": [
100
- {
101
- "condition": "An assistant reply contains more than about three cards",
102
- "decision": "Wrap them in ChatCardListLayout with ChatCardListLayoutItems and a ChatCardListLayoutShowMore, keeping the default visibleCount of 3 unless there is a reason to change it",
103
- "reason": "Three reads as \"there is more\" without making the collapsed reply taller than a phone viewport"
104
- },
105
- {
106
- "condition": "The show-more label should state how many cards are hidden",
107
- "decision": "Render ChatCardListLayoutShowMore inside a small component that reads hiddenCount from useChatCardListLayoutContext",
108
- "reason": "The label is a required child, not a built-in string, so the count and the translation both come from the app"
109
- },
110
- {
111
- "condition": "Something outside the list must know or restore whether it is expanded (history restore, analytics, a collapse control)",
112
- "decision": "Use defaultExpanded for a one-time restore, or expanded with onExpandedChange when outside state owns it",
113
- "reason": "The uncontrolled layout can only expand; only the controlled form can re-collapse"
114
- },
115
- {
116
- "condition": "Cards should run horizontally or in a grid",
117
- "decision": "Set flex direction, grid and gap with className on ChatCardListLayout, not on a wrapper around the cards",
118
- "reason": "ChatCardListLayoutItems renders no element, so the root's layout classes apply to the cards directly"
119
- }
120
- ],
121
- "relatedComponents": [
122
- "web-chat-ui/chat-card",
123
- "web-chat-ui/chat-card-container",
124
- "ui/button",
125
- "ui/carousel",
126
- "ui/collapsible",
127
- "ui/list"
128
- ]
129
- }
@@ -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
- };