|
| 1 | +/** |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import React, { useState, type ReactNode } from 'react'; |
| 18 | +import { |
| 19 | + View, |
| 20 | + Text, |
| 21 | + TouchableOpacity, |
| 22 | + StyleSheet, |
| 23 | + LayoutAnimation, |
| 24 | +} from 'react-native'; |
| 25 | +import { Colors, Spacing, BorderRadius, Typography } from '../styles/theme'; |
| 26 | + |
| 27 | +type AccordionProps = { |
| 28 | + title: string; |
| 29 | + children: ReactNode; |
| 30 | + defaultExpanded?: boolean; |
| 31 | +}; |
| 32 | + |
| 33 | +export const Accordion = ({ |
| 34 | + title, |
| 35 | + children, |
| 36 | + defaultExpanded = false, |
| 37 | +}: AccordionProps) => { |
| 38 | + const [expanded, setExpanded] = useState(defaultExpanded); |
| 39 | + |
| 40 | + const toggleExpanded = () => { |
| 41 | + LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut); |
| 42 | + setExpanded(!expanded); |
| 43 | + }; |
| 44 | + |
| 45 | + return ( |
| 46 | + <View style={styles.container}> |
| 47 | + <TouchableOpacity |
| 48 | + style={styles.header} |
| 49 | + onPress={toggleExpanded} |
| 50 | + activeOpacity={0.7} |
| 51 | + > |
| 52 | + <Text style={styles.title}>{title}</Text> |
| 53 | + <Text style={styles.chevron}>{expanded ? '▲' : '▼'}</Text> |
| 54 | + </TouchableOpacity> |
| 55 | + {expanded && <View style={styles.content}>{children}</View>} |
| 56 | + </View> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +const styles = StyleSheet.create({ |
| 61 | + container: { |
| 62 | + marginVertical: Spacing.xs, |
| 63 | + marginRight: Spacing.md, |
| 64 | + borderRadius: BorderRadius.md, |
| 65 | + backgroundColor: Colors.surface, |
| 66 | + overflow: 'hidden', |
| 67 | + borderWidth: 1, |
| 68 | + borderColor: Colors.borderLight, |
| 69 | + }, |
| 70 | + header: { |
| 71 | + flexDirection: 'row', |
| 72 | + justifyContent: 'space-between', |
| 73 | + alignItems: 'center', |
| 74 | + paddingVertical: Spacing.md, |
| 75 | + paddingHorizontal: Spacing.lg, |
| 76 | + backgroundColor: Colors.surfaceVariant, |
| 77 | + }, |
| 78 | + title: { |
| 79 | + fontSize: Typography.fontSize.md, |
| 80 | + fontWeight: Typography.fontWeight.semibold, |
| 81 | + color: Colors.text, |
| 82 | + flex: 1, |
| 83 | + }, |
| 84 | + chevron: { |
| 85 | + fontSize: Typography.fontSize.sm, |
| 86 | + color: Colors.textSecondary, |
| 87 | + marginLeft: Spacing.sm, |
| 88 | + }, |
| 89 | + content: { |
| 90 | + paddingVertical: Spacing.sm, |
| 91 | + paddingHorizontal: Spacing.xs, |
| 92 | + }, |
| 93 | +}); |
| 94 | + |
| 95 | +export default Accordion; |
0 commit comments