UNPKG

7.2 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8var React = _interopRequireWildcard(require("react"));
9
10var _reactNative = require("react-native");
11
12var _Surface = _interopRequireDefault(require("./Surface"));
13
14var _Text = _interopRequireDefault(require("./Typography/Text"));
15
16var _Button = _interopRequireDefault(require("./Button"));
17
18var _Icon = _interopRequireDefault(require("./Icon"));
19
20var _theming = require("../core/theming");
21
22var _shadow = _interopRequireDefault(require("../styles/shadow"));
23
24function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
26function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
27
28function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
29
30function _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); }
31
32const ELEVATION = 1;
33const DEFAULT_MAX_WIDTH = 960;
34
35/**
36 * Banner displays a prominent message and related actions.
37 *
38 * <div class="screenshots">
39 * <img class="medium" src="screenshots/banner.gif" />
40 * </div>
41 *
42 * ## Usage
43 * ```js
44 * import * as React from 'react';
45 * import { Image } from 'react-native';
46 * import { Banner } from 'react-native-paper';
47 *
48 * const MyComponent = () => {
49 * const [visible, setVisible] = React.useState(true);
50 *
51 * return (
52 * <Banner
53 * visible={visible}
54 * actions={[
55 * {
56 * label: 'Fix it',
57 * onPress: () => setVisible(false),
58 * },
59 * {
60 * label: 'Learn more',
61 * onPress: () => setVisible(false),
62 * },
63 * ]}
64 * icon={({size}) => (
65 * <Image
66 * source={{
67 * uri: 'https://avatars3.githubusercontent.com/u/17571969?s=400&v=4',
68 * }}
69 * style={{
70 * width: size,
71 * height: size,
72 * }}
73 * />
74 * )}>
75 * There was a problem processing a transaction on your credit card.
76 * </Banner>
77 * );
78 * };
79 *
80 * export default MyComponent;
81 * ```
82 */
83const Banner = ({
84 visible,
85 icon,
86 children,
87 actions,
88 contentStyle,
89 style,
90 theme,
91 ...rest
92}) => {
93 const {
94 current: position
95 } = React.useRef(new _reactNative.Animated.Value(visible ? 1 : 0));
96 const [layout, setLayout] = React.useState({
97 height: 0,
98 measured: false
99 });
100 const {
101 scale
102 } = theme.animation;
103 React.useEffect(() => {
104 if (visible) {
105 // show
106 _reactNative.Animated.timing(position, {
107 duration: 250 * scale,
108 toValue: 1,
109 useNativeDriver: false
110 }).start();
111 } else {
112 // hide
113 _reactNative.Animated.timing(position, {
114 duration: 200 * scale,
115 toValue: 0,
116 useNativeDriver: false
117 }).start();
118 }
119 }, [visible, position, scale]);
120
121 const handleLayout = ({
122 nativeEvent
123 }) => {
124 const {
125 height
126 } = nativeEvent.layout;
127 setLayout({
128 height,
129 measured: true
130 });
131 }; // The banner animation has 2 parts:
132 // 1. Blank spacer element which animates its height to move the content
133 // 2. Actual banner which animates its translateY
134 // In initial render, we position everything normally and measure the height of the banner
135 // Once we have the height, we apply the height to the spacer and switch the banner to position: absolute
136 // We need this because we need to move the content below as if banner's height was being animated
137 // However we can't animated banner's height directly as it'll also resize the content inside
138
139
140 const height = _reactNative.Animated.multiply(position, layout.height);
141
142 const translateY = _reactNative.Animated.multiply(_reactNative.Animated.add(position, -1), layout.height);
143
144 return /*#__PURE__*/React.createElement(_Surface.default, _extends({}, rest, {
145 style: [styles.container, (0, _shadow.default)(ELEVATION), style]
146 }), /*#__PURE__*/React.createElement(_reactNative.View, {
147 style: [styles.wrapper, contentStyle]
148 }, /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
149 style: {
150 height
151 }
152 }), /*#__PURE__*/React.createElement(_reactNative.Animated.View, {
153 onLayout: handleLayout,
154 style: [layout.measured || !visible ? // If we have measured banner's height or it's invisible,
155 // Position it absolutely, the layout will be taken care of the spacer
156 [styles.absolute, {
157 transform: [{
158 translateY
159 }]
160 }] : // Otherwise position it normally
161 null, !layout.measured && !visible ? // If we haven't measured banner's height yet and it's invisible,
162 // hide it with opacity: 0 so user doesn't see it
163 {
164 opacity: 0
165 } : null]
166 }, /*#__PURE__*/React.createElement(_reactNative.View, {
167 style: styles.content
168 }, icon ? /*#__PURE__*/React.createElement(_reactNative.View, {
169 style: styles.icon
170 }, /*#__PURE__*/React.createElement(_Icon.default, {
171 source: icon,
172 size: 40
173 })) : null, /*#__PURE__*/React.createElement(_Text.default, {
174 style: styles.message
175 }, children)), /*#__PURE__*/React.createElement(_reactNative.View, {
176 style: styles.actions
177 }, actions.map(({
178 label,
179 ...others
180 }, i) => /*#__PURE__*/React.createElement(_Button.default, _extends({
181 key:
182 /* eslint-disable-line react/no-array-index-key */
183 i,
184 compact: true,
185 mode: "text",
186 style: styles.button
187 }, others), label))))));
188};
189
190const styles = _reactNative.StyleSheet.create({
191 container: {
192 elevation: ELEVATION
193 },
194 wrapper: {
195 overflow: 'hidden',
196 alignSelf: 'center',
197 width: '100%',
198 maxWidth: DEFAULT_MAX_WIDTH
199 },
200 absolute: {
201 position: 'absolute',
202 top: 0,
203 width: '100%'
204 },
205 content: {
206 flexDirection: 'row',
207 justifyContent: 'flex-start',
208 marginHorizontal: 8,
209 marginTop: 16,
210 marginBottom: 0
211 },
212 icon: {
213 margin: 8
214 },
215 message: {
216 flex: 1,
217 margin: 8
218 },
219 actions: {
220 flexDirection: 'row',
221 justifyContent: 'flex-end',
222 margin: 4
223 },
224 button: {
225 margin: 4
226 }
227});
228
229var _default = (0, _theming.withTheme)(Banner);
230
231exports.default = _default;
232//# sourceMappingURL=Banner.js.map
\No newline at end of file