@appica/ui-react 1.3.0 → 1.3.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 (77) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/agent-rules.md +2 -0
  3. package/appica.css +1 -1
  4. package/dist/components/autocomplete/autocomplete.d.ts.map +1 -1
  5. package/dist/components/autocomplete/autocomplete.js +1 -5
  6. package/dist/components/autocomplete/autocomplete.js.map +1 -1
  7. package/dist/components/breadcrumb/breadcrumb.js +1 -1
  8. package/dist/components/breadcrumb/breadcrumb.js.map +1 -1
  9. package/dist/components/carousel/carousel.d.ts +2 -2
  10. package/dist/components/carousel/carousel.d.ts.map +1 -1
  11. package/dist/components/carousel/carousel.js +151 -32
  12. package/dist/components/carousel/carousel.js.map +1 -1
  13. package/dist/components/combobox/combobox.d.ts.map +1 -1
  14. package/dist/components/combobox/combobox.js +1 -5
  15. package/dist/components/combobox/combobox.js.map +1 -1
  16. package/dist/components/copy-button/copy-button.d.ts +2 -2
  17. package/dist/components/copy-button/copy-button.d.ts.map +1 -1
  18. package/dist/components/copy-button/copy-button.js +49 -46
  19. package/dist/components/copy-button/copy-button.js.map +1 -1
  20. package/dist/components/date-field/date-field.d.ts.map +1 -1
  21. package/dist/components/date-field/date-field.js +7 -0
  22. package/dist/components/date-field/date-field.js.map +1 -1
  23. package/dist/components/date-picker/date-picker.d.ts +10 -0
  24. package/dist/components/date-picker/date-picker.d.ts.map +1 -1
  25. package/dist/components/date-picker/date-picker.js +14 -0
  26. package/dist/components/date-picker/date-picker.js.map +1 -1
  27. package/dist/components/field/field.d.ts.map +1 -1
  28. package/dist/components/field/field.js +1 -0
  29. package/dist/components/field/field.js.map +1 -1
  30. package/dist/components/loader/loader.d.ts +1 -1
  31. package/dist/components/loader/loader.d.ts.map +1 -1
  32. package/dist/components/loader/loader.js +7 -5
  33. package/dist/components/loader/loader.js.map +1 -1
  34. package/dist/components/number-field/number-field.d.ts +1 -1
  35. package/dist/components/number-field/number-field.d.ts.map +1 -1
  36. package/dist/components/number-field/number-field.js +7 -0
  37. package/dist/components/number-field/number-field.js.map +1 -1
  38. package/dist/components/pagination/pagination.d.ts.map +1 -1
  39. package/dist/components/pagination/pagination.js +1 -2
  40. package/dist/components/pagination/pagination.js.map +1 -1
  41. package/dist/components/spinner/spinner.d.ts +1 -1
  42. package/dist/components/spinner/spinner.d.ts.map +1 -1
  43. package/dist/components/spinner/spinner.js +7 -5
  44. package/dist/components/spinner/spinner.js.map +1 -1
  45. package/dist/components/time-field/time-field.d.ts.map +1 -1
  46. package/dist/components/time-field/time-field.js +7 -0
  47. package/dist/components/time-field/time-field.js.map +1 -1
  48. package/package.json +11 -11
  49. package/src/components/autocomplete/autocomplete.test.tsx +33 -0
  50. package/src/components/autocomplete/autocomplete.tsx +1 -5
  51. package/src/components/breadcrumb/breadcrumb.test.tsx +57 -2
  52. package/src/components/breadcrumb/breadcrumb.tsx +1 -1
  53. package/src/components/carousel/carousel.test.tsx +512 -12
  54. package/src/components/carousel/carousel.tsx +171 -33
  55. package/src/components/chip/chip.test.tsx +4 -5
  56. package/src/components/combobox/combobox.test.tsx +33 -0
  57. package/src/components/combobox/combobox.tsx +1 -5
  58. package/src/components/context-menu/context-menu.test.tsx +2 -1
  59. package/src/components/copy-button/copy-button.test.tsx +68 -3
  60. package/src/components/copy-button/copy-button.tsx +49 -43
  61. package/src/components/date-field/date-field.test.tsx +82 -0
  62. package/src/components/date-field/date-field.tsx +7 -0
  63. package/src/components/date-picker/date-picker.test.tsx +70 -0
  64. package/src/components/date-picker/date-picker.tsx +26 -0
  65. package/src/components/dropdown-menu/dropdown-menu.test.tsx +2 -1
  66. package/src/components/field/field.test.tsx +34 -0
  67. package/src/components/field/field.tsx +1 -0
  68. package/src/components/loader/loader.test.tsx +17 -4
  69. package/src/components/loader/loader.tsx +2 -2
  70. package/src/components/number-field/number-field.test.tsx +59 -0
  71. package/src/components/number-field/number-field.tsx +8 -0
  72. package/src/components/pagination/pagination.test.tsx +83 -0
  73. package/src/components/pagination/pagination.tsx +1 -2
  74. package/src/components/spinner/spinner.test.tsx +17 -4
  75. package/src/components/spinner/spinner.tsx +2 -2
  76. package/src/components/time-field/time-field.test.tsx +64 -0
  77. package/src/components/time-field/time-field.tsx +7 -0
@@ -12,7 +12,7 @@ import ClassNames, { type ClassNamesOptionsType } from 'embla-carousel-class-nam
12
12
  import Fade, { type FadeOptionsType } from 'embla-carousel-fade'
13
13
  import { WheelGesturesPlugin, type WheelGesturesPluginOptions } from 'embla-carousel-wheel-gestures'
14
14
  import type { EmblaCarouselType, EmblaOptionsType, EmblaPluginType } from 'embla-carousel'
15
- import { cn } from '../../internal/utils'
15
+ import { cn, useComposedRefs } from '../../internal/utils'
16
16
  import { useDirection } from '../../hooks/use-direction'
17
17
  import { useReducedMotion } from '../../hooks/use-reduced-motion'
18
18
 
@@ -67,6 +67,80 @@ function useCarousel(): CarouselContextValue {
67
67
  return ctx
68
68
  }
69
69
 
70
+ // The root already names the carousel, so the plugin's empty values keep the
71
+ // viewport from becoming a second landmark (its setter skips empty strings).
72
+ const CAROUSEL_ACCESSIBILITY_VIEWPORT: CarouselAccessibilityOptions = {
73
+ carouselRole: '',
74
+ carouselAriaLabel: '',
75
+ carouselAriaRoleDescription: '',
76
+ }
77
+
78
+ function getShownSlides(api: CarouselApi): Set<number> {
79
+ const { options, scrollSnapList } = api.internalEngine()
80
+ if (!options.active) return new Set(api.slideNodes().keys())
81
+ // Fade stacks every slide in one place, and the in-view list stays empty
82
+ // until the engine's IntersectionObserver first reports.
83
+ const inView = api.plugins().fade ? [] : api.slidesInView()
84
+ return new Set(inView.length ? inView : (scrollSnapList.slidesBySnap[api.selectedSnap()] ?? []))
85
+ }
86
+
87
+ function useSlideVisibility(api: CarouselApi | undefined, enabled: boolean) {
88
+ React.useEffect(() => {
89
+ if (!api || !enabled) return
90
+ const inert = new Set<HTMLElement>()
91
+ const labels = new Map<HTMLElement, string>()
92
+
93
+ const release = (node: HTMLElement) => {
94
+ if (inert.delete(node)) node.removeAttribute('inert')
95
+ if (labels.has(node) && node.getAttribute('aria-label') === labels.get(node)) node.removeAttribute('aria-label')
96
+ labels.delete(node)
97
+ }
98
+
99
+ const sync = (focused: EventTarget | null) => {
100
+ const slides = api.slideNodes()
101
+ const shown = getShownSlides(api)
102
+ new Set([...inert, ...labels.keys()]).forEach((node) => {
103
+ if (!slides.includes(node)) release(node)
104
+ })
105
+ slides.forEach((node, index) => {
106
+ const hidden = !shown.has(index) && !(focused instanceof Node && node.contains(focused))
107
+ if (hidden && !inert.has(node)) {
108
+ node.setAttribute('inert', '')
109
+ inert.add(node)
110
+ } else if (!hidden && inert.delete(node)) {
111
+ node.removeAttribute('inert')
112
+ }
113
+
114
+ const ownLabel = node.getAttribute('aria-label')
115
+ if (node.hasAttribute('aria-labelledby') || (ownLabel !== null && ownLabel !== labels.get(node))) {
116
+ labels.delete(node)
117
+ return
118
+ }
119
+ const label = `${index + 1} of ${slides.length}`
120
+ if (ownLabel !== label) node.setAttribute('aria-label', label)
121
+ labels.set(node, label)
122
+ })
123
+ }
124
+
125
+ const onChange = () => sync(document.activeElement)
126
+ const onFocusOut = (event: FocusEvent) => sync(event.relatedTarget)
127
+ const viewport = api.rootNode()
128
+
129
+ onChange()
130
+ api.on('select', onChange)
131
+ api.on('reinit', onChange)
132
+ api.on('slidesinview', onChange)
133
+ viewport?.addEventListener('focusout', onFocusOut)
134
+ return () => {
135
+ api.off('select', onChange)
136
+ api.off('reinit', onChange)
137
+ api.off('slidesinview', onChange)
138
+ viewport?.removeEventListener('focusout', onFocusOut)
139
+ new Set([...inert, ...labels.keys()]).forEach(release)
140
+ }
141
+ }, [api, enabled])
142
+ }
143
+
70
144
  interface CarouselProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onSelect' | 'onScroll' | 'children'> {
71
145
  /**
72
146
  * Scroll axis. Maps to Embla's `axis`; exposed as `data-orientation`.
@@ -111,7 +185,7 @@ interface CarouselProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onS
111
185
  duration?: number
112
186
  /** Escape hatch: a raw Embla options object, merged last so it wins over the flat props. */
113
187
  options?: CarouselOptions
114
- /** Enable the Autoplay plugin. `resumeAfter` (ms) restarts the timer after user interaction. */
188
+ /** Enable the Autoplay plugin. It pauses while hovered or focused and stays off under reduced motion. `resumeAfter` (ms) restarts the timer after user interaction. */
115
189
  autoplay?: boolean | CarouselAutoplayOptions
116
190
  /** Enable continuous Auto Scroll. Mutually exclusive with `autoplay`; paused under reduced motion. */
117
191
  autoScroll?: boolean | CarouselAutoScrollOptions
@@ -122,7 +196,7 @@ interface CarouselProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onS
122
196
  /** Enable the Class Names plugin, which toggles `snapped` / `inView` classes on slides. */
123
197
  classNames?: boolean | CarouselClassNamesOptions
124
198
  /**
125
- * Keyboard/ARIA plugin for the viewport. Set `false` to opt out.
199
+ * Slide semantics. `true` makes slides outside the viewport `inert` and labels each slide "n of m". An options object hands slides to Embla's Accessibility plugin instead, and `false` leaves them alone.
126
200
  * @default true
127
201
  */
128
202
  accessibility?: boolean | CarouselAccessibilityOptions
@@ -207,8 +281,8 @@ function Carousel({
207
281
 
208
282
  const plugins = React.useMemo<CarouselPlugin[]>(() => {
209
283
  const list: CarouselPlugin[] = []
210
- if (accessibility !== false) {
211
- list.push(Accessibility(typeof accessibility === 'object' ? accessibility : undefined))
284
+ if (typeof accessibility === 'object') {
285
+ list.push(Accessibility({ ...CAROUSEL_ACCESSIBILITY_VIEWPORT, ...accessibility }))
212
286
  }
213
287
  if (wheelGestures !== false) {
214
288
  list.push(WheelGesturesPlugin(typeof wheelGestures === 'object' ? wheelGestures : undefined))
@@ -270,6 +344,11 @@ function Carousel({
270
344
  ])
271
345
 
272
346
  const [viewportRef, emblaApi] = useEmblaCarousel(emblaOptions, plugins)
347
+ useSlideVisibility(emblaApi, accessibility === true)
348
+
349
+ const { ref: forwardedRef, ...rootProps } = rest as typeof rest & { ref?: React.Ref<HTMLDivElement> }
350
+ const rootRef = React.useRef<HTMLDivElement>(null)
351
+ const composedRootRef = useComposedRefs(rootRef, forwardedRef)
273
352
 
274
353
  const setApiRef = React.useRef(setApi)
275
354
  const onReInitRef = React.useRef(onReInit)
@@ -346,7 +425,6 @@ function Carousel({
346
425
  const syncScroll = () => {
347
426
  notifyScroll()
348
427
  }
349
- const syncInView = () => {}
350
428
 
351
429
  syncFull()
352
430
  setApiRef.current?.(emblaApi)
@@ -372,7 +450,6 @@ function Carousel({
372
450
  emblaApi.on('reinit', handleReInit)
373
451
  emblaApi.on('select', handleSelect)
374
452
  emblaApi.on('scroll', handleScroll)
375
- emblaApi.on('slidesinview', syncInView)
376
453
  emblaApi.on('slideschanged', syncFull)
377
454
 
378
455
  const autoplayPlugin = emblaApi.plugins().autoplay
@@ -396,6 +473,21 @@ function Carousel({
396
473
  }, resumeAfter)
397
474
  }
398
475
 
476
+ const root = rootRef.current
477
+ let hovered = false
478
+ let focused = false
479
+ // Rotation that hover or focus stopped, owed a restart once both have left.
480
+ let held = false
481
+ const playAutoplay = () => {
482
+ if (hovered || focused) held = true
483
+ else emblaApi.plugins().autoplay?.play()
484
+ }
485
+ const interruptAutoplay = () => {
486
+ held = false
487
+ emblaApi.plugins().autoplay?.stop()
488
+ scheduleResume(playAutoplay)
489
+ }
490
+
399
491
  let cleanupAutoplay: (() => void) | undefined
400
492
  if (autoplayPlugin) {
401
493
  const rawDelay = autoplayPlugin.options.delay
@@ -406,6 +498,7 @@ function Carousel({
406
498
  isPlaying: autoplayPlugin.isPlaying(),
407
499
  })
408
500
  const onPlay = () => {
501
+ held = false
409
502
  clearResumeTimer()
410
503
  setAutoplayState((s) => (s ? { ...s, isPlaying: true } : s))
411
504
  }
@@ -413,26 +506,71 @@ function Carousel({
413
506
  const onTimerSet = () => setAutoplayState((s) => (s ? { ...s, cycleId: s.cycleId + 1, isPlaying: true } : s))
414
507
  const onTimerStopped = () => setAutoplayState((s) => (s ? { ...s, isPlaying: false } : s))
415
508
  const resetOnSelect = () => autoplayPlugin.reset()
509
+ const onUserStop = () => {
510
+ held = false
511
+ }
416
512
  const onInteraction = (_api: CarouselApi, event: { detail: { interaction: string } }) => {
417
513
  if (event.detail.interaction === 'pointerdown' || event.detail.interaction === 'slidefocus') {
418
- autoplayPlugin.stop()
419
- scheduleResume(() => emblaApi.plugins().autoplay?.play())
514
+ interruptAutoplay()
420
515
  }
421
516
  }
517
+ const hold = () => {
518
+ if (!emblaApi.plugins().autoplay?.isPlaying()) return
519
+ held = true
520
+ emblaApi.plugins().autoplay?.stop()
521
+ }
522
+ const release = () => {
523
+ if (held) playAutoplay()
524
+ }
525
+ const onPointerEnter = () => {
526
+ hovered = true
527
+ hold()
528
+ }
529
+ const onPointerLeave = () => {
530
+ hovered = false
531
+ release()
532
+ }
533
+ const onFocusIn = () => {
534
+ focused = true
535
+ hold()
536
+ }
537
+ const onFocusOut = (event: FocusEvent) => {
538
+ if (event.relatedTarget instanceof Node && root?.contains(event.relatedTarget)) return
539
+ focused = false
540
+ release()
541
+ }
422
542
  emblaApi.on('autoplay:play', onPlay)
423
543
  emblaApi.on('autoplay:stop', onStop)
424
544
  emblaApi.on('autoplay:timerset', onTimerSet)
425
545
  emblaApi.on('autoplay:timerstopped', onTimerStopped)
426
546
  emblaApi.on('select', resetOnSelect)
547
+ emblaApi.on('pointerdown', onUserStop)
548
+ emblaApi.on('slidefocus', onUserStop)
427
549
  if (resumeAfter > 0) emblaApi.on('autoplay:interaction', onInteraction)
428
- if (autoplayEnabled) autoplayPlugin.play()
550
+ root?.addEventListener('pointerenter', onPointerEnter)
551
+ root?.addEventListener('pointerleave', onPointerLeave)
552
+ root?.addEventListener('focusin', onFocusIn)
553
+ root?.addEventListener('focusout', onFocusOut)
554
+ if (autoplayEnabled && !reducedMotion) {
555
+ hovered = !!root?.matches(':hover')
556
+ focused = !!root?.contains(document.activeElement)
557
+ playAutoplay()
558
+ } else {
559
+ autoplayPlugin.stop()
560
+ }
429
561
  cleanupAutoplay = () => {
430
562
  emblaApi.off('autoplay:play', onPlay)
431
563
  emblaApi.off('autoplay:stop', onStop)
432
564
  emblaApi.off('autoplay:timerset', onTimerSet)
433
565
  emblaApi.off('autoplay:timerstopped', onTimerStopped)
434
566
  emblaApi.off('select', resetOnSelect)
567
+ emblaApi.off('pointerdown', onUserStop)
568
+ emblaApi.off('slidefocus', onUserStop)
435
569
  if (resumeAfter > 0) emblaApi.off('autoplay:interaction', onInteraction)
570
+ root?.removeEventListener('pointerenter', onPointerEnter)
571
+ root?.removeEventListener('pointerleave', onPointerLeave)
572
+ root?.removeEventListener('focusin', onFocusIn)
573
+ root?.removeEventListener('focusout', onFocusOut)
436
574
  }
437
575
  } else {
438
576
  setAutoplayState(null)
@@ -461,9 +599,8 @@ function Carousel({
461
599
  const along = Math.abs(vertical ? event.deltaY : event.deltaX)
462
600
  const across = Math.abs(vertical ? event.deltaX : event.deltaY)
463
601
  if (along <= across) return
464
- if (autoplayPlugin?.isPlaying()) {
465
- autoplayPlugin.stop()
466
- scheduleResume(() => emblaApi.plugins().autoplay?.play())
602
+ if (autoplayPlugin?.isPlaying() || held) {
603
+ interruptAutoplay()
467
604
  } else if (autoScrollPlugin?.isPlaying()) {
468
605
  autoScrollPlugin.stop()
469
606
  scheduleResume(() => emblaApi.plugins().autoScroll?.play())
@@ -477,14 +614,13 @@ function Carousel({
477
614
  emblaApi.off('reinit', handleReInit)
478
615
  emblaApi.off('select', handleSelect)
479
616
  emblaApi.off('scroll', handleScroll)
480
- emblaApi.off('slidesinview', syncInView)
481
617
  emblaApi.off('slideschanged', syncFull)
482
618
  cleanupAutoplay?.()
483
619
  cleanupAutoScroll?.()
484
620
  cleanupWheel?.()
485
621
  clearResumeTimer()
486
622
  }
487
- }, [emblaApi, autoplayEnabled, autoScrollEnabled, resumeAfterMs])
623
+ }, [emblaApi, autoplayEnabled, autoScrollEnabled, resumeAfterMs, reducedMotion])
488
624
 
489
625
  const scrollPrev = React.useCallback(() => emblaApi?.goToPrev(), [emblaApi])
490
626
  const scrollNext = React.useCallback(() => emblaApi?.goToNext(), [emblaApi])
@@ -536,11 +672,11 @@ function Carousel({
536
672
  <div
537
673
  data-slot="carousel"
538
674
  data-orientation={orientation}
539
- role="region"
675
+ role={rootProps['aria-label'] || rootProps['aria-labelledby'] ? 'region' : 'group'}
540
676
  aria-roledescription="carousel"
541
- aria-label="Carousel"
542
677
  className={cn('relative', className)}
543
- {...rest}
678
+ {...rootProps}
679
+ ref={composedRootRef}
544
680
  >
545
681
  {children}
546
682
  </div>
@@ -732,6 +868,7 @@ function CarouselThumbs({
732
868
  // means the same thing on both carousels.
733
869
  containScroll="keepSnaps"
734
870
  options={CAROUSEL_THUMB_OPTIONS}
871
+ accessibility={false}
735
872
  setApi={setApi}
736
873
  light={light}
737
874
  role="group"
@@ -882,9 +1019,9 @@ function CarouselPrev({ className, position = 'inside', disabled: disabledProp,
882
1019
  type: 'button',
883
1020
  'data-slot': 'carousel-prev',
884
1021
  'data-disabled': disabled || undefined,
1022
+ 'aria-disabled': disabled || undefined,
885
1023
  'aria-label': 'Previous slide',
886
- disabled,
887
- onClick: scrollPrev,
1024
+ onClick: disabled ? undefined : scrollPrev,
888
1025
  suppressHydrationWarning: true,
889
1026
  } as unknown as React.ButtonHTMLAttributes<HTMLButtonElement>,
890
1027
  props,
@@ -922,9 +1059,9 @@ function CarouselNext({ className, position = 'inside', disabled: disabledProp,
922
1059
  type: 'button',
923
1060
  'data-slot': 'carousel-next',
924
1061
  'data-disabled': disabled || undefined,
1062
+ 'aria-disabled': disabled || undefined,
925
1063
  'aria-label': 'Next slide',
926
- disabled,
927
- onClick: scrollNext,
1064
+ onClick: disabled ? undefined : scrollNext,
928
1065
  suppressHydrationWarning: true,
929
1066
  } as unknown as React.ButtonHTMLAttributes<HTMLButtonElement>,
930
1067
  props,
@@ -1021,6 +1158,13 @@ function CarouselPagination({
1021
1158
  )
1022
1159
  }
1023
1160
 
1161
+ // The bar slides in under its parent's `overflow-hidden` instead of growing, so
1162
+ // the countdown never triggers layout.
1163
+ const AUTOPLAY_INDICATOR_TRAVEL: Record<CarouselOrientation, { start: string; end: string }> = {
1164
+ horizontal: { start: '-translate-x-full rtl:translate-x-full', end: 'translate-x-0' },
1165
+ vertical: { start: '-translate-y-full', end: 'translate-y-0' },
1166
+ }
1167
+
1024
1168
  interface CarouselAutoplayIndicatorProps {
1025
1169
  delay: number
1026
1170
  isPlaying: boolean
@@ -1042,8 +1186,7 @@ function CarouselAutoplayIndicator({ delay, isPlaying, light, orientation }: Car
1042
1186
  }
1043
1187
  }, [])
1044
1188
 
1045
- const isHorizontal = orientation === 'horizontal'
1046
- const fillValue = armed && isPlaying ? '100%' : '0%'
1189
+ const travel = AUTOPLAY_INDICATOR_TRAVEL[orientation]
1047
1190
 
1048
1191
  return (
1049
1192
  <span
@@ -1052,17 +1195,12 @@ function CarouselAutoplayIndicator({ delay, isPlaying, light, orientation }: Car
1052
1195
  data-orientation={orientation}
1053
1196
  data-playing={isPlaying || undefined}
1054
1197
  className={cn(
1055
- 'absolute rounded-full',
1056
- isHorizontal ? 'inset-y-0 inset-s-0' : 'inset-x-0 top-0',
1057
- isHorizontal ? 'motion-safe:transition-[width]' : 'motion-safe:transition-[height]',
1058
- 'motion-safe:ease-linear',
1198
+ 'absolute inset-0 rounded-full',
1199
+ 'motion-safe:transition-[translate] motion-safe:ease-linear',
1200
+ armed && isPlaying ? travel.end : travel.start,
1059
1201
  light ? 'bg-white' : 'bg-primary',
1060
1202
  )}
1061
- style={{
1062
- transitionDuration: `${delay}ms`,
1063
- width: isHorizontal ? fillValue : undefined,
1064
- height: isHorizontal ? undefined : fillValue,
1065
- }}
1203
+ style={{ transitionDuration: `${delay}ms` }}
1066
1204
  />
1067
1205
  )
1068
1206
  }
@@ -86,8 +86,8 @@ describe('Chip', () => {
86
86
 
87
87
  await waitFor(() => {
88
88
  expect(screen.queryByRole('button', { name: /Red/ })).toBeNull()
89
+ expect(onDismiss).toHaveBeenCalledTimes(1)
89
90
  })
90
- expect(onDismiss).toHaveBeenCalledTimes(1)
91
91
  })
92
92
 
93
93
  it('calls user onClick before dismissing and respects preventDefault', async () => {
@@ -215,11 +215,10 @@ describe('ChipGroup', () => {
215
215
  expect(screen.queryByRole('button', { name: /A/ })).toBeNull()
216
216
  expect(screen.queryByRole('button', { name: /B/ })).toBeNull()
217
217
  expect(screen.queryByRole('button', { name: /C/ })).toBeNull()
218
+ expect(onDismissA).toHaveBeenCalledTimes(1)
219
+ expect(onDismissB).toHaveBeenCalledTimes(1)
220
+ expect(onDismissC).toHaveBeenCalledTimes(1)
218
221
  })
219
-
220
- expect(onDismissA).toHaveBeenCalledTimes(1)
221
- expect(onDismissB).toHaveBeenCalledTimes(1)
222
- expect(onDismissC).toHaveBeenCalledTimes(1)
223
222
  })
224
223
 
225
224
  it('clearAll() ignores non-dismissible chips', async () => {
@@ -252,6 +252,39 @@ describe('Combobox', () => {
252
252
  expect(screen.queryByRole('button', { name: 'Toggle popup' })).toBeNull()
253
253
  })
254
254
 
255
+ it('keeps ComboboxEmpty in the accessibility tree as a status region while results show', async () => {
256
+ const user = setupUser()
257
+ renderSingle()
258
+ await user.click(screen.getByRole('combobox'))
259
+ await screen.findByRole('option', { name: 'Next.js' })
260
+
261
+ const empty = document.querySelector('[data-slot="combobox-empty"]')
262
+ expect(empty).toBeEmptyDOMElement()
263
+ expect(empty).not.toHaveClass('hidden')
264
+ expect(screen.getByRole('status')).toBe(empty)
265
+ })
266
+
267
+ it('renders the no-results text inside the status region', async () => {
268
+ const user = setupUser()
269
+ renderSingle()
270
+ const input = screen.getByRole('combobox')
271
+ await user.click(input)
272
+ await user.type(input, 'zzzzzz')
273
+ const status = await screen.findByRole('status')
274
+ expect(status).toHaveTextContent('No items found.')
275
+ expect(status).toHaveAttribute('data-slot', 'combobox-empty')
276
+ })
277
+
278
+ it('has no accessibility violations with the popup open and no matches', async () => {
279
+ const user = setupUser()
280
+ renderSingle()
281
+ const input = screen.getByRole('combobox')
282
+ await user.click(input)
283
+ await user.type(input, 'zzzzzz')
284
+ await screen.findByText('No items found.')
285
+ expect(await axe(document.body, { rules: { region: { enabled: false } } })).toHaveNoViolations()
286
+ })
287
+
255
288
  it('shows ComboboxEmpty when the filter has no matches', async () => {
256
289
  const user = setupUser()
257
290
  renderSingle()
@@ -492,11 +492,7 @@ function ComboboxEmpty({ className, ...props }: ComboboxEmptyProps) {
492
492
  return (
493
493
  <BaseCombobox.Empty
494
494
  data-slot="combobox-empty"
495
- className={cn(
496
- 'text-foreground-muted hidden text-center group-data-empty/combobox-content:block',
497
- EMPTY_SIZE[size],
498
- className,
499
- )}
495
+ className={cn('text-foreground-muted text-center empty:p-0', EMPTY_SIZE[size], className)}
500
496
  {...props}
501
497
  />
502
498
  )
@@ -146,7 +146,8 @@ describe('ContextMenu', () => {
146
146
  const user = setupUser()
147
147
  render(<BasicMenu />)
148
148
  await user.pointer({ keys: '[MouseRight]', target: screen.getByTestId('trigger') })
149
- await screen.findByRole('menuitem', { name: 'Profile' })
149
+ const menu = await screen.findByRole('menu')
150
+ await waitFor(() => expect(menu.contains(document.activeElement)).toBe(true))
150
151
  await user.keyboard('{ArrowDown}')
151
152
  await waitFor(() => {
152
153
  const profile = screen.getByRole('menuitem', { name: 'Profile' })
@@ -24,6 +24,35 @@ describe('CopyButton', () => {
24
24
  expect(screen.getByText('Copy SVG')).toBeInTheDocument()
25
25
  })
26
26
 
27
+ it('names the button from its visible children instead of an aria-label', () => {
28
+ render(
29
+ <CopyButton value="hello" label="Copy markup">
30
+ Copy SVG
31
+ </CopyButton>,
32
+ )
33
+ const button = screen.getByRole('button', { name: 'Copy SVG' })
34
+ expect(button).not.toHaveAttribute('aria-label')
35
+ })
36
+
37
+ it('keeps the aria-label on an icon-only button', () => {
38
+ render(<CopyButton value="hello" label="Copy code" />)
39
+ expect(screen.getByRole('button', { name: 'Copy code' })).toHaveAttribute('aria-label', 'Copy code')
40
+ })
41
+
42
+ it('treats empty children as icon-only', () => {
43
+ render(<CopyButton value="hello">{false}</CopyButton>)
44
+ expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'Copy')
45
+ })
46
+
47
+ it('lets an explicit aria-label win', () => {
48
+ render(
49
+ <CopyButton value="hello" aria-label="Copy the install command">
50
+ Copy
51
+ </CopyButton>,
52
+ )
53
+ expect(screen.getByRole('button', { name: 'Copy the install command' })).toBeInTheDocument()
54
+ })
55
+
27
56
  it('swaps a string label to the copied label on success', async () => {
28
57
  const { user } = setup()
29
58
  render(
@@ -32,11 +61,39 @@ describe('CopyButton', () => {
32
61
  </CopyButton>,
33
62
  )
34
63
 
35
- await user.click(screen.getByRole('button'))
64
+ const button = screen.getByRole('button')
65
+ await user.click(button)
36
66
 
37
67
  await waitFor(() => expect(screen.queryByText('Copy SVG')).not.toBeInTheDocument())
38
- // Both the visible label and the sr-only status announce "Copied".
39
- expect(screen.getAllByText('Copied').length).toBeGreaterThanOrEqual(1)
68
+ expect(button).toHaveAccessibleName('Copied')
69
+ expect(button).not.toHaveAttribute('aria-label')
70
+ expect(screen.getByRole('status')).toHaveTextContent('Copied')
71
+ })
72
+
73
+ it('renders the status region as a sibling of the button, not inside it', async () => {
74
+ const { user } = setup()
75
+ const { container } = render(
76
+ <div data-testid="parent">
77
+ <CopyButton value="hello" copiedLabel="Code copied" />
78
+ </div>,
79
+ )
80
+ const button = screen.getByRole('button')
81
+ const status = screen.getByRole('status')
82
+
83
+ expect(button.contains(status)).toBe(false)
84
+ expect(status.parentElement).toBe(button.parentElement)
85
+ expect(status.parentElement).toBe(screen.getByTestId('parent'))
86
+ expect(status).toBeEmptyDOMElement()
87
+ expect(container.querySelectorAll('[data-slot="copy-button"]')).toHaveLength(1)
88
+
89
+ await user.click(button)
90
+ await waitFor(() => expect(status).toHaveTextContent('Code copied'))
91
+ })
92
+
93
+ it('forwards the ref to the button', () => {
94
+ const ref = React.createRef<HTMLButtonElement>()
95
+ render(<CopyButton value="hello" ref={ref} />)
96
+ expect(ref.current).toBe(screen.getByRole('button'))
40
97
  })
41
98
 
42
99
  it('renders a custom resting icon in place of the copy glyph and still swaps to the check', async () => {
@@ -157,4 +214,12 @@ describe('CopyButton', () => {
157
214
  const { container } = render(<CopyButton value="hello" />)
158
215
  expect(await axe(container)).toHaveNoViolations()
159
216
  })
217
+
218
+ it('has no accessibility violations with a visible label in the copied state', async () => {
219
+ const { user } = setup()
220
+ const { container } = render(<CopyButton value="hello">Copy SVG</CopyButton>)
221
+ await user.click(screen.getByRole('button'))
222
+ await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('Copied'))
223
+ expect(await axe(container)).toHaveNoViolations()
224
+ })
160
225
  })
@@ -56,12 +56,12 @@ interface CopyButtonProps extends Omit<ButtonProps, 'value' | 'onCopy'> {
56
56
  */
57
57
  timeout?: number
58
58
  /**
59
- * Accessible name (and tooltip via `title`) in the idle state.
59
+ * Accessible name of an icon-only button in the idle state. Visible children name the button instead.
60
60
  * @default 'Copy'
61
61
  */
62
62
  label?: string
63
63
  /**
64
- * Accessible name after a successful copy; a string child also swaps to this.
64
+ * Announced after a successful copy, and names an icon-only button until it reverts; a string child also swaps to this.
65
65
  * @default 'Copied'
66
66
  */
67
67
  copiedLabel?: string
@@ -110,51 +110,57 @@ function CopyButton({
110
110
  resetRef.current = setTimeout(() => setCopied(false), timeout)
111
111
  }
112
112
 
113
+ const iconOnly = React.Children.toArray(children).length === 0
114
+
113
115
  return (
114
- <Button
115
- data-slot="copy-button"
116
- data-copied={copied || undefined}
117
- type="button"
118
- variant={variant}
119
- size={size}
120
- aria-label={copied ? copiedLabel : label}
121
- onClick={handleClick}
122
- className={cn('group/copy', className)}
123
- {...props}
124
- >
125
- <span data-icon={children != null ? 'start' : undefined} className="grid place-items-center *:[grid-area:1/1]">
126
- <span
127
- className={cn(
128
- 'grid place-items-center transition-[opacity,scale] delay-150 duration-150 ease-out group-data-copied/copy:delay-0',
129
- 'group-data-copied/copy:scale-50 group-data-copied/copy:opacity-0',
130
- 'motion-reduce:transition-none',
131
- )}
132
- >
133
- {icon ?? (
134
- <svg {...ICON_SVG_PROPS}>
135
- <path d="M4.012 16.737c-.307-.175-.562-.427-.739-.732S3.001 15.353 3 15V5c0-1.1.9-2 2-2h10c.75 0 1.158.385 1.5 1M7 9.667c0-.707.281-1.386.781-1.886S8.96 7 9.667 7h8.666c.35 0 .697.069 1.021.203s.618.33.865.578.444.542.578.865A2.67 2.67 0 0 1 21 9.667v8.666a2.67 2.67 0 0 1-.781 1.886 2.67 2.67 0 0 1-1.886.781H9.667a2.67 2.67 0 0 1-1.021-.203c-.324-.134-.618-.331-.865-.578s-.444-.542-.578-.865S7 18.683 7 18.333V9.667z" />
136
- </svg>
137
- )}
138
- </span>
139
- <svg {...ICON_SVG_PROPS}>
140
- <path
141
- d={CHECK_PATH}
142
- pathLength={1}
143
- strokeDasharray="1 2"
116
+ <>
117
+ {/* First, so last-child rules (`space-x-*`, `divide-*`) still see the button as last, and
118
+ `!important`, so a parent's `*:` utilities (ButtonGroup's `*:relative`) can't pull it into flow. */}
119
+ <span role="status" className="sr-only!">
120
+ {copied ? copiedLabel : ''}
121
+ </span>
122
+ <Button
123
+ data-slot="copy-button"
124
+ data-copied={copied || undefined}
125
+ type="button"
126
+ variant={variant}
127
+ size={size}
128
+ aria-label={iconOnly ? (copied ? copiedLabel : label) : undefined}
129
+ onClick={handleClick}
130
+ className={cn('group/copy', className)}
131
+ {...props}
132
+ >
133
+ <span data-icon={children != null ? 'start' : undefined} className="grid place-items-center *:[grid-area:1/1]">
134
+ <span
144
135
  className={cn(
145
- '[stroke-dashoffset:1.02] group-data-copied/copy:[stroke-dashoffset:0]',
146
- 'transition-[stroke-dashoffset] duration-200 ease-in',
147
- 'group-data-copied/copy:delay-100 group-data-copied/copy:duration-350 group-data-copied/copy:ease-out',
136
+ 'grid place-items-center transition-[opacity,scale] delay-150 duration-150 ease-out group-data-copied/copy:delay-0',
137
+ 'group-data-copied/copy:scale-50 group-data-copied/copy:opacity-0',
148
138
  'motion-reduce:transition-none',
149
139
  )}
150
- />
151
- </svg>
152
- </span>
153
- {typeof children === 'string' && copied ? copiedLabel : children}
154
- <span role="status" className="sr-only">
155
- {copied ? copiedLabel : ''}
156
- </span>
157
- </Button>
140
+ >
141
+ {icon ?? (
142
+ <svg {...ICON_SVG_PROPS}>
143
+ <path d="M4.012 16.737c-.307-.175-.562-.427-.739-.732S3.001 15.353 3 15V5c0-1.1.9-2 2-2h10c.75 0 1.158.385 1.5 1M7 9.667c0-.707.281-1.386.781-1.886S8.96 7 9.667 7h8.666c.35 0 .697.069 1.021.203s.618.33.865.578.444.542.578.865A2.67 2.67 0 0 1 21 9.667v8.666a2.67 2.67 0 0 1-.781 1.886 2.67 2.67 0 0 1-1.886.781H9.667a2.67 2.67 0 0 1-1.021-.203c-.324-.134-.618-.331-.865-.578s-.444-.542-.578-.865S7 18.683 7 18.333V9.667z" />
144
+ </svg>
145
+ )}
146
+ </span>
147
+ <svg {...ICON_SVG_PROPS}>
148
+ <path
149
+ d={CHECK_PATH}
150
+ pathLength={1}
151
+ strokeDasharray="1 2"
152
+ className={cn(
153
+ '[stroke-dashoffset:1.02] group-data-copied/copy:[stroke-dashoffset:0]',
154
+ 'transition-[stroke-dashoffset] duration-200 ease-in',
155
+ 'group-data-copied/copy:delay-100 group-data-copied/copy:duration-350 group-data-copied/copy:ease-out',
156
+ 'motion-reduce:transition-none',
157
+ )}
158
+ />
159
+ </svg>
160
+ </span>
161
+ {typeof children === 'string' && copied ? copiedLabel : children}
162
+ </Button>
163
+ </>
158
164
  )
159
165
  }
160
166