-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathHeader.tsx
More file actions
179 lines (163 loc) · 4.23 KB
/
Header.tsx
File metadata and controls
179 lines (163 loc) · 4.23 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { useEffect } from 'react';
import { Platform, StyleSheet } from 'react-native';
import Animated, {
Easing,
SharedValue,
interpolate,
measure,
useAnimatedRef,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import { COLORS } from '../common';
const SIGNET = require('./signet.png');
const TEXT = require('./text.png');
export const HEADER_HEIGHT = Platform.OS === 'web' ? 64 : 192;
export const COLLAPSED_HEADER_HEIGHT = 64;
export interface HeaderProps {
scrollOffset: SharedValue<number>;
}
export default function Header(props: HeaderProps) {
if (Platform.OS === 'web') {
return <HeaderWeb {...props} />;
}
return <HeaderNative {...props} />;
}
function HeaderNative(props: HeaderProps) {
const containerRef = useAnimatedRef();
const headerHeight = useDerivedValue(() => {
return Math.max(
HEADER_HEIGHT - props.scrollOffset.value,
COLLAPSED_HEADER_HEIGHT
);
});
const expandFactor = useDerivedValue(() => {
return Math.min(
1,
(headerHeight.value - COLLAPSED_HEADER_HEIGHT) /
(HEADER_HEIGHT - COLLAPSED_HEADER_HEIGHT)
);
});
const isMounted = useSharedValue(false);
const opacity = useSharedValue(0);
const headerStyle = useAnimatedStyle(() => {
return {
height: headerHeight.value,
};
});
const signetStyle = useAnimatedStyle(() => {
const size = isMounted.value ? measure(containerRef) : undefined;
const imageSize = interpolate(
expandFactor.value,
[0, 1],
[headerHeight.value * 0.7, headerHeight.value * 0.5]
);
const clampedHeight = Math.min(headerHeight.value, HEADER_HEIGHT);
return {
position: 'absolute',
width: imageSize,
height: imageSize,
top: interpolate(
Math.sqrt(expandFactor.value),
[0, 1],
[clampedHeight * 0.1, 0]
),
left: interpolate(
expandFactor.value,
[0, 1],
[COLLAPSED_HEADER_HEIGHT * 0.25, ((size?.width ?? 0) - imageSize) * 0.5]
),
opacity: opacity.value,
transform: [{ translateY: (1 - opacity.value) * 20 }],
};
});
const textStyle = useAnimatedStyle(() => {
const size = isMounted.value ? measure(containerRef) : undefined;
const height = interpolate(
expandFactor.value,
[0, 1],
[headerHeight.value * 0.7, headerHeight.value * 0.5]
);
const width = (size?.width ?? 0) * (expandFactor.value * 0.2 + 0.4);
return {
position: 'absolute',
width: width,
height: height,
bottom: interpolate(
expandFactor.value,
[0, 1],
[COLLAPSED_HEADER_HEIGHT * 0.2, 0]
),
left: ((size?.width ?? 0) - width) * 0.5,
opacity: opacity.value,
transform: [{ translateY: (1 - opacity.value) * 20 }],
};
});
useEffect(() => {
setTimeout(() => {
isMounted.value = true;
opacity.value = withTiming(1, {
duration: 200,
easing: Easing.out(Easing.quad),
});
}, 100);
}, [opacity, isMounted]);
return (
<Animated.View
ref={containerRef}
collapsable={false}
style={[headerStyle, styles.nativeHeader]}>
<Animated.Image
source={SIGNET}
style={signetStyle}
resizeMode="contain"
/>
<Animated.Image source={TEXT} style={textStyle} resizeMode="contain" />
</Animated.View>
);
}
function HeaderWeb(_props: HeaderProps) {
return (
<Animated.View collapsable={false} style={styles.webHeader}>
<Animated.Image
source={SIGNET}
style={styles.webSignet}
resizeMode="contain"
/>
<Animated.Image
source={TEXT}
style={styles.webText}
resizeMode="contain"
/>
</Animated.View>
);
}
const styles = StyleSheet.create({
nativeHeader: {
width: '100%',
position: 'absolute',
backgroundColor: COLORS.offWhite,
zIndex: 100,
flexDirection: 'row',
},
webHeader: {
width: '100%',
position: 'absolute',
backgroundColor: COLORS.offWhite,
zIndex: 100,
flexDirection: 'row',
alignItems: 'center',
paddingStart: 16,
height: HEADER_HEIGHT,
},
webSignet: {
width: 48,
height: 48,
},
webText: {
width: 170,
height: 32,
},
});