-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathHeaderCompactStandard.tsx
More file actions
145 lines (136 loc) · 3.65 KB
/
HeaderCompactStandard.tsx
File metadata and controls
145 lines (136 loc) · 3.65 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
// Third party dependencies.
import React, { useMemo } from 'react';
// External dependencies.
import {
Box,
BoxAlignItems,
Text,
TextVariant,
TextColor,
FontWeight,
IconName,
ButtonIconProps,
} from '@metamask/design-system-react-native';
// Internal dependencies.
import HeaderBase from '../../components/HeaderBase';
import { HeaderCompactStandardProps } from './HeaderCompactStandard.types';
/**
* HeaderCompactStandard is a header component with centered title and optional back/close buttons.
* Extends HeaderBase with convenient props for common header patterns.
*
* @example
* ```tsx
* <HeaderCompactStandard
* title="Page Title"
* onBack={handleBack}
* onClose={handleClose}
* />
*
* // Or with custom button props
* <HeaderCompactStandard
* title="Page Title"
* backButtonProps={{ onPress: handleBack, isDisabled: true }}
* closeButtonProps={{ onPress: handleClose }}
* />
* ```
*/
const HeaderCompactStandard: React.FC<HeaderCompactStandardProps> = ({
title,
titleProps,
subtitle,
subtitleProps,
children,
onBack,
backButtonProps,
onClose,
closeButtonProps,
endButtonIconProps,
startButtonIconProps,
twClassName = '',
testID,
...headerBaseProps
}) => {
// Build the startButtonIconProps with back button if needed
const resolvedStartButtonIconProps = useMemo(() => {
if (startButtonIconProps) {
return startButtonIconProps;
}
if (onBack || backButtonProps) {
return {
iconName: IconName.ArrowLeft,
...(backButtonProps || {}),
onPress: backButtonProps?.onPress ?? onBack,
} as ButtonIconProps;
}
return undefined;
}, [onBack, backButtonProps, startButtonIconProps]);
// Build the endButtonIconProps array with close button if needed
const resolvedEndButtonIconProps = useMemo(() => {
const props: ButtonIconProps[] = [];
// Add close button if onClose or closeButtonProps is provided
if (onClose || closeButtonProps) {
const closeProps: ButtonIconProps = {
iconName: IconName.Close,
...(closeButtonProps || {}),
onPress: closeButtonProps?.onPress ?? onClose,
};
props.push(closeProps);
}
// Add existing endButtonIconProps last
if (endButtonIconProps) {
props.push(...endButtonIconProps);
}
return props.length > 0 ? props : undefined;
}, [endButtonIconProps, onClose, closeButtonProps]);
// Render title if children is not provided
const renderContent = () => {
if (children) {
return children;
}
if (title) {
return (
<Box alignItems={BoxAlignItems.Center}>
{typeof title === 'string' ? (
<Text
variant={TextVariant.BodyMd}
fontWeight={FontWeight.Bold}
{...titleProps}
>
{title}
</Text>
) : (
title
)}
{subtitle && (
<Box twClassName="-mt-0.5">
{typeof subtitle === 'string' ? (
<Text
variant={TextVariant.BodySm}
color={TextColor.TextAlternative}
{...subtitleProps}
>
{subtitle}
</Text>
) : (
subtitle
)}
</Box>
)}
</Box>
);
}
return null;
};
return (
<HeaderBase
testID={testID}
startButtonIconProps={resolvedStartButtonIconProps}
endButtonIconProps={resolvedEndButtonIconProps}
{...headerBaseProps}
twClassName={`px-2 ${twClassName}`.trim()}
>
{renderContent()}
</HeaderBase>
);
};
export default HeaderCompactStandard;