@cognitap/radiant 0.0.68 → 0.0.69
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 +4 -0
- package/dist/library/Carousel.d.ts +46 -0
- package/dist/library/Carousel.d.ts.map +1 -0
- package/dist/library/Carousel.js +317 -0
- package/dist/library/Carousel.js.map +1 -0
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +3 -0
- package/dist/library/cn.js.map +1 -1
- package/dist/library/index.d.ts +1 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +1 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +5 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +1 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +5 -4
- package/package.json +1 -1
- package/src/Carousel.tsx +540 -0
- package/src/cn.ts +3 -0
- package/src/index.ts +16 -0
- package/src/themes/components.ts +1 -0
package/src/Carousel.tsx
ADDED
|
@@ -0,0 +1,540 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
|
|
3
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- Slides and their viewport are non-form ARIA groups with native scrolling. */
|
|
4
|
+
import {
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useId,
|
|
9
|
+
useLayoutEffect,
|
|
10
|
+
useRef,
|
|
11
|
+
useState,
|
|
12
|
+
type ComponentProps,
|
|
13
|
+
type ReactNode
|
|
14
|
+
} from 'react'
|
|
15
|
+
import { Button, type ButtonProps } from './Button.js'
|
|
16
|
+
import { cn } from './cn.js'
|
|
17
|
+
import { joinIds } from './internal/ids.js'
|
|
18
|
+
import { requireAccessibleName, type AccessibleName } from './internal/accessibleName.js'
|
|
19
|
+
import type { WithoutToggle } from './internal/types.js'
|
|
20
|
+
import { useControllableState } from './react/useControllableState.js'
|
|
21
|
+
import type { ValueOwner } from './react/useNativeOwner.js'
|
|
22
|
+
import { useComposedRefs } from './react/useComposedRefs.js'
|
|
23
|
+
import type { ThemeStyle } from './themes/defineTheme.js'
|
|
24
|
+
|
|
25
|
+
export type CarouselItem = { value: string; label?: string; children: ReactNode }
|
|
26
|
+
export type CarouselProps = Omit<ComponentProps<'section'>, 'style' | 'role'> &
|
|
27
|
+
AccessibleName &
|
|
28
|
+
ValueOwner<string | null> & {
|
|
29
|
+
items: readonly CarouselItem[]
|
|
30
|
+
onValueChange?: (value: string | null) => void
|
|
31
|
+
autoPlay?: boolean
|
|
32
|
+
interval?: number
|
|
33
|
+
slideLabel?: (index: number, count: number) => string
|
|
34
|
+
style?: ThemeStyle
|
|
35
|
+
'data-slot'?: string
|
|
36
|
+
}
|
|
37
|
+
export type CarouselState = {
|
|
38
|
+
value: string | null
|
|
39
|
+
index: number
|
|
40
|
+
count: number
|
|
41
|
+
playing: boolean
|
|
42
|
+
setValue: (value: string) => void
|
|
43
|
+
setPlaying: (playing: boolean) => void
|
|
44
|
+
}
|
|
45
|
+
type Binding = CarouselState & {
|
|
46
|
+
items: readonly CarouselItem[]
|
|
47
|
+
viewportId: string
|
|
48
|
+
setViewport: (node: HTMLDivElement | null) => void
|
|
49
|
+
slideLabel: (index: number, count: number) => string
|
|
50
|
+
}
|
|
51
|
+
const Context = createContext<Binding | null>(null)
|
|
52
|
+
export function useCarousel(): CarouselState | null {
|
|
53
|
+
return useContext(Context)
|
|
54
|
+
}
|
|
55
|
+
function binding() {
|
|
56
|
+
const value = useContext(Context)
|
|
57
|
+
if (!value) throw new Error('Carousel parts must be inside Carousel.')
|
|
58
|
+
return value
|
|
59
|
+
}
|
|
60
|
+
function slides(viewport: HTMLElement) {
|
|
61
|
+
return Array.from(viewport.children).filter(
|
|
62
|
+
(node): node is HTMLElement =>
|
|
63
|
+
node instanceof viewport.ownerDocument.defaultView!.HTMLElement &&
|
|
64
|
+
node.hasAttribute('data-rad-carousel-value')
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
function nearest(viewport: HTMLElement) {
|
|
68
|
+
const bounds = viewport.getBoundingClientRect(),
|
|
69
|
+
rtl = viewport.ownerDocument.defaultView!.getComputedStyle(viewport).direction === 'rtl'
|
|
70
|
+
return slides(viewport).reduce<HTMLElement | null>((best, node) => {
|
|
71
|
+
const distance = (item: HTMLElement) =>
|
|
72
|
+
Math.abs(
|
|
73
|
+
rtl
|
|
74
|
+
? item.getBoundingClientRect().right - bounds.right
|
|
75
|
+
: item.getBoundingClientRect().left - bounds.left
|
|
76
|
+
)
|
|
77
|
+
return !best || distance(node) < distance(best) ? node : best
|
|
78
|
+
}, null)
|
|
79
|
+
}
|
|
80
|
+
function scrollTo(viewport: HTMLElement, value: string | null, smooth = false) {
|
|
81
|
+
const node = slides(viewport).find(node => node.dataset.radCarouselValue === value)
|
|
82
|
+
if (!node) return
|
|
83
|
+
const view = viewport.ownerDocument.defaultView!,
|
|
84
|
+
box = viewport.getBoundingClientRect(),
|
|
85
|
+
rect = node.getBoundingClientRect(),
|
|
86
|
+
rtl = view.getComputedStyle(viewport).direction === 'rtl'
|
|
87
|
+
const delta = rtl ? rect.right - box.right : rect.left - box.left
|
|
88
|
+
if (Math.abs(delta) < 0.5) return false
|
|
89
|
+
viewport.scrollTo({
|
|
90
|
+
left: viewport.scrollLeft + delta,
|
|
91
|
+
behavior:
|
|
92
|
+
smooth && !view.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'smooth' : 'instant'
|
|
93
|
+
})
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
export function Carousel({
|
|
97
|
+
items,
|
|
98
|
+
value,
|
|
99
|
+
defaultValue,
|
|
100
|
+
onValueChange,
|
|
101
|
+
autoPlay = false,
|
|
102
|
+
interval = 5000,
|
|
103
|
+
slideLabel = (index, count) => `${index + 1} of ${count}`,
|
|
104
|
+
children,
|
|
105
|
+
ref,
|
|
106
|
+
className,
|
|
107
|
+
onPointerEnter,
|
|
108
|
+
onPointerLeave,
|
|
109
|
+
onFocus,
|
|
110
|
+
onBlur,
|
|
111
|
+
...props
|
|
112
|
+
}: CarouselProps) {
|
|
113
|
+
requireAccessibleName('Carousel', props)
|
|
114
|
+
if (!Number.isFinite(interval) || interval < 1000)
|
|
115
|
+
throw new TypeError('Carousel interval must be at least 1000ms.')
|
|
116
|
+
const keys = items.map(item => item.value)
|
|
117
|
+
if (keys.some(key => !key.trim()) || new Set(keys).size !== keys.length)
|
|
118
|
+
throw new TypeError('Carousel items need unique nonempty values.')
|
|
119
|
+
const owner = useControllableState({
|
|
120
|
+
name: 'Carousel value',
|
|
121
|
+
value,
|
|
122
|
+
defaultValue,
|
|
123
|
+
initialValue: items[0]?.value ?? null,
|
|
124
|
+
onChange: onValueChange
|
|
125
|
+
})
|
|
126
|
+
if (owner.value !== null && !keys.includes(owner.value))
|
|
127
|
+
throw new TypeError('Carousel value must match an item.')
|
|
128
|
+
const [viewport, setViewport] = useState<HTMLDivElement | null>(null),
|
|
129
|
+
[playing, setPlaying] = useState(autoPlay),
|
|
130
|
+
[hovered, setHovered] = useState(false),
|
|
131
|
+
[focused, setFocused] = useState(false),
|
|
132
|
+
[reduced, setReduced] = useState(false),
|
|
133
|
+
[hidden, setHidden] = useState(false)
|
|
134
|
+
const programmatic = useRef<string | null>(null)
|
|
135
|
+
const current = useRef(owner.value),
|
|
136
|
+
request = useRef(owner.request),
|
|
137
|
+
controlled = useRef(owner.controlled)
|
|
138
|
+
useLayoutEffect(() => {
|
|
139
|
+
current.current = owner.value
|
|
140
|
+
request.current = owner.request
|
|
141
|
+
controlled.current = owner.controlled
|
|
142
|
+
})
|
|
143
|
+
const id = useId(),
|
|
144
|
+
index = keys.indexOf(owner.value ?? ''),
|
|
145
|
+
viewportId = `${id}-viewport`
|
|
146
|
+
const [priorAutoPlay, setPriorAutoPlay] = useState(autoPlay)
|
|
147
|
+
if (priorAutoPlay !== autoPlay) {
|
|
148
|
+
setPriorAutoPlay(autoPlay)
|
|
149
|
+
setPlaying(autoPlay)
|
|
150
|
+
}
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (!viewport) return
|
|
153
|
+
const view = viewport.ownerDocument.defaultView!,
|
|
154
|
+
doc = viewport.ownerDocument,
|
|
155
|
+
query = view.matchMedia('(prefers-reduced-motion: reduce)')
|
|
156
|
+
const preferences = () => {
|
|
157
|
+
setReduced(query.matches)
|
|
158
|
+
setHidden(doc.hidden)
|
|
159
|
+
}
|
|
160
|
+
preferences()
|
|
161
|
+
query.addEventListener('change', preferences)
|
|
162
|
+
doc.addEventListener('visibilitychange', preferences)
|
|
163
|
+
return () => {
|
|
164
|
+
query.removeEventListener('change', preferences)
|
|
165
|
+
doc.removeEventListener('visibilitychange', preferences)
|
|
166
|
+
}
|
|
167
|
+
}, [viewport])
|
|
168
|
+
useLayoutEffect(() => {
|
|
169
|
+
if (viewport) {
|
|
170
|
+
programmatic.current = scrollTo(viewport, owner.value, true) ? owner.value : null
|
|
171
|
+
}
|
|
172
|
+
}, [viewport, owner.value])
|
|
173
|
+
const itemsKey = keys.join('\u0000')
|
|
174
|
+
useEffect(() => {
|
|
175
|
+
if (!viewport) return
|
|
176
|
+
const view = viewport.ownerDocument.defaultView!
|
|
177
|
+
let timer: number | undefined, frame: number | undefined
|
|
178
|
+
let lastRequested: string | null = null
|
|
179
|
+
const settled = () => {
|
|
180
|
+
view.clearTimeout(timer)
|
|
181
|
+
if (viewport.hasAttribute('data-dragging')) return
|
|
182
|
+
const next = nearest(viewport)?.dataset.radCarouselValue
|
|
183
|
+
if (programmatic.current !== null) {
|
|
184
|
+
if (next === programmatic.current) programmatic.current = null
|
|
185
|
+
return
|
|
186
|
+
}
|
|
187
|
+
if (!next || next === current.current) {
|
|
188
|
+
lastRequested = null
|
|
189
|
+
return
|
|
190
|
+
}
|
|
191
|
+
if (next === lastRequested) return
|
|
192
|
+
lastRequested = next
|
|
193
|
+
request.current(next)
|
|
194
|
+
if (controlled.current)
|
|
195
|
+
frame = view.requestAnimationFrame(() => scrollTo(viewport, current.current))
|
|
196
|
+
}
|
|
197
|
+
const scrolling = () => {
|
|
198
|
+
view.clearTimeout(timer)
|
|
199
|
+
timer = view.setTimeout(settled, 150)
|
|
200
|
+
}
|
|
201
|
+
const resize = new view.ResizeObserver(() => {
|
|
202
|
+
scrollTo(viewport, current.current)
|
|
203
|
+
})
|
|
204
|
+
const interrupt = () => {
|
|
205
|
+
programmatic.current = null
|
|
206
|
+
}
|
|
207
|
+
viewport.addEventListener('pointerdown', interrupt)
|
|
208
|
+
viewport.addEventListener('wheel', interrupt)
|
|
209
|
+
viewport.addEventListener('keydown', interrupt)
|
|
210
|
+
resize.observe(viewport)
|
|
211
|
+
viewport.addEventListener('scroll', scrolling)
|
|
212
|
+
viewport.addEventListener('scrollend', settled)
|
|
213
|
+
return () => {
|
|
214
|
+
view.clearTimeout(timer)
|
|
215
|
+
if (frame !== undefined) view.cancelAnimationFrame(frame)
|
|
216
|
+
resize.disconnect()
|
|
217
|
+
viewport.removeEventListener('pointerdown', interrupt)
|
|
218
|
+
viewport.removeEventListener('wheel', interrupt)
|
|
219
|
+
viewport.removeEventListener('keydown', interrupt)
|
|
220
|
+
viewport.removeEventListener('scroll', scrolling)
|
|
221
|
+
viewport.removeEventListener('scrollend', settled)
|
|
222
|
+
}
|
|
223
|
+
}, [viewport, itemsKey])
|
|
224
|
+
useEffect(() => {
|
|
225
|
+
if (!viewport || !playing || hovered || focused || reduced || hidden || items.length < 2) return
|
|
226
|
+
const view = viewport.ownerDocument.defaultView!,
|
|
227
|
+
timer = view.setTimeout(
|
|
228
|
+
() => request.current(items[(index + 1) % items.length]!.value),
|
|
229
|
+
interval
|
|
230
|
+
)
|
|
231
|
+
return () => view.clearTimeout(timer)
|
|
232
|
+
}, [viewport, playing, hovered, focused, reduced, hidden, items, index, interval])
|
|
233
|
+
const setValue = (next: string) => {
|
|
234
|
+
setPlaying(false)
|
|
235
|
+
owner.request(next)
|
|
236
|
+
}
|
|
237
|
+
return (
|
|
238
|
+
<Context.Provider
|
|
239
|
+
value={{
|
|
240
|
+
value: owner.value,
|
|
241
|
+
index,
|
|
242
|
+
count: items.length,
|
|
243
|
+
playing,
|
|
244
|
+
setValue,
|
|
245
|
+
setPlaying,
|
|
246
|
+
items,
|
|
247
|
+
viewportId,
|
|
248
|
+
setViewport,
|
|
249
|
+
slideLabel
|
|
250
|
+
}}
|
|
251
|
+
>
|
|
252
|
+
<section
|
|
253
|
+
{...props}
|
|
254
|
+
ref={ref}
|
|
255
|
+
aria-roledescription="carousel"
|
|
256
|
+
data-slot={joinIds('carousel', props['data-slot'])}
|
|
257
|
+
data-playing={playing && !hovered && !focused && !reduced && !hidden ? '' : undefined}
|
|
258
|
+
className={cn(
|
|
259
|
+
'grid min-w-0 gap-[var(--rad-carousel-gap,var(--rad-spacing-group-gap,1rem))]',
|
|
260
|
+
className
|
|
261
|
+
)}
|
|
262
|
+
onPointerEnter={event => {
|
|
263
|
+
onPointerEnter?.(event)
|
|
264
|
+
setHovered(true)
|
|
265
|
+
}}
|
|
266
|
+
onPointerLeave={event => {
|
|
267
|
+
onPointerLeave?.(event)
|
|
268
|
+
setHovered(false)
|
|
269
|
+
}}
|
|
270
|
+
onFocus={event => {
|
|
271
|
+
onFocus?.(event)
|
|
272
|
+
setFocused(true)
|
|
273
|
+
}}
|
|
274
|
+
onBlur={event => {
|
|
275
|
+
onBlur?.(event)
|
|
276
|
+
if (!event.currentTarget.contains(event.relatedTarget)) setFocused(false)
|
|
277
|
+
}}
|
|
278
|
+
>
|
|
279
|
+
{children}
|
|
280
|
+
<output
|
|
281
|
+
className="sr-only"
|
|
282
|
+
aria-live={playing ? 'off' : 'polite'}
|
|
283
|
+
aria-atomic="true"
|
|
284
|
+
>
|
|
285
|
+
{index >= 0 ? slideLabel(index, items.length) : ''}
|
|
286
|
+
</output>
|
|
287
|
+
</section>
|
|
288
|
+
</Context.Provider>
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
export type CarouselViewportProps = Omit<
|
|
292
|
+
ComponentProps<'div'>,
|
|
293
|
+
'children' | 'id' | 'style' | 'role' | 'tabIndex'
|
|
294
|
+
> & { style?: ThemeStyle; 'data-slot'?: string }
|
|
295
|
+
/* oxlint-disable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/no-noninteractive-tabindex -- The named native scroll viewport is keyboard reachable and retains group semantics. */
|
|
296
|
+
export function CarouselViewport({
|
|
297
|
+
ref,
|
|
298
|
+
className,
|
|
299
|
+
onKeyDown,
|
|
300
|
+
onPointerDown,
|
|
301
|
+
onPointerMove,
|
|
302
|
+
onPointerUp,
|
|
303
|
+
onPointerCancel,
|
|
304
|
+
onLostPointerCapture,
|
|
305
|
+
onClickCapture,
|
|
306
|
+
...props
|
|
307
|
+
}: CarouselViewportProps) {
|
|
308
|
+
const state = binding(),
|
|
309
|
+
host = useRef<HTMLDivElement>(null),
|
|
310
|
+
owned = useComposedRefs(host, state.setViewport),
|
|
311
|
+
composed = useComposedRefs(owned, ref),
|
|
312
|
+
gesture = useRef<{
|
|
313
|
+
id: number
|
|
314
|
+
x: number
|
|
315
|
+
left: number
|
|
316
|
+
moved: boolean
|
|
317
|
+
snap?: { value: string; priority: string }
|
|
318
|
+
} | null>(null),
|
|
319
|
+
suppress = useRef(false)
|
|
320
|
+
function finish(node: HTMLDivElement) {
|
|
321
|
+
const active = gesture.current
|
|
322
|
+
if (!active) return
|
|
323
|
+
gesture.current = null
|
|
324
|
+
if (active.snap && node.style.getPropertyValue('scroll-snap-type') === 'none') {
|
|
325
|
+
if (active.snap.value)
|
|
326
|
+
node.style.setProperty('scroll-snap-type', active.snap.value, active.snap.priority)
|
|
327
|
+
else node.style.removeProperty('scroll-snap-type')
|
|
328
|
+
}
|
|
329
|
+
node.removeAttribute('data-dragging')
|
|
330
|
+
if (node.hasPointerCapture(active.id)) node.releasePointerCapture(active.id)
|
|
331
|
+
if (active.moved) {
|
|
332
|
+
suppress.current = true
|
|
333
|
+
node.dispatchEvent(new Event('scroll'))
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
return (
|
|
337
|
+
<div
|
|
338
|
+
{...props}
|
|
339
|
+
ref={composed}
|
|
340
|
+
id={state.viewportId}
|
|
341
|
+
role="group"
|
|
342
|
+
aria-label={props['aria-label'] ?? 'Slides'}
|
|
343
|
+
tabIndex={0}
|
|
344
|
+
data-slot={joinIds('carousel-viewport', props['data-slot'])}
|
|
345
|
+
className={cn(
|
|
346
|
+
'flex min-w-0 overflow-x-auto overscroll-x-contain snap-x snap-mandatory [touch-action:pan-x_pan-y] gap-[var(--rad-carousel-slide-gap,0px)] rounded-[var(--rad-carousel-radius,var(--rad-surface-radius,0.75rem))] focus-visible:outline-[color:var(--rad-color-focus)] focus-visible:outline-[length:var(--rad-focus-width,2px)] focus-visible:outline-offset-2',
|
|
347
|
+
className
|
|
348
|
+
)}
|
|
349
|
+
onKeyDown={event => {
|
|
350
|
+
onKeyDown?.(event)
|
|
351
|
+
if (
|
|
352
|
+
event.defaultPrevented ||
|
|
353
|
+
event.target !== event.currentTarget ||
|
|
354
|
+
event.altKey ||
|
|
355
|
+
event.metaKey ||
|
|
356
|
+
event.ctrlKey
|
|
357
|
+
)
|
|
358
|
+
return
|
|
359
|
+
const rtl =
|
|
360
|
+
event.currentTarget.ownerDocument.defaultView!.getComputedStyle(event.currentTarget)
|
|
361
|
+
.direction === 'rtl'
|
|
362
|
+
let next: number | undefined
|
|
363
|
+
if (event.key === 'Home') next = 0
|
|
364
|
+
else if (event.key === 'End') next = state.count - 1
|
|
365
|
+
else if (event.key === 'ArrowRight') next = state.index + (rtl ? -1 : 1)
|
|
366
|
+
else if (event.key === 'ArrowLeft') next = state.index + (rtl ? 1 : -1)
|
|
367
|
+
if (next !== undefined) {
|
|
368
|
+
event.preventDefault()
|
|
369
|
+
const item = state.items[Math.max(0, Math.min(state.count - 1, next))]
|
|
370
|
+
if (item) state.setValue(item.value)
|
|
371
|
+
}
|
|
372
|
+
}}
|
|
373
|
+
onPointerDown={event => {
|
|
374
|
+
onPointerDown?.(event)
|
|
375
|
+
suppress.current = false
|
|
376
|
+
if (
|
|
377
|
+
event.defaultPrevented ||
|
|
378
|
+
event.pointerType === 'touch' ||
|
|
379
|
+
event.button !== 0 ||
|
|
380
|
+
!event.isPrimary ||
|
|
381
|
+
(event.target as Element).closest(
|
|
382
|
+
'a,button,input,textarea,select,[contenteditable="true"]'
|
|
383
|
+
)
|
|
384
|
+
)
|
|
385
|
+
return
|
|
386
|
+
gesture.current = {
|
|
387
|
+
id: event.pointerId,
|
|
388
|
+
x: event.clientX,
|
|
389
|
+
left: event.currentTarget.scrollLeft,
|
|
390
|
+
moved: false
|
|
391
|
+
}
|
|
392
|
+
}}
|
|
393
|
+
onPointerMove={event => {
|
|
394
|
+
onPointerMove?.(event)
|
|
395
|
+
const active = gesture.current
|
|
396
|
+
if (!active || active.id !== event.pointerId || event.defaultPrevented) return
|
|
397
|
+
const delta = event.clientX - active.x
|
|
398
|
+
if (!active.moved && Math.abs(delta) < 5) return
|
|
399
|
+
if (!active.moved) {
|
|
400
|
+
active.moved = true
|
|
401
|
+
active.snap = {
|
|
402
|
+
value: event.currentTarget.style.getPropertyValue('scroll-snap-type'),
|
|
403
|
+
priority: event.currentTarget.style.getPropertyPriority('scroll-snap-type')
|
|
404
|
+
}
|
|
405
|
+
event.currentTarget.setPointerCapture(event.pointerId)
|
|
406
|
+
event.currentTarget.style.scrollSnapType = 'none'
|
|
407
|
+
event.currentTarget.setAttribute('data-dragging', '')
|
|
408
|
+
}
|
|
409
|
+
event.preventDefault()
|
|
410
|
+
event.currentTarget.scrollLeft = active.left - delta
|
|
411
|
+
}}
|
|
412
|
+
onPointerUp={event => {
|
|
413
|
+
onPointerUp?.(event)
|
|
414
|
+
finish(event.currentTarget)
|
|
415
|
+
}}
|
|
416
|
+
onPointerCancel={event => {
|
|
417
|
+
onPointerCancel?.(event)
|
|
418
|
+
finish(event.currentTarget)
|
|
419
|
+
}}
|
|
420
|
+
onLostPointerCapture={event => {
|
|
421
|
+
onLostPointerCapture?.(event)
|
|
422
|
+
finish(event.currentTarget)
|
|
423
|
+
}}
|
|
424
|
+
onClickCapture={event => {
|
|
425
|
+
onClickCapture?.(event)
|
|
426
|
+
if (suppress.current) {
|
|
427
|
+
suppress.current = false
|
|
428
|
+
event.preventDefault()
|
|
429
|
+
event.stopPropagation()
|
|
430
|
+
}
|
|
431
|
+
}}
|
|
432
|
+
>
|
|
433
|
+
{state.items.map((item, index) => (
|
|
434
|
+
<div
|
|
435
|
+
key={item.value}
|
|
436
|
+
role="group"
|
|
437
|
+
aria-roledescription="slide"
|
|
438
|
+
aria-label={`${item.label ? `${item.label}, ` : ''}${state.slideLabel(index, state.count)}`}
|
|
439
|
+
data-slot="carousel-slide"
|
|
440
|
+
inert={state.value !== item.value || undefined}
|
|
441
|
+
data-rad-carousel-value={item.value}
|
|
442
|
+
data-selected={state.value === item.value ? '' : undefined}
|
|
443
|
+
className="box-border min-w-0 flex-[0_0_100%] snap-start"
|
|
444
|
+
>
|
|
445
|
+
{item.children}
|
|
446
|
+
</div>
|
|
447
|
+
))}
|
|
448
|
+
</div>
|
|
449
|
+
)
|
|
450
|
+
}
|
|
451
|
+
/* oxlint-enable jsx-a11y/no-noninteractive-element-interactions, jsx-a11y/no-noninteractive-tabindex */
|
|
452
|
+
export type CarouselActionProps = WithoutToggle<ButtonProps>
|
|
453
|
+
function Action({ direction, ...props }: CarouselActionProps & { direction: -1 | 1 }) {
|
|
454
|
+
const state = binding(),
|
|
455
|
+
item = state.items[state.index + direction]
|
|
456
|
+
return (
|
|
457
|
+
<Button
|
|
458
|
+
{...props}
|
|
459
|
+
pressed={undefined}
|
|
460
|
+
defaultPressed={undefined}
|
|
461
|
+
onPressedChange={undefined}
|
|
462
|
+
aria-pressed={undefined}
|
|
463
|
+
disabled={props.disabled || !item}
|
|
464
|
+
aria-controls={state.viewportId}
|
|
465
|
+
data-slot={joinIds(direction < 0 ? 'carousel-previous' : 'carousel-next', props['data-slot'])}
|
|
466
|
+
onClick={event => {
|
|
467
|
+
props.onClick?.(event)
|
|
468
|
+
if (!event.defaultPrevented && item) state.setValue(item.value)
|
|
469
|
+
}}
|
|
470
|
+
/>
|
|
471
|
+
)
|
|
472
|
+
}
|
|
473
|
+
export function CarouselPrevious(props: CarouselActionProps) {
|
|
474
|
+
return (
|
|
475
|
+
<Action
|
|
476
|
+
{...props}
|
|
477
|
+
direction={-1}
|
|
478
|
+
/>
|
|
479
|
+
)
|
|
480
|
+
}
|
|
481
|
+
export function CarouselNext(props: CarouselActionProps) {
|
|
482
|
+
return (
|
|
483
|
+
<Action
|
|
484
|
+
{...props}
|
|
485
|
+
direction={1}
|
|
486
|
+
/>
|
|
487
|
+
)
|
|
488
|
+
}
|
|
489
|
+
export type CarouselIndicatorsProps = Omit<ComponentProps<'div'>, 'children' | 'style'> & {
|
|
490
|
+
style?: ThemeStyle
|
|
491
|
+
indicatorLabel?: (index: number, count: number) => string
|
|
492
|
+
'data-slot'?: string
|
|
493
|
+
}
|
|
494
|
+
export function CarouselIndicators({
|
|
495
|
+
indicatorLabel = (index, count) => `Show slide ${index + 1} of ${count}`,
|
|
496
|
+
className,
|
|
497
|
+
...props
|
|
498
|
+
}: CarouselIndicatorsProps) {
|
|
499
|
+
const state = binding()
|
|
500
|
+
return (
|
|
501
|
+
<div
|
|
502
|
+
{...props}
|
|
503
|
+
data-slot={joinIds('carousel-indicators', props['data-slot'])}
|
|
504
|
+
className={cn('flex flex-wrap gap-2', className)}
|
|
505
|
+
>
|
|
506
|
+
{state.items.map((item, index) => (
|
|
507
|
+
<Button
|
|
508
|
+
key={item.value}
|
|
509
|
+
variant="ghost"
|
|
510
|
+
tone="neutral"
|
|
511
|
+
aria-label={indicatorLabel(index, state.count)}
|
|
512
|
+
aria-current={state.value === item.value ? 'true' : undefined}
|
|
513
|
+
aria-controls={state.viewportId}
|
|
514
|
+
onClick={() => state.setValue(item.value)}
|
|
515
|
+
>
|
|
516
|
+
{index + 1}
|
|
517
|
+
</Button>
|
|
518
|
+
))}
|
|
519
|
+
</div>
|
|
520
|
+
)
|
|
521
|
+
}
|
|
522
|
+
export function CarouselAutoPlay({ onClick, ...props }: CarouselActionProps) {
|
|
523
|
+
const state = binding()
|
|
524
|
+
return (
|
|
525
|
+
<Button
|
|
526
|
+
{...props}
|
|
527
|
+
pressed={undefined}
|
|
528
|
+
defaultPressed={undefined}
|
|
529
|
+
onPressedChange={undefined}
|
|
530
|
+
aria-pressed={undefined}
|
|
531
|
+
data-slot={joinIds('carousel-auto-play', props['data-slot'])}
|
|
532
|
+
onClick={event => {
|
|
533
|
+
onClick?.(event)
|
|
534
|
+
if (!event.defaultPrevented) state.setPlaying(!state.playing)
|
|
535
|
+
}}
|
|
536
|
+
>
|
|
537
|
+
{props.children ?? (state.playing ? 'Pause slides' : 'Play slides')}
|
|
538
|
+
</Button>
|
|
539
|
+
)
|
|
540
|
+
}
|
package/src/cn.ts
CHANGED
|
@@ -118,6 +118,8 @@ export const cn: CnFunction = createCn({
|
|
|
118
118
|
],
|
|
119
119
|
mb: ['mb-(--rad-multi-select-gap)', 'mb-(--rad-fieldset-gap)'],
|
|
120
120
|
gap: [
|
|
121
|
+
'gap-(--rad-carousel-gap)',
|
|
122
|
+
'gap-(--rad-carousel-slide-gap)',
|
|
121
123
|
'gap-(--rad-form-gap)',
|
|
122
124
|
'gap-(--rad-sidebar-gap)',
|
|
123
125
|
'gap-(--rad-navigation-menu-gap)',
|
|
@@ -199,6 +201,7 @@ export const cn: CnFunction = createCn({
|
|
|
199
201
|
'p-(--rad-tabs-content-padding)'
|
|
200
202
|
],
|
|
201
203
|
rounded: [
|
|
204
|
+
'rounded-(--rad-carousel-radius)',
|
|
202
205
|
'rounded-(--rad-navigation-menu-radius)',
|
|
203
206
|
'rounded-(--rad-code-block-radius)',
|
|
204
207
|
'rounded-(--rad-inline-code-radius)',
|
package/src/index.ts
CHANGED
|
@@ -751,3 +751,19 @@ export {
|
|
|
751
751
|
type FormState,
|
|
752
752
|
type FormSubmitContext
|
|
753
753
|
} from './Form.js'
|
|
754
|
+
|
|
755
|
+
export {
|
|
756
|
+
Carousel,
|
|
757
|
+
CarouselViewport,
|
|
758
|
+
CarouselPrevious,
|
|
759
|
+
CarouselNext,
|
|
760
|
+
CarouselIndicators,
|
|
761
|
+
CarouselAutoPlay,
|
|
762
|
+
useCarousel,
|
|
763
|
+
type CarouselProps,
|
|
764
|
+
type CarouselItem,
|
|
765
|
+
type CarouselState,
|
|
766
|
+
type CarouselViewportProps,
|
|
767
|
+
type CarouselActionProps,
|
|
768
|
+
type CarouselIndicatorsProps
|
|
769
|
+
} from './Carousel.js'
|
package/src/themes/components.ts
CHANGED