@appica/ui-react 1.4.0 → 1.5.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.
- package/CHANGELOG.md +20 -0
- package/README.md +8 -2
- package/agent-rules.md +8 -2
- package/appica.css +2 -2
- package/dist/components/badge/badge.js +1 -1
- package/dist/components/badge/badge.js.map +1 -1
- package/dist/components/button/button-variants.js +1 -1
- package/dist/components/button/button-variants.js.map +1 -1
- package/dist/components/card/card.js +1 -1
- package/dist/components/card/card.js.map +1 -1
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +12 -4
- package/dist/components/carousel/carousel.js.map +1 -1
- package/dist/components/checkbox/checkbox.js +1 -1
- package/dist/components/checkbox/checkbox.js.map +1 -1
- package/dist/components/command-palette/command-palette.d.ts +91 -0
- package/dist/components/command-palette/command-palette.d.ts.map +1 -0
- package/dist/components/command-palette/command-palette.js +376 -0
- package/dist/components/command-palette/command-palette.js.map +1 -0
- package/dist/components/date-field/date-field.js +1 -1
- package/dist/components/date-field/date-field.js.map +1 -1
- package/dist/components/date-picker/date-picker.js +1 -1
- package/dist/components/date-picker/date-picker.js.map +1 -1
- package/dist/components/input/input-variants.js +2 -2
- package/dist/components/input/input-variants.js.map +1 -1
- package/dist/components/radio/radio.js +1 -1
- package/dist/components/radio/radio.js.map +1 -1
- package/dist/components/splitter/splitter.d.ts +57 -0
- package/dist/components/splitter/splitter.d.ts.map +1 -0
- package/dist/components/splitter/splitter.js +395 -0
- package/dist/components/splitter/splitter.js.map +1 -0
- package/dist/components/textarea/textarea.js +1 -1
- package/dist/components/textarea/textarea.js.map +1 -1
- package/dist/components/time-field/time-field.js +1 -1
- package/dist/components/time-field/time-field.js.map +1 -1
- package/dist/components/toc/toc.d.ts +6 -1
- package/dist/components/toc/toc.d.ts.map +1 -1
- package/dist/components/toc/toc.js +15 -7
- package/dist/components/toc/toc.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +24 -14
- package/src/components/badge/badge.tsx +1 -1
- package/src/components/button/button-variants.ts +1 -1
- package/src/components/card/card.tsx +1 -1
- package/src/components/carousel/carousel.test.tsx +134 -1
- package/src/components/carousel/carousel.tsx +13 -4
- package/src/components/checkbox/checkbox.tsx +1 -1
- package/src/components/command-palette/command-palette.test.tsx +280 -0
- package/src/components/command-palette/command-palette.tsx +544 -0
- package/src/components/compare-slider/compare-slider.test.tsx +8 -7
- package/src/components/date-field/date-field.tsx +1 -1
- package/src/components/date-picker/date-picker.tsx +1 -1
- package/src/components/input/input-variants.ts +2 -2
- package/src/components/radio/radio.tsx +1 -1
- package/src/components/rating/rating.test.tsx +10 -3
- package/src/components/splitter/splitter.test.tsx +415 -0
- package/src/components/splitter/splitter.tsx +555 -0
- package/src/components/textarea/textarea.tsx +1 -1
- package/src/components/time-field/time-field.tsx +1 -1
- package/src/components/toc/toc.test.tsx +15 -0
- package/src/components/toc/toc.tsx +22 -7
- package/src/index.ts +2 -0
- package/src/test/css.ts +9 -0
- package/src/test/setup.ts +13 -0
- package/styles.css +3 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { render, screen, waitFor } from '@testing-library/react'
|
|
3
|
+
import userEvent from '@testing-library/user-event'
|
|
4
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
5
|
+
import { axe } from 'vitest-axe'
|
|
6
|
+
import { Button } from '../button/button'
|
|
7
|
+
import {
|
|
8
|
+
CommandPalette,
|
|
9
|
+
CommandPaletteCollection,
|
|
10
|
+
CommandPaletteContent,
|
|
11
|
+
CommandPaletteEmpty,
|
|
12
|
+
CommandPaletteFooter,
|
|
13
|
+
CommandPaletteGroup,
|
|
14
|
+
CommandPaletteInput,
|
|
15
|
+
CommandPaletteItem,
|
|
16
|
+
CommandPaletteLabel,
|
|
17
|
+
CommandPaletteList,
|
|
18
|
+
CommandPaletteTrigger,
|
|
19
|
+
type CommandPaletteContentProps,
|
|
20
|
+
type CommandPaletteProps,
|
|
21
|
+
} from './command-palette'
|
|
22
|
+
|
|
23
|
+
interface Command {
|
|
24
|
+
value: string
|
|
25
|
+
label: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const COMMANDS: Command[] = [
|
|
29
|
+
{ value: 'new-file', label: 'New file' },
|
|
30
|
+
{ value: 'open-folder', label: 'Open folder' },
|
|
31
|
+
{ value: 'settings', label: 'Settings' },
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
const GROUPS = [
|
|
35
|
+
{ value: 'Files', items: COMMANDS.slice(0, 2) },
|
|
36
|
+
{ value: 'App', items: COMMANDS.slice(2) },
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
function Palette({
|
|
40
|
+
onRun = () => {},
|
|
41
|
+
content,
|
|
42
|
+
closeOnSelect,
|
|
43
|
+
...props
|
|
44
|
+
}: Partial<CommandPaletteProps> & {
|
|
45
|
+
onRun?: (value: string) => void
|
|
46
|
+
content?: Partial<CommandPaletteContentProps>
|
|
47
|
+
closeOnSelect?: boolean
|
|
48
|
+
}) {
|
|
49
|
+
return (
|
|
50
|
+
<CommandPalette items={COMMANDS} {...props}>
|
|
51
|
+
<CommandPaletteTrigger render={<Button>Open palette</Button>} />
|
|
52
|
+
<CommandPaletteContent {...content}>
|
|
53
|
+
<CommandPaletteInput placeholder="Type a command" />
|
|
54
|
+
<CommandPaletteList>
|
|
55
|
+
{(command: Command) => (
|
|
56
|
+
<CommandPaletteItem
|
|
57
|
+
key={command.value}
|
|
58
|
+
value={command}
|
|
59
|
+
closeOnSelect={closeOnSelect}
|
|
60
|
+
onClick={() => onRun(command.value)}
|
|
61
|
+
>
|
|
62
|
+
{command.label}
|
|
63
|
+
</CommandPaletteItem>
|
|
64
|
+
)}
|
|
65
|
+
</CommandPaletteList>
|
|
66
|
+
<CommandPaletteEmpty>No commands found.</CommandPaletteEmpty>
|
|
67
|
+
<CommandPaletteFooter>Footer</CommandPaletteFooter>
|
|
68
|
+
</CommandPaletteContent>
|
|
69
|
+
</CommandPalette>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async function openPalette(user: ReturnType<typeof userEvent.setup>) {
|
|
74
|
+
await user.click(screen.getByRole('button', { name: 'Open palette' }))
|
|
75
|
+
return screen.findByRole('dialog', { name: 'Command palette' })
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
describe('CommandPalette', () => {
|
|
79
|
+
it('opens from the trigger as a named dialog with the input focused', async () => {
|
|
80
|
+
const user = userEvent.setup()
|
|
81
|
+
render(<Palette />)
|
|
82
|
+
const dialog = await openPalette(user)
|
|
83
|
+
expect(dialog).toHaveAttribute('data-slot', 'command-palette-content')
|
|
84
|
+
const input = screen.getByRole('combobox', { name: 'Command palette' })
|
|
85
|
+
await waitFor(() => expect(input).toHaveFocus())
|
|
86
|
+
expect(screen.getAllByRole('option')).toHaveLength(3)
|
|
87
|
+
expect(screen.getByText('Footer')).toHaveAttribute('data-slot', 'command-palette-footer')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('names the dialog and the input from label', async () => {
|
|
91
|
+
const user = userEvent.setup()
|
|
92
|
+
render(<Palette content={{ label: 'Search the docs' }} />)
|
|
93
|
+
await user.click(screen.getByRole('button', { name: 'Open palette' }))
|
|
94
|
+
expect(await screen.findByRole('dialog', { name: 'Search the docs' })).toBeInTheDocument()
|
|
95
|
+
expect(screen.getByRole('combobox', { name: 'Search the docs' })).toBeInTheDocument()
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('filters the items as the user types and shows the empty state', async () => {
|
|
99
|
+
const user = userEvent.setup()
|
|
100
|
+
render(<Palette />)
|
|
101
|
+
await openPalette(user)
|
|
102
|
+
await user.type(screen.getByRole('combobox'), 'fold')
|
|
103
|
+
expect(screen.getAllByRole('option').map((option) => option.textContent)).toEqual(['Open folder'])
|
|
104
|
+
await user.type(screen.getByRole('combobox'), 'zzz')
|
|
105
|
+
expect(screen.queryAllByRole('option')).toHaveLength(0)
|
|
106
|
+
expect(screen.getByText('No commands found.')).toBeInTheDocument()
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
it('highlights the first match and runs it on Enter, then closes', async () => {
|
|
110
|
+
const user = userEvent.setup()
|
|
111
|
+
const onRun = vi.fn()
|
|
112
|
+
render(<Palette onRun={onRun} />)
|
|
113
|
+
await openPalette(user)
|
|
114
|
+
const input = screen.getByRole('combobox')
|
|
115
|
+
await user.type(input, 'set')
|
|
116
|
+
expect(screen.getByRole('option', { name: 'Settings' })).toHaveAttribute('data-highlighted')
|
|
117
|
+
await user.keyboard('{Enter}')
|
|
118
|
+
expect(onRun).toHaveBeenCalledWith('settings')
|
|
119
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('moves the highlight with the arrow keys', async () => {
|
|
123
|
+
const user = userEvent.setup()
|
|
124
|
+
const onRun = vi.fn()
|
|
125
|
+
render(<Palette onRun={onRun} />)
|
|
126
|
+
await openPalette(user)
|
|
127
|
+
await waitFor(() => expect(screen.getByRole('combobox')).toHaveFocus())
|
|
128
|
+
expect(screen.getByRole('option', { name: 'New file' })).toHaveAttribute('data-highlighted')
|
|
129
|
+
await user.keyboard('{ArrowDown}')
|
|
130
|
+
expect(screen.getByRole('option', { name: 'Open folder' })).toHaveAttribute('data-highlighted')
|
|
131
|
+
await user.keyboard('{Enter}')
|
|
132
|
+
expect(onRun).toHaveBeenCalledWith('open-folder')
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('stays open and keeps the query when an item has closeOnSelect={false}', async () => {
|
|
136
|
+
const user = userEvent.setup()
|
|
137
|
+
const onRun = vi.fn()
|
|
138
|
+
render(<Palette onRun={onRun} closeOnSelect={false} />)
|
|
139
|
+
await openPalette(user)
|
|
140
|
+
const input = screen.getByRole('combobox')
|
|
141
|
+
await user.type(input, 'new')
|
|
142
|
+
await user.click(screen.getByRole('option', { name: 'New file' }))
|
|
143
|
+
expect(onRun).toHaveBeenCalledWith('new-file')
|
|
144
|
+
expect(screen.getByRole('dialog')).toBeInTheDocument()
|
|
145
|
+
expect(input).toHaveValue('new')
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('reports the query through onValueChange but not an item press', async () => {
|
|
149
|
+
const user = userEvent.setup()
|
|
150
|
+
const onValueChange = vi.fn()
|
|
151
|
+
render(<Palette onValueChange={onValueChange} closeOnSelect={false} />)
|
|
152
|
+
await openPalette(user)
|
|
153
|
+
await user.type(screen.getByRole('combobox'), 'o')
|
|
154
|
+
expect(onValueChange).toHaveBeenLastCalledWith('o', expect.objectContaining({ reason: 'input-change' }))
|
|
155
|
+
onValueChange.mockClear()
|
|
156
|
+
await user.click(screen.getByRole('option', { name: 'Open folder' }))
|
|
157
|
+
expect(onValueChange).not.toHaveBeenCalled()
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it('closes on Escape', async () => {
|
|
161
|
+
const user = userEvent.setup()
|
|
162
|
+
render(<Palette />)
|
|
163
|
+
await openPalette(user)
|
|
164
|
+
await user.keyboard('{Escape}')
|
|
165
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it('starts with an empty query each time it opens', async () => {
|
|
169
|
+
const user = userEvent.setup()
|
|
170
|
+
render(<Palette />)
|
|
171
|
+
await openPalette(user)
|
|
172
|
+
await user.type(screen.getByRole('combobox'), 'set')
|
|
173
|
+
await user.keyboard('{Escape}')
|
|
174
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
175
|
+
await openPalette(user)
|
|
176
|
+
expect(screen.getByRole('combobox')).toHaveValue('')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('toggles with the shortcut key and ⌘ or Ctrl', async () => {
|
|
180
|
+
const user = userEvent.setup()
|
|
181
|
+
render(<Palette shortcut="k" />)
|
|
182
|
+
await user.keyboard('{Meta>}k{/Meta}')
|
|
183
|
+
expect(await screen.findByRole('dialog')).toBeInTheDocument()
|
|
184
|
+
await user.keyboard('{Control>}k{/Control}')
|
|
185
|
+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
|
|
186
|
+
await user.keyboard('k')
|
|
187
|
+
expect(screen.queryByRole('dialog')).toBeNull()
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('opens through the shortcut while controlled', async () => {
|
|
191
|
+
const user = userEvent.setup()
|
|
192
|
+
function Controlled() {
|
|
193
|
+
const [open, setOpen] = React.useState(false)
|
|
194
|
+
return (
|
|
195
|
+
<>
|
|
196
|
+
<output data-testid="state">{String(open)}</output>
|
|
197
|
+
<Palette shortcut="k" open={open} onOpenChange={setOpen} />
|
|
198
|
+
</>
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
render(<Controlled />)
|
|
202
|
+
await user.keyboard('{Meta>}k{/Meta}')
|
|
203
|
+
expect(screen.getByTestId('state')).toHaveTextContent('true')
|
|
204
|
+
expect(await screen.findByRole('dialog')).toBeInTheDocument()
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('renders grouped items under their labels', async () => {
|
|
208
|
+
const user = userEvent.setup()
|
|
209
|
+
render(
|
|
210
|
+
<CommandPalette items={GROUPS}>
|
|
211
|
+
<CommandPaletteTrigger render={<Button>Open palette</Button>} />
|
|
212
|
+
<CommandPaletteContent>
|
|
213
|
+
<CommandPaletteInput />
|
|
214
|
+
<CommandPaletteList>
|
|
215
|
+
{(group: (typeof GROUPS)[number]) => (
|
|
216
|
+
<CommandPaletteGroup key={group.value} items={group.items}>
|
|
217
|
+
<CommandPaletteLabel>{group.value}</CommandPaletteLabel>
|
|
218
|
+
<CommandPaletteCollection>
|
|
219
|
+
{(command: Command) => (
|
|
220
|
+
<CommandPaletteItem key={command.value} value={command}>
|
|
221
|
+
{command.label}
|
|
222
|
+
</CommandPaletteItem>
|
|
223
|
+
)}
|
|
224
|
+
</CommandPaletteCollection>
|
|
225
|
+
</CommandPaletteGroup>
|
|
226
|
+
)}
|
|
227
|
+
</CommandPaletteList>
|
|
228
|
+
</CommandPaletteContent>
|
|
229
|
+
</CommandPalette>,
|
|
230
|
+
)
|
|
231
|
+
await openPalette(user)
|
|
232
|
+
expect(screen.getByRole('group', { name: 'Files' })).toBeInTheDocument()
|
|
233
|
+
await user.type(screen.getByRole('combobox'), 'sett')
|
|
234
|
+
expect(screen.queryByRole('group', { name: 'Files' })).toBeNull()
|
|
235
|
+
expect(screen.getByRole('group', { name: 'App' })).toBeInTheDocument()
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
it('renders the frame and backdrop by default and drops them on request', async () => {
|
|
239
|
+
const user = userEvent.setup()
|
|
240
|
+
const { unmount } = render(<Palette />)
|
|
241
|
+
const framed = await openPalette(user)
|
|
242
|
+
expect(framed).toHaveAttribute('data-frame')
|
|
243
|
+
expect(document.querySelector('[data-slot="command-palette-backdrop"]')).not.toBeNull()
|
|
244
|
+
unmount()
|
|
245
|
+
|
|
246
|
+
render(<Palette content={{ frame: false }} />)
|
|
247
|
+
expect(await openPalette(user)).not.toHaveAttribute('data-frame')
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
it('omits the backdrop with backdrop={false}, which also drops the frame', async () => {
|
|
251
|
+
const user = userEvent.setup()
|
|
252
|
+
render(<Palette content={{ backdrop: false }} />)
|
|
253
|
+
const dialog = await openPalette(user)
|
|
254
|
+
expect(dialog).not.toHaveAttribute('data-frame')
|
|
255
|
+
expect(document.querySelector('[data-slot="command-palette-backdrop"]')).toBeNull()
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it('swaps the input appearance and its start adornment', async () => {
|
|
259
|
+
const user = userEvent.setup()
|
|
260
|
+
render(
|
|
261
|
+
<CommandPalette items={COMMANDS}>
|
|
262
|
+
<CommandPaletteTrigger render={<Button>Open palette</Button>} />
|
|
263
|
+
<CommandPaletteContent>
|
|
264
|
+
<CommandPaletteInput variant="outline" startSlot={null} endSlot={<span>⌘K</span>} />
|
|
265
|
+
<CommandPaletteList>{() => null}</CommandPaletteList>
|
|
266
|
+
</CommandPaletteContent>
|
|
267
|
+
</CommandPalette>,
|
|
268
|
+
)
|
|
269
|
+
await openPalette(user)
|
|
270
|
+
expect(document.querySelector('[data-slot="command-palette-input-start"]')).toBeNull()
|
|
271
|
+
expect(document.querySelector('[data-slot="command-palette-input-end"]')).toHaveTextContent('⌘K')
|
|
272
|
+
})
|
|
273
|
+
|
|
274
|
+
it('has no accessibility violations while open', async () => {
|
|
275
|
+
const user = userEvent.setup()
|
|
276
|
+
render(<Palette />)
|
|
277
|
+
await openPalette(user)
|
|
278
|
+
expect(await axe(document.body)).toHaveNoViolations()
|
|
279
|
+
})
|
|
280
|
+
})
|