Skip to content

Commit b51ce95

Browse files
committed
Fix full featured simple gallery example
1 parent 04d0882 commit b51ce95

File tree

4 files changed

+505
-6
lines changed

4 files changed

+505
-6
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { useRoute } from '@react-navigation/native';
2+
import { Header, StackHeaderProps } from '@react-navigation/stack';
3+
import React, { useEffect, useState } from 'react';
4+
import type { ViewStyle } from 'react-native';
5+
import Animated from 'react-native-reanimated';
6+
7+
const headerPropsMap = new Map<string, StackHeaderProps>();
8+
const subs: Array<() => void> = [];
9+
10+
function setProps(name: string, props: StackHeaderProps) {
11+
headerPropsMap.set(name, props);
12+
13+
setTimeout(() => {
14+
subs.forEach((cb) => cb());
15+
}, 0);
16+
}
17+
18+
function useHeaderProps() {
19+
const route = useRoute();
20+
21+
return headerPropsMap.get(route.name);
22+
}
23+
24+
export function HeaderPropsScrapper(props: StackHeaderProps) {
25+
setProps(props.scene.route.name, props);
26+
27+
return null;
28+
}
29+
30+
export function DetachedHeader() {
31+
const [, forceUpdate] = useState(false);
32+
useEffect(() => {
33+
const onPropsChange = () => forceUpdate((v) => !v);
34+
35+
subs.push(onPropsChange);
36+
37+
return () => {
38+
const index = subs.findIndex((i) => i === onPropsChange);
39+
40+
subs.splice(index);
41+
};
42+
}, []);
43+
44+
const headerProps = useHeaderProps();
45+
46+
return headerProps ? <Header {...headerProps} /> : null;
47+
}
48+
49+
DetachedHeader.Container = ({
50+
children,
51+
style,
52+
}: {
53+
children: JSX.Element;
54+
style?: ViewStyle;
55+
}) => {
56+
return (
57+
<Animated.View
58+
style={[
59+
{ position: 'absolute', top: 0, left: 0, right: 0 },
60+
style,
61+
]}
62+
>
63+
{children}
64+
</Animated.View>
65+
);
66+
};

0 commit comments

Comments
 (0)