UNPKG

3.24 kBJavaScriptView Raw
1function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
3import * as React from 'react';
4import { View, StyleSheet } from 'react-native';
5import color from 'color';
6import TouchableRipple from './TouchableRipple/TouchableRipple';
7import Icon from './Icon';
8import CrossFadeIcon from './CrossFadeIcon';
9import { withTheme } from '../core/theming';
10
11/**
12 * An icon button is a button which displays only an icon without a label.
13 * By default button has 150% size of the icon.
14 *
15 * <div class="screenshots">
16 * <figure>
17 * <img src="screenshots/icon-button-1.png" />
18 * <figcaption>Icon button</figcaption>
19 * </figure>
20 * <figure>
21 * <img src="screenshots/icon-button-2.png" />
22 * <figcaption>Pressed icon button</figcaption>
23 * </figure>
24 * </div>
25 *
26 * ## Usage
27 * ```js
28 * import * as React from 'react';
29 * import { IconButton, Colors } from 'react-native-paper';
30 *
31 * const MyComponent = () => (
32 * <IconButton
33 * icon="camera"
34 * color={Colors.red500}
35 * size={20}
36 * onPress={() => console.log('Pressed')}
37 * />
38 * );
39 *
40 * export default MyComponent;
41 * ```
42 *
43 * @extends TouchableRipple props https://callstack.github.io/react-native-paper/touchable-ripple.html
44 */
45const IconButton = _ref => {
46 let {
47 icon,
48 color: customColor,
49 size = 24,
50 accessibilityLabel,
51 disabled,
52 onPress,
53 animated = false,
54 theme,
55 style,
56 ...rest
57 } = _ref;
58 const iconColor = typeof customColor !== 'undefined' ? customColor : theme.colors.text;
59 const rippleColor = color(iconColor).alpha(0.32).rgb().string();
60 const IconComponent = animated ? CrossFadeIcon : Icon;
61 const buttonSize = size * 1.5;
62 return /*#__PURE__*/React.createElement(TouchableRipple, _extends({
63 borderless: true,
64 centered: true,
65 onPress: onPress,
66 rippleColor: rippleColor,
67 style: [styles.container, {
68 width: buttonSize,
69 height: buttonSize,
70 borderRadius: buttonSize / 2
71 }, disabled && styles.disabled, style],
72 accessibilityLabel: accessibilityLabel // @ts-expect-error We keep old a11y props for backwards compat with old RN versions
73 ,
74 accessibilityTraits: disabled ? ['button', 'disabled'] : 'button',
75 accessibilityComponentType: "button",
76 accessibilityRole: "button",
77 accessibilityState: {
78 disabled
79 },
80 disabled: disabled,
81 hitSlop: TouchableRipple.supported ? {
82 top: 10,
83 left: 10,
84 bottom: 10,
85 right: 10
86 } : {
87 top: 6,
88 left: 6,
89 bottom: 6,
90 right: 6
91 }
92 }, rest), /*#__PURE__*/React.createElement(View, null, /*#__PURE__*/React.createElement(IconComponent, {
93 color: iconColor,
94 source: icon,
95 size: size
96 })));
97};
98
99const styles = StyleSheet.create({
100 container: {
101 alignItems: 'center',
102 justifyContent: 'center',
103 overflow: 'hidden',
104 margin: 6
105 },
106 disabled: {
107 opacity: 0.32
108 }
109});
110export default withTheme(IconButton);
111//# sourceMappingURL=IconButton.js.map
\No newline at end of file