-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
66 lines (59 loc) · 1.95 KB
/
index.tsx
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
// Inspiration: https://interfacemarket.com/ui-kits/boston-grocery-delivery-app-ui-kit
import React, { useState, useRef, useCallback } from 'react';
import { Transition, Transitioning } from 'react-native-reanimated';
import { Entypo } from '@expo/vector-icons';
import { Container, Card, Header, Heading, SubList, SubText } from './styles';
import { categories } from './data';
const transition = (
<Transition.Change durationMs={400} interpolation="easeInOut" />
);
const AnimatedAccordion: React.FC = () => {
const [selectedIndex, setSelectedIndex] = useState<null | number>(null);
const transitionRef = useRef(null);
const toggleAccordion = useCallback(
(index) => {
transitionRef.current?.animateNextTransition();
setSelectedIndex(index === selectedIndex ? null : index);
},
[selectedIndex, setSelectedIndex],
);
return (
<Container
as={Transitioning.View}
ref={transitionRef}
transition={transition}>
{categories.map(({ category, subCategories, bg, color }, index) => {
return (
<Card
key={category}
background={bg}
onPress={() => toggleAccordion(index)}>
<Header>
<Heading color={color}>{category}</Heading>
<Entypo
name="chevron-down"
size={30}
color={color}
style={{
transform: [
{ rotate: index === selectedIndex ? '180deg' : '0deg' },
],
}}
/>
</Header>
{index === selectedIndex && (
<SubList>
{subCategories.map((subcategory) => (
<SubText key={subcategory} color={color}>
{subcategory}
</SubText>
))}
</SubList>
)}
</Card>
);
})}
</Container>
);
};
export default AnimatedAccordion;