@appica/ui-react 1.3.0 → 1.4.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 +37 -0
- package/agent-rules.md +3 -0
- package/appica.css +1 -1
- package/dist/components/autocomplete/autocomplete.d.ts.map +1 -1
- package/dist/components/autocomplete/autocomplete.js +1 -5
- package/dist/components/autocomplete/autocomplete.js.map +1 -1
- package/dist/components/breadcrumb/breadcrumb.js +1 -1
- package/dist/components/breadcrumb/breadcrumb.js.map +1 -1
- package/dist/components/carousel/carousel.d.ts +25 -4
- package/dist/components/carousel/carousel.d.ts.map +1 -1
- package/dist/components/carousel/carousel.js +277 -48
- package/dist/components/carousel/carousel.js.map +1 -1
- package/dist/components/combobox/combobox.d.ts.map +1 -1
- package/dist/components/combobox/combobox.js +1 -5
- package/dist/components/combobox/combobox.js.map +1 -1
- package/dist/components/copy-button/copy-button.d.ts +2 -2
- package/dist/components/copy-button/copy-button.d.ts.map +1 -1
- package/dist/components/copy-button/copy-button.js +49 -46
- package/dist/components/copy-button/copy-button.js.map +1 -1
- package/dist/components/date-field/date-field.d.ts.map +1 -1
- package/dist/components/date-field/date-field.js +7 -0
- package/dist/components/date-field/date-field.js.map +1 -1
- package/dist/components/date-picker/date-picker.d.ts +10 -0
- package/dist/components/date-picker/date-picker.d.ts.map +1 -1
- package/dist/components/date-picker/date-picker.js +14 -0
- package/dist/components/date-picker/date-picker.js.map +1 -1
- package/dist/components/field/field.d.ts.map +1 -1
- package/dist/components/field/field.js +1 -0
- package/dist/components/field/field.js.map +1 -1
- package/dist/components/loader/loader.d.ts +1 -1
- package/dist/components/loader/loader.d.ts.map +1 -1
- package/dist/components/loader/loader.js +7 -5
- package/dist/components/loader/loader.js.map +1 -1
- package/dist/components/number-field/number-field.d.ts +1 -1
- package/dist/components/number-field/number-field.d.ts.map +1 -1
- package/dist/components/number-field/number-field.js +7 -0
- package/dist/components/number-field/number-field.js.map +1 -1
- package/dist/components/pagination/pagination.d.ts.map +1 -1
- package/dist/components/pagination/pagination.js +1 -2
- package/dist/components/pagination/pagination.js.map +1 -1
- package/dist/components/spinner/spinner.d.ts +1 -1
- package/dist/components/spinner/spinner.d.ts.map +1 -1
- package/dist/components/spinner/spinner.js +7 -5
- package/dist/components/spinner/spinner.js.map +1 -1
- package/dist/components/time-field/time-field.d.ts.map +1 -1
- package/dist/components/time-field/time-field.js +7 -0
- package/dist/components/time-field/time-field.js.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +11 -11
- package/src/components/autocomplete/autocomplete.test.tsx +33 -0
- package/src/components/autocomplete/autocomplete.tsx +1 -5
- package/src/components/breadcrumb/breadcrumb.test.tsx +57 -2
- package/src/components/breadcrumb/breadcrumb.tsx +1 -1
- package/src/components/carousel/carousel.test.tsx +918 -13
- package/src/components/carousel/carousel.tsx +338 -54
- package/src/components/chip/chip.test.tsx +4 -5
- package/src/components/combobox/combobox.test.tsx +33 -0
- package/src/components/combobox/combobox.tsx +1 -5
- package/src/components/context-menu/context-menu.test.tsx +2 -1
- package/src/components/copy-button/copy-button.test.tsx +68 -3
- package/src/components/copy-button/copy-button.tsx +49 -43
- package/src/components/date-field/date-field.test.tsx +82 -0
- package/src/components/date-field/date-field.tsx +7 -0
- package/src/components/date-picker/date-picker.test.tsx +70 -0
- package/src/components/date-picker/date-picker.tsx +26 -0
- package/src/components/dropdown-menu/dropdown-menu.test.tsx +2 -1
- package/src/components/field/field.test.tsx +34 -0
- package/src/components/field/field.tsx +1 -0
- package/src/components/loader/loader.test.tsx +17 -4
- package/src/components/loader/loader.tsx +2 -2
- package/src/components/number-field/number-field.test.tsx +59 -0
- package/src/components/number-field/number-field.tsx +8 -0
- package/src/components/pagination/pagination.test.tsx +83 -0
- package/src/components/pagination/pagination.tsx +1 -2
- package/src/components/spinner/spinner.test.tsx +17 -4
- package/src/components/spinner/spinner.tsx +2 -2
- package/src/components/time-field/time-field.test.tsx +64 -0
- 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
|
|
|
@@ -22,7 +22,19 @@ type CarouselApi = EmblaCarouselType
|
|
|
22
22
|
type CarouselOptions = EmblaOptionsType
|
|
23
23
|
type CarouselPlugin = EmblaPluginType
|
|
24
24
|
|
|
25
|
-
type CarouselAutoplayOptions = AutoplayOptionsType & {
|
|
25
|
+
type CarouselAutoplayOptions = AutoplayOptionsType & {
|
|
26
|
+
resumeAfter?: number
|
|
27
|
+
/**
|
|
28
|
+
* Pause while the pointer is over the carousel. Turn it off only beside a `CarouselPlayToggle`.
|
|
29
|
+
* @default true
|
|
30
|
+
*/
|
|
31
|
+
pauseOnHover?: boolean
|
|
32
|
+
/**
|
|
33
|
+
* Pause while keyboard focus is inside the carousel. Turn it off only beside a `CarouselPlayToggle`.
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
pauseOnFocus?: boolean
|
|
37
|
+
}
|
|
26
38
|
type CarouselAutoScrollOptions = AutoScrollOptionsType & { resumeAfter?: number }
|
|
27
39
|
type CarouselAutoHeightOptions = AutoHeightOptionsType
|
|
28
40
|
type CarouselFadeOptions = FadeOptionsType
|
|
@@ -52,12 +64,19 @@ interface CarouselContextValue {
|
|
|
52
64
|
subscribeScrollProgress: (onChange: () => void) => () => void
|
|
53
65
|
getScrollProgress: () => number
|
|
54
66
|
autoplay: CarouselAutoplayState | null
|
|
67
|
+
/** Whether Autoplay or Auto Scroll stays stopped until `play()`: paused by the user, or stopped for good by a drag. */
|
|
68
|
+
paused: boolean
|
|
69
|
+
/** Stop Autoplay or Auto Scroll until `play()`. Hover, focus, `resumeAfter`, drags and the wheel don't restart it. */
|
|
70
|
+
pause: () => void
|
|
71
|
+
/** Restart Autoplay or Auto Scroll on a fresh timer, even while hovered or focused. */
|
|
72
|
+
play: () => void
|
|
55
73
|
scrollPrev: () => void
|
|
56
74
|
scrollNext: () => void
|
|
57
75
|
scrollTo: (index: number) => void
|
|
58
76
|
}
|
|
59
77
|
|
|
60
78
|
const CarouselContext = React.createContext<CarouselContextValue | null>(null)
|
|
79
|
+
const CarouselRotationContext = React.createContext(false)
|
|
61
80
|
|
|
62
81
|
function useCarousel(): CarouselContextValue {
|
|
63
82
|
const ctx = React.useContext(CarouselContext)
|
|
@@ -67,6 +86,85 @@ function useCarousel(): CarouselContextValue {
|
|
|
67
86
|
return ctx
|
|
68
87
|
}
|
|
69
88
|
|
|
89
|
+
// The root already names the carousel, so the plugin's empty values keep the
|
|
90
|
+
// viewport from becoming a second landmark (its setter skips empty strings).
|
|
91
|
+
const CAROUSEL_ACCESSIBILITY_VIEWPORT: CarouselAccessibilityOptions = {
|
|
92
|
+
carouselRole: '',
|
|
93
|
+
carouselAriaLabel: '',
|
|
94
|
+
carouselAriaRoleDescription: '',
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Browsers match :focus-visible for keyboard focus, not for focus from a mouse or touch press.
|
|
98
|
+
function hasKeyboardFocus(node: EventTarget | null): boolean {
|
|
99
|
+
return node instanceof Element && node.matches(':focus-visible')
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function getShownSlides(api: CarouselApi): Set<number> {
|
|
103
|
+
const { options, scrollSnapList } = api.internalEngine()
|
|
104
|
+
if (!options.active) return new Set(api.slideNodes().keys())
|
|
105
|
+
// Fade stacks every slide in one place, and the in-view list stays empty
|
|
106
|
+
// until the engine's IntersectionObserver first reports.
|
|
107
|
+
const inView = api.plugins().fade ? [] : api.slidesInView()
|
|
108
|
+
return new Set(inView.length ? inView : (scrollSnapList.slidesBySnap[api.selectedSnap()] ?? []))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function useSlideVisibility(api: CarouselApi | undefined, enabled: boolean) {
|
|
112
|
+
React.useEffect(() => {
|
|
113
|
+
if (!api || !enabled) return
|
|
114
|
+
const inert = new Set<HTMLElement>()
|
|
115
|
+
const labels = new Map<HTMLElement, string>()
|
|
116
|
+
|
|
117
|
+
const release = (node: HTMLElement) => {
|
|
118
|
+
if (inert.delete(node)) node.removeAttribute('inert')
|
|
119
|
+
if (labels.has(node) && node.getAttribute('aria-label') === labels.get(node)) node.removeAttribute('aria-label')
|
|
120
|
+
labels.delete(node)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const sync = (focused: EventTarget | null) => {
|
|
124
|
+
const slides = api.slideNodes()
|
|
125
|
+
const shown = getShownSlides(api)
|
|
126
|
+
new Set([...inert, ...labels.keys()]).forEach((node) => {
|
|
127
|
+
if (!slides.includes(node)) release(node)
|
|
128
|
+
})
|
|
129
|
+
slides.forEach((node, index) => {
|
|
130
|
+
const hidden = !shown.has(index) && !(focused instanceof Node && node.contains(focused))
|
|
131
|
+
if (hidden && !inert.has(node)) {
|
|
132
|
+
node.setAttribute('inert', '')
|
|
133
|
+
inert.add(node)
|
|
134
|
+
} else if (!hidden && inert.delete(node)) {
|
|
135
|
+
node.removeAttribute('inert')
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const ownLabel = node.getAttribute('aria-label')
|
|
139
|
+
if (node.hasAttribute('aria-labelledby') || (ownLabel !== null && ownLabel !== labels.get(node))) {
|
|
140
|
+
labels.delete(node)
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
const label = `${index + 1} of ${slides.length}`
|
|
144
|
+
if (ownLabel !== label) node.setAttribute('aria-label', label)
|
|
145
|
+
labels.set(node, label)
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const onChange = () => sync(document.activeElement)
|
|
150
|
+
const onFocusOut = (event: FocusEvent) => sync(event.relatedTarget)
|
|
151
|
+
const viewport = api.rootNode()
|
|
152
|
+
|
|
153
|
+
onChange()
|
|
154
|
+
api.on('select', onChange)
|
|
155
|
+
api.on('reinit', onChange)
|
|
156
|
+
api.on('slidesinview', onChange)
|
|
157
|
+
viewport?.addEventListener('focusout', onFocusOut)
|
|
158
|
+
return () => {
|
|
159
|
+
api.off('select', onChange)
|
|
160
|
+
api.off('reinit', onChange)
|
|
161
|
+
api.off('slidesinview', onChange)
|
|
162
|
+
viewport?.removeEventListener('focusout', onFocusOut)
|
|
163
|
+
new Set([...inert, ...labels.keys()]).forEach(release)
|
|
164
|
+
}
|
|
165
|
+
}, [api, enabled])
|
|
166
|
+
}
|
|
167
|
+
|
|
70
168
|
interface CarouselProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onSelect' | 'onScroll' | 'children'> {
|
|
71
169
|
/**
|
|
72
170
|
* Scroll axis. Maps to Embla's `axis`; exposed as `data-orientation`.
|
|
@@ -111,7 +209,7 @@ interface CarouselProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onS
|
|
|
111
209
|
duration?: number
|
|
112
210
|
/** Escape hatch: a raw Embla options object, merged last so it wins over the flat props. */
|
|
113
211
|
options?: CarouselOptions
|
|
114
|
-
/** Enable the Autoplay plugin. `resumeAfter` (ms) restarts the timer after user interaction. */
|
|
212
|
+
/** Enable the Autoplay plugin. It pauses while hovered or while keyboard focus is inside, unless `pauseOnHover` or `pauseOnFocus` is `false`, and stays off under reduced motion. `resumeAfter` (ms) restarts the timer after user interaction. */
|
|
115
213
|
autoplay?: boolean | CarouselAutoplayOptions
|
|
116
214
|
/** Enable continuous Auto Scroll. Mutually exclusive with `autoplay`; paused under reduced motion. */
|
|
117
215
|
autoScroll?: boolean | CarouselAutoScrollOptions
|
|
@@ -122,7 +220,7 @@ interface CarouselProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'onS
|
|
|
122
220
|
/** Enable the Class Names plugin, which toggles `snapped` / `inView` classes on slides. */
|
|
123
221
|
classNames?: boolean | CarouselClassNamesOptions
|
|
124
222
|
/**
|
|
125
|
-
*
|
|
223
|
+
* 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
224
|
* @default true
|
|
127
225
|
*/
|
|
128
226
|
accessibility?: boolean | CarouselAccessibilityOptions
|
|
@@ -204,18 +302,20 @@ function Carousel({
|
|
|
204
302
|
(typeof autoplay === 'object' && typeof autoplay.resumeAfter === 'number' && autoplay.resumeAfter) ||
|
|
205
303
|
(typeof autoScroll === 'object' && typeof autoScroll.resumeAfter === 'number' && autoScroll.resumeAfter) ||
|
|
206
304
|
0
|
|
305
|
+
const pauseOnHover = typeof autoplay !== 'object' || autoplay.pauseOnHover !== false
|
|
306
|
+
const pauseOnFocus = typeof autoplay !== 'object' || autoplay.pauseOnFocus !== false
|
|
207
307
|
|
|
208
308
|
const plugins = React.useMemo<CarouselPlugin[]>(() => {
|
|
209
309
|
const list: CarouselPlugin[] = []
|
|
210
|
-
if (accessibility
|
|
211
|
-
list.push(Accessibility(
|
|
310
|
+
if (typeof accessibility === 'object') {
|
|
311
|
+
list.push(Accessibility({ ...CAROUSEL_ACCESSIBILITY_VIEWPORT, ...accessibility }))
|
|
212
312
|
}
|
|
213
313
|
if (wheelGestures !== false) {
|
|
214
314
|
list.push(WheelGesturesPlugin(typeof wheelGestures === 'object' ? wheelGestures : undefined))
|
|
215
315
|
}
|
|
216
316
|
if (autoplay) {
|
|
217
317
|
const opts = typeof autoplay === 'object' ? autoplay : undefined
|
|
218
|
-
const { resumeAfter, ...emblaOpts } = opts ?? {}
|
|
318
|
+
const { resumeAfter, pauseOnHover: _pauseOnHover, pauseOnFocus: _pauseOnFocus, ...emblaOpts } = opts ?? {}
|
|
219
319
|
const autoResume = typeof resumeAfter === 'number' && resumeAfter > 0
|
|
220
320
|
list.push(Autoplay(autoResume ? { ...emblaOpts, defaultInteraction: false } : emblaOpts))
|
|
221
321
|
} else if (autoScroll && !reducedMotion) {
|
|
@@ -270,6 +370,11 @@ function Carousel({
|
|
|
270
370
|
])
|
|
271
371
|
|
|
272
372
|
const [viewportRef, emblaApi] = useEmblaCarousel(emblaOptions, plugins)
|
|
373
|
+
useSlideVisibility(emblaApi, accessibility === true)
|
|
374
|
+
|
|
375
|
+
const { ref: forwardedRef, ...rootProps } = rest as typeof rest & { ref?: React.Ref<HTMLDivElement> }
|
|
376
|
+
const rootRef = React.useRef<HTMLDivElement>(null)
|
|
377
|
+
const composedRootRef = useComposedRefs(rootRef, forwardedRef)
|
|
273
378
|
|
|
274
379
|
const setApiRef = React.useRef(setApi)
|
|
275
380
|
const onReInitRef = React.useRef(onReInit)
|
|
@@ -308,6 +413,21 @@ function Carousel({
|
|
|
308
413
|
canScrollNext: false,
|
|
309
414
|
})
|
|
310
415
|
const [autoplayState, setAutoplayState] = React.useState<CarouselAutoplayState | null>(null)
|
|
416
|
+
const [paused, setPausedState] = React.useState(false)
|
|
417
|
+
const pausedRef = React.useRef(false)
|
|
418
|
+
const setPaused = React.useCallback((next: boolean) => {
|
|
419
|
+
pausedRef.current = next
|
|
420
|
+
setPausedState(next)
|
|
421
|
+
}, [])
|
|
422
|
+
const rotationRef = React.useRef<{ pause: () => void; play: () => void } | null>(null)
|
|
423
|
+
const pause = React.useCallback(() => {
|
|
424
|
+
setPaused(true)
|
|
425
|
+
rotationRef.current?.pause()
|
|
426
|
+
}, [setPaused])
|
|
427
|
+
const play = React.useCallback(() => {
|
|
428
|
+
setPaused(false)
|
|
429
|
+
rotationRef.current?.play()
|
|
430
|
+
}, [setPaused])
|
|
311
431
|
|
|
312
432
|
const scrollProgressRef = React.useRef(0)
|
|
313
433
|
const scrollListenersRef = React.useRef<Set<() => void>>(new Set())
|
|
@@ -346,7 +466,6 @@ function Carousel({
|
|
|
346
466
|
const syncScroll = () => {
|
|
347
467
|
notifyScroll()
|
|
348
468
|
}
|
|
349
|
-
const syncInView = () => {}
|
|
350
469
|
|
|
351
470
|
syncFull()
|
|
352
471
|
setApiRef.current?.(emblaApi)
|
|
@@ -372,7 +491,6 @@ function Carousel({
|
|
|
372
491
|
emblaApi.on('reinit', handleReInit)
|
|
373
492
|
emblaApi.on('select', handleSelect)
|
|
374
493
|
emblaApi.on('scroll', handleScroll)
|
|
375
|
-
emblaApi.on('slidesinview', syncInView)
|
|
376
494
|
emblaApi.on('slideschanged', syncFull)
|
|
377
495
|
|
|
378
496
|
const autoplayPlugin = emblaApi.plugins().autoplay
|
|
@@ -387,15 +505,39 @@ function Carousel({
|
|
|
387
505
|
resumeTimer = undefined
|
|
388
506
|
}
|
|
389
507
|
}
|
|
390
|
-
const scheduleResume = (
|
|
391
|
-
if (resumeAfter <= 0) return
|
|
508
|
+
const scheduleResume = (restart: () => void) => {
|
|
392
509
|
clearResumeTimer()
|
|
393
510
|
resumeTimer = setTimeout(() => {
|
|
394
511
|
resumeTimer = undefined
|
|
395
|
-
|
|
512
|
+
restart()
|
|
396
513
|
}, resumeAfter)
|
|
397
514
|
}
|
|
398
515
|
|
|
516
|
+
const root = rootRef.current
|
|
517
|
+
let hovered = false
|
|
518
|
+
let focused = false
|
|
519
|
+
// Rotation that hover or focus stopped, owed a restart once both have left.
|
|
520
|
+
let held = false
|
|
521
|
+
const playAutoplay = () => {
|
|
522
|
+
if (pausedRef.current) return
|
|
523
|
+
if (hovered || focused) held = true
|
|
524
|
+
else emblaApi.plugins().autoplay?.play()
|
|
525
|
+
}
|
|
526
|
+
const interruptAutoplay = () => {
|
|
527
|
+
held = false
|
|
528
|
+
emblaApi.plugins().autoplay?.stop()
|
|
529
|
+
if (resumeAfter > 0) scheduleResume(playAutoplay)
|
|
530
|
+
else setPaused(true)
|
|
531
|
+
}
|
|
532
|
+
const playAutoScroll = () => {
|
|
533
|
+
if (!pausedRef.current) emblaApi.plugins().autoScroll?.play()
|
|
534
|
+
}
|
|
535
|
+
const interruptAutoScroll = () => {
|
|
536
|
+
emblaApi.plugins().autoScroll?.stop()
|
|
537
|
+
if (resumeAfter > 0) scheduleResume(playAutoScroll)
|
|
538
|
+
else setPaused(true)
|
|
539
|
+
}
|
|
540
|
+
|
|
399
541
|
let cleanupAutoplay: (() => void) | undefined
|
|
400
542
|
if (autoplayPlugin) {
|
|
401
543
|
const rawDelay = autoplayPlugin.options.delay
|
|
@@ -406,33 +548,99 @@ function Carousel({
|
|
|
406
548
|
isPlaying: autoplayPlugin.isPlaying(),
|
|
407
549
|
})
|
|
408
550
|
const onPlay = () => {
|
|
551
|
+
held = false
|
|
409
552
|
clearResumeTimer()
|
|
553
|
+
if (pausedRef.current) setPaused(false)
|
|
410
554
|
setAutoplayState((s) => (s ? { ...s, isPlaying: true } : s))
|
|
411
555
|
}
|
|
412
556
|
const onStop = () => setAutoplayState((s) => (s ? { ...s, isPlaying: false } : s))
|
|
413
557
|
const onTimerSet = () => setAutoplayState((s) => (s ? { ...s, cycleId: s.cycleId + 1, isPlaying: true } : s))
|
|
414
558
|
const onTimerStopped = () => setAutoplayState((s) => (s ? { ...s, isPlaying: false } : s))
|
|
415
559
|
const resetOnSelect = () => autoplayPlugin.reset()
|
|
560
|
+
const stopsOnPress = autoplayPlugin.options.defaultInteraction !== false
|
|
561
|
+
const onUserStop = () => {
|
|
562
|
+
held = false
|
|
563
|
+
if (stopsOnPress && !reducedMotion) setPaused(true)
|
|
564
|
+
}
|
|
416
565
|
const onInteraction = (_api: CarouselApi, event: { detail: { interaction: string } }) => {
|
|
417
566
|
if (event.detail.interaction === 'pointerdown' || event.detail.interaction === 'slidefocus') {
|
|
418
|
-
|
|
419
|
-
scheduleResume(() => emblaApi.plugins().autoplay?.play())
|
|
567
|
+
interruptAutoplay()
|
|
420
568
|
}
|
|
421
569
|
}
|
|
570
|
+
const hold = () => {
|
|
571
|
+
if (!emblaApi.plugins().autoplay?.isPlaying()) return
|
|
572
|
+
held = true
|
|
573
|
+
emblaApi.plugins().autoplay?.stop()
|
|
574
|
+
}
|
|
575
|
+
const release = () => {
|
|
576
|
+
if (held) playAutoplay()
|
|
577
|
+
}
|
|
578
|
+
const onPointerEnter = () => {
|
|
579
|
+
hovered = true
|
|
580
|
+
hold()
|
|
581
|
+
}
|
|
582
|
+
const onPointerLeave = () => {
|
|
583
|
+
hovered = false
|
|
584
|
+
release()
|
|
585
|
+
}
|
|
586
|
+
const onFocusIn = (event: FocusEvent) => {
|
|
587
|
+
focused = hasKeyboardFocus(event.target)
|
|
588
|
+
if (focused) hold()
|
|
589
|
+
else release()
|
|
590
|
+
}
|
|
591
|
+
const onFocusOut = (event: FocusEvent) => {
|
|
592
|
+
if (event.relatedTarget instanceof Node && root?.contains(event.relatedTarget)) return
|
|
593
|
+
focused = false
|
|
594
|
+
release()
|
|
595
|
+
}
|
|
422
596
|
emblaApi.on('autoplay:play', onPlay)
|
|
423
597
|
emblaApi.on('autoplay:stop', onStop)
|
|
424
598
|
emblaApi.on('autoplay:timerset', onTimerSet)
|
|
425
599
|
emblaApi.on('autoplay:timerstopped', onTimerStopped)
|
|
426
600
|
emblaApi.on('select', resetOnSelect)
|
|
601
|
+
emblaApi.on('pointerdown', onUserStop)
|
|
602
|
+
emblaApi.on('slidefocus', onUserStop)
|
|
427
603
|
if (resumeAfter > 0) emblaApi.on('autoplay:interaction', onInteraction)
|
|
428
|
-
if (
|
|
604
|
+
if (pauseOnHover) {
|
|
605
|
+
root?.addEventListener('pointerenter', onPointerEnter)
|
|
606
|
+
root?.addEventListener('pointerleave', onPointerLeave)
|
|
607
|
+
}
|
|
608
|
+
if (pauseOnFocus) {
|
|
609
|
+
root?.addEventListener('focusin', onFocusIn)
|
|
610
|
+
root?.addEventListener('focusout', onFocusOut)
|
|
611
|
+
}
|
|
612
|
+
if (autoplayEnabled && !reducedMotion) {
|
|
613
|
+
hovered = pauseOnHover && !!root?.matches(':hover')
|
|
614
|
+
focused = pauseOnFocus && !!root?.contains(document.activeElement) && hasKeyboardFocus(document.activeElement)
|
|
615
|
+
playAutoplay()
|
|
616
|
+
} else {
|
|
617
|
+
autoplayPlugin.stop()
|
|
618
|
+
}
|
|
619
|
+
rotationRef.current = {
|
|
620
|
+
pause: () => {
|
|
621
|
+
held = false
|
|
622
|
+
clearResumeTimer()
|
|
623
|
+
emblaApi.plugins().autoplay?.stop()
|
|
624
|
+
},
|
|
625
|
+
play: () => {
|
|
626
|
+
held = false
|
|
627
|
+
clearResumeTimer()
|
|
628
|
+
if (!reducedMotion) emblaApi.plugins().autoplay?.play()
|
|
629
|
+
},
|
|
630
|
+
}
|
|
429
631
|
cleanupAutoplay = () => {
|
|
430
632
|
emblaApi.off('autoplay:play', onPlay)
|
|
431
633
|
emblaApi.off('autoplay:stop', onStop)
|
|
432
634
|
emblaApi.off('autoplay:timerset', onTimerSet)
|
|
433
635
|
emblaApi.off('autoplay:timerstopped', onTimerStopped)
|
|
434
636
|
emblaApi.off('select', resetOnSelect)
|
|
637
|
+
emblaApi.off('pointerdown', onUserStop)
|
|
638
|
+
emblaApi.off('slidefocus', onUserStop)
|
|
435
639
|
if (resumeAfter > 0) emblaApi.off('autoplay:interaction', onInteraction)
|
|
640
|
+
root?.removeEventListener('pointerenter', onPointerEnter)
|
|
641
|
+
root?.removeEventListener('pointerleave', onPointerLeave)
|
|
642
|
+
root?.removeEventListener('focusin', onFocusIn)
|
|
643
|
+
root?.removeEventListener('focusout', onFocusOut)
|
|
436
644
|
}
|
|
437
645
|
} else {
|
|
438
646
|
setAutoplayState(null)
|
|
@@ -440,16 +648,38 @@ function Carousel({
|
|
|
440
648
|
|
|
441
649
|
let cleanupAutoScroll: (() => void) | undefined
|
|
442
650
|
if (autoScrollPlugin && autoScrollEnabled && !autoplayEnabled) {
|
|
443
|
-
|
|
651
|
+
playAutoScroll()
|
|
652
|
+
const onPlay = () => {
|
|
653
|
+
if (pausedRef.current) setPaused(false)
|
|
654
|
+
}
|
|
655
|
+
const stopsOnPress = autoScrollPlugin.options.defaultInteraction !== false
|
|
656
|
+
const onUserStop = () => {
|
|
657
|
+
if (stopsOnPress) setPaused(true)
|
|
658
|
+
}
|
|
444
659
|
const onInteraction = (_api: CarouselApi, event: { detail: { interaction: string } }) => {
|
|
445
660
|
if (event.detail.interaction === 'pointerdown' || event.detail.interaction === 'slidefocus') {
|
|
446
|
-
|
|
447
|
-
scheduleResume(() => emblaApi.plugins().autoScroll?.play())
|
|
661
|
+
interruptAutoScroll()
|
|
448
662
|
}
|
|
449
663
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
664
|
+
emblaApi.on('autoscroll:play', onPlay)
|
|
665
|
+
emblaApi.on('pointerdown', onUserStop)
|
|
666
|
+
emblaApi.on('slidefocus', onUserStop)
|
|
667
|
+
if (resumeAfter > 0) emblaApi.on('autoscroll:interaction', onInteraction)
|
|
668
|
+
rotationRef.current = {
|
|
669
|
+
pause: () => {
|
|
670
|
+
clearResumeTimer()
|
|
671
|
+
emblaApi.plugins().autoScroll?.stop()
|
|
672
|
+
},
|
|
673
|
+
play: () => {
|
|
674
|
+
clearResumeTimer()
|
|
675
|
+
emblaApi.plugins().autoScroll?.play()
|
|
676
|
+
},
|
|
677
|
+
}
|
|
678
|
+
cleanupAutoScroll = () => {
|
|
679
|
+
emblaApi.off('autoscroll:play', onPlay)
|
|
680
|
+
emblaApi.off('pointerdown', onUserStop)
|
|
681
|
+
emblaApi.off('slidefocus', onUserStop)
|
|
682
|
+
if (resumeAfter > 0) emblaApi.off('autoscroll:interaction', onInteraction)
|
|
453
683
|
}
|
|
454
684
|
}
|
|
455
685
|
|
|
@@ -461,12 +691,10 @@ function Carousel({
|
|
|
461
691
|
const along = Math.abs(vertical ? event.deltaY : event.deltaX)
|
|
462
692
|
const across = Math.abs(vertical ? event.deltaX : event.deltaY)
|
|
463
693
|
if (along <= across) return
|
|
464
|
-
if (autoplayPlugin?.isPlaying()) {
|
|
465
|
-
|
|
466
|
-
scheduleResume(() => emblaApi.plugins().autoplay?.play())
|
|
694
|
+
if (autoplayPlugin?.isPlaying() || held) {
|
|
695
|
+
interruptAutoplay()
|
|
467
696
|
} else if (autoScrollPlugin?.isPlaying()) {
|
|
468
|
-
|
|
469
|
-
scheduleResume(() => emblaApi.plugins().autoScroll?.play())
|
|
697
|
+
interruptAutoScroll()
|
|
470
698
|
}
|
|
471
699
|
}
|
|
472
700
|
viewport.addEventListener('wheel', onWheel, { passive: true })
|
|
@@ -477,14 +705,23 @@ function Carousel({
|
|
|
477
705
|
emblaApi.off('reinit', handleReInit)
|
|
478
706
|
emblaApi.off('select', handleSelect)
|
|
479
707
|
emblaApi.off('scroll', handleScroll)
|
|
480
|
-
emblaApi.off('slidesinview', syncInView)
|
|
481
708
|
emblaApi.off('slideschanged', syncFull)
|
|
482
709
|
cleanupAutoplay?.()
|
|
483
710
|
cleanupAutoScroll?.()
|
|
484
711
|
cleanupWheel?.()
|
|
485
712
|
clearResumeTimer()
|
|
713
|
+
rotationRef.current = null
|
|
486
714
|
}
|
|
487
|
-
}, [
|
|
715
|
+
}, [
|
|
716
|
+
emblaApi,
|
|
717
|
+
autoplayEnabled,
|
|
718
|
+
autoScrollEnabled,
|
|
719
|
+
resumeAfterMs,
|
|
720
|
+
reducedMotion,
|
|
721
|
+
pauseOnHover,
|
|
722
|
+
pauseOnFocus,
|
|
723
|
+
setPaused,
|
|
724
|
+
])
|
|
488
725
|
|
|
489
726
|
const scrollPrev = React.useCallback(() => emblaApi?.goToPrev(), [emblaApi])
|
|
490
727
|
const scrollNext = React.useCallback(() => emblaApi?.goToNext(), [emblaApi])
|
|
@@ -508,6 +745,9 @@ function Carousel({
|
|
|
508
745
|
subscribeScrollProgress,
|
|
509
746
|
getScrollProgress,
|
|
510
747
|
autoplay: autoplayState,
|
|
748
|
+
paused,
|
|
749
|
+
pause,
|
|
750
|
+
play,
|
|
511
751
|
scrollPrev,
|
|
512
752
|
scrollNext,
|
|
513
753
|
scrollTo,
|
|
@@ -525,6 +765,9 @@ function Carousel({
|
|
|
525
765
|
subscribeScrollProgress,
|
|
526
766
|
getScrollProgress,
|
|
527
767
|
autoplayState,
|
|
768
|
+
paused,
|
|
769
|
+
pause,
|
|
770
|
+
play,
|
|
528
771
|
scrollPrev,
|
|
529
772
|
scrollNext,
|
|
530
773
|
scrollTo,
|
|
@@ -533,17 +776,19 @@ function Carousel({
|
|
|
533
776
|
|
|
534
777
|
return (
|
|
535
778
|
<CarouselContext.Provider value={contextValue}>
|
|
536
|
-
<
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
779
|
+
<CarouselRotationContext.Provider value={autoplayEnabled || autoScrollEnabled}>
|
|
780
|
+
<div
|
|
781
|
+
data-slot="carousel"
|
|
782
|
+
data-orientation={orientation}
|
|
783
|
+
role={rootProps['aria-label'] || rootProps['aria-labelledby'] ? 'region' : 'group'}
|
|
784
|
+
aria-roledescription="carousel"
|
|
785
|
+
className={cn('relative', className)}
|
|
786
|
+
{...rootProps}
|
|
787
|
+
ref={composedRootRef}
|
|
788
|
+
>
|
|
789
|
+
{children}
|
|
790
|
+
</div>
|
|
791
|
+
</CarouselRotationContext.Provider>
|
|
547
792
|
</CarouselContext.Provider>
|
|
548
793
|
)
|
|
549
794
|
}
|
|
@@ -732,6 +977,7 @@ function CarouselThumbs({
|
|
|
732
977
|
// means the same thing on both carousels.
|
|
733
978
|
containScroll="keepSnaps"
|
|
734
979
|
options={CAROUSEL_THUMB_OPTIONS}
|
|
980
|
+
accessibility={false}
|
|
735
981
|
setApi={setApi}
|
|
736
982
|
light={light}
|
|
737
983
|
role="group"
|
|
@@ -882,9 +1128,9 @@ function CarouselPrev({ className, position = 'inside', disabled: disabledProp,
|
|
|
882
1128
|
type: 'button',
|
|
883
1129
|
'data-slot': 'carousel-prev',
|
|
884
1130
|
'data-disabled': disabled || undefined,
|
|
1131
|
+
'aria-disabled': disabled || undefined,
|
|
885
1132
|
'aria-label': 'Previous slide',
|
|
886
|
-
disabled,
|
|
887
|
-
onClick: scrollPrev,
|
|
1133
|
+
onClick: disabled ? undefined : scrollPrev,
|
|
888
1134
|
suppressHydrationWarning: true,
|
|
889
1135
|
} as unknown as React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
890
1136
|
props,
|
|
@@ -922,9 +1168,9 @@ function CarouselNext({ className, position = 'inside', disabled: disabledProp,
|
|
|
922
1168
|
type: 'button',
|
|
923
1169
|
'data-slot': 'carousel-next',
|
|
924
1170
|
'data-disabled': disabled || undefined,
|
|
1171
|
+
'aria-disabled': disabled || undefined,
|
|
925
1172
|
'aria-label': 'Next slide',
|
|
926
|
-
disabled,
|
|
927
|
-
onClick: scrollNext,
|
|
1173
|
+
onClick: disabled ? undefined : scrollNext,
|
|
928
1174
|
suppressHydrationWarning: true,
|
|
929
1175
|
} as unknown as React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
930
1176
|
props,
|
|
@@ -943,6 +1189,41 @@ function CarouselNext({ className, position = 'inside', disabled: disabledProp,
|
|
|
943
1189
|
)
|
|
944
1190
|
}
|
|
945
1191
|
|
|
1192
|
+
type CarouselPlayToggleState = { paused: boolean }
|
|
1193
|
+
|
|
1194
|
+
type CarouselPlayToggleProps = useRender.ComponentProps<'button', CarouselPlayToggleState>
|
|
1195
|
+
|
|
1196
|
+
function CarouselPlayToggle({ className, render, ...props }: CarouselPlayToggleProps) {
|
|
1197
|
+
const { reducedMotion, paused, pause, play } = useCarousel()
|
|
1198
|
+
const rotates = React.useContext(CarouselRotationContext)
|
|
1199
|
+
const state: CarouselPlayToggleState = { paused }
|
|
1200
|
+
|
|
1201
|
+
const trigger = useRender({
|
|
1202
|
+
defaultTagName: 'button',
|
|
1203
|
+
render,
|
|
1204
|
+
state,
|
|
1205
|
+
props: mergeProps<'button'>(
|
|
1206
|
+
{
|
|
1207
|
+
type: 'button',
|
|
1208
|
+
'data-slot': 'carousel-play-toggle',
|
|
1209
|
+
'data-paused': paused || undefined,
|
|
1210
|
+
'aria-pressed': paused,
|
|
1211
|
+
'aria-label': 'Pause slides',
|
|
1212
|
+
onClick: paused ? play : pause,
|
|
1213
|
+
} as unknown as React.ButtonHTMLAttributes<HTMLButtonElement>,
|
|
1214
|
+
props,
|
|
1215
|
+
),
|
|
1216
|
+
})
|
|
1217
|
+
|
|
1218
|
+
if (!rotates || reducedMotion) return null
|
|
1219
|
+
|
|
1220
|
+
return (
|
|
1221
|
+
<div data-slot="carousel-play-toggle-positioner" className={cn('motion-reduce:hidden', className)}>
|
|
1222
|
+
{trigger}
|
|
1223
|
+
</div>
|
|
1224
|
+
)
|
|
1225
|
+
}
|
|
1226
|
+
|
|
946
1227
|
interface CarouselPaginationProps extends React.ComponentPropsWithoutRef<'div'> {
|
|
947
1228
|
/**
|
|
948
1229
|
* Lay the bullets out in a row or a column.
|
|
@@ -1021,6 +1302,13 @@ function CarouselPagination({
|
|
|
1021
1302
|
)
|
|
1022
1303
|
}
|
|
1023
1304
|
|
|
1305
|
+
// The bar slides in under its parent's `overflow-hidden` instead of growing, so
|
|
1306
|
+
// the countdown never triggers layout.
|
|
1307
|
+
const AUTOPLAY_INDICATOR_TRAVEL: Record<CarouselOrientation, { start: string; end: string }> = {
|
|
1308
|
+
horizontal: { start: '-translate-x-full rtl:translate-x-full', end: 'translate-x-0' },
|
|
1309
|
+
vertical: { start: '-translate-y-full', end: 'translate-y-0' },
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1024
1312
|
interface CarouselAutoplayIndicatorProps {
|
|
1025
1313
|
delay: number
|
|
1026
1314
|
isPlaying: boolean
|
|
@@ -1042,8 +1330,7 @@ function CarouselAutoplayIndicator({ delay, isPlaying, light, orientation }: Car
|
|
|
1042
1330
|
}
|
|
1043
1331
|
}, [])
|
|
1044
1332
|
|
|
1045
|
-
const
|
|
1046
|
-
const fillValue = armed && isPlaying ? '100%' : '0%'
|
|
1333
|
+
const travel = AUTOPLAY_INDICATOR_TRAVEL[orientation]
|
|
1047
1334
|
|
|
1048
1335
|
return (
|
|
1049
1336
|
<span
|
|
@@ -1052,17 +1339,12 @@ function CarouselAutoplayIndicator({ delay, isPlaying, light, orientation }: Car
|
|
|
1052
1339
|
data-orientation={orientation}
|
|
1053
1340
|
data-playing={isPlaying || undefined}
|
|
1054
1341
|
className={cn(
|
|
1055
|
-
'absolute rounded-full',
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
'motion-safe:ease-linear',
|
|
1342
|
+
'absolute inset-0 rounded-full',
|
|
1343
|
+
'motion-safe:transition-[translate] motion-safe:ease-linear',
|
|
1344
|
+
armed && isPlaying ? travel.end : travel.start,
|
|
1059
1345
|
light ? 'bg-white' : 'bg-primary',
|
|
1060
1346
|
)}
|
|
1061
|
-
style={{
|
|
1062
|
-
transitionDuration: `${delay}ms`,
|
|
1063
|
-
width: isHorizontal ? fillValue : undefined,
|
|
1064
|
-
height: isHorizontal ? undefined : fillValue,
|
|
1065
|
-
}}
|
|
1347
|
+
style={{ transitionDuration: `${delay}ms` }}
|
|
1066
1348
|
/>
|
|
1067
1349
|
)
|
|
1068
1350
|
}
|
|
@@ -1291,6 +1573,7 @@ export {
|
|
|
1291
1573
|
CarouselThumb,
|
|
1292
1574
|
CarouselPrev,
|
|
1293
1575
|
CarouselNext,
|
|
1576
|
+
CarouselPlayToggle,
|
|
1294
1577
|
CarouselPagination,
|
|
1295
1578
|
CarouselProgress,
|
|
1296
1579
|
useCarousel,
|
|
@@ -1304,6 +1587,7 @@ export type {
|
|
|
1304
1587
|
CarouselThumbProps,
|
|
1305
1588
|
CarouselPrevProps,
|
|
1306
1589
|
CarouselNextProps,
|
|
1590
|
+
CarouselPlayToggleProps,
|
|
1307
1591
|
CarouselPaginationProps,
|
|
1308
1592
|
CarouselProgressProps,
|
|
1309
1593
|
CarouselApi,
|
|
@@ -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 () => {
|