@oxy.so/bloom 7.9.2 → 7.11.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/docs/adoption-matrix.mdx +1 -1
- package/docs/carousel.mdx +58 -2
- package/lib/commonjs/carousel/Carousel.js +246 -66
- package/lib/commonjs/carousel/Carousel.js.map +1 -1
- package/lib/module/carousel/Carousel.js +247 -67
- package/lib/module/carousel/Carousel.js.map +1 -1
- package/lib/typescript/commonjs/carousel/Carousel.d.ts +1 -1
- package/lib/typescript/commonjs/carousel/Carousel.d.ts.map +1 -1
- package/lib/typescript/commonjs/carousel/types.d.ts +13 -3
- package/lib/typescript/commonjs/carousel/types.d.ts.map +1 -1
- package/lib/typescript/module/carousel/Carousel.d.ts +1 -1
- package/lib/typescript/module/carousel/Carousel.d.ts.map +1 -1
- package/lib/typescript/module/carousel/types.d.ts +13 -3
- package/lib/typescript/module/carousel/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/carousel/Carousel.tsx +228 -47
- package/src/carousel/types.ts +13 -3
|
@@ -11,11 +11,13 @@ export interface CarouselProps {
|
|
|
11
11
|
/**
|
|
12
12
|
* Leading content of the row above the track — typically the section title
|
|
13
13
|
* and a "See all" link. It shares the row with the arrows (header at the
|
|
14
|
-
* start, arrows at the end)
|
|
14
|
+
* start, arrows at the end). With overlay arrows this row contains only the header.
|
|
15
15
|
*/
|
|
16
16
|
header?: ReactNode;
|
|
17
|
-
/** Previous / next buttons
|
|
17
|
+
/** Previous / next buttons. Defaults to `true`; callers may hide them at their mobile breakpoint. */
|
|
18
18
|
showArrows?: boolean;
|
|
19
|
+
/** Arrows share the header row or overlay the track's edges. Defaults to `'header'`. */
|
|
20
|
+
arrowsPlacement?: 'header' | 'overlay';
|
|
19
21
|
/** Position indicator below the track. Defaults to `true`. */
|
|
20
22
|
showDots?: boolean;
|
|
21
23
|
/** Where a slide comes to rest when it snaps. Defaults to `'start'`. */
|
|
@@ -29,7 +31,15 @@ export interface CarouselProps {
|
|
|
29
31
|
* rests at the inset, not flush. Defaults to `0`.
|
|
30
32
|
*/
|
|
31
33
|
inset?: number;
|
|
32
|
-
/**
|
|
34
|
+
/**
|
|
35
|
+
* Controlled zero-based child index. Out-of-range values clamp to the list;
|
|
36
|
+
* omitted keeps the carousel uncontrolled. Pair with `onIndexChange`.
|
|
37
|
+
*/
|
|
38
|
+
index?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Uncontrolled: the slide in view changed. Controlled: arrows/dots or a
|
|
41
|
+
* settled swipe request a child index. Prop-driven scrolls do not echo here.
|
|
42
|
+
*/
|
|
33
43
|
onIndexChange?: (index: number) => void;
|
|
34
44
|
/** Accessible name of the previous button. Defaults to the localised `'Previous slide'` (in English). */
|
|
35
45
|
previousLabel?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/carousel/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD,MAAM,WAAW,aAAa;IAC5B,2EAA2E;IAC3E,QAAQ,EAAE,SAAS,CAAC;IACpB;;;OAGG;IACH,kBAAkB,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/carousel/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACvC,OAAO,KAAK,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzD,MAAM,WAAW,aAAa;IAC5B,2EAA2E;IAC3E,QAAQ,EAAE,SAAS,CAAC;IACpB;;;OAGG;IACH,kBAAkB,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;IACnB,qGAAqG;IACrG,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,wFAAwF;IACxF,eAAe,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;IACvC,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,wEAAwE;IACxE,KAAK,CAAC,EAAE,OAAO,GAAG,QAAQ,CAAC;IAC3B,mDAAmD;IACnD,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACxC,yGAAyG;IACzG,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iGAAiG;IACjG,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,sGAAsG;IACtG,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IACrC,uDAAuD;IACvD,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,SAAS,CAAC;IACpB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sDAAsD;IACtD,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,KAAK,CAAC,EAAE,SAAS,CAAC,SAAS,CAAC,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB"}
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ import React, {
|
|
|
5
5
|
useCallback,
|
|
6
6
|
useContext,
|
|
7
7
|
useEffect,
|
|
8
|
+
useLayoutEffect,
|
|
8
9
|
useMemo,
|
|
9
10
|
useRef,
|
|
10
11
|
useState,
|
|
@@ -25,6 +26,7 @@ import { Button } from '../button';
|
|
|
25
26
|
import { resolveButtonRamps } from '../button/shared';
|
|
26
27
|
import { RiArrowLeftSLine } from '../icons/remix/RiArrowLeftSLine';
|
|
27
28
|
import { RiArrowRightSLine } from '../icons/remix/RiArrowRightSLine';
|
|
29
|
+
import { useDirectionProps, useIsRtl } from '../hooks/use-is-rtl';
|
|
28
30
|
import { useInteractionState } from '../hooks/use-interaction-state';
|
|
29
31
|
import { useMessages } from '../locale/messages';
|
|
30
32
|
import { useInteractiveWebCss } from '../styles/interactive-web-css';
|
|
@@ -157,6 +159,7 @@ const CarouselItemComponent = function CarouselItem({
|
|
|
157
159
|
const index = useContext(CarouselItemIndexContext);
|
|
158
160
|
const { messages } = useMessages(CAROUSEL_MESSAGES);
|
|
159
161
|
const reportOffset = ctx?.reportOffset;
|
|
162
|
+
const itemRef = useRef<View>(null);
|
|
160
163
|
|
|
161
164
|
const onLayout = useCallback(
|
|
162
165
|
(event: LayoutChangeEvent) => {
|
|
@@ -167,9 +170,20 @@ const CarouselItemComponent = function CarouselItem({
|
|
|
167
170
|
);
|
|
168
171
|
|
|
169
172
|
const slideWidth = width ?? (ctx && ctx.trackWidth > 0 ? ctx.trackWidth : undefined);
|
|
173
|
+
useLayoutEffect(() => {
|
|
174
|
+
const node: unknown = itemRef.current;
|
|
175
|
+
if (!IS_WEB || typeof HTMLElement === 'undefined' || !(node instanceof HTMLElement)) return;
|
|
176
|
+
// RNW observes sizes, so a keyed slide reordered without resizing might
|
|
177
|
+
// receive no onLayout. Read its new position after that DOM commit too.
|
|
178
|
+
const parent = node.parentElement;
|
|
179
|
+
if (!parent) return;
|
|
180
|
+
const bounds = node.getBoundingClientRect();
|
|
181
|
+
reportOffset?.(index, bounds.left - parent.getBoundingClientRect().left, bounds.width);
|
|
182
|
+
}, [index, reportOffset, slideWidth]);
|
|
170
183
|
|
|
171
184
|
return (
|
|
172
185
|
<View
|
|
186
|
+
ref={itemRef}
|
|
173
187
|
{...webDataSet({ bloomCarouselItem: '' })}
|
|
174
188
|
role="group"
|
|
175
189
|
{...(IS_WEB ? { 'aria-roledescription': messages.slideRole } : {})}
|
|
@@ -302,10 +316,12 @@ const CarouselComponent = function Carousel({
|
|
|
302
316
|
accessibilityLabel,
|
|
303
317
|
header,
|
|
304
318
|
showArrows = true,
|
|
319
|
+
arrowsPlacement = 'header',
|
|
305
320
|
showDots = true,
|
|
306
321
|
align = 'start',
|
|
307
322
|
gap = 16,
|
|
308
323
|
inset = 0,
|
|
324
|
+
index,
|
|
309
325
|
onIndexChange,
|
|
310
326
|
previousLabel: previousLabelProp,
|
|
311
327
|
nextLabel: nextLabelProp,
|
|
@@ -317,15 +333,25 @@ const CarouselComponent = function Carousel({
|
|
|
317
333
|
const previousLabel = previousLabelProp ?? messages.previousSlide;
|
|
318
334
|
const nextLabel = nextLabelProp ?? messages.nextSlide;
|
|
319
335
|
const theme = useTheme();
|
|
336
|
+
const rtl = useIsRtl();
|
|
337
|
+
const directionProps = useDirectionProps();
|
|
320
338
|
useInteractiveWebCss(STYLE_ID, BLOOM_CAROUSEL_CSS);
|
|
321
339
|
const reducedMotion = useReducedMotion();
|
|
322
340
|
const paint = useMemo(() => resolveCarouselPaint(theme), [theme]);
|
|
323
341
|
|
|
324
342
|
const slides = Children.toArray(children).filter(isValidElement);
|
|
325
343
|
const count = slides.length;
|
|
344
|
+
// React's child keys keep measurements attached to slides across insertions,
|
|
345
|
+
// removals and reordering. Do not reuse a removed slide's numeric-index slot.
|
|
346
|
+
const keySignature = JSON.stringify(slides.map(child => child.key));
|
|
347
|
+
const slideKeys = useMemo(() => JSON.parse(keySignature) as string[], [keySignature]);
|
|
348
|
+
const controlledIndex = index === undefined ? undefined
|
|
349
|
+
: Math.min(Math.max(0, count - 1), Math.max(0, Math.trunc(Number.isFinite(index) ? index : 0)));
|
|
350
|
+
const controlledIndexRef = useRef(controlledIndex);
|
|
351
|
+
controlledIndexRef.current = controlledIndex;
|
|
326
352
|
|
|
327
353
|
const scrollRef = useRef<ScrollView>(null);
|
|
328
|
-
const offsets = useRef<SlideOffset
|
|
354
|
+
const offsets = useRef(new Map<string, SlideOffset>());
|
|
329
355
|
const scroll = useRef({ x: 0, contentWidth: 0 });
|
|
330
356
|
const [trackWidth, setTrackWidth] = useState(0);
|
|
331
357
|
const [stops, setStops] = useState<Stop[]>([]);
|
|
@@ -336,14 +362,42 @@ const CarouselComponent = function Carousel({
|
|
|
336
362
|
const onIndexChangeRef = useRef(onIndexChange);
|
|
337
363
|
onIndexChangeRef.current = onIndexChange;
|
|
338
364
|
const activeSlideRef = useRef(0);
|
|
365
|
+
const pendingTarget = useRef<number | null>(null);
|
|
366
|
+
const appliedOffset = useRef<number | null>(null);
|
|
367
|
+
const dragging = useRef(false);
|
|
368
|
+
const settleTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
369
|
+
const [settledRevision, setSettledRevision] = useState(0);
|
|
370
|
+
const lastAppliedSelection = useRef<{ index: number | undefined; keys: string; stops: Stop[] } | null>(null);
|
|
371
|
+
const clearSettleTimer = useCallback(() => {
|
|
372
|
+
if (settleTimer.current !== null) clearTimeout(settleTimer.current);
|
|
373
|
+
settleTimer.current = null;
|
|
374
|
+
}, []);
|
|
375
|
+
useEffect(() => clearSettleTimer, [clearSettleTimer]);
|
|
376
|
+
const scheduleSettled = useCallback(() => {
|
|
377
|
+
clearSettleTimer();
|
|
378
|
+
if (controlledIndexRef.current === undefined || dragging.current) return;
|
|
379
|
+
settleTimer.current = setTimeout(() => {
|
|
380
|
+
settleTimer.current = null;
|
|
381
|
+
const current = controlledIndexRef.current;
|
|
382
|
+
if (current === undefined || dragging.current) return;
|
|
383
|
+
if (activeSlideRef.current !== current) onIndexChangeRef.current?.(activeSlideRef.current);
|
|
384
|
+
// If the owner declined the request, restore its selected slide.
|
|
385
|
+
setSettledRevision(revision => revision + 1);
|
|
386
|
+
}, 150);
|
|
387
|
+
}, [clearSettleTimer]);
|
|
388
|
+
useEffect(() => {
|
|
389
|
+
const current = new Set(slideKeys);
|
|
390
|
+
for (const key of offsets.current.keys()) if (!current.has(key)) offsets.current.delete(key);
|
|
391
|
+
}, [slideKeys]);
|
|
339
392
|
|
|
340
393
|
const targetFor = useCallback(
|
|
341
394
|
(item: SlideOffset) => {
|
|
342
|
-
const
|
|
395
|
+
const start = rtl ? scroll.current.contentWidth - item.x - item.width : item.x;
|
|
396
|
+
const raw = align === 'center' ? start - (trackWidth - item.width) / 2 : start - inset;
|
|
343
397
|
const max = Math.max(0, scroll.current.contentWidth - trackWidth);
|
|
344
398
|
return Math.min(Math.max(0, raw), max);
|
|
345
399
|
},
|
|
346
|
-
[align, inset, trackWidth],
|
|
400
|
+
[align, inset, trackWidth, rtl],
|
|
347
401
|
);
|
|
348
402
|
|
|
349
403
|
/**
|
|
@@ -352,10 +406,11 @@ const CarouselComponent = function Carousel({
|
|
|
352
406
|
* ONE list, so they cannot disagree about where the track can go.
|
|
353
407
|
*/
|
|
354
408
|
const computeStops = useCallback((): Stop[] | null => {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
409
|
+
if (trackWidth <= 0 || scroll.current.contentWidth <= 0) return null;
|
|
410
|
+
const all = slideKeys.map(key => offsets.current.get(key));
|
|
411
|
+
if (all.some(item => !item || item.width <= 0)) return null;
|
|
412
|
+
return toStops((all as SlideOffset[]).map(targetFor));
|
|
413
|
+
}, [slideKeys, targetFor, trackWidth]);
|
|
359
414
|
|
|
360
415
|
const measure = useCallback(() => {
|
|
361
416
|
const { x, contentWidth } = scroll.current;
|
|
@@ -391,22 +446,25 @@ const CarouselComponent = function Carousel({
|
|
|
391
446
|
const slide = all[next]?.slide ?? 0;
|
|
392
447
|
if (slide !== activeSlideRef.current) {
|
|
393
448
|
activeSlideRef.current = slide;
|
|
394
|
-
onIndexChangeRef.current?.(slide);
|
|
449
|
+
if (controlledIndexRef.current === undefined) onIndexChangeRef.current?.(slide);
|
|
395
450
|
}
|
|
396
451
|
}, [computeStops, trackWidth]);
|
|
397
452
|
|
|
398
453
|
const recomputeStops = useCallback(() => {
|
|
399
454
|
const next = computeStops();
|
|
400
|
-
if (next) setStops(next)
|
|
455
|
+
if (next) setStops(previous => previous.length === next.length && previous.every((stop, i) =>
|
|
456
|
+
stop.offset === next[i]?.offset && stop.slide === next[i]?.slide) ? previous : next);
|
|
401
457
|
}, [computeStops]);
|
|
402
458
|
|
|
403
459
|
const reportOffset = useCallback(
|
|
404
460
|
(index: number, x: number, width: number) => {
|
|
405
|
-
|
|
461
|
+
const key = slideKeys[index];
|
|
462
|
+
if (key === undefined) return;
|
|
463
|
+
offsets.current.set(key, { x, width });
|
|
406
464
|
recomputeStops();
|
|
407
465
|
measure();
|
|
408
466
|
},
|
|
409
|
-
[measure, recomputeStops],
|
|
467
|
+
[measure, recomputeStops, slideKeys],
|
|
410
468
|
);
|
|
411
469
|
|
|
412
470
|
useEffect(() => {
|
|
@@ -414,9 +472,94 @@ const CarouselComponent = function Carousel({
|
|
|
414
472
|
measure();
|
|
415
473
|
}, [recomputeStops, measure]);
|
|
416
474
|
|
|
475
|
+
const scrollToOffset = useCallback((offset: number, animated: boolean) => {
|
|
476
|
+
// Web exposes negative scrollLeft in RTL; Android uses physical x, while
|
|
477
|
+
// iOS's ScrollView command already converts a logical offset internally.
|
|
478
|
+
const max = Math.max(0, scroll.current.contentWidth - trackWidth);
|
|
479
|
+
const x = !rtl ? offset : IS_WEB ? -offset
|
|
480
|
+
: Platform.OS === 'android' ? max - offset : offset;
|
|
481
|
+
scrollRef.current?.scrollTo({ x, animated });
|
|
482
|
+
}, [rtl, trackWidth]);
|
|
483
|
+
|
|
484
|
+
useLayoutEffect(() => {
|
|
485
|
+
if (controlledIndex === undefined) {
|
|
486
|
+
pendingTarget.current = null;
|
|
487
|
+
appliedOffset.current = null;
|
|
488
|
+
lastAppliedSelection.current = null;
|
|
489
|
+
clearSettleTimer();
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
clearSettleTimer();
|
|
493
|
+
const item = offsets.current.get(slideKeys[controlledIndex]!);
|
|
494
|
+
if (!item || !trackWidth || !scroll.current.contentWidth || !computeStops()) {
|
|
495
|
+
pendingTarget.current = null;
|
|
496
|
+
appliedOffset.current = null;
|
|
497
|
+
if (!slideKeys.length) lastAppliedSelection.current = null;
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
const target = targetFor(item);
|
|
501
|
+
// CSS snapping may retain the same keyed element after a reorder before
|
|
502
|
+
// RNW delivers onScroll. Read the committed DOM position, not that old event.
|
|
503
|
+
const node: unknown = IS_WEB ? scrollRef.current?.getScrollableNode() : null;
|
|
504
|
+
if (typeof HTMLElement !== 'undefined' && node instanceof HTMLElement) {
|
|
505
|
+
scroll.current.x = rtl ? -node.scrollLeft : node.scrollLeft;
|
|
506
|
+
}
|
|
507
|
+
appliedOffset.current = target;
|
|
508
|
+
const previous = lastAppliedSelection.current;
|
|
509
|
+
const changedIndex = previous !== null && previous.keys === keySignature && previous.stops === stops
|
|
510
|
+
&& !Object.is(previous.index, index);
|
|
511
|
+
lastAppliedSelection.current = { index, keys: keySignature, stops };
|
|
512
|
+
if (Math.abs(scroll.current.x - target) <= 1) {
|
|
513
|
+
pendingTarget.current = null;
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
pendingTarget.current = target;
|
|
517
|
+
// Initial positioning, resize and list changes are immediate. Only a new
|
|
518
|
+
// selection animates; intermediate scroll events must not rewrite that selection.
|
|
519
|
+
scrollToOffset(target, changedIndex && !reducedMotion);
|
|
520
|
+
}, [index, controlledIndex, keySignature, slideKeys, stops, trackWidth, computeStops, targetFor,
|
|
521
|
+
reducedMotion, scrollToOffset, settledRevision, clearSettleTimer, rtl]);
|
|
522
|
+
|
|
523
|
+
const interruptControlledScroll = useCallback(() => {
|
|
524
|
+
pendingTarget.current = null;
|
|
525
|
+
appliedOffset.current = null;
|
|
526
|
+
clearSettleTimer();
|
|
527
|
+
}, [clearSettleTimer]);
|
|
528
|
+
const beginDrag = useCallback(() => {
|
|
529
|
+
dragging.current = true;
|
|
530
|
+
interruptControlledScroll();
|
|
531
|
+
}, [interruptControlledScroll]);
|
|
532
|
+
const endDrag = useCallback(() => {
|
|
533
|
+
if (!dragging.current) return;
|
|
534
|
+
dragging.current = false;
|
|
535
|
+
scheduleSettled();
|
|
536
|
+
}, [scheduleSettled]);
|
|
537
|
+
useEffect(() => {
|
|
538
|
+
if (!IS_WEB || controlledIndex === undefined) return;
|
|
539
|
+
const node: unknown = scrollRef.current?.getScrollableNode();
|
|
540
|
+
if (typeof HTMLElement === 'undefined' || !(node instanceof HTMLElement)) return;
|
|
541
|
+
// RNW does not emit onScrollBeginDrag. Real input interrupts a requested
|
|
542
|
+
// smooth scroll so its resulting swipe/keyboard position can notify the owner.
|
|
543
|
+
node.addEventListener('pointerdown', beginDrag);
|
|
544
|
+
window.addEventListener('pointerup', endDrag);
|
|
545
|
+
window.addEventListener('pointercancel', endDrag);
|
|
546
|
+
for (const event of ['wheel', 'keydown']) node.addEventListener(event, interruptControlledScroll);
|
|
547
|
+
return () => {
|
|
548
|
+
node.removeEventListener('pointerdown', beginDrag);
|
|
549
|
+
window.removeEventListener('pointerup', endDrag);
|
|
550
|
+
window.removeEventListener('pointercancel', endDrag);
|
|
551
|
+
for (const event of ['wheel', 'keydown']) node.removeEventListener(event, interruptControlledScroll);
|
|
552
|
+
};
|
|
553
|
+
}, [controlledIndex !== undefined, interruptControlledScroll, beginDrag, endDrag]);
|
|
554
|
+
|
|
417
555
|
const scrollToStop = (stop: Stop | undefined) => {
|
|
418
556
|
if (!stop) return;
|
|
419
|
-
|
|
557
|
+
if (controlledIndex !== undefined) {
|
|
558
|
+
if (stop.slide !== controlledIndex) onIndexChangeRef.current?.(stop.slide);
|
|
559
|
+
setSettledRevision(revision => revision + 1);
|
|
560
|
+
} else {
|
|
561
|
+
scrollToOffset(stop.offset, !reducedMotion);
|
|
562
|
+
}
|
|
420
563
|
};
|
|
421
564
|
|
|
422
565
|
/**
|
|
@@ -427,7 +570,7 @@ const CarouselComponent = function Carousel({
|
|
|
427
570
|
const step = (direction: -1 | 1) => {
|
|
428
571
|
const all = computeStops();
|
|
429
572
|
if (!all) return;
|
|
430
|
-
const x = scroll.current.x;
|
|
573
|
+
const x = pendingTarget.current ?? scroll.current.x;
|
|
431
574
|
scrollToStop(
|
|
432
575
|
direction === 1
|
|
433
576
|
? all.find((stop) => stop.offset > x + 1)
|
|
@@ -437,8 +580,20 @@ const CarouselComponent = function Carousel({
|
|
|
437
580
|
|
|
438
581
|
const onScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
439
582
|
const { contentOffset, contentSize } = event.nativeEvent;
|
|
440
|
-
|
|
583
|
+
const max = Math.max(0, contentSize.width - trackWidth);
|
|
584
|
+
const x = !rtl ? contentOffset.x : IS_WEB ? -contentOffset.x
|
|
585
|
+
: Platform.OS === 'android' ? max - contentOffset.x : contentOffset.x;
|
|
586
|
+
scroll.current = { x, contentWidth: contentSize.width };
|
|
441
587
|
measure();
|
|
588
|
+
if (controlledIndexRef.current === undefined || count === 0 || !computeStops()) return;
|
|
589
|
+
if (pendingTarget.current !== null) {
|
|
590
|
+
if (Math.abs(x - pendingTarget.current) <= 1) pendingTarget.current = null;
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
if (appliedOffset.current !== null && Math.abs(x - appliedOffset.current) <= 1) return;
|
|
594
|
+
// Commit a real swipe only after it settles, preserving momentum and
|
|
595
|
+
// preventing intermediate controlled updates from interrupting the gesture.
|
|
596
|
+
scheduleSettled();
|
|
442
597
|
};
|
|
443
598
|
|
|
444
599
|
const onContentSizeChange = (width: number) => {
|
|
@@ -462,52 +617,78 @@ const CarouselComponent = function Carousel({
|
|
|
462
617
|
'--bloom-carousel-inset': `${inset}px`,
|
|
463
618
|
};
|
|
464
619
|
|
|
620
|
+
const overlay = arrowsPlacement === 'overlay';
|
|
621
|
+
const arrowsVisible = showArrows && count > 0;
|
|
622
|
+
const arrowButtons = <>
|
|
623
|
+
<Button size={overlay ? 'lg' : 'sm'} icon={rtl ? RiArrowRightSLine : RiArrowLeftSLine}
|
|
624
|
+
accessibilityLabel={previousLabel} disabled={atStart} onPress={() => step(-1)}
|
|
625
|
+
appearance="subtle" tone="neutral" />
|
|
626
|
+
<Button size={overlay ? 'lg' : 'sm'} icon={rtl ? RiArrowLeftSLine : RiArrowRightSLine}
|
|
627
|
+
accessibilityLabel={nextLabel} disabled={atEnd} onPress={() => step(1)}
|
|
628
|
+
appearance="subtle" tone="neutral" />
|
|
629
|
+
</>;
|
|
630
|
+
// iOS snapping reads physical offsets even though its imperative command and
|
|
631
|
+
// scroll events use logical offsets. Android converts snap offsets internally.
|
|
632
|
+
const snapOffsets = rtl && Platform.OS === 'ios'
|
|
633
|
+
? stops.map(stop => Math.max(0, scroll.current.contentWidth - trackWidth) - stop.offset).reverse()
|
|
634
|
+
: stops.map(stop => stop.offset);
|
|
635
|
+
|
|
636
|
+
const track = (
|
|
637
|
+
<CarouselContext.Provider value={contextValue}>
|
|
638
|
+
<ScrollView
|
|
639
|
+
ref={scrollRef}
|
|
640
|
+
{...webDataSet({ bloomCarouselTrack: align })}
|
|
641
|
+
// Focusable so the arrow keys scroll it once it has focus, the
|
|
642
|
+
// browser's own behaviour (`tabIndex={0}`).
|
|
643
|
+
{...(IS_WEB ? { tabIndex: 0 } : {})}
|
|
644
|
+
horizontal
|
|
645
|
+
showsHorizontalScrollIndicator={false}
|
|
646
|
+
scrollEventThrottle={16}
|
|
647
|
+
onScroll={onScroll}
|
|
648
|
+
onScrollBeginDrag={beginDrag}
|
|
649
|
+
onScrollEndDrag={endDrag}
|
|
650
|
+
onContentSizeChange={onContentSizeChange}
|
|
651
|
+
onLayout={(event) => setTrackWidth(event.nativeEvent.layout.width)}
|
|
652
|
+
snapToOffsets={IS_WEB ? undefined : snapOffsets}
|
|
653
|
+
decelerationRate={IS_WEB ? undefined : 'fast'}
|
|
654
|
+
disableIntervalMomentum
|
|
655
|
+
style={trackStyle}
|
|
656
|
+
contentContainerStyle={{ gap, paddingHorizontal: inset }}
|
|
657
|
+
>
|
|
658
|
+
{slides.map((child, index) => (
|
|
659
|
+
<CarouselItemIndexContext.Provider key={child.key ?? index} value={index}>
|
|
660
|
+
{child}
|
|
661
|
+
</CarouselItemIndexContext.Provider>
|
|
662
|
+
))}
|
|
663
|
+
</ScrollView>
|
|
664
|
+
</CarouselContext.Provider>
|
|
665
|
+
);
|
|
666
|
+
|
|
465
667
|
return (
|
|
466
668
|
<View
|
|
467
669
|
role="group"
|
|
670
|
+
{...directionProps}
|
|
468
671
|
{...(IS_WEB ? { 'aria-roledescription': messages.carouselRole } : {})}
|
|
469
672
|
accessibilityLabel={accessibilityLabel}
|
|
470
673
|
style={[{ width: '100%', flexDirection: 'column', gap: 16 }, style]}
|
|
471
674
|
testID={testID}
|
|
472
675
|
>
|
|
473
|
-
{header != null || (
|
|
676
|
+
{header != null || (arrowsVisible && !overlay) ? (
|
|
474
677
|
<View style={{ flexDirection: 'row', alignItems: 'center', gap: 8, paddingHorizontal: inset }}>
|
|
475
678
|
<View style={{ flex: 1, minWidth: 0 }}>{header}</View>
|
|
476
|
-
{
|
|
477
|
-
<>
|
|
478
|
-
<Button size="sm" icon={RiArrowLeftSLine} accessibilityLabel={previousLabel} disabled={atStart} onPress={() => step(-1)} appearance="subtle" tone="neutral" />
|
|
479
|
-
<Button size="sm" icon={RiArrowRightSLine} accessibilityLabel={nextLabel} disabled={atEnd} onPress={() => step(1)} appearance="subtle" tone="neutral" />
|
|
480
|
-
</>
|
|
481
|
-
) : null}
|
|
679
|
+
{arrowsVisible && !overlay ? arrowButtons : null}
|
|
482
680
|
</View>
|
|
483
681
|
) : null}
|
|
484
682
|
|
|
485
|
-
<
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
{
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
{
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
scrollEventThrottle={16}
|
|
495
|
-
onScroll={onScroll}
|
|
496
|
-
onContentSizeChange={onContentSizeChange}
|
|
497
|
-
onLayout={(event) => setTrackWidth(event.nativeEvent.layout.width)}
|
|
498
|
-
snapToOffsets={IS_WEB ? undefined : stops.map((stop) => stop.offset)}
|
|
499
|
-
decelerationRate={IS_WEB ? undefined : 'fast'}
|
|
500
|
-
disableIntervalMomentum
|
|
501
|
-
style={trackStyle}
|
|
502
|
-
contentContainerStyle={{ gap, paddingHorizontal: inset }}
|
|
503
|
-
>
|
|
504
|
-
{slides.map((child, index) => (
|
|
505
|
-
<CarouselItemIndexContext.Provider key={child.key ?? index} value={index}>
|
|
506
|
-
{child}
|
|
507
|
-
</CarouselItemIndexContext.Provider>
|
|
508
|
-
))}
|
|
509
|
-
</ScrollView>
|
|
510
|
-
</CarouselContext.Provider>
|
|
683
|
+
{overlay ? <View style={{ position: 'relative', width: '100%' }} testID={testID ? `${testID}-track-frame` : undefined}>
|
|
684
|
+
{track}
|
|
685
|
+
{arrowsVisible && overlay ? <View pointerEvents="box-none"
|
|
686
|
+
testID={testID ? `${testID}-overlay-arrows` : undefined}
|
|
687
|
+
style={{ position: 'absolute', top: 0, bottom: 0, insetInlineStart: 8, insetInlineEnd: 8,
|
|
688
|
+
flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
|
|
689
|
+
{arrowButtons}
|
|
690
|
+
</View> : null}
|
|
691
|
+
</View> : track}
|
|
511
692
|
|
|
512
693
|
{showDots && dotCount > 1 ? (
|
|
513
694
|
<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: 6, paddingHorizontal: inset }}>
|
package/src/carousel/types.ts
CHANGED
|
@@ -12,11 +12,13 @@ export interface CarouselProps {
|
|
|
12
12
|
/**
|
|
13
13
|
* Leading content of the row above the track — typically the section title
|
|
14
14
|
* and a "See all" link. It shares the row with the arrows (header at the
|
|
15
|
-
* start, arrows at the end)
|
|
15
|
+
* start, arrows at the end). With overlay arrows this row contains only the header.
|
|
16
16
|
*/
|
|
17
17
|
header?: ReactNode;
|
|
18
|
-
/** Previous / next buttons
|
|
18
|
+
/** Previous / next buttons. Defaults to `true`; callers may hide them at their mobile breakpoint. */
|
|
19
19
|
showArrows?: boolean;
|
|
20
|
+
/** Arrows share the header row or overlay the track's edges. Defaults to `'header'`. */
|
|
21
|
+
arrowsPlacement?: 'header' | 'overlay';
|
|
20
22
|
/** Position indicator below the track. Defaults to `true`. */
|
|
21
23
|
showDots?: boolean;
|
|
22
24
|
/** Where a slide comes to rest when it snaps. Defaults to `'start'`. */
|
|
@@ -30,7 +32,15 @@ export interface CarouselProps {
|
|
|
30
32
|
* rests at the inset, not flush. Defaults to `0`.
|
|
31
33
|
*/
|
|
32
34
|
inset?: number;
|
|
33
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* Controlled zero-based child index. Out-of-range values clamp to the list;
|
|
37
|
+
* omitted keeps the carousel uncontrolled. Pair with `onIndexChange`.
|
|
38
|
+
*/
|
|
39
|
+
index?: number;
|
|
40
|
+
/**
|
|
41
|
+
* Uncontrolled: the slide in view changed. Controlled: arrows/dots or a
|
|
42
|
+
* settled swipe request a child index. Prop-driven scrolls do not echo here.
|
|
43
|
+
*/
|
|
34
44
|
onIndexChange?: (index: number) => void;
|
|
35
45
|
/** Accessible name of the previous button. Defaults to the localised `'Previous slide'` (in English). */
|
|
36
46
|
previousLabel?: string;
|