Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 3x 106x 106x 106x 106x 106x 106x 106x 106x 106x 86x 80x 6x 86x 86x 264x 106x 106x 106x 97x 106x 9x 106x 3x 627x 627x 627x | import { useState, useCallback, ReactNode, useEffect } from 'react';
import { View, Text, StyleSheet, LayoutChangeEvent, StyleProp, ViewStyle } from 'react-native';
import { useAccordion } from '../Context/AccordionContext';
import Animated, {
useSharedValue,
useAnimatedStyle,
withTiming,
Easing,
} from 'react-native-reanimated';
import { Header } from '../Atoms';
import { getDirection, getOrientation, extractTextClasses } from '../Utils';
import type { HorizontalDirection, VerticalDirection } from '../../../Accordion.types';
import { cn } from '@sb/libs';
const AnimatedView = Animated.createAnimatedComponent(View);
type AccordionLayoutProps = {
id: string;
headerClassName?: string;
contentClassName?: string;
headerLabel: string | ReactNode;
headerContent: string | ReactNode;
content: string | ReactNode;
footer: string | ReactNode;
style?: StyleProp<ViewStyle>;
};
function AccordionLayout({
id,
headerLabel,
headerContent,
content,
footer,
style,
headerClassName,
contentClassName,
}: AccordionLayoutProps) {
const { headerPosition, isItemOpen } = useAccordion();
const orientation = getOrientation(headerPosition);
const direction = getDirection(headerPosition);
const isExpanded = isItemOpen(id);
// Shared animated values
const height = useSharedValue(0);
const opacity = useSharedValue(0);
const [measuredHeight, setMeasuredHeight] = useState(0);
// Additional measurements for horizontal accordion
const [fullContentHeight, setFullContentHeight] = useState(0);
// Animate whenever isExpanded or measurement changes
useEffect(() => {
let toSize: number;
if (orientation === 'vertical') {
toSize = isExpanded ? measuredHeight : 0;
} else {
// For horizontal: collapsed = 0 (no content/footer), expanded = content + footer height
toSize = isExpanded ? fullContentHeight : 0;
}
height.value = withTiming(toSize, {
duration: 350,
easing: Easing.inOut(Easing.sin),
});
opacity.value =
orientation === 'vertical'
? withTiming(isExpanded ? 1 : 0, {
duration: 300,
easing: Easing.linear,
})
: 1;
}, [isExpanded, measuredHeight, fullContentHeight, orientation]);
// Animated styles
const animatedStyle = useAnimatedStyle(() => ({
height: height.value,
opacity: opacity.value,
overflow: 'hidden',
}));
// Always capture dimensions (no guard)
const onContentLayout = useCallback(
(e: LayoutChangeEvent) => {
const dimension =
orientation === 'vertical' ? e.nativeEvent.layout.height : e.nativeEvent.layout.width;
setMeasuredHeight(dimension);
},
[orientation]
);
const onFullContentLayout = useCallback((e: LayoutChangeEvent) => {
const newHeight = e.nativeEvent.layout.height;
setFullContentHeight(newHeight);
}, []);
// Renders vertical accordion
const renderVertical = (dir: VerticalDirection) => (
<View style={[styles.container, style, dir === 'top' ? styles.top : styles.bottom]}>
<Header id={id} label={headerLabel} className={headerClassName} />
{/* hidden measurement view always rendered */}
<View style={styles.hidden} onLayout={onContentLayout}>
<AccordionItem
content={headerContent}
orientation="vertical"
className={contentClassName}
/>
<AccordionItem content={content} orientation="vertical" className={contentClassName} />
<AccordionItem content={footer} orientation="vertical" className={contentClassName} />
</View>
<AnimatedView style={animatedStyle}>
<View>
<AccordionItem
content={headerContent}
orientation="vertical"
className={contentClassName}
/>
<AccordionItem content={content} orientation="vertical" className={contentClassName} />
<AccordionItem content={footer} orientation="vertical" className={contentClassName} />
</View>
</AnimatedView>
</View>
);
// Renders horizontal accordion
const renderHorizontal = (dir: HorizontalDirection) => (
<View style={[styles.container, style]}>
<View style={[styles.horizontalContent, dir === 'left' ? styles.left : styles.right]}>
<View style={{ flex: 1 }}>
{/* Always visible headerContent */}
<AccordionItem
content={headerContent}
orientation="horizontal"
className={contentClassName}
/>
{/* Hidden measurement view always rendered */}
<View style={styles.hidden} onLayout={onFullContentLayout}>
{content ? (
<AccordionItem
content={content}
orientation="horizontal"
className={contentClassName}
/>
) : null}
{footer ? (
<AccordionItem
content={footer}
orientation="horizontal"
className={contentClassName}
/>
) : null}
</View>
{/* Animated content and footer */}
<AnimatedView style={animatedStyle}>
<View style={{ marginBottom: 0, paddingBottom: 0 }}>
{content ? (
<AccordionItem
content={content}
orientation="horizontal"
className={contentClassName}
/>
) : null}
{footer ? (
<AccordionItem
content={footer}
orientation="horizontal"
className={contentClassName}
/>
) : null}
</View>
</AnimatedView>
</View>
<Header id={id} label={headerLabel} className={headerClassName} />
</View>
</View>
);
return orientation === 'vertical'
? renderVertical(direction as VerticalDirection)
: renderHorizontal(direction as HorizontalDirection);
}
export default AccordionLayout;
// Simplified styles
const styles = StyleSheet.create({
container: {},
top: { flexDirection: 'column' },
bottom: { flexDirection: 'column-reverse' },
left: { flexDirection: 'row-reverse' },
right: { flexDirection: 'row' },
horizontalContent: { flex: 1 },
sectionText: {},
hidden: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
width: '100%',
opacity: 0,
zIndex: -1,
},
horizontalSection: {},
});
type AccordionItemProps = {
content: string | ReactNode;
orientation: 'horizontal' | 'vertical';
className?: string;
onLayout?: (e: LayoutChangeEvent) => void;
};
function AccordionItem({ content, orientation, className, onLayout }: AccordionItemProps) {
const { isFlush } = useAccordion();
const classNameStyles = cn('p-2', className, {
'p-0': isFlush,
});
return (
<View
className={classNameStyles}
style={orientation === 'horizontal' ? styles.horizontalSection : {}}
onLayout={onLayout}
>
{typeof content === 'string' ? (
<Text
style={[styles.sectionText]}
className={cn('text-text-main font-medium', extractTextClasses(className), className)}
>
{content}
</Text>
) : (
content
)}
</View>
);
}
|