@oxy.so/bloom 7.21.0 → 7.23.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/accordion.mdx +5 -0
- package/docs/adoption-matrix.mdx +6 -5
- package/docs/carousel.mdx +31 -0
- package/docs/collapsible.mdx +72 -0
- package/docs/migrating-to-1.0.mdx +1 -1
- package/docs/radio.mdx +15 -0
- package/lib/commonjs/accordion/Accordion.js +18 -45
- package/lib/commonjs/accordion/Accordion.js.map +1 -1
- package/lib/commonjs/accordion/Accordion.web.js +16 -53
- package/lib/commonjs/accordion/Accordion.web.js.map +1 -1
- package/lib/commonjs/accordion/use-accordion.js +3 -30
- package/lib/commonjs/accordion/use-accordion.js.map +1 -1
- package/lib/commonjs/carousel/Carousel.js +21 -13
- package/lib/commonjs/carousel/Carousel.js.map +1 -1
- package/lib/commonjs/collapsible/Collapsible.js +22 -0
- package/lib/commonjs/collapsible/Collapsible.js.map +1 -0
- package/lib/commonjs/collapsible/Collapsible.web.js +22 -0
- package/lib/commonjs/collapsible/Collapsible.web.js.map +1 -0
- package/lib/commonjs/collapsible/CollapsibleFrame.js +97 -0
- package/lib/commonjs/collapsible/CollapsibleFrame.js.map +1 -0
- package/lib/commonjs/collapsible/CollapsibleFrame.web.js +116 -0
- package/lib/commonjs/collapsible/CollapsibleFrame.web.js.map +1 -0
- package/lib/commonjs/collapsible/context.js +15 -0
- package/lib/commonjs/collapsible/context.js.map +1 -0
- package/lib/commonjs/collapsible/frame-types.js +6 -0
- package/lib/commonjs/collapsible/frame-types.js.map +1 -0
- package/lib/commonjs/collapsible/index.js +13 -0
- package/lib/commonjs/collapsible/index.js.map +1 -0
- package/lib/commonjs/collapsible/index.web.js +13 -0
- package/lib/commonjs/collapsible/index.web.js.map +1 -0
- package/lib/commonjs/collapsible/types.js +6 -0
- package/lib/commonjs/collapsible/types.js.map +1 -0
- package/lib/commonjs/collapsible/use-collapsible-motion.js +60 -0
- package/lib/commonjs/collapsible/use-collapsible-motion.js.map +1 -0
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +12 -0
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/radio/Radio.js +54 -26
- package/lib/commonjs/radio/Radio.js.map +1 -1
- package/lib/commonjs/radio/RadioCard.js +8 -1
- package/lib/commonjs/radio/RadioCard.js.map +1 -1
- package/lib/commonjs/radio/use-radio-group-item.js +3 -1
- package/lib/commonjs/radio/use-radio-group-item.js.map +1 -1
- package/lib/commonjs/select/Select.js +10 -5
- package/lib/commonjs/select/Select.js.map +1 -1
- package/lib/commonjs/select/Select.web.js +11 -3
- package/lib/commonjs/select/Select.web.js.map +1 -1
- package/lib/module/accordion/Accordion.js +19 -46
- package/lib/module/accordion/Accordion.js.map +1 -1
- package/lib/module/accordion/Accordion.web.js +17 -54
- package/lib/module/accordion/Accordion.web.js.map +1 -1
- package/lib/module/accordion/use-accordion.js +4 -31
- package/lib/module/accordion/use-accordion.js.map +1 -1
- package/lib/module/carousel/Carousel.js +21 -13
- package/lib/module/carousel/Carousel.js.map +1 -1
- package/lib/module/collapsible/Collapsible.js +17 -0
- package/lib/module/collapsible/Collapsible.js.map +1 -0
- package/lib/module/collapsible/Collapsible.web.js +17 -0
- package/lib/module/collapsible/Collapsible.web.js.map +1 -0
- package/lib/module/collapsible/CollapsibleFrame.js +92 -0
- package/lib/module/collapsible/CollapsibleFrame.js.map +1 -0
- package/lib/module/collapsible/CollapsibleFrame.web.js +111 -0
- package/lib/module/collapsible/CollapsibleFrame.web.js.map +1 -0
- package/lib/module/collapsible/context.js +11 -0
- package/lib/module/collapsible/context.js.map +1 -0
- package/lib/module/collapsible/frame-types.js +4 -0
- package/lib/module/collapsible/frame-types.js.map +1 -0
- package/lib/module/collapsible/index.js +4 -0
- package/lib/module/collapsible/index.js.map +1 -0
- package/lib/module/collapsible/index.web.js +9 -0
- package/lib/module/collapsible/index.web.js.map +1 -0
- package/lib/module/collapsible/types.js +4 -0
- package/lib/module/collapsible/types.js.map +1 -0
- package/lib/module/collapsible/use-collapsible-motion.js +55 -0
- package/lib/module/collapsible/use-collapsible-motion.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +1 -0
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/radio/Radio.js +54 -26
- package/lib/module/radio/Radio.js.map +1 -1
- package/lib/module/radio/RadioCard.js +8 -1
- package/lib/module/radio/RadioCard.js.map +1 -1
- package/lib/module/radio/use-radio-group-item.js +3 -1
- package/lib/module/radio/use-radio-group-item.js.map +1 -1
- package/lib/module/select/Select.js +7 -2
- package/lib/module/select/Select.js.map +1 -1
- package/lib/module/select/Select.web.js +11 -3
- package/lib/module/select/Select.web.js.map +1 -1
- package/lib/typescript/commonjs/accordion/Accordion.d.ts.map +1 -1
- package/lib/typescript/commonjs/accordion/Accordion.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/accordion/use-accordion.d.ts +2 -3
- package/lib/typescript/commonjs/accordion/use-accordion.d.ts.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 +7 -0
- package/lib/typescript/commonjs/carousel/types.d.ts.map +1 -1
- package/lib/typescript/commonjs/collapsible/Collapsible.d.ts +4 -0
- package/lib/typescript/commonjs/collapsible/Collapsible.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/Collapsible.web.d.ts +4 -0
- package/lib/typescript/commonjs/collapsible/Collapsible.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/CollapsibleFrame.d.ts +7 -0
- package/lib/typescript/commonjs/collapsible/CollapsibleFrame.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/CollapsibleFrame.web.d.ts +3 -0
- package/lib/typescript/commonjs/collapsible/CollapsibleFrame.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/context.d.ts +4 -0
- package/lib/typescript/commonjs/collapsible/context.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/frame-types.d.ts +11 -0
- package/lib/typescript/commonjs/collapsible/frame-types.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/index.d.ts +3 -0
- package/lib/typescript/commonjs/collapsible/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/index.web.d.ts +3 -0
- package/lib/typescript/commonjs/collapsible/index.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/types.d.ts +24 -0
- package/lib/typescript/commonjs/collapsible/types.d.ts.map +1 -0
- package/lib/typescript/commonjs/collapsible/use-collapsible-motion.d.ts +9 -0
- package/lib/typescript/commonjs/collapsible/use-collapsible-motion.d.ts.map +1 -0
- package/lib/typescript/commonjs/index.d.ts +1 -0
- package/lib/typescript/commonjs/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/index.web.d.ts +1 -0
- package/lib/typescript/commonjs/index.web.d.ts.map +1 -1
- package/lib/typescript/commonjs/radio/Radio.d.ts.map +1 -1
- package/lib/typescript/commonjs/radio/RadioCard.d.ts +1 -1
- package/lib/typescript/commonjs/radio/RadioCard.d.ts.map +1 -1
- package/lib/typescript/commonjs/radio/index.d.ts +1 -1
- package/lib/typescript/commonjs/radio/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/radio/types.d.ts +16 -2
- package/lib/typescript/commonjs/radio/types.d.ts.map +1 -1
- package/lib/typescript/commonjs/radio/use-radio-group-item.d.ts +17 -1
- package/lib/typescript/commonjs/radio/use-radio-group-item.d.ts.map +1 -1
- package/lib/typescript/commonjs/select/Select.d.ts.map +1 -1
- package/lib/typescript/commonjs/select/Select.web.d.ts.map +1 -1
- package/lib/typescript/module/accordion/Accordion.d.ts.map +1 -1
- package/lib/typescript/module/accordion/Accordion.web.d.ts.map +1 -1
- package/lib/typescript/module/accordion/use-accordion.d.ts +2 -3
- package/lib/typescript/module/accordion/use-accordion.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 +7 -0
- package/lib/typescript/module/carousel/types.d.ts.map +1 -1
- package/lib/typescript/module/collapsible/Collapsible.d.ts +4 -0
- package/lib/typescript/module/collapsible/Collapsible.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/Collapsible.web.d.ts +4 -0
- package/lib/typescript/module/collapsible/Collapsible.web.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/CollapsibleFrame.d.ts +7 -0
- package/lib/typescript/module/collapsible/CollapsibleFrame.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/CollapsibleFrame.web.d.ts +3 -0
- package/lib/typescript/module/collapsible/CollapsibleFrame.web.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/context.d.ts +4 -0
- package/lib/typescript/module/collapsible/context.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/frame-types.d.ts +11 -0
- package/lib/typescript/module/collapsible/frame-types.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/index.d.ts +3 -0
- package/lib/typescript/module/collapsible/index.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/index.web.d.ts +3 -0
- package/lib/typescript/module/collapsible/index.web.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/types.d.ts +24 -0
- package/lib/typescript/module/collapsible/types.d.ts.map +1 -0
- package/lib/typescript/module/collapsible/use-collapsible-motion.d.ts +9 -0
- package/lib/typescript/module/collapsible/use-collapsible-motion.d.ts.map +1 -0
- package/lib/typescript/module/index.d.ts +1 -0
- package/lib/typescript/module/index.d.ts.map +1 -1
- package/lib/typescript/module/index.web.d.ts +1 -0
- package/lib/typescript/module/index.web.d.ts.map +1 -1
- package/lib/typescript/module/radio/Radio.d.ts.map +1 -1
- package/lib/typescript/module/radio/RadioCard.d.ts +1 -1
- package/lib/typescript/module/radio/RadioCard.d.ts.map +1 -1
- package/lib/typescript/module/radio/index.d.ts +1 -1
- package/lib/typescript/module/radio/index.d.ts.map +1 -1
- package/lib/typescript/module/radio/types.d.ts +16 -2
- package/lib/typescript/module/radio/types.d.ts.map +1 -1
- package/lib/typescript/module/radio/use-radio-group-item.d.ts +17 -1
- package/lib/typescript/module/radio/use-radio-group-item.d.ts.map +1 -1
- package/lib/typescript/module/select/Select.d.ts.map +1 -1
- package/lib/typescript/module/select/Select.web.d.ts.map +1 -1
- package/package.json +23 -1
- package/src/accordion/Accordion.tsx +7 -60
- package/src/accordion/Accordion.web.tsx +8 -32
- package/src/accordion/use-accordion.ts +4 -21
- package/src/carousel/Carousel.tsx +22 -14
- package/src/carousel/types.ts +7 -0
- package/src/collapsible/Collapsible.tsx +15 -0
- package/src/collapsible/Collapsible.web.tsx +15 -0
- package/src/collapsible/CollapsibleFrame.tsx +120 -0
- package/src/collapsible/CollapsibleFrame.web.tsx +131 -0
- package/src/collapsible/context.ts +8 -0
- package/src/collapsible/frame-types.ts +14 -0
- package/src/collapsible/index.ts +2 -0
- package/src/collapsible/index.web.ts +7 -0
- package/src/collapsible/types.ts +22 -0
- package/src/collapsible/use-collapsible-motion.ts +61 -0
- package/src/index.ts +2 -0
- package/src/index.web.ts +2 -0
- package/src/radio/Radio.tsx +31 -12
- package/src/radio/RadioCard.tsx +3 -2
- package/src/radio/index.ts +1 -1
- package/src/radio/types.ts +17 -2
- package/src/radio/use-radio-group-item.ts +2 -2
- package/src/select/Select.tsx +5 -2
- package/src/select/Select.web.tsx +7 -3
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { CollapsibleFrame } from '../collapsible/CollapsibleFrame.web';
|
|
2
|
+
import { useCollapsibleMotion } from '../collapsible/use-collapsible-motion';
|
|
3
|
+
import React, { memo, useContext } from 'react';
|
|
4
|
+
import { Animated } from 'react-native';
|
|
3
5
|
import { RiArrowDownSLine } from '../icons/remix/RiArrowDownSLine';
|
|
4
6
|
import { resolveNativeWebStyle } from '../styles/resolve-native-web-style';
|
|
5
7
|
import { useInteractiveWebCss } from '../styles/interactive-web-css';
|
|
@@ -20,12 +22,6 @@ const CSS = `@layer base {
|
|
|
20
22
|
}`;
|
|
21
23
|
function useStyles() { useInteractiveWebCss('bloom-accordion', CSS); }
|
|
22
24
|
|
|
23
|
-
/** RN Animated resolves numeric layout/opacity before this DOM host receives them. */
|
|
24
|
-
const PanelHost = forwardRef<HTMLDivElement, Omit<React.HTMLAttributes<HTMLDivElement>, 'style'> & { style?: StyleProp<ViewStyle>; collapsable?: boolean }>(function PanelHost({ style, collapsable: _collapsable, ...props }, ref) {
|
|
25
|
-
return <div {...props} ref={ref} style={resolveNativeWebStyle(style)} />;
|
|
26
|
-
});
|
|
27
|
-
const AnimatedPanel = Animated.createAnimatedComponent(PanelHost);
|
|
28
|
-
|
|
29
25
|
export const Accordion = memo(function Accordion(props: AccordionProps) {
|
|
30
26
|
useStyles();
|
|
31
27
|
const state = useAccordionState(props);
|
|
@@ -64,28 +60,8 @@ export const AccordionTrigger = memo(function AccordionTrigger({ children, icon,
|
|
|
64
60
|
export const AccordionContent = memo(function AccordionContent({ children, style, className, contentClassName }: AccordionContentProps) {
|
|
65
61
|
useStyles();
|
|
66
62
|
const { isExpanded, triggerId, contentId } = useContext(AccordionItemContext);
|
|
67
|
-
const
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
useLayoutEffect(() => {
|
|
72
|
-
const node = body.current;
|
|
73
|
-
if (!node) return;
|
|
74
|
-
const measure = () => {
|
|
75
|
-
const next = node.getBoundingClientRect().height;
|
|
76
|
-
setHeight(previous => Math.abs(previous - next) > .5 ? next : previous);
|
|
77
|
-
};
|
|
78
|
-
measure();
|
|
79
|
-
const observer = new ResizeObserver(measure); observer.observe(node);
|
|
80
|
-
return () => observer.disconnect();
|
|
81
|
-
}, []);
|
|
82
|
-
useLayoutEffect(() => {
|
|
83
|
-
if (!isExpanded && panel.current?.contains(document.activeElement)) document.getElementById(triggerId)?.focus();
|
|
84
|
-
}, [isExpanded, triggerId]);
|
|
85
|
-
const maxHeight = progress.interpolate({ inputRange: [0, 1], outputRange: [0, height] });
|
|
86
|
-
return <AnimatedPanel ref={panel} id={contentId} role="region" aria-labelledby={triggerId} aria-hidden={!isExpanded} inert={!isExpanded}
|
|
87
|
-
className={['bloom-accordion-panel', className].filter(Boolean).join(' ')}
|
|
88
|
-
style={[{ opacity: progress, maxHeight: height === 0 && isExpanded ? undefined : maxHeight }, style]}>
|
|
89
|
-
<div ref={body} className={['bloom-accordion-body', contentClassName].filter(Boolean).join(' ')}>{children}</div>
|
|
90
|
-
</AnimatedPanel>;
|
|
63
|
+
const { transition } = useContext(AccordionContext);
|
|
64
|
+
const motion = useCollapsibleMotion(isExpanded, transition);
|
|
65
|
+
return <CollapsibleFrame open={isExpanded} {...motion} nativeID={contentId} region labelledBy={triggerId} returnFocusId={triggerId}
|
|
66
|
+
className={['bloom-accordion-panel',className].filter(Boolean).join(' ')} contentClassName={['bloom-accordion-body',contentClassName].filter(Boolean).join(' ')} style={style}>{children}</CollapsibleFrame>;
|
|
91
67
|
});
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { useCallback, useContext,
|
|
2
|
-
import {
|
|
3
|
-
import { usePrefersReducedMotion } from '../hooks/use-prefers-reduced-motion';
|
|
4
|
-
import { animation } from '../styles/tokens';
|
|
1
|
+
import { useCallback, useContext, useId, useMemo } from 'react';
|
|
2
|
+
import { useCollapsibleMotion } from '../collapsible/use-collapsible-motion';
|
|
5
3
|
import { AccordionContext } from './context';
|
|
6
4
|
import type { AccordionProps } from './types';
|
|
7
5
|
|
|
@@ -25,23 +23,8 @@ export function useAccordionItem(value: string, disabled: boolean) {
|
|
|
25
23
|
return useMemo(() => ({ value, disabled, isExpanded, triggerId: `${id}-trigger`, contentId: `${id}-content` }), [value, disabled, isExpanded, id]);
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
/**
|
|
26
|
+
/** Preserve accordion's spring defaults while sharing reveal cancellation. */
|
|
29
27
|
export function useAccordionMotion(expanded: boolean, kind: 'trigger' | 'content', useNativeDriver: boolean) {
|
|
30
28
|
const { transition } = useContext(AccordionContext);
|
|
31
|
-
|
|
32
|
-
const progress = useRef(new Animated.Value(expanded ? 1 : 0)).current;
|
|
33
|
-
const duration = transition === 'spring' ? undefined : transition.duration;
|
|
34
|
-
const [x1, y1, x2, y2] = transition === 'spring' || !transition.easing ? [.25, .1, .25, 1] : transition.easing;
|
|
35
|
-
const easing = useMemo(() => Easing.bezier(x1, y1, x2, y2), [x1, y1, x2, y2]);
|
|
36
|
-
useLayoutEffect(() => {
|
|
37
|
-
progress.stopAnimation();
|
|
38
|
-
const toValue = expanded ? 1 : 0;
|
|
39
|
-
if (reduced || duration === 0) { progress.setValue(toValue); return; }
|
|
40
|
-
const motion = duration === undefined
|
|
41
|
-
? Animated.spring(progress, { toValue, useNativeDriver, ...animation.spring[kind === 'trigger' ? 'snappy' : 'gentle'] })
|
|
42
|
-
: Animated.timing(progress, { toValue, useNativeDriver, duration: Math.max(0, duration), easing });
|
|
43
|
-
motion.start();
|
|
44
|
-
return () => motion.stop();
|
|
45
|
-
}, [expanded, progress, reduced, duration, easing, kind, useNativeDriver]);
|
|
46
|
-
return progress;
|
|
29
|
+
return useCollapsibleMotion(expanded, transition, kind, useNativeDriver).progress;
|
|
47
30
|
}
|
|
@@ -311,9 +311,10 @@ interface Stop {
|
|
|
311
311
|
* except the end, which stands for the LAST — the far end is where the last
|
|
312
312
|
* slide is, and naming anything else would leave it unreachable.
|
|
313
313
|
*/
|
|
314
|
-
function toStops(resting: readonly number[]): Stop[] {
|
|
314
|
+
function toStops(resting: readonly number[], groupSize = 1): Stop[] {
|
|
315
315
|
const stops: Stop[] = [];
|
|
316
316
|
resting.forEach((offset, slide) => {
|
|
317
|
+
if (slide % groupSize !== 0 && slide !== resting.length - 1) return;
|
|
317
318
|
const last = stops[stops.length - 1];
|
|
318
319
|
if (last && Math.abs(offset - last.offset) <= 1) return;
|
|
319
320
|
stops.push({ offset, slide });
|
|
@@ -329,6 +330,7 @@ const CarouselComponent = function Carousel({
|
|
|
329
330
|
header,
|
|
330
331
|
footer,
|
|
331
332
|
loop = false,
|
|
333
|
+
slidesPerGroup = 1,
|
|
332
334
|
showArrows = true,
|
|
333
335
|
arrowsPlacement = 'header',
|
|
334
336
|
arrowsInset = 8,
|
|
@@ -347,6 +349,8 @@ const CarouselComponent = function Carousel({
|
|
|
347
349
|
style,
|
|
348
350
|
testID,
|
|
349
351
|
}: CarouselProps) {
|
|
352
|
+
const groupSize = Number.isFinite(slidesPerGroup) ? Math.max(1, Math.trunc(slidesPerGroup)) : 1;
|
|
353
|
+
const ownsTrackKeys = loop || groupSize > 1;
|
|
350
354
|
const { messages } = useMessages(CAROUSEL_MESSAGES);
|
|
351
355
|
const previousLabel = previousLabelProp ?? messages.previousSlide;
|
|
352
356
|
const nextLabel = nextLabelProp ?? messages.nextSlide;
|
|
@@ -422,14 +426,14 @@ const CarouselComponent = function Carousel({
|
|
|
422
426
|
|
|
423
427
|
/**
|
|
424
428
|
* The distinct places the track can rest — or `null` until every slide has
|
|
425
|
-
* been measured.
|
|
426
|
-
*
|
|
429
|
+
* been measured. Dots, snapping and the active slide use individual stops;
|
|
430
|
+
* grouped controls filter the same measured offsets before deduplicating.
|
|
427
431
|
*/
|
|
428
|
-
const computeStops = useCallback((): Stop[] | null => {
|
|
432
|
+
const computeStops = useCallback((navigationGroupSize = 1): Stop[] | null => {
|
|
429
433
|
if (trackWidth <= 0 || scroll.current.contentWidth <= 0) return null;
|
|
430
434
|
const all = slideKeys.map(key => offsets.current.get(key));
|
|
431
435
|
if (all.some(item => !item || item.width <= 0)) return null;
|
|
432
|
-
return toStops((all as SlideOffset[]).map(targetFor));
|
|
436
|
+
return toStops((all as SlideOffset[]).map(targetFor), navigationGroupSize);
|
|
433
437
|
}, [slideKeys, targetFor, trackWidth]);
|
|
434
438
|
|
|
435
439
|
const measure = useCallback(() => {
|
|
@@ -564,9 +568,9 @@ const CarouselComponent = function Carousel({
|
|
|
564
568
|
window.addEventListener('pointerup', endDrag);
|
|
565
569
|
window.addEventListener('pointercancel', endDrag);
|
|
566
570
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
567
|
-
// Owned loop navigation must retain its pending destination so rapid
|
|
571
|
+
// Owned grouped/loop navigation must retain its pending destination so rapid
|
|
568
572
|
// key presses advance from the requested stop, not an animation frame.
|
|
569
|
-
if (
|
|
573
|
+
if (ownsTrackKeys && event.target === node && !event.altKey && !event.ctrlKey && !event.metaKey
|
|
570
574
|
&& (event.key === 'ArrowLeft' || event.key === 'ArrowRight')) return;
|
|
571
575
|
interruptControlledScroll();
|
|
572
576
|
};
|
|
@@ -579,7 +583,7 @@ const CarouselComponent = function Carousel({
|
|
|
579
583
|
node.removeEventListener('wheel', interruptControlledScroll);
|
|
580
584
|
node.removeEventListener('keydown', onKeyDown);
|
|
581
585
|
};
|
|
582
|
-
}, [controlledIndex !== undefined, interruptControlledScroll, beginDrag, endDrag,
|
|
586
|
+
}, [controlledIndex !== undefined, interruptControlledScroll, beginDrag, endDrag, ownsTrackKeys]);
|
|
583
587
|
|
|
584
588
|
const scrollToStop = (stop: Stop | undefined) => {
|
|
585
589
|
if (!stop) return;
|
|
@@ -593,13 +597,17 @@ const CarouselComponent = function Carousel({
|
|
|
593
597
|
|
|
594
598
|
/**
|
|
595
599
|
* The arrows step from where the track IS, to the nearest stop before or after
|
|
596
|
-
* it (1px of slack, as in `measure`)
|
|
597
|
-
*
|
|
600
|
+
* it (1px of slack, as in `measure`). Group destinations use every Nth child
|
|
601
|
+
* plus the physical end; individual snap stops and dot destinations remain
|
|
602
|
+
* unchanged. After a swipe/dot between groups, choose the adjacent boundary.
|
|
598
603
|
*/
|
|
599
604
|
const step = (direction: -1 | 1) => {
|
|
600
|
-
const all = computeStops();
|
|
605
|
+
const all = computeStops(groupSize);
|
|
601
606
|
if (!all) return;
|
|
602
|
-
|
|
607
|
+
// An accepted controlled destination stays authoritative after its animation
|
|
608
|
+
// settles: browser snapping may round its physical position by a pixel.
|
|
609
|
+
// Real drag/wheel input clears appliedOffset and resumes physical navigation.
|
|
610
|
+
const x = pendingTarget.current ?? appliedOffset.current ?? scroll.current.x;
|
|
603
611
|
const target = direction === 1
|
|
604
612
|
? all.find((stop) => stop.offset > x + 1)
|
|
605
613
|
: [...all].reverse().find((stop) => stop.offset < x - 1);
|
|
@@ -696,10 +704,10 @@ const CarouselComponent = function Carousel({
|
|
|
696
704
|
<ScrollView
|
|
697
705
|
ref={scrollRef}
|
|
698
706
|
{...webDataSet({ bloomCarouselTrack: align })}
|
|
699
|
-
// The browser scrolls a focused track by default; wrapping
|
|
707
|
+
// The browser scrolls a focused track by default; groups/wrapping opt into
|
|
700
708
|
// stop-based Left/Right navigation without intercepting slide controls.
|
|
701
709
|
{...(IS_WEB ? { tabIndex: 0, onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => {
|
|
702
|
-
if (!
|
|
710
|
+
if (!ownsTrackKeys || event.target !== event.currentTarget || event.altKey || event.ctrlKey || event.metaKey) return;
|
|
703
711
|
if (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight') return;
|
|
704
712
|
event.preventDefault(); event.stopPropagation();
|
|
705
713
|
step((event.key === 'ArrowRight') !== rtl ? 1 : -1);
|
package/src/carousel/types.ts
CHANGED
|
@@ -24,6 +24,13 @@ export interface CarouselProps {
|
|
|
24
24
|
footer?: ReactNode;
|
|
25
25
|
/** Controls and track arrow keys wrap to the other end; no cloned slides. Default false. */
|
|
26
26
|
loop?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Arrow/track-key destinations are anchored at children 0, N, 2N, plus the
|
|
29
|
+
* reachable end. Dots, direct index selection and swipe keep individual stops.
|
|
30
|
+
* Default 1. Finite values truncate to integers and clamp to at least 1;
|
|
31
|
+
* non-finite values use 1. May change responsively without moving selection.
|
|
32
|
+
*/
|
|
33
|
+
slidesPerGroup?: number;
|
|
27
34
|
/** Previous / next buttons. Defaults to `true`; callers may hide them at their mobile breakpoint. */
|
|
28
35
|
showArrows?: boolean;
|
|
29
36
|
/** Overlay control offsets from logical track edges. Negative values extend outside. Default 8. */
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React, { memo } from 'react';
|
|
2
|
+
import { CollapsibleFrame } from './CollapsibleFrame';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_COLLAPSIBLE_TRANSITION,
|
|
5
|
+
useCollapsibleMotion,
|
|
6
|
+
} from './use-collapsible-motion';
|
|
7
|
+
import type { CollapsibleProps } from './types';
|
|
8
|
+
|
|
9
|
+
export const Collapsible = memo(function Collapsible({
|
|
10
|
+
transition = DEFAULT_COLLAPSIBLE_TRANSITION,
|
|
11
|
+
...props
|
|
12
|
+
}: CollapsibleProps) {
|
|
13
|
+
const motion = useCollapsibleMotion(props.open, transition);
|
|
14
|
+
return <CollapsibleFrame {...props} {...motion} />;
|
|
15
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import React, { memo } from 'react';
|
|
2
|
+
import { CollapsibleFrame } from './CollapsibleFrame.web';
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_COLLAPSIBLE_TRANSITION,
|
|
5
|
+
useCollapsibleMotion,
|
|
6
|
+
} from './use-collapsible-motion';
|
|
7
|
+
import type { CollapsibleProps } from './types';
|
|
8
|
+
|
|
9
|
+
export const Collapsible = memo(function Collapsible({
|
|
10
|
+
transition = DEFAULT_COLLAPSIBLE_TRANSITION,
|
|
11
|
+
...props
|
|
12
|
+
}: CollapsibleProps) {
|
|
13
|
+
const motion = useCollapsibleMotion(props.open, transition);
|
|
14
|
+
return <CollapsibleFrame {...props} {...motion} />;
|
|
15
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
AccessibilityInfo,
|
|
4
|
+
Animated,
|
|
5
|
+
findNodeHandle,
|
|
6
|
+
TextInput,
|
|
7
|
+
View,
|
|
8
|
+
type LayoutChangeEvent,
|
|
9
|
+
} from 'react-native';
|
|
10
|
+
import { styled } from 'react-native-css';
|
|
11
|
+
import {
|
|
12
|
+
CollapsibleVisibilityContext,
|
|
13
|
+
useCollapsibleVisibility,
|
|
14
|
+
} from './context';
|
|
15
|
+
import type { CollapsibleFrameProps } from './frame-types';
|
|
16
|
+
|
|
17
|
+
function CollapsibleFrameComponent({
|
|
18
|
+
open,
|
|
19
|
+
hidden,
|
|
20
|
+
progress,
|
|
21
|
+
children,
|
|
22
|
+
style,
|
|
23
|
+
contentStyle,
|
|
24
|
+
returnFocusRef,
|
|
25
|
+
nativeID,
|
|
26
|
+
testID,
|
|
27
|
+
labelledBy,
|
|
28
|
+
returnFocusId,
|
|
29
|
+
}: CollapsibleFrameProps) {
|
|
30
|
+
const visible = useCollapsibleVisibility() && open;
|
|
31
|
+
const panel = useRef<View>(null);
|
|
32
|
+
const initiallyOpen = useRef(open).current;
|
|
33
|
+
const focusWithin = useRef(false);
|
|
34
|
+
const [height, setHeight] = useState(0);
|
|
35
|
+
const measure = useCallback((event: LayoutChangeEvent) => {
|
|
36
|
+
const next = event.nativeEvent.layout.height;
|
|
37
|
+
if (next > 0) setHeight((old) => (Math.abs(old - next) > 0.5 ? next : old));
|
|
38
|
+
}, []);
|
|
39
|
+
const restore = useCallback(() => {
|
|
40
|
+
const input = TextInput.State?.currentlyFocusedInput?.();
|
|
41
|
+
// Native host nodes expose contains without importing a private renderer.
|
|
42
|
+
const root = panel.current;
|
|
43
|
+
const inputInside = Boolean(
|
|
44
|
+
root &&
|
|
45
|
+
input &&
|
|
46
|
+
'contains' in root &&
|
|
47
|
+
typeof root.contains === 'function' &&
|
|
48
|
+
root.contains(input),
|
|
49
|
+
);
|
|
50
|
+
if (!focusWithin.current && !inputInside) return;
|
|
51
|
+
focusWithin.current = false;
|
|
52
|
+
if (inputInside) input?.blur();
|
|
53
|
+
const target =
|
|
54
|
+
returnFocusRef?.current ??
|
|
55
|
+
(returnFocusId && root && 'ownerDocument' in root
|
|
56
|
+
? root.ownerDocument?.getElementById(returnFocusId)
|
|
57
|
+
: null);
|
|
58
|
+
if (target && 'focus' in target && typeof target.focus === 'function') {
|
|
59
|
+
target.focus();
|
|
60
|
+
const handle =
|
|
61
|
+
'measureInWindow' in target
|
|
62
|
+
? findNodeHandle(target as unknown as React.Component)
|
|
63
|
+
: null;
|
|
64
|
+
if (handle != null) AccessibilityInfo.setAccessibilityFocus(handle);
|
|
65
|
+
}
|
|
66
|
+
}, [returnFocusRef, returnFocusId]);
|
|
67
|
+
useLayoutEffect(() => {
|
|
68
|
+
if (!visible) restore();
|
|
69
|
+
}, [visible, restore]);
|
|
70
|
+
const maxHeight = progress.interpolate({
|
|
71
|
+
inputRange: [0, 1],
|
|
72
|
+
outputRange: [0, height],
|
|
73
|
+
});
|
|
74
|
+
return (
|
|
75
|
+
<CollapsibleVisibilityContext.Provider value={visible}>
|
|
76
|
+
<Animated.View
|
|
77
|
+
ref={panel}
|
|
78
|
+
nativeID={nativeID}
|
|
79
|
+
testID={testID}
|
|
80
|
+
{...{ 'aria-labelledby': labelledBy }}
|
|
81
|
+
aria-hidden={!visible}
|
|
82
|
+
accessibilityElementsHidden={!visible}
|
|
83
|
+
importantForAccessibility={visible ? 'auto' : 'no-hide-descendants'}
|
|
84
|
+
pointerEvents={visible ? 'auto' : 'none'}
|
|
85
|
+
onFocus={() => {
|
|
86
|
+
focusWithin.current = true;
|
|
87
|
+
if (!visible) restore();
|
|
88
|
+
}}
|
|
89
|
+
onBlur={() => {
|
|
90
|
+
focusWithin.current = false;
|
|
91
|
+
}}
|
|
92
|
+
style={[
|
|
93
|
+
{
|
|
94
|
+
overflow: 'hidden',
|
|
95
|
+
opacity: progress,
|
|
96
|
+
maxHeight:
|
|
97
|
+
height === 0 && open && initiallyOpen ? undefined : maxHeight,
|
|
98
|
+
},
|
|
99
|
+
style,
|
|
100
|
+
]}
|
|
101
|
+
>
|
|
102
|
+
<View
|
|
103
|
+
collapsable={false}
|
|
104
|
+
onLayout={measure}
|
|
105
|
+
style={[
|
|
106
|
+
{ flexShrink: 0 },
|
|
107
|
+
contentStyle,
|
|
108
|
+
hidden ? { display: 'none' } : undefined,
|
|
109
|
+
]}
|
|
110
|
+
>
|
|
111
|
+
{children}
|
|
112
|
+
</View>
|
|
113
|
+
</Animated.View>
|
|
114
|
+
</CollapsibleVisibilityContext.Provider>
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
export const CollapsibleFrame = styled(CollapsibleFrameComponent, {
|
|
118
|
+
className: 'style',
|
|
119
|
+
contentClassName: 'contentStyle',
|
|
120
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import React, { forwardRef, useLayoutEffect, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { resolveNativeWebStyle } from '../styles/resolve-native-web-style';
|
|
4
|
+
import { useInteractiveWebCss } from '../styles/interactive-web-css';
|
|
5
|
+
import {
|
|
6
|
+
CollapsibleVisibilityContext,
|
|
7
|
+
useCollapsibleVisibility,
|
|
8
|
+
} from './context';
|
|
9
|
+
import type { CollapsibleFrameProps } from './frame-types';
|
|
10
|
+
|
|
11
|
+
const CSS = `@layer base {
|
|
12
|
+
.bloom-collapsible-panel,.bloom-collapsible-content { box-sizing:border-box;display:flex;flex-direction:column;flex-shrink:0;min-width:0; }
|
|
13
|
+
.bloom-collapsible-panel { overflow:hidden; }
|
|
14
|
+
}`;
|
|
15
|
+
const PanelHost = forwardRef<
|
|
16
|
+
HTMLDivElement,
|
|
17
|
+
Omit<React.HTMLAttributes<HTMLDivElement>, 'style'> & {
|
|
18
|
+
style?: StyleProp<ViewStyle>;
|
|
19
|
+
collapsable?: boolean;
|
|
20
|
+
}
|
|
21
|
+
>(function PanelHost({ style, collapsable: _collapsable, ...props }, ref) {
|
|
22
|
+
return <div {...props} ref={ref} style={resolveNativeWebStyle(style)} />;
|
|
23
|
+
});
|
|
24
|
+
const AnimatedPanel = Animated.createAnimatedComponent(PanelHost);
|
|
25
|
+
|
|
26
|
+
export function CollapsibleFrame({
|
|
27
|
+
open,
|
|
28
|
+
progress,
|
|
29
|
+
children,
|
|
30
|
+
style,
|
|
31
|
+
className,
|
|
32
|
+
contentStyle,
|
|
33
|
+
contentClassName,
|
|
34
|
+
returnFocusRef,
|
|
35
|
+
nativeID,
|
|
36
|
+
testID,
|
|
37
|
+
labelledBy,
|
|
38
|
+
returnFocusId,
|
|
39
|
+
region,
|
|
40
|
+
}: CollapsibleFrameProps) {
|
|
41
|
+
useInteractiveWebCss('bloom-collapsible', CSS);
|
|
42
|
+
const visible = useCollapsibleVisibility() && open;
|
|
43
|
+
const panel = useRef<HTMLDivElement>(null);
|
|
44
|
+
const focusWithin = useRef(false);
|
|
45
|
+
const body = useRef<HTMLDivElement>(null);
|
|
46
|
+
const previousOutside = useRef<HTMLElement | null>(null);
|
|
47
|
+
const [height, setHeight] = useState(0);
|
|
48
|
+
useLayoutEffect(() => {
|
|
49
|
+
const node = body.current;
|
|
50
|
+
if (!node) return;
|
|
51
|
+
const measure = () => {
|
|
52
|
+
const next = node.getBoundingClientRect().height;
|
|
53
|
+
if (next > 0)
|
|
54
|
+
setHeight((old) => (Math.abs(old - next) > 0.5 ? next : old));
|
|
55
|
+
};
|
|
56
|
+
measure();
|
|
57
|
+
const observer = new ResizeObserver(measure);
|
|
58
|
+
observer.observe(node);
|
|
59
|
+
return () => observer.disconnect();
|
|
60
|
+
}, []);
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
const active = document.activeElement;
|
|
63
|
+
if (visible) {
|
|
64
|
+
if (active instanceof HTMLElement && !panel.current?.contains(active))
|
|
65
|
+
previousOutside.current = active;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
if (!panel.current?.contains(active) && !focusWithin.current) return;
|
|
69
|
+
focusWithin.current = false;
|
|
70
|
+
const target =
|
|
71
|
+
returnFocusRef?.current ??
|
|
72
|
+
(returnFocusId ? document.getElementById(returnFocusId) : null) ??
|
|
73
|
+
previousOutside.current;
|
|
74
|
+
if (
|
|
75
|
+
target instanceof HTMLElement &&
|
|
76
|
+
(!target.isConnected || target.closest('[inert]'))
|
|
77
|
+
) {
|
|
78
|
+
if (active instanceof HTMLElement) active.blur();
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
target?.focus({ preventScroll: true });
|
|
82
|
+
if (
|
|
83
|
+
panel.current?.contains(document.activeElement) &&
|
|
84
|
+
active instanceof HTMLElement
|
|
85
|
+
)
|
|
86
|
+
active.blur();
|
|
87
|
+
}, [visible, returnFocusRef, returnFocusId]);
|
|
88
|
+
const maxHeight = progress.interpolate({
|
|
89
|
+
inputRange: [0, 1],
|
|
90
|
+
outputRange: [0, height],
|
|
91
|
+
});
|
|
92
|
+
return (
|
|
93
|
+
<CollapsibleVisibilityContext.Provider value={visible}>
|
|
94
|
+
<AnimatedPanel
|
|
95
|
+
ref={panel}
|
|
96
|
+
id={nativeID}
|
|
97
|
+
data-testid={testID}
|
|
98
|
+
role={region ? 'region' : undefined}
|
|
99
|
+
aria-labelledby={labelledBy}
|
|
100
|
+
aria-hidden={!visible}
|
|
101
|
+
inert={!visible}
|
|
102
|
+
onFocusCapture={() => {
|
|
103
|
+
focusWithin.current = true;
|
|
104
|
+
}}
|
|
105
|
+
onBlurCapture={() => {
|
|
106
|
+
focusWithin.current = false;
|
|
107
|
+
}}
|
|
108
|
+
className={['bloom-collapsible-panel', className]
|
|
109
|
+
.filter(Boolean)
|
|
110
|
+
.join(' ')}
|
|
111
|
+
style={[
|
|
112
|
+
{
|
|
113
|
+
opacity: progress,
|
|
114
|
+
maxHeight: height === 0 && open ? undefined : maxHeight,
|
|
115
|
+
},
|
|
116
|
+
style,
|
|
117
|
+
]}
|
|
118
|
+
>
|
|
119
|
+
<div
|
|
120
|
+
ref={body}
|
|
121
|
+
className={['bloom-collapsible-content', contentClassName]
|
|
122
|
+
.filter(Boolean)
|
|
123
|
+
.join(' ')}
|
|
124
|
+
style={resolveNativeWebStyle(contentStyle)}
|
|
125
|
+
>
|
|
126
|
+
{children}
|
|
127
|
+
</div>
|
|
128
|
+
</AnimatedPanel>
|
|
129
|
+
</CollapsibleVisibilityContext.Provider>
|
|
130
|
+
);
|
|
131
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
|
|
3
|
+
/** Logical visibility reaches portaled controls, unlike host-only inert/display. */
|
|
4
|
+
export const CollapsibleVisibilityContext = createContext(true);
|
|
5
|
+
CollapsibleVisibilityContext.displayName = 'CollapsibleVisibility';
|
|
6
|
+
export function useCollapsibleVisibility() {
|
|
7
|
+
return useContext(CollapsibleVisibilityContext);
|
|
8
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Animated } from 'react-native';
|
|
2
|
+
import type { CollapsibleProps } from './types';
|
|
3
|
+
|
|
4
|
+
export interface CollapsibleFrameProps extends Omit<
|
|
5
|
+
CollapsibleProps,
|
|
6
|
+
'transition'
|
|
7
|
+
> {
|
|
8
|
+
progress: Animated.Value;
|
|
9
|
+
hidden: boolean;
|
|
10
|
+
/** Accordion supplies its real relationship; standalone Collapsible supplies neither. */
|
|
11
|
+
labelledBy?: string;
|
|
12
|
+
returnFocusId?: string;
|
|
13
|
+
region?: boolean;
|
|
14
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// AUTO-GENERATED by scripts/generate-platform-exports.mjs — DO NOT EDIT.
|
|
2
|
+
// Source of truth: src/collapsible/index.ts.
|
|
3
|
+
// Re-run `bun run generate:exports` (or any `bun run build`) after
|
|
4
|
+
// changing that barrel or the set of web-forked subpaths.
|
|
5
|
+
|
|
6
|
+
export { Collapsible } from './Collapsible.web';
|
|
7
|
+
export type { CollapsibleProps, CollapsibleFocusTarget } from './types';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReactNode, RefObject } from 'react';
|
|
2
|
+
import type { StyleProp, ViewStyle } from 'react-native';
|
|
3
|
+
import type { SurfaceTransition } from '../motion/types';
|
|
4
|
+
|
|
5
|
+
/** A DOM or native host capable of receiving focus, without a fabricated trigger. */
|
|
6
|
+
export interface CollapsibleFocusTarget {
|
|
7
|
+
focus(options?: { preventScroll?: boolean }): void;
|
|
8
|
+
}
|
|
9
|
+
export interface CollapsibleProps {
|
|
10
|
+
open: boolean;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
/** Defaults to 300ms CSS ease-in-out. Live reduced motion settles immediately. */
|
|
13
|
+
transition?: SurfaceTransition;
|
|
14
|
+
/** Used only if focus was inside when closing; never steals deliberate outside focus. */
|
|
15
|
+
returnFocusRef?: RefObject<CollapsibleFocusTarget | null>;
|
|
16
|
+
className?: string;
|
|
17
|
+
style?: StyleProp<ViewStyle>;
|
|
18
|
+
contentClassName?: string;
|
|
19
|
+
contentStyle?: StyleProp<ViewStyle>;
|
|
20
|
+
nativeID?: string;
|
|
21
|
+
testID?: string;
|
|
22
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useLayoutEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import { Animated, Easing } from 'react-native';
|
|
3
|
+
import { usePrefersReducedMotion } from '../hooks/use-prefers-reduced-motion';
|
|
4
|
+
import { resolveSurfaceTransition } from '../motion/surface-transition';
|
|
5
|
+
import type { SurfaceTransition } from '../motion/types';
|
|
6
|
+
import { animation } from '../styles/tokens';
|
|
7
|
+
|
|
8
|
+
export const DEFAULT_COLLAPSIBLE_TRANSITION: SurfaceTransition = {
|
|
9
|
+
duration: 300,
|
|
10
|
+
easing: [0.42, 0, 0.58, 1],
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Shared measured-reveal motion; Accordion retains its existing springs. */
|
|
14
|
+
export function useCollapsibleMotion(
|
|
15
|
+
open: boolean,
|
|
16
|
+
transition: SurfaceTransition | 'spring',
|
|
17
|
+
kind: 'trigger' | 'content' = 'content',
|
|
18
|
+
useNativeDriver = false,
|
|
19
|
+
) {
|
|
20
|
+
const reduced = usePrefersReducedMotion();
|
|
21
|
+
const progress = useRef(new Animated.Value(open ? 1 : 0)).current;
|
|
22
|
+
const [hidden, setHidden] = useState(!open);
|
|
23
|
+
if (open && hidden) setHidden(false);
|
|
24
|
+
const resolved = resolveSurfaceTransition(
|
|
25
|
+
transition === 'spring' ? undefined : transition,
|
|
26
|
+
);
|
|
27
|
+
const duration = transition === 'spring' ? undefined : resolved.duration;
|
|
28
|
+
const [x1, y1, x2, y2] = resolved.easing;
|
|
29
|
+
const easing = useMemo(() => Easing.bezier(x1, y1, x2, y2), [x1, y1, x2, y2]);
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
let cancelled = false;
|
|
32
|
+
progress.stopAnimation();
|
|
33
|
+
const toValue = open ? 1 : 0;
|
|
34
|
+
if (reduced || duration === 0) {
|
|
35
|
+
progress.setValue(toValue);
|
|
36
|
+
setHidden(!open);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const motion =
|
|
40
|
+
duration === undefined
|
|
41
|
+
? Animated.spring(progress, {
|
|
42
|
+
toValue,
|
|
43
|
+
useNativeDriver,
|
|
44
|
+
...animation.spring[kind === 'trigger' ? 'snappy' : 'gentle'],
|
|
45
|
+
})
|
|
46
|
+
: Animated.timing(progress, {
|
|
47
|
+
toValue,
|
|
48
|
+
useNativeDriver,
|
|
49
|
+
duration,
|
|
50
|
+
easing,
|
|
51
|
+
});
|
|
52
|
+
motion.start((result) => {
|
|
53
|
+
if (!cancelled && result?.finished !== false) setHidden(!open);
|
|
54
|
+
});
|
|
55
|
+
return () => {
|
|
56
|
+
cancelled = true;
|
|
57
|
+
motion.stop();
|
|
58
|
+
};
|
|
59
|
+
}, [open, progress, reduced, duration, easing, kind, useNativeDriver]);
|
|
60
|
+
return { progress, hidden };
|
|
61
|
+
}
|
package/src/index.ts
CHANGED