-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathDrawerItem.tsx
More file actions
197 lines (186 loc) · 4.62 KB
/
DrawerItem.tsx
File metadata and controls
197 lines (186 loc) · 4.62 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import * as React from 'react';
import {
ColorValue,
GestureResponderEvent,
PressableAndroidRippleConfig,
StyleProp,
StyleSheet,
View,
ViewStyle,
} from 'react-native';
import color from 'color';
import { useInternalTheme } from '../../core/theming';
import type { ThemeProp } from '../../types';
import Icon, { IconSource } from '../Icon';
import TouchableRipple, {
Props as TouchableRippleProps,
} from '../TouchableRipple/TouchableRipple';
import Text from '../Typography/Text';
export type Props = React.ComponentPropsWithRef<typeof View> & {
/**
* The label text of the item.
*/
label: string;
/**
* Icon to display for the `DrawerItem`.
*/
icon?: IconSource;
/**
* Whether to highlight the drawer item as active.
*/
active?: boolean;
/**
* Whether the item is disabled.
*/
disabled?: boolean;
/**
* Function to execute on press.
*/
onPress?: (e: GestureResponderEvent) => void;
/**
* Type of background drawabale to display the feedback (Android).
* https://reactnative.dev/docs/pressable#rippleconfig
*/
background?: PressableAndroidRippleConfig;
/**
* Accessibility label for the button. This is read by the screen reader when the user taps the button.
*/
accessibilityLabel?: string;
/**
* Callback which returns a React element to display on the right side. For instance a Badge.
*/
right?: (props: { color: string }) => React.ReactNode;
/**
* Specifies the largest possible scale a label font can reach.
*/
labelMaxFontSizeMultiplier?: number;
/**
* Color of the ripple effect.
*/
rippleColor?: ColorValue;
/**
* Sets additional distance outside of element in which a press can be detected.
*/
hitSlop?: TouchableRippleProps['hitSlop'];
style?: StyleProp<ViewStyle>;
/**
* @optional
*/
theme?: ThemeProp;
};
/**
* A component used to show an action item with an icon and a label in a navigation drawer.
*
* ## Usage
* ```js
* import * as React from 'react';
* import { Drawer } from 'react-native-paper';
*
* const MyComponent = () => (
* <Drawer.Item
* style={{ backgroundColor: '#64ffda' }}
* icon="star"
* label="First Item"
* />
* );
*
* export default MyComponent;
* ```
*/
const DrawerItem = ({
icon,
label,
active,
disabled,
theme: themeOverrides,
rippleColor: customRippleColor,
style,
onPress,
background,
accessibilityLabel,
right,
labelMaxFontSizeMultiplier,
hitSlop,
...rest
}: Props) => {
const theme = useInternalTheme(themeOverrides);
const {
roundness,
colors: { secondaryContainer, onSurfaceVariant, onSecondaryContainer },
fonts: { labelLarge },
} = theme;
const backgroundColor = active ? secondaryContainer : undefined;
const contentColor = active ? onSecondaryContainer : onSurfaceVariant;
const labelMargin = icon ? 12 : 0;
const borderRadius = 7 * roundness;
const rippleColor = color(contentColor).alpha(0.12).rgb().string();
return (
<View {...rest}>
<TouchableRipple
borderless
disabled={disabled}
background={background}
onPress={onPress}
style={[styles.container, { backgroundColor, borderRadius }, style]}
role="button"
aria-selected={active}
aria-label={accessibilityLabel}
rippleColor={customRippleColor || rippleColor}
theme={theme}
hitSlop={hitSlop}
>
<View style={styles.wrapper}>
<View style={styles.content}>
{icon ? (
<Icon source={icon} size={24} color={contentColor} />
) : null}
<Text
variant="labelLarge"
selectable={false}
numberOfLines={1}
style={[
styles.label,
{
color: contentColor,
marginLeft: labelMargin,
...labelLarge,
},
]}
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
>
{label}
</Text>
</View>
{right?.({ color: contentColor })}
</View>
</TouchableRipple>
</View>
);
};
DrawerItem.displayName = 'Drawer.Item';
const styles = StyleSheet.create({
container: {
marginHorizontal: 10,
justifyContent: 'center',
height: 56,
marginLeft: 12,
marginRight: 12,
marginVertical: 0,
},
wrapper: {
flexDirection: 'row',
alignItems: 'center',
marginLeft: 16,
marginRight: 24,
padding: 0,
},
content: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
},
label: {
marginRight: 32,
},
});
export default DrawerItem;