|
| 1 | +import {useTheme} from '@yoroi/theme' |
| 2 | +import * as React from 'react' |
| 3 | +import {Animated, LayoutAnimation, StyleSheet, Text, TouchableOpacity, View} from 'react-native' |
| 4 | + |
| 5 | +import {Icon} from '../../../components/Icon' |
| 6 | + |
| 7 | +export const CollapsibleSection = ({label, children}: {label: string; children: React.ReactNode}) => { |
| 8 | + const {styles, colors} = useStyles() |
| 9 | + const [isOpen, setIsOpen] = React.useState(false) |
| 10 | + const animatedHeight = React.useRef(new Animated.Value(0)).current |
| 11 | + |
| 12 | + const toggleSection = () => { |
| 13 | + LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut) |
| 14 | + setIsOpen(!isOpen) |
| 15 | + Animated.timing(animatedHeight, { |
| 16 | + toValue: isOpen ? 0 : 1, |
| 17 | + duration: 300, |
| 18 | + useNativeDriver: false, |
| 19 | + }).start() |
| 20 | + } |
| 21 | + |
| 22 | + return ( |
| 23 | + <> |
| 24 | + <View style={styles.sectionHeader}> |
| 25 | + <Text style={styles.sectionHeaderText}>{label}</Text> |
| 26 | + |
| 27 | + <TouchableOpacity activeOpacity={0.5} onPress={toggleSection}> |
| 28 | + <Icon.Chevron direction={isOpen ? 'up' : 'down'} size={28} color={colors.chevron} /> |
| 29 | + </TouchableOpacity> |
| 30 | + </View> |
| 31 | + |
| 32 | + <Animated.View |
| 33 | + style={[ |
| 34 | + styles.childrenContainer, |
| 35 | + { |
| 36 | + maxHeight: animatedHeight.interpolate({ |
| 37 | + inputRange: [0, 1], |
| 38 | + outputRange: [0, 1000], |
| 39 | + }), |
| 40 | + opacity: animatedHeight, |
| 41 | + }, |
| 42 | + ]} |
| 43 | + > |
| 44 | + {children} |
| 45 | + </Animated.View> |
| 46 | + </> |
| 47 | + ) |
| 48 | +} |
| 49 | + |
| 50 | +const useStyles = () => { |
| 51 | + const {atoms, color} = useTheme() |
| 52 | + const styles = StyleSheet.create({ |
| 53 | + sectionHeader: { |
| 54 | + ...atoms.flex_row, |
| 55 | + ...atoms.justify_between, |
| 56 | + }, |
| 57 | + sectionHeaderText: { |
| 58 | + ...atoms.body_1_lg_medium, |
| 59 | + color: color.text_gray_medium, |
| 60 | + }, |
| 61 | + childrenContainer: { |
| 62 | + overflow: 'hidden', |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + const colors = { |
| 67 | + chevron: color.gray_900, |
| 68 | + } |
| 69 | + |
| 70 | + return {styles, colors} as const |
| 71 | +} |
0 commit comments