-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2808: Added Accordion Animation for EventsDateFilter
- Loading branch information
1 parent
aeceee3
commit 1f1ca05
Showing
6 changed files
with
101 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { ReactElement } from 'react' | ||
import { StyleProp, StyleSheet, View, ViewStyle } from 'react-native' | ||
import Animated, { useAnimatedStyle, useDerivedValue, useSharedValue, withTiming } from 'react-native-reanimated' | ||
|
||
// For some reason I couldn't replicate this styling in styled-components | ||
const styles = StyleSheet.create({ | ||
wrapper: { | ||
width: '100%', | ||
position: 'absolute', | ||
display: 'flex', | ||
alignItems: 'center', | ||
}, | ||
animatedView: { | ||
width: '100%', | ||
overflow: 'hidden', | ||
}, | ||
}) | ||
|
||
type AccordionProps = { | ||
isOpen: boolean | ||
style?: StyleProp<ViewStyle> | ||
children: React.ReactNode | ||
duration?: number | ||
viewKey: string | ||
} | ||
|
||
const defaultDuration = 500 | ||
|
||
const Accordion = ({ isOpen, style, duration = defaultDuration, children, viewKey }: AccordionProps): ReactElement => { | ||
const height = useSharedValue(0) | ||
const derivedHeight = useDerivedValue(() => | ||
withTiming(height.value * Number(isOpen), { | ||
duration, | ||
}), | ||
) | ||
const bodyStyle = useAnimatedStyle(() => ({ | ||
height: derivedHeight.value, | ||
})) | ||
return ( | ||
<Animated.View key={`accordionItem_${viewKey}`} style={[styles.animatedView, bodyStyle, style]}> | ||
<View | ||
onLayout={e => { | ||
height.value = e.nativeEvent.layout.height | ||
}} | ||
style={styles.wrapper}> | ||
{children} | ||
</View> | ||
</Animated.View> | ||
) | ||
} | ||
|
||
export default Accordion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useRef, useEffect, useState, ReactElement } from 'react' | ||
import styled from 'styled-components' | ||
|
||
const AccordionWrapper = styled.div<{ height: number }>` | ||
overflow: hidden; | ||
transition: height 0.3s ease; | ||
height: ${props => props.height}px; | ||
width: 100%; | ||
` | ||
|
||
type AccordionProps = { | ||
isOpen: boolean | ||
children: React.ReactNode | ||
} | ||
|
||
const Accordion = ({ isOpen, children }: AccordionProps): ReactElement => { | ||
const [height, setHeight] = useState(0) | ||
const contentRef = useRef<HTMLDivElement>(null) | ||
|
||
useEffect(() => { | ||
if (contentRef.current) { | ||
setHeight(isOpen ? contentRef.current.scrollHeight : 0) | ||
} | ||
}, [isOpen, children]) | ||
|
||
return ( | ||
<AccordionWrapper ref={contentRef} height={height}> | ||
{children} | ||
</AccordionWrapper> | ||
) | ||
} | ||
|
||
export default Accordion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters