126 lines
3.2 KiB
TypeScript
126 lines
3.2 KiB
TypeScript
import React, { type PropsWithChildren, useEffect, useMemo, useState } from "react";
|
|
import {
|
|
LayoutAnimation,
|
|
Platform,
|
|
Pressable,
|
|
StyleSheet,
|
|
Text,
|
|
UIManager,
|
|
View,
|
|
} from "react-native";
|
|
import { Ionicons } from "@expo/vector-icons";
|
|
import { moderateScale, verticalScale } from "react-native-size-matters";
|
|
|
|
import { readingToneForTitle } from "./reading-palette";
|
|
|
|
if (Platform.OS === "android" && UIManager.setLayoutAnimationEnabledExperimental) {
|
|
UIManager.setLayoutAnimationEnabledExperimental(true);
|
|
}
|
|
|
|
export interface AccordionSectionProps {
|
|
title: string;
|
|
defaultExpanded?: boolean;
|
|
icon?: keyof typeof Ionicons.glyphMap;
|
|
iconColor?: string;
|
|
}
|
|
|
|
export function AccordionSection({
|
|
title,
|
|
defaultExpanded = true,
|
|
icon,
|
|
iconColor,
|
|
children,
|
|
}: PropsWithChildren<AccordionSectionProps>) {
|
|
const [expanded, setExpanded] = useState(defaultExpanded);
|
|
const tone = useMemo(() => readingToneForTitle(title), [title]);
|
|
|
|
useEffect(() => {
|
|
setExpanded(defaultExpanded);
|
|
}, [defaultExpanded]);
|
|
|
|
const toggle = () => {
|
|
LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut);
|
|
setExpanded((value) => !value);
|
|
};
|
|
|
|
return (
|
|
<View style={[styles.card, { borderColor: tone.border }]}>
|
|
<Pressable
|
|
accessibilityRole="button"
|
|
accessibilityState={{ expanded }}
|
|
accessibilityLabel={`${expanded ? "Collapse" : "Expand"} ${title}`}
|
|
onPress={toggle}
|
|
style={({ pressed }) => [
|
|
styles.header,
|
|
{ backgroundColor: tone.soft },
|
|
pressed && styles.pressed,
|
|
]}
|
|
>
|
|
<View style={[styles.accent, { backgroundColor: tone.accent }]} />
|
|
{icon ? (
|
|
<Ionicons
|
|
name={icon}
|
|
size={moderateScale(18)}
|
|
color={iconColor ?? tone.accent}
|
|
/>
|
|
) : null}
|
|
<Text style={[styles.title, { color: tone.title }]}>{title}</Text>
|
|
<View style={styles.chevron}>
|
|
<Ionicons
|
|
name={expanded ? "chevron-up" : "chevron-down"}
|
|
size={18}
|
|
color={tone.accent}
|
|
/>
|
|
</View>
|
|
</Pressable>
|
|
|
|
{expanded ? <View style={styles.body}>{children}</View> : null}
|
|
</View>
|
|
);
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
card: {
|
|
backgroundColor: "#FFFFFF",
|
|
borderRadius: moderateScale(18),
|
|
borderWidth: 1,
|
|
overflow: "hidden",
|
|
},
|
|
header: {
|
|
minHeight: verticalScale(54),
|
|
paddingHorizontal: moderateScale(14),
|
|
paddingVertical: verticalScale(12),
|
|
flexDirection: "row",
|
|
alignItems: "center",
|
|
gap: moderateScale(10),
|
|
},
|
|
accent: {
|
|
width: moderateScale(4),
|
|
alignSelf: "stretch",
|
|
minHeight: verticalScale(26),
|
|
borderRadius: moderateScale(999),
|
|
},
|
|
pressed: { opacity: 0.74 },
|
|
title: {
|
|
flex: 1,
|
|
fontSize: moderateScale(15),
|
|
lineHeight: moderateScale(21),
|
|
fontWeight: "700",
|
|
letterSpacing: 0.1,
|
|
},
|
|
chevron: {
|
|
width: moderateScale(30),
|
|
height: moderateScale(30),
|
|
borderRadius: moderateScale(15),
|
|
alignItems: "center",
|
|
justifyContent: "center",
|
|
backgroundColor: "#FFFFFFB8",
|
|
},
|
|
body: {
|
|
paddingHorizontal: moderateScale(17),
|
|
paddingTop: verticalScale(15),
|
|
paddingBottom: verticalScale(18),
|
|
gap: verticalScale(12),
|
|
},
|
|
});
|