/
index.tsx
190 lines (177 loc) · 5.25 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
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
180
181
182
183
184
185
186
187
188
189
190
import React from 'react';
import { TouchableOpacity, TouchableOpacityProps, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { conditionChaining } from '@sendbird/uikit-utils';
import Text, { TextProps } from '../../components/Text';
import type { BaseHeaderProps } from '../../index';
import createStyleSheet from '../../styles/createStyleSheet';
import useHeaderStyle from '../../styles/useHeaderStyle';
import useUIKitTheme from '../../theme/useUIKitTheme';
type HeaderElement = string | React.ReactElement | null;
export type HeaderProps = BaseHeaderProps<
{
title?: HeaderElement;
left?: HeaderElement;
right?: HeaderElement;
onPressLeft?: () => void;
onPressRight?: () => void;
},
{
clearTitleMargin?: boolean;
clearStatusBarTopInset?: boolean;
statusBarTopInsetAs?: 'padding' | 'margin';
}
>;
const AlignMapper = { left: 'flex-start', center: 'center', right: 'flex-end' } as const;
const Header: ((props: HeaderProps) => JSX.Element) & {
Button: typeof HeaderButton;
Title: typeof HeaderTitle;
Subtitle: typeof HeaderSubtitle;
} = ({
children,
titleAlign,
title = null,
left = null,
right = null,
onPressLeft,
onPressRight,
clearTitleMargin = false,
clearStatusBarTopInset = false,
statusBarTopInsetAs = 'padding',
}) => {
const { topInset, defaultTitleAlign, defaultHeight } = useHeaderStyle();
const { colors } = useUIKitTheme();
const { left: paddingLeft, right: paddingRight } = useSafeAreaInsets();
const actualTitleAlign = titleAlign ?? defaultTitleAlign;
const actualTopInset = clearStatusBarTopInset ? 0 : topInset;
if (!title && !left && !right) {
return (
<View style={{ paddingTop: actualTopInset, backgroundColor: colors.ui.header.nav.none.background }}>
{children}
</View>
);
}
return (
<View
style={[
styles.container,
{
[statusBarTopInsetAs === 'padding' ? 'paddingTop' : 'marginTop']: actualTopInset,
paddingLeft: paddingLeft + styles.container.paddingHorizontal,
paddingRight: paddingRight + styles.container.paddingHorizontal,
backgroundColor: colors.ui.header.nav.none.background,
borderBottomColor: colors.ui.header.nav.none.borderBottom,
},
]}
>
<View style={[styles.header, { height: defaultHeight }]}>
<LeftSide titleAlign={actualTitleAlign} left={left} onPressLeft={onPressLeft} />
<View
style={[
styles.title,
clearTitleMargin && { marginHorizontal: 0 },
{ justifyContent: AlignMapper[actualTitleAlign] },
]}
>
{typeof title === 'string' ? <HeaderTitle>{title}</HeaderTitle> : title}
</View>
<RightSide titleAlign={actualTitleAlign} right={right} onPressRight={onPressRight} />
</View>
{children}
</View>
);
};
const LeftSide = ({ titleAlign, onPressLeft, left }: HeaderProps) => {
if (titleAlign === 'center') {
return <View style={styles.left}>{left && <HeaderButton onPress={onPressLeft}>{left}</HeaderButton>}</View>;
}
if (!left) return null;
return (
<View style={styles.left}>
<HeaderButton onPress={onPressLeft}>{left}</HeaderButton>
</View>
);
};
const RightSide = ({ titleAlign, onPressRight, right }: HeaderProps) => {
if (titleAlign === 'center') {
return <View style={styles.right}>{right && <HeaderButton onPress={onPressRight}>{right}</HeaderButton>}</View>;
}
if (!right) return null;
return (
<View style={styles.right}>
<HeaderButton onPress={onPressRight}>{right}</HeaderButton>
</View>
);
};
const HeaderTitle = ({ children, style, ...props }: TextProps) => {
return (
<Text {...props} h1 numberOfLines={1} style={style}>
{children}
</Text>
);
};
const HeaderSubtitle = ({ children, style, ...props }: TextProps) => {
const { colors } = useUIKitTheme();
return (
<Text color={colors.onBackground03} {...props} caption2 numberOfLines={1} style={style}>
{children}
</Text>
);
};
const HeaderButton = ({ children, disabled, onPress, color, ...props }: TouchableOpacityProps & { color?: string }) => {
return (
<TouchableOpacity
style={styles.button}
{...props}
disabled={!onPress || disabled}
onPress={(e) => onPress?.(e)}
activeOpacity={0.7}
>
{conditionChaining(
[typeof children === 'string' || typeof children === 'number'],
[
<Text button numberOfLines={1} color={color}>
{children}
</Text>,
children,
],
)}
</TouchableOpacity>
);
};
const styles = createStyleSheet({
container: {
paddingHorizontal: 12,
borderBottomWidth: 1,
},
header: {
flexDirection: 'row',
},
title: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
marginHorizontal: 12,
},
left: {
height: '100%',
minWidth: 32,
alignItems: 'center',
justifyContent: 'center',
marginRight: 12,
},
right: {
height: '100%',
minWidth: 32,
alignItems: 'center',
justifyContent: 'center',
marginLeft: 12,
},
button: {
padding: 4,
},
});
Header.Button = HeaderButton;
Header.Title = HeaderTitle;
Header.Subtitle = HeaderSubtitle;
export default Header;