@appica/ui-react 1.3.1 → 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.
Files changed (69) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/README.md +8 -2
  3. package/agent-rules.md +9 -2
  4. package/appica.css +2 -2
  5. package/dist/components/badge/badge.js +1 -1
  6. package/dist/components/badge/badge.js.map +1 -1
  7. package/dist/components/button/button-variants.js +1 -1
  8. package/dist/components/button/button-variants.js.map +1 -1
  9. package/dist/components/card/card.js +1 -1
  10. package/dist/components/card/card.js.map +1 -1
  11. package/dist/components/carousel/carousel.d.ts +24 -3
  12. package/dist/components/carousel/carousel.d.ts.map +1 -1
  13. package/dist/components/carousel/carousel.js +149 -31
  14. package/dist/components/carousel/carousel.js.map +1 -1
  15. package/dist/components/checkbox/checkbox.js +1 -1
  16. package/dist/components/checkbox/checkbox.js.map +1 -1
  17. package/dist/components/command-palette/command-palette.d.ts +91 -0
  18. package/dist/components/command-palette/command-palette.d.ts.map +1 -0
  19. package/dist/components/command-palette/command-palette.js +376 -0
  20. package/dist/components/command-palette/command-palette.js.map +1 -0
  21. package/dist/components/date-field/date-field.js +1 -1
  22. package/dist/components/date-field/date-field.js.map +1 -1
  23. package/dist/components/date-picker/date-picker.js +1 -1
  24. package/dist/components/date-picker/date-picker.js.map +1 -1
  25. package/dist/components/input/input-variants.js +2 -2
  26. package/dist/components/input/input-variants.js.map +1 -1
  27. package/dist/components/radio/radio.js +1 -1
  28. package/dist/components/radio/radio.js.map +1 -1
  29. package/dist/components/splitter/splitter.d.ts +57 -0
  30. package/dist/components/splitter/splitter.d.ts.map +1 -0
  31. package/dist/components/splitter/splitter.js +395 -0
  32. package/dist/components/splitter/splitter.js.map +1 -0
  33. package/dist/components/textarea/textarea.js +1 -1
  34. package/dist/components/textarea/textarea.js.map +1 -1
  35. package/dist/components/time-field/time-field.js +1 -1
  36. package/dist/components/time-field/time-field.js.map +1 -1
  37. package/dist/components/toc/toc.d.ts +6 -1
  38. package/dist/components/toc/toc.d.ts.map +1 -1
  39. package/dist/components/toc/toc.js +15 -7
  40. package/dist/components/toc/toc.js.map +1 -1
  41. package/dist/index.d.ts +2 -0
  42. package/dist/index.d.ts.map +1 -1
  43. package/dist/index.js +3 -1
  44. package/dist/index.js.map +1 -1
  45. package/package.json +24 -14
  46. package/src/components/badge/badge.tsx +1 -1
  47. package/src/components/button/button-variants.ts +1 -1
  48. package/src/components/card/card.tsx +1 -1
  49. package/src/components/carousel/carousel.test.tsx +559 -21
  50. package/src/components/carousel/carousel.tsx +195 -40
  51. package/src/components/checkbox/checkbox.tsx +1 -1
  52. package/src/components/command-palette/command-palette.test.tsx +280 -0
  53. package/src/components/command-palette/command-palette.tsx +544 -0
  54. package/src/components/compare-slider/compare-slider.test.tsx +8 -7
  55. package/src/components/date-field/date-field.tsx +1 -1
  56. package/src/components/date-picker/date-picker.tsx +1 -1
  57. package/src/components/input/input-variants.ts +2 -2
  58. package/src/components/radio/radio.tsx +1 -1
  59. package/src/components/rating/rating.test.tsx +10 -3
  60. package/src/components/splitter/splitter.test.tsx +415 -0
  61. package/src/components/splitter/splitter.tsx +555 -0
  62. package/src/components/textarea/textarea.tsx +1 -1
  63. package/src/components/time-field/time-field.tsx +1 -1
  64. package/src/components/toc/toc.test.tsx +15 -0
  65. package/src/components/toc/toc.tsx +22 -7
  66. package/src/index.ts +2 -0
  67. package/src/test/css.ts +9 -0
  68. package/src/test/setup.ts +13 -0
  69. package/styles.css +3 -0
@@ -69,7 +69,7 @@ vi.mock('embla-carousel-accessibility', () => ({
69
69
  default: (options?: object) => ({ name: 'accessibility', options: options ?? {}, init: () => {} }),
70
70
  }))
71
71
  vi.mock('embla-carousel-autoplay', () => ({
72
- default: () => ({ name: 'autoplay', options: {}, init: () => {} }),
72
+ default: (options?: object) => ({ name: 'autoplay', options: options ?? {}, init: () => {} }),
73
73
  }))
74
74
  vi.mock('embla-carousel-auto-scroll', () => ({
75
75
  default: () => ({ name: 'autoScroll', options: {}, init: () => {} }),
@@ -96,6 +96,7 @@ import {
96
96
  CarouselThumb,
97
97
  CarouselPrev,
98
98
  CarouselNext,
99
+ CarouselPlayToggle,
99
100
  CarouselPagination,
100
101
  CarouselProgress,
101
102
  useCarousel,
@@ -118,6 +119,9 @@ import { Button } from '../button/button'
118
119
  // eslint-disable-next-line import/first
119
120
  import { ReducedMotionProvider } from '../../providers/reduced-motion-provider'
120
121
 
122
+ // eslint-disable-next-line import/first
123
+ import { cssValue } from '../../test/css'
124
+
121
125
  /** Calls every handler still subscribed to an Embla event on the stub api. */
122
126
  function dispatch(event: string, payload?: unknown) {
123
127
  const removed = new Set(stubApi.off.mock.calls.filter((c: unknown[]) => c[0] === event).map((c: unknown[]) => c[1]))
@@ -137,14 +141,17 @@ function mockDomSlides() {
137
141
  }
138
142
 
139
143
  /** A stand-in for the Autoplay plugin that reports play and stop through the stub's events. */
140
- function mockAutoplay() {
144
+ function mockAutoplay(options: object = {}) {
141
145
  let playing = false
142
146
  const autoplay = {
143
- options: { delay: 4000 },
147
+ options: { delay: 4000, ...options },
144
148
  isPlaying: vi.fn(() => playing),
145
149
  play: vi.fn(() => {
146
- playing = true
147
- dispatch('autoplay:play')
150
+ if (!playing) {
151
+ playing = true
152
+ dispatch('autoplay:play')
153
+ }
154
+ dispatch('autoplay:timerset')
148
155
  }),
149
156
  stop: vi.fn(() => {
150
157
  if (!playing) return
@@ -157,6 +164,27 @@ function mockAutoplay() {
157
164
  return autoplay
158
165
  }
159
166
 
167
+ /** A stand-in for the Auto Scroll plugin, in the same shape as `mockAutoplay`. */
168
+ function mockAutoScroll() {
169
+ let playing = false
170
+ const autoScroll = {
171
+ options: {},
172
+ isPlaying: vi.fn(() => playing),
173
+ play: vi.fn(() => {
174
+ if (playing) return
175
+ playing = true
176
+ dispatch('autoscroll:play')
177
+ }),
178
+ stop: vi.fn(() => {
179
+ if (!playing) return
180
+ playing = false
181
+ dispatch('autoscroll:stop')
182
+ }),
183
+ }
184
+ stubApi.plugins.mockReturnValue({ autoScroll })
185
+ return autoScroll
186
+ }
187
+
160
188
  function Basic(props?: Partial<CarouselProps>) {
161
189
  return (
162
190
  <Carousel {...props}>
@@ -795,7 +823,7 @@ describe('CarouselThumbs / CarouselThumb', () => {
795
823
 
796
824
  const indicator = container.querySelector('[data-slot="carousel-thumbs-indicator"]') as HTMLElement
797
825
  expect(indicator).not.toBeNull()
798
- expect(indicator.style.translate).toBe('0px 0px')
826
+ expect(indicator.style.translate).toBe(cssValue('translate', '0px 0px'))
799
827
  })
800
828
 
801
829
  it('excludes the indicator from the rail snap count', () => {
@@ -994,7 +1022,8 @@ describe('Carousel autoplay', () => {
994
1022
  expect(autoplay.play).toHaveBeenCalledTimes(2)
995
1023
  })
996
1024
 
997
- it('pauses while focus is inside and resumes once it leaves the carousel', () => {
1025
+ it('pauses while keyboard focus is inside and resumes once it leaves the carousel', async () => {
1026
+ const user = userEvent.setup()
998
1027
  const autoplay = mockAutoplay()
999
1028
  render(
1000
1029
  <>
@@ -1002,15 +1031,77 @@ describe('Carousel autoplay', () => {
1002
1031
  <button type="button">Outside</button>
1003
1032
  </>,
1004
1033
  )
1005
- act(() => screen.getByRole('button', { name: 'Next slide' }).focus())
1034
+ await user.tab()
1035
+ expect(screen.getByRole('button', { name: 'Previous slide' })).toHaveFocus()
1006
1036
  expect(autoplay.isPlaying()).toBe(false)
1007
- act(() => screen.getByRole('button', { name: 'Previous slide' }).focus())
1037
+ await user.tab()
1038
+ expect(screen.getByRole('button', { name: 'Next slide' })).toHaveFocus()
1008
1039
  expect(autoplay.isPlaying()).toBe(false)
1009
1040
  act(() => screen.getByRole('button', { name: 'Outside' }).focus())
1010
1041
  expect(autoplay.isPlaying()).toBe(true)
1011
1042
  })
1012
1043
 
1013
- it('waits for both the pointer and focus to leave', () => {
1044
+ it('keeps playing after a mouse click on an arrow, and pauses once Tab lands on it', async () => {
1045
+ const user = userEvent.setup()
1046
+ const autoplay = mockAutoplay()
1047
+ render(
1048
+ <>
1049
+ <button type="button">Outside</button>
1050
+ <Basic autoplay />
1051
+ </>,
1052
+ )
1053
+ const next = screen.getByRole('button', { name: 'Next slide' })
1054
+ await user.click(next)
1055
+ expect(autoplay.isPlaying()).toBe(false)
1056
+ await user.unhover(next)
1057
+ expect(next).toHaveFocus()
1058
+ expect(autoplay.isPlaying()).toBe(true)
1059
+
1060
+ act(() => screen.getByRole('button', { name: 'Outside' }).focus())
1061
+ await user.tab()
1062
+ await user.tab()
1063
+ expect(next).toHaveFocus()
1064
+ expect(autoplay.isPlaying()).toBe(false)
1065
+ })
1066
+
1067
+ it('hands rotation back when a mouse click follows keyboard focus inside', async () => {
1068
+ const user = userEvent.setup()
1069
+ const autoplay = mockAutoplay()
1070
+ render(<Basic autoplay={{ pauseOnHover: false }} />)
1071
+ await user.tab()
1072
+ expect(autoplay.isPlaying()).toBe(false)
1073
+ await user.click(screen.getByRole('button', { name: 'Next slide' }))
1074
+ expect(autoplay.isPlaying()).toBe(true)
1075
+ })
1076
+
1077
+ it('starts again with mouse focus still inside when reduced motion turns off', async () => {
1078
+ const user = userEvent.setup()
1079
+ const autoplay = mockAutoplay()
1080
+ const { rerender } = render(
1081
+ <ReducedMotionProvider>
1082
+ <Basic autoplay />
1083
+ </ReducedMotionProvider>,
1084
+ )
1085
+ const next = screen.getByRole('button', { name: 'Next slide' })
1086
+ await user.click(next)
1087
+ await user.unhover(next)
1088
+ expect(next).toHaveFocus()
1089
+ rerender(
1090
+ <ReducedMotionProvider disableAnimations>
1091
+ <Basic autoplay />
1092
+ </ReducedMotionProvider>,
1093
+ )
1094
+ expect(autoplay.isPlaying()).toBe(false)
1095
+ rerender(
1096
+ <ReducedMotionProvider>
1097
+ <Basic autoplay />
1098
+ </ReducedMotionProvider>,
1099
+ )
1100
+ expect(autoplay.isPlaying()).toBe(true)
1101
+ })
1102
+
1103
+ it('waits for both the pointer and focus to leave', async () => {
1104
+ const user = userEvent.setup()
1014
1105
  const autoplay = mockAutoplay()
1015
1106
  const { container } = render(
1016
1107
  <>
@@ -1019,13 +1110,46 @@ describe('Carousel autoplay', () => {
1019
1110
  </>,
1020
1111
  )
1021
1112
  fireEvent.pointerEnter(root(container))
1022
- act(() => screen.getByRole('button', { name: 'Next slide' }).focus())
1113
+ await user.tab()
1023
1114
  fireEvent.pointerLeave(root(container))
1024
1115
  expect(autoplay.isPlaying()).toBe(false)
1025
1116
  act(() => screen.getByRole('button', { name: 'Outside' }).focus())
1026
1117
  expect(autoplay.isPlaying()).toBe(true)
1027
1118
  })
1028
1119
 
1120
+ it('keeps playing while hovered with pauseOnHover: false', async () => {
1121
+ const user = userEvent.setup()
1122
+ const autoplay = mockAutoplay()
1123
+ const { container } = render(<Basic autoplay={{ pauseOnHover: false }} />)
1124
+ fireEvent.pointerEnter(root(container))
1125
+ expect(autoplay.isPlaying()).toBe(true)
1126
+ await user.tab()
1127
+ expect(autoplay.isPlaying()).toBe(false)
1128
+ })
1129
+
1130
+ it('keeps playing with keyboard focus inside with pauseOnFocus: false', async () => {
1131
+ const user = userEvent.setup()
1132
+ const autoplay = mockAutoplay()
1133
+ const { container } = render(<Basic autoplay={{ pauseOnFocus: false }} />)
1134
+ await user.tab()
1135
+ expect(screen.getByRole('button', { name: 'Previous slide' })).toHaveFocus()
1136
+ expect(autoplay.isPlaying()).toBe(true)
1137
+ fireEvent.pointerEnter(root(container))
1138
+ expect(autoplay.isPlaying()).toBe(false)
1139
+ })
1140
+
1141
+ it('keeps pauseOnHover and pauseOnFocus away from the Embla plugin', () => {
1142
+ render(<Basic autoplay={{ delay: 3000, resumeAfter: 1000, pauseOnHover: false, pauseOnFocus: false }} />)
1143
+ const plugins = (useEmblaCarousel as unknown as ReturnType<typeof vi.fn>).mock.calls.at(-1)?.[1] as {
1144
+ name: string
1145
+ options: object
1146
+ }[]
1147
+ expect(plugins.find((plugin) => plugin.name === 'autoplay')?.options).toEqual({
1148
+ delay: 3000,
1149
+ defaultInteraction: false,
1150
+ })
1151
+ })
1152
+
1029
1153
  it('keeps a pointerdown stop after the pointer leaves, without resumeAfter', () => {
1030
1154
  const autoplay = mockAutoplay()
1031
1155
  const { container } = render(<Basic autoplay />)
@@ -1036,23 +1160,24 @@ describe('Carousel autoplay', () => {
1036
1160
  expect(autoplay.play).toHaveBeenCalledTimes(1)
1037
1161
  })
1038
1162
 
1039
- it('holds the resumeAfter restart while hovered or focused', () => {
1163
+ it('holds the resumeAfter restart while hovered or focused', async () => {
1164
+ const user = userEvent.setup()
1165
+ const autoplay = mockAutoplay({ defaultInteraction: false })
1166
+ const { container } = render(
1167
+ <>
1168
+ <Basic autoplay={{ resumeAfter: 1000 }} />
1169
+ <button type="button">Outside</button>
1170
+ </>,
1171
+ )
1172
+ await user.tab()
1040
1173
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
1041
1174
  try {
1042
- const autoplay = mockAutoplay()
1043
- const { container } = render(
1044
- <>
1045
- <Basic autoplay={{ resumeAfter: 1000 }} />
1046
- <button type="button">Outside</button>
1047
- </>,
1048
- )
1049
1175
  fireEvent.pointerEnter(root(container))
1050
1176
  emit('pointerdown')
1051
1177
  emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
1052
1178
  act(() => vi.advanceTimersByTime(1000))
1053
1179
  expect(autoplay.isPlaying()).toBe(false)
1054
1180
 
1055
- act(() => screen.getByRole('button', { name: 'Next slide' }).focus())
1056
1181
  fireEvent.pointerLeave(root(container))
1057
1182
  expect(autoplay.isPlaying()).toBe(false)
1058
1183
 
@@ -1063,10 +1188,29 @@ describe('Carousel autoplay', () => {
1063
1188
  }
1064
1189
  })
1065
1190
 
1191
+ it('restarts after a drag with resumeAfter while the pointer and focus stay, with both pauses off', async () => {
1192
+ const user = userEvent.setup()
1193
+ const autoplay = mockAutoplay({ defaultInteraction: false })
1194
+ const { container } = render(<Basic autoplay={{ resumeAfter: 1000, pauseOnHover: false, pauseOnFocus: false }} />)
1195
+ fireEvent.pointerEnter(root(container))
1196
+ await user.tab()
1197
+ expect(autoplay.isPlaying()).toBe(true)
1198
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
1199
+ try {
1200
+ emit('pointerdown')
1201
+ emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
1202
+ expect(autoplay.isPlaying()).toBe(false)
1203
+ act(() => vi.advanceTimersByTime(1000))
1204
+ expect(autoplay.isPlaying()).toBe(true)
1205
+ } finally {
1206
+ vi.useRealTimers()
1207
+ }
1208
+ })
1209
+
1066
1210
  it('restarts after resumeAfter once nothing is inside', () => {
1067
1211
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
1068
1212
  try {
1069
- const autoplay = mockAutoplay()
1213
+ const autoplay = mockAutoplay({ defaultInteraction: false })
1070
1214
  render(<Basic autoplay={{ resumeAfter: 1000 }} />)
1071
1215
  emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
1072
1216
  expect(autoplay.isPlaying()).toBe(false)
@@ -1078,6 +1222,380 @@ describe('Carousel autoplay', () => {
1078
1222
  vi.useRealTimers()
1079
1223
  }
1080
1224
  })
1225
+
1226
+ describe('after a re-init', () => {
1227
+ // Embla destroys the plugin on a re-init (a resize, a slide added or removed), which stops it.
1228
+ function reInit(plugin: { stop: () => void }) {
1229
+ act(() => {
1230
+ plugin.stop()
1231
+ dispatch('reinit')
1232
+ })
1233
+ }
1234
+
1235
+ function PlayingProbe() {
1236
+ const { autoplay } = useCarousel()
1237
+ return <output data-testid="playing">{String(autoplay?.isPlaying)}</output>
1238
+ }
1239
+
1240
+ it('plays again when it was playing', () => {
1241
+ const autoplay = mockAutoplay()
1242
+ render(
1243
+ <Carousel autoplay>
1244
+ <CarouselContent>
1245
+ <CarouselSlide>Slide 1</CarouselSlide>
1246
+ <CarouselSlide>Slide 2</CarouselSlide>
1247
+ </CarouselContent>
1248
+ <PlayingProbe />
1249
+ </Carousel>,
1250
+ )
1251
+ reInit(autoplay)
1252
+ expect(autoplay.play).toHaveBeenCalledTimes(2)
1253
+ expect(autoplay.isPlaying()).toBe(true)
1254
+ expect(screen.getByTestId('playing')).toHaveTextContent('true')
1255
+ })
1256
+
1257
+ it('stays held while hovered and plays once the pointer leaves', () => {
1258
+ const autoplay = mockAutoplay()
1259
+ const { container } = render(<Basic autoplay />)
1260
+ fireEvent.pointerEnter(root(container))
1261
+ reInit(autoplay)
1262
+ expect(autoplay.isPlaying()).toBe(false)
1263
+ fireEvent.pointerLeave(root(container))
1264
+ expect(autoplay.isPlaying()).toBe(true)
1265
+ })
1266
+
1267
+ it('stays held with keyboard focus inside and plays once it leaves', async () => {
1268
+ const user = userEvent.setup()
1269
+ const autoplay = mockAutoplay()
1270
+ render(
1271
+ <>
1272
+ <Basic autoplay />
1273
+ <button type="button">Outside</button>
1274
+ </>,
1275
+ )
1276
+ await user.tab()
1277
+ reInit(autoplay)
1278
+ expect(autoplay.isPlaying()).toBe(false)
1279
+ act(() => screen.getByRole('button', { name: 'Outside' }).focus())
1280
+ expect(autoplay.isPlaying()).toBe(true)
1281
+ })
1282
+
1283
+ it('stays stopped after a drag stopped it for good', () => {
1284
+ const autoplay = mockAutoplay()
1285
+ render(<Basic autoplay />)
1286
+ act(() => {
1287
+ dispatch('pointerdown')
1288
+ autoplay.stop()
1289
+ })
1290
+ reInit(autoplay)
1291
+ expect(autoplay.isPlaying()).toBe(false)
1292
+ expect(autoplay.play).toHaveBeenCalledTimes(1)
1293
+ })
1294
+
1295
+ it('leaves a pending resumeAfter restart to its timer', () => {
1296
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
1297
+ try {
1298
+ const autoplay = mockAutoplay({ defaultInteraction: false })
1299
+ render(<Basic autoplay={{ resumeAfter: 1000 }} />)
1300
+ emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
1301
+ reInit(autoplay)
1302
+ expect(autoplay.isPlaying()).toBe(false)
1303
+ act(() => vi.advanceTimersByTime(1000))
1304
+ expect(autoplay.isPlaying()).toBe(true)
1305
+ } finally {
1306
+ vi.useRealTimers()
1307
+ }
1308
+ })
1309
+
1310
+ it('stays off under reduced motion', () => {
1311
+ const autoplay = mockAutoplay()
1312
+ render(
1313
+ <ReducedMotionProvider disableAnimations>
1314
+ <Basic autoplay />
1315
+ </ReducedMotionProvider>,
1316
+ )
1317
+ reInit(autoplay)
1318
+ expect(autoplay.play).not.toHaveBeenCalled()
1319
+ })
1320
+
1321
+ it('stays stopped on the last snap with stopOnLastSnap', () => {
1322
+ const autoplay = mockAutoplay({ stopOnLastSnap: true })
1323
+ render(<Basic autoplay={{ stopOnLastSnap: true }} />)
1324
+ stubApi.canGoToNext.mockReturnValue(false)
1325
+ reInit(autoplay)
1326
+ expect(autoplay.isPlaying()).toBe(false)
1327
+ })
1328
+
1329
+ it('plays Auto Scroll again when it was playing', () => {
1330
+ const autoScroll = mockAutoScroll()
1331
+ render(<Basic autoScroll />)
1332
+ reInit(autoScroll)
1333
+ expect(autoScroll.play).toHaveBeenCalledTimes(2)
1334
+ expect(autoScroll.isPlaying()).toBe(true)
1335
+ })
1336
+ })
1337
+ })
1338
+
1339
+ describe('CarouselPlayToggle', () => {
1340
+ function WithToggle({ children, ...props }: Partial<CarouselProps>) {
1341
+ return (
1342
+ <Carousel {...props}>
1343
+ <CarouselPlayToggle>
1344
+ <svg data-icon="pause" aria-hidden="true" className="in-data-paused:hidden" />
1345
+ <svg data-icon="play" aria-hidden="true" className="not-in-data-paused:hidden" />
1346
+ </CarouselPlayToggle>
1347
+ <CarouselContent>
1348
+ <CarouselSlide>Slide 1</CarouselSlide>
1349
+ <CarouselSlide>Slide 2</CarouselSlide>
1350
+ <CarouselSlide>Slide 3</CarouselSlide>
1351
+ </CarouselContent>
1352
+ <CarouselPrev />
1353
+ <CarouselNext />
1354
+ <CarouselPagination />
1355
+ {children}
1356
+ </Carousel>
1357
+ )
1358
+ }
1359
+
1360
+ function CycleProbe() {
1361
+ const { autoplay } = useCarousel()
1362
+ return <output data-testid="cycle">{autoplay?.cycleId}</output>
1363
+ }
1364
+
1365
+ function root(container: HTMLElement) {
1366
+ return container.querySelector('[data-slot="carousel"]') as HTMLElement
1367
+ }
1368
+
1369
+ const toggle = () => screen.getByRole('button', { name: 'Pause slides' })
1370
+
1371
+ it('renders nothing without Autoplay or Auto Scroll', () => {
1372
+ render(<WithToggle />)
1373
+ expect(screen.queryByRole('button', { name: 'Pause slides' })).toBeNull()
1374
+ })
1375
+
1376
+ it('renders nothing under reduced motion', () => {
1377
+ mockAutoplay()
1378
+ render(
1379
+ <ReducedMotionProvider disableAnimations>
1380
+ <WithToggle autoplay />
1381
+ </ReducedMotionProvider>,
1382
+ )
1383
+ expect(screen.queryByRole('button', { name: 'Pause slides' })).toBeNull()
1384
+ })
1385
+
1386
+ it('renders both icons as children and marks the pause with aria-pressed and data-paused', async () => {
1387
+ const user = userEvent.setup()
1388
+ const autoplay = mockAutoplay()
1389
+ render(<WithToggle autoplay />)
1390
+ const button = toggle()
1391
+ expect(button).toHaveAttribute('data-slot', 'carousel-play-toggle')
1392
+ expect(button).toHaveAttribute('aria-pressed', 'false')
1393
+ expect(button).not.toHaveAttribute('data-paused')
1394
+ expect(button.querySelector('[data-icon="pause"]')).not.toBeNull()
1395
+ expect(button.querySelector('[data-icon="play"]')).not.toBeNull()
1396
+
1397
+ await user.click(button)
1398
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1399
+ expect(toggle()).toHaveAttribute('data-paused')
1400
+ expect(autoplay.isPlaying()).toBe(false)
1401
+
1402
+ await user.click(toggle())
1403
+ expect(toggle()).toHaveAttribute('aria-pressed', 'false')
1404
+ expect(toggle()).not.toHaveAttribute('data-paused')
1405
+ expect(autoplay.isPlaying()).toBe(true)
1406
+ })
1407
+
1408
+ it('keeps a pause through a re-init, for Autoplay and Auto Scroll', async () => {
1409
+ const user = userEvent.setup()
1410
+ const autoplay = mockAutoplay()
1411
+ const { unmount } = render(<WithToggle autoplay />)
1412
+ await user.click(toggle())
1413
+ act(() => dispatch('reinit'))
1414
+ expect(autoplay.isPlaying()).toBe(false)
1415
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1416
+ unmount()
1417
+
1418
+ const autoScroll = mockAutoScroll()
1419
+ render(<WithToggle autoScroll />)
1420
+ await user.click(toggle())
1421
+ act(() => dispatch('reinit'))
1422
+ expect(autoScroll.isPlaying()).toBe(false)
1423
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1424
+ })
1425
+
1426
+ it('passes paused to a render function', async () => {
1427
+ const user = userEvent.setup()
1428
+ mockAutoplay()
1429
+ render(
1430
+ <Carousel autoplay>
1431
+ <CarouselPlayToggle render={(props, state) => <button {...props}>{state.paused ? 'Play' : 'Pause'}</button>} />
1432
+ <CarouselContent>
1433
+ <CarouselSlide>Slide 1</CarouselSlide>
1434
+ <CarouselSlide>Slide 2</CarouselSlide>
1435
+ </CarouselContent>
1436
+ </Carousel>,
1437
+ )
1438
+ expect(toggle()).toHaveTextContent('Pause')
1439
+ await user.click(toggle())
1440
+ expect(toggle()).toHaveTextContent('Play')
1441
+ })
1442
+
1443
+ it('keeps a pause through hover and focus leaving, resumeAfter, a drag, slide focus and the wheel', async () => {
1444
+ const user = userEvent.setup()
1445
+ const viewport = document.createElement('div')
1446
+ const autoplay = mockAutoplay({ defaultInteraction: false })
1447
+ stubApi.plugins.mockReturnValue({ autoplay, wheelGestures: {} })
1448
+ stubApi.rootNode.mockReturnValue(viewport)
1449
+ render(
1450
+ <>
1451
+ <WithToggle autoplay={{ resumeAfter: 1000 }} />
1452
+ <button type="button">Outside</button>
1453
+ </>,
1454
+ )
1455
+ await user.click(toggle())
1456
+ await user.tab()
1457
+ expect(screen.getByRole('button', { name: 'Previous slide' })).toHaveFocus()
1458
+ await user.unhover(toggle())
1459
+ act(() => screen.getByRole('button', { name: 'Outside' }).focus())
1460
+ expect(autoplay.isPlaying()).toBe(false)
1461
+
1462
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
1463
+ try {
1464
+ emit('pointerdown')
1465
+ emit('autoplay:interaction', { detail: { interaction: 'pointerdown' } })
1466
+ emit('slidefocus')
1467
+ emit('autoplay:interaction', { detail: { interaction: 'slidefocus' } })
1468
+ act(() => {
1469
+ viewport.dispatchEvent(new WheelEvent('wheel', { deltaX: 40, deltaY: 2 }))
1470
+ })
1471
+ act(() => vi.advanceTimersByTime(1000))
1472
+ expect(autoplay.isPlaying()).toBe(false)
1473
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1474
+ } finally {
1475
+ vi.useRealTimers()
1476
+ }
1477
+ })
1478
+
1479
+ it('stays paused when paused during a hold, once the hold ends', () => {
1480
+ const autoplay = mockAutoplay()
1481
+ const { container } = render(<WithToggle autoplay />)
1482
+ fireEvent.pointerEnter(root(container))
1483
+ expect(autoplay.isPlaying()).toBe(false)
1484
+ fireEvent.click(toggle())
1485
+ fireEvent.pointerLeave(root(container))
1486
+ expect(autoplay.isPlaying()).toBe(false)
1487
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1488
+ })
1489
+
1490
+ it('keeps the pause when new options restart the plugin wiring', () => {
1491
+ const autoplay = mockAutoplay()
1492
+ const { rerender } = render(<WithToggle autoplay />)
1493
+ fireEvent.click(toggle())
1494
+ rerender(<WithToggle autoplay={{ resumeAfter: 500, pauseOnHover: false }} />)
1495
+ expect(autoplay.isPlaying()).toBe(false)
1496
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1497
+ })
1498
+
1499
+ it('starts a fresh cycle on play, even while hovered', () => {
1500
+ const autoplay = mockAutoplay()
1501
+ const { container } = render(
1502
+ <WithToggle autoplay>
1503
+ <CycleProbe />
1504
+ </WithToggle>,
1505
+ )
1506
+ fireEvent.click(toggle())
1507
+ fireEvent.pointerEnter(root(container))
1508
+ const cycle = Number(screen.getByTestId('cycle').textContent)
1509
+ autoplay.play.mockClear()
1510
+ fireEvent.click(toggle())
1511
+ expect(autoplay.play).toHaveBeenCalledTimes(1)
1512
+ expect(Number(screen.getByTestId('cycle').textContent)).toBe(cycle + 1)
1513
+ expect(autoplay.isPlaying()).toBe(true)
1514
+ })
1515
+
1516
+ it('toggles with Enter and Space', async () => {
1517
+ const user = userEvent.setup()
1518
+ const autoplay = mockAutoplay()
1519
+ render(<WithToggle autoplay />)
1520
+ await user.tab()
1521
+ expect(toggle()).toHaveFocus()
1522
+ await user.keyboard('{Enter}')
1523
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1524
+ expect(autoplay.isPlaying()).toBe(false)
1525
+ await user.keyboard(' ')
1526
+ expect(toggle()).toHaveAttribute('aria-pressed', 'false')
1527
+ expect(autoplay.isPlaying()).toBe(true)
1528
+ })
1529
+
1530
+ it('reads a drag that stops autoplay for good as a pause', () => {
1531
+ mockAutoplay()
1532
+ render(<WithToggle autoplay />)
1533
+ emit('pointerdown')
1534
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1535
+ })
1536
+
1537
+ it('stays unpressed after a drag when autoplay keeps running through it', () => {
1538
+ mockAutoplay({ defaultInteraction: false })
1539
+ render(<WithToggle autoplay={{ defaultInteraction: false }} />)
1540
+ emit('pointerdown')
1541
+ expect(toggle()).toHaveAttribute('aria-pressed', 'false')
1542
+ })
1543
+
1544
+ it('clears the pause when the plugin is played directly', () => {
1545
+ const autoplay = mockAutoplay()
1546
+ render(<WithToggle autoplay />)
1547
+ fireEvent.click(toggle())
1548
+ act(() => autoplay.play())
1549
+ expect(toggle()).toHaveAttribute('aria-pressed', 'false')
1550
+ })
1551
+
1552
+ it('drives Auto Scroll, and resumeAfter leaves its pause alone', () => {
1553
+ const autoScroll = mockAutoScroll()
1554
+ render(<WithToggle autoScroll={{ resumeAfter: 1000 }} />)
1555
+ expect(autoScroll.isPlaying()).toBe(true)
1556
+ fireEvent.click(toggle())
1557
+ expect(autoScroll.isPlaying()).toBe(false)
1558
+ expect(toggle()).toHaveAttribute('aria-pressed', 'true')
1559
+
1560
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] })
1561
+ try {
1562
+ emit('autoscroll:interaction', { detail: { interaction: 'pointerdown' } })
1563
+ act(() => vi.advanceTimersByTime(1000))
1564
+ expect(autoScroll.isPlaying()).toBe(false)
1565
+ } finally {
1566
+ vi.useRealTimers()
1567
+ }
1568
+
1569
+ fireEvent.click(toggle())
1570
+ expect(autoScroll.isPlaying()).toBe(true)
1571
+ expect(toggle()).toHaveAttribute('aria-pressed', 'false')
1572
+ })
1573
+
1574
+ it('gives a custom control the same paused, pause and play through useCarousel()', async () => {
1575
+ const user = userEvent.setup()
1576
+ const autoplay = mockAutoplay()
1577
+ function Custom() {
1578
+ const { paused, pause, play } = useCarousel()
1579
+ return (
1580
+ <button type="button" onClick={paused ? play : pause}>
1581
+ {paused ? 'Resume' : 'Hold'}
1582
+ </button>
1583
+ )
1584
+ }
1585
+ render(
1586
+ <Carousel autoplay={{ pauseOnHover: false }}>
1587
+ <Custom />
1588
+ <CarouselContent>
1589
+ <CarouselSlide>Slide 1</CarouselSlide>
1590
+ <CarouselSlide>Slide 2</CarouselSlide>
1591
+ </CarouselContent>
1592
+ </Carousel>,
1593
+ )
1594
+ await user.click(screen.getByRole('button', { name: 'Hold' }))
1595
+ expect(autoplay.isPlaying()).toBe(false)
1596
+ await user.click(screen.getByRole('button', { name: 'Resume' }))
1597
+ expect(autoplay.isPlaying()).toBe(true)
1598
+ })
1081
1599
  })
1082
1600
 
1083
1601
  describe('useLinkedCarousels', () => {
@@ -1164,6 +1682,26 @@ describe('a11y', () => {
1164
1682
  expect(results).toHaveNoViolations()
1165
1683
  })
1166
1684
 
1685
+ it('has no accessibility violations with a play toggle, playing or paused', async () => {
1686
+ mockAutoplay()
1687
+ const { container } = render(
1688
+ <Carousel autoplay aria-label="Featured">
1689
+ <CarouselPlayToggle>
1690
+ <svg aria-hidden="true" />
1691
+ </CarouselPlayToggle>
1692
+ <CarouselContent>
1693
+ <CarouselSlide>Slide 1</CarouselSlide>
1694
+ <CarouselSlide>Slide 2</CarouselSlide>
1695
+ </CarouselContent>
1696
+ <CarouselPrev />
1697
+ <CarouselNext />
1698
+ </Carousel>,
1699
+ )
1700
+ expect(await axe(container)).toHaveNoViolations()
1701
+ fireEvent.click(screen.getByRole('button', { name: 'Pause slides' }))
1702
+ expect(await axe(container)).toHaveNoViolations()
1703
+ })
1704
+
1167
1705
  it('has no accessibility violations with a thumbnail rail', async () => {
1168
1706
  const { container } = render(
1169
1707
  <Carousel>