|
| 1 | +import * as React from "react"; |
| 2 | +import { StyleSheet, View, Image, Text } from "react-native"; |
| 3 | +import { NavigationStackProp } from "react-navigation-stack"; |
| 4 | + |
| 5 | +import { TouchableScale } from "../components"; |
| 6 | +import { Item, defaultItem, items } from "../data"; |
| 7 | +import { DetailComponent } from "./DetailComponent"; |
| 8 | +import { getMoreDetailSharedElements } from "./getMoreDetailsSharedElements"; |
| 9 | +import { SharedElement } from "react-navigation-shared-element"; |
| 10 | + |
| 11 | +type Props = { |
| 12 | + navigation: NavigationStackProp<any>; |
| 13 | + route: any; // v5 |
| 14 | + modal: "none" | "full" | "sheet"; |
| 15 | + onPress?: ({ |
| 16 | + navigation, |
| 17 | + item, |
| 18 | + nextItem, |
| 19 | + }: { |
| 20 | + navigation: NavigationStackProp<any>; |
| 21 | + item?: Item; |
| 22 | + nextItem?: Item; |
| 23 | + }) => void; |
| 24 | +}; |
| 25 | + |
| 26 | +export const DetailScreenWithMore = (props: Props) => { |
| 27 | + const { navigation, route, modal, onPress } = props; |
| 28 | + const params = route?.params || navigation?.state?.params; |
| 29 | + const item: Item = params?.item || defaultItem; |
| 30 | + const content = ( |
| 31 | + <DetailComponent item={item} navigation={navigation} modal={modal} /> |
| 32 | + ); |
| 33 | + |
| 34 | + // get 3 items that aren't the item we're viewing now |
| 35 | + const moreItems = items.filter((_i) => _i.id !== item.id).slice(0, 3); |
| 36 | + const moreContent = ( |
| 37 | + <View |
| 38 | + style={{ |
| 39 | + zIndex: 1000, |
| 40 | + bottom: 20, |
| 41 | + left: 0, |
| 42 | + right: 0, |
| 43 | + position: "absolute", |
| 44 | + flexDirection: "row", |
| 45 | + justifyContent: "space-between", |
| 46 | + paddingHorizontal: 20, |
| 47 | + }} |
| 48 | + > |
| 49 | + {moreItems.map((_i, index) => ( |
| 50 | + <TouchableScale |
| 51 | + key={index} |
| 52 | + style={styles.flex} |
| 53 | + onPress={ |
| 54 | + onPress ? () => onPress({ navigation, nextItem: _i }) : undefined |
| 55 | + } |
| 56 | + > |
| 57 | + <View style={styles.container}> |
| 58 | + <SharedElement id={`${_i.id}.image`}> |
| 59 | + <Image style={styles.image} source={_i.image} /> |
| 60 | + </SharedElement> |
| 61 | + <SharedElement id={`${_i.id}.title`}> |
| 62 | + <Text style={styles.text}>{`${_i.title}`}</Text> |
| 63 | + </SharedElement> |
| 64 | + <Text style={styles.caption}>tap me</Text> |
| 65 | + </View> |
| 66 | + </TouchableScale> |
| 67 | + ))} |
| 68 | + </View> |
| 69 | + ); |
| 70 | + return ( |
| 71 | + <> |
| 72 | + {moreContent} |
| 73 | + {content} |
| 74 | + </> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +DetailScreenWithMore.navigationOptions = { |
| 79 | + title: "Boys will be boys", |
| 80 | +}; |
| 81 | + |
| 82 | +// Add the `sharedElements` function to the component, which |
| 83 | +// should return a list of shared-elements to transition. |
| 84 | +// The `sharedElements` function is called whenever you navigate |
| 85 | +// to or from this screen. You can use the provided navigation |
| 86 | +// states or trigger or disable animations. |
| 87 | +DetailScreenWithMore.sharedElements = getMoreDetailSharedElements; |
| 88 | + |
| 89 | +const styles = StyleSheet.create({ |
| 90 | + flex: { |
| 91 | + width: 100, |
| 92 | + backgroundColor: "white", |
| 93 | + borderRadius: 50, |
| 94 | + padding: 10, |
| 95 | + }, |
| 96 | + container: { |
| 97 | + flex: 1, |
| 98 | + justifyContent: "center", |
| 99 | + alignItems: "center", |
| 100 | + }, |
| 101 | + text: { |
| 102 | + fontSize: 15, |
| 103 | + }, |
| 104 | + caption: { |
| 105 | + fontSize: 15, |
| 106 | + opacity: 0.5, |
| 107 | + }, |
| 108 | + image: { |
| 109 | + width: 50, |
| 110 | + height: 30, |
| 111 | + resizeMode: "cover", |
| 112 | + }, |
| 113 | +}); |
0 commit comments