@oxy.so/bloom 7.21.0 → 7.22.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/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/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/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/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/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/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,15 +1,15 @@
|
|
|
1
|
+
import { CollapsibleFrame } from '../collapsible/CollapsibleFrame';
|
|
2
|
+
import { useCollapsibleMotion } from '../collapsible/use-collapsible-motion';
|
|
1
3
|
import React, {
|
|
2
4
|
memo,
|
|
3
5
|
useCallback,
|
|
4
6
|
useContext,
|
|
5
|
-
useState,
|
|
6
7
|
} from 'react';
|
|
7
8
|
import {
|
|
8
9
|
View,
|
|
9
10
|
Text,
|
|
10
11
|
Pressable,
|
|
11
12
|
Animated,
|
|
12
|
-
type LayoutChangeEvent,
|
|
13
13
|
StyleSheet,
|
|
14
14
|
type TextStyle,
|
|
15
15
|
} from 'react-native';
|
|
@@ -176,65 +176,12 @@ const AccordionTriggerComponent: React.FC<AccordionTriggerProps> = ({
|
|
|
176
176
|
|
|
177
177
|
// ---- Accordion Content ----
|
|
178
178
|
|
|
179
|
-
const AccordionContentComponent: React.FC<AccordionContentProps & { contentStyle?: AccordionContentProps['style'] }> = ({
|
|
180
|
-
children,
|
|
181
|
-
style,
|
|
182
|
-
contentStyle,
|
|
183
|
-
}) => {
|
|
179
|
+
const AccordionContentComponent: React.FC<AccordionContentProps & { contentStyle?: AccordionContentProps['style'] }> = ({children,style,contentStyle}) => {
|
|
184
180
|
const { isExpanded, contentId, triggerId } = useContext(AccordionItemContext);
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
// and no scrollbar — an accordion holding a form or a paragraph of prose just
|
|
190
|
-
// lost its bottom. The content still lays out at its natural height inside the
|
|
191
|
-
// clip, so `onLayout` reports the real one whether the item is open or shut.
|
|
192
|
-
const [contentHeight, setContentHeight] = useState(0);
|
|
193
|
-
const handleContentLayout = useCallback((event: LayoutChangeEvent) => {
|
|
194
|
-
const next = event.nativeEvent.layout.height;
|
|
195
|
-
// Sub-pixel churn would re-render on every frame of the spring.
|
|
196
|
-
setContentHeight((prev) => (Math.abs(prev - next) > 0.5 ? next : prev));
|
|
197
|
-
}, []);
|
|
198
|
-
|
|
199
|
-
const opacity = heightAnim.interpolate({
|
|
200
|
-
inputRange: [0, 1],
|
|
201
|
-
outputRange: [0, 1],
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
const maxHeight = heightAnim.interpolate({
|
|
205
|
-
inputRange: [0, 1],
|
|
206
|
-
outputRange: [0, contentHeight],
|
|
207
|
-
});
|
|
208
|
-
|
|
209
|
-
return (
|
|
210
|
-
<Animated.View
|
|
211
|
-
nativeID={contentId}
|
|
212
|
-
{...{ 'aria-labelledby': triggerId }}
|
|
213
|
-
aria-hidden={!isExpanded}
|
|
214
|
-
pointerEvents={isExpanded ? 'auto' : 'none'}
|
|
215
|
-
accessibilityElementsHidden={!isExpanded}
|
|
216
|
-
importantForAccessibility={isExpanded ? 'auto' : 'no-hide-descendants'}
|
|
217
|
-
style={[
|
|
218
|
-
{
|
|
219
|
-
overflow: 'hidden',
|
|
220
|
-
opacity,
|
|
221
|
-
// Before the first measurement an OPEN item must not be clipped to
|
|
222
|
-
// zero, and a SHUT one must not flash open — so the unmeasured frame
|
|
223
|
-
// takes the answer its state already implies, and every frame after
|
|
224
|
-
// that is driven by the real height.
|
|
225
|
-
maxHeight: contentHeight === 0 && isExpanded ? undefined : maxHeight,
|
|
226
|
-
},
|
|
227
|
-
style,
|
|
228
|
-
]}
|
|
229
|
-
>
|
|
230
|
-
<View
|
|
231
|
-
onLayout={handleContentLayout}
|
|
232
|
-
style={[{ paddingBottom: space.md, paddingLeft: space.xs, paddingRight: space.xs }, contentStyle]}
|
|
233
|
-
>
|
|
234
|
-
{children}
|
|
235
|
-
</View>
|
|
236
|
-
</Animated.View>
|
|
237
|
-
);
|
|
181
|
+
const { transition } = useContext(AccordionContext);
|
|
182
|
+
const motion = useCollapsibleMotion(isExpanded, transition);
|
|
183
|
+
return <CollapsibleFrame open={isExpanded} {...motion} nativeID={contentId} labelledBy={triggerId} returnFocusId={triggerId}
|
|
184
|
+
style={style} contentStyle={[{paddingBottom:space.md,paddingLeft:space.xs,paddingRight:space.xs},contentStyle]}>{children}</CollapsibleFrame>;
|
|
238
185
|
};
|
|
239
186
|
|
|
240
187
|
export const Accordion = memo(styled(AccordionComponent, { className: 'style' }));
|
|
@@ -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
|
}
|
|
@@ -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