1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | Object.defineProperty(exports, "__esModule", {
|
8 | value: true
|
9 | });
|
10 | exports.default = exports.styles = void 0;
|
11 |
|
12 | var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
13 |
|
14 | var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
15 |
|
16 | var React = _interopRequireWildcard(require("react"));
|
17 |
|
18 | var _reactIs = require("react-is");
|
19 |
|
20 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
21 |
|
22 | var _clsx = _interopRequireDefault(require("clsx"));
|
23 |
|
24 | var _styles = require("@material-ui/core/styles");
|
25 |
|
26 | var _Zoom = _interopRequireDefault(require("@material-ui/core/Zoom"));
|
27 |
|
28 | var _Fab = _interopRequireDefault(require("@material-ui/core/Fab"));
|
29 |
|
30 | var _utils = require("@material-ui/core/utils");
|
31 |
|
32 | function getOrientation(direction) {
|
33 | if (direction === 'up' || direction === 'down') {
|
34 | return 'vertical';
|
35 | }
|
36 |
|
37 | if (direction === 'right' || direction === 'left') {
|
38 | return 'horizontal';
|
39 | }
|
40 |
|
41 | return undefined;
|
42 | }
|
43 |
|
44 | function clamp(value, min, max) {
|
45 | if (value < min) {
|
46 | return min;
|
47 | }
|
48 |
|
49 | if (value > max) {
|
50 | return max;
|
51 | }
|
52 |
|
53 | return value;
|
54 | }
|
55 |
|
56 | var dialRadius = 32;
|
57 | var spacingActions = 16;
|
58 |
|
59 | var styles = function styles(theme) {
|
60 | return {
|
61 |
|
62 | root: {
|
63 | zIndex: theme.zIndex.speedDial,
|
64 | display: 'flex',
|
65 | alignItems: 'center',
|
66 | pointerEvents: 'none'
|
67 | },
|
68 |
|
69 |
|
70 | fab: {
|
71 | pointerEvents: 'auto'
|
72 | },
|
73 |
|
74 |
|
75 | directionUp: {
|
76 | flexDirection: 'column-reverse',
|
77 | '& $actions': {
|
78 | flexDirection: 'column-reverse',
|
79 | marginBottom: -dialRadius,
|
80 | paddingBottom: spacingActions + dialRadius
|
81 | }
|
82 | },
|
83 |
|
84 |
|
85 | directionDown: {
|
86 | flexDirection: 'column',
|
87 | '& $actions': {
|
88 | flexDirection: 'column',
|
89 | marginTop: -dialRadius,
|
90 | paddingTop: spacingActions + dialRadius
|
91 | }
|
92 | },
|
93 |
|
94 |
|
95 | directionLeft: {
|
96 | flexDirection: 'row-reverse',
|
97 | '& $actions': {
|
98 | flexDirection: 'row-reverse',
|
99 | marginRight: -dialRadius,
|
100 | paddingRight: spacingActions + dialRadius
|
101 | }
|
102 | },
|
103 |
|
104 |
|
105 | directionRight: {
|
106 | flexDirection: 'row',
|
107 | '& $actions': {
|
108 | flexDirection: 'row',
|
109 | marginLeft: -dialRadius,
|
110 | paddingLeft: spacingActions + dialRadius
|
111 | }
|
112 | },
|
113 |
|
114 |
|
115 | actions: {
|
116 | display: 'flex',
|
117 | pointerEvents: 'auto'
|
118 | },
|
119 |
|
120 |
|
121 | actionsClosed: {
|
122 | transition: 'top 0s linear 0.2s',
|
123 | pointerEvents: 'none'
|
124 | }
|
125 | };
|
126 | };
|
127 |
|
128 | exports.styles = styles;
|
129 | var SpeedDial = React.forwardRef(function SpeedDial(props, ref) {
|
130 | var ariaLabel = props.ariaLabel,
|
131 | _props$FabProps = props.FabProps;
|
132 | _props$FabProps = _props$FabProps === void 0 ? {} : _props$FabProps;
|
133 | var origDialButtonRef = _props$FabProps.ref,
|
134 | FabProps = (0, _objectWithoutProperties2.default)(_props$FabProps, ["ref"]),
|
135 | childrenProp = props.children,
|
136 | classes = props.classes,
|
137 | className = props.className,
|
138 | _props$direction = props.direction,
|
139 | direction = _props$direction === void 0 ? 'up' : _props$direction,
|
140 | _props$hidden = props.hidden,
|
141 | hidden = _props$hidden === void 0 ? false : _props$hidden,
|
142 | icon = props.icon,
|
143 | onBlur = props.onBlur,
|
144 | onClose = props.onClose,
|
145 | onFocus = props.onFocus,
|
146 | onKeyDown = props.onKeyDown,
|
147 | onMouseEnter = props.onMouseEnter,
|
148 | onMouseLeave = props.onMouseLeave,
|
149 | onOpen = props.onOpen,
|
150 | open = props.open,
|
151 | openIcon = props.openIcon,
|
152 | _props$TransitionComp = props.TransitionComponent,
|
153 | TransitionComponent = _props$TransitionComp === void 0 ? _Zoom.default : _props$TransitionComp,
|
154 | _props$transitionDura = props.transitionDuration,
|
155 | transitionDuration = _props$transitionDura === void 0 ? {
|
156 | enter: _styles.duration.enteringScreen,
|
157 | exit: _styles.duration.leavingScreen
|
158 | } : _props$transitionDura,
|
159 | TransitionProps = props.TransitionProps,
|
160 | other = (0, _objectWithoutProperties2.default)(props, ["ariaLabel", "FabProps", "children", "classes", "className", "direction", "hidden", "icon", "onBlur", "onClose", "onFocus", "onKeyDown", "onMouseEnter", "onMouseLeave", "onOpen", "open", "openIcon", "TransitionComponent", "transitionDuration", "TransitionProps"]);
|
161 | var eventTimer = React.useRef();
|
162 | React.useEffect(function () {
|
163 | return function () {
|
164 | clearTimeout(eventTimer.current);
|
165 | };
|
166 | }, []);
|
167 | |
168 |
|
169 |
|
170 |
|
171 | var focusedAction = React.useRef(0);
|
172 | |
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | var nextItemArrowKey = React.useRef();
|
181 | |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | var actions = React.useRef([]);
|
188 | actions.current = [actions.current[0]];
|
189 | var handleOwnFabRef = React.useCallback(function (fabFef) {
|
190 | actions.current[0] = fabFef;
|
191 | }, []);
|
192 | var handleFabRef = (0, _utils.useForkRef)(origDialButtonRef, handleOwnFabRef);
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | var createHandleSpeedDialActionButtonRef = function createHandleSpeedDialActionButtonRef(dialActionIndex, origButtonRef) {
|
202 | return function (buttonRef) {
|
203 | actions.current[dialActionIndex + 1] = buttonRef;
|
204 |
|
205 | if (origButtonRef) {
|
206 | origButtonRef(buttonRef);
|
207 | }
|
208 | };
|
209 | };
|
210 |
|
211 | var handleKeyDown = function handleKeyDown(event) {
|
212 | if (onKeyDown) {
|
213 | onKeyDown(event);
|
214 | }
|
215 |
|
216 | var key = event.key.replace('Arrow', '').toLowerCase();
|
217 | var _nextItemArrowKey$cur = nextItemArrowKey.current,
|
218 | nextItemArrowKeyCurrent = _nextItemArrowKey$cur === void 0 ? key : _nextItemArrowKey$cur;
|
219 |
|
220 | if (event.key === 'Escape') {
|
221 | if (onClose) {
|
222 | actions.current[0].focus();
|
223 | onClose(event, 'escapeKeyDown');
|
224 | }
|
225 |
|
226 | return;
|
227 | }
|
228 |
|
229 | if (getOrientation(key) === getOrientation(nextItemArrowKeyCurrent) && getOrientation(key) !== undefined) {
|
230 | event.preventDefault();
|
231 | var actionStep = key === nextItemArrowKeyCurrent ? 1 : -1;
|
232 |
|
233 | var nextAction = clamp(focusedAction.current + actionStep, 0, actions.current.length - 1);
|
234 | actions.current[nextAction].focus();
|
235 | focusedAction.current = nextAction;
|
236 | nextItemArrowKey.current = nextItemArrowKeyCurrent;
|
237 | }
|
238 | };
|
239 |
|
240 | React.useEffect(function () {
|
241 |
|
242 | if (!open) {
|
243 | focusedAction.current = 0;
|
244 | nextItemArrowKey.current = undefined;
|
245 | }
|
246 | }, [open]);
|
247 |
|
248 | var handleClose = function handleClose(event) {
|
249 | if (event.type === 'mouseleave' && onMouseLeave) {
|
250 | onMouseLeave(event);
|
251 | }
|
252 |
|
253 | if (event.type === 'blur' && onBlur) {
|
254 | onBlur(event);
|
255 | }
|
256 |
|
257 | clearTimeout(eventTimer.current);
|
258 |
|
259 | if (onClose) {
|
260 | if (event.type === 'blur') {
|
261 | event.persist();
|
262 | eventTimer.current = setTimeout(function () {
|
263 | onClose(event, 'blur');
|
264 | });
|
265 | } else {
|
266 | onClose(event, 'mouseLeave');
|
267 | }
|
268 | }
|
269 | };
|
270 |
|
271 | var handleClick = function handleClick(event) {
|
272 | if (FabProps.onClick) {
|
273 | FabProps.onClick(event);
|
274 | }
|
275 |
|
276 | clearTimeout(eventTimer.current);
|
277 |
|
278 | if (open) {
|
279 | if (onClose) {
|
280 | onClose(event, 'toggle');
|
281 | }
|
282 | } else if (onOpen) {
|
283 | onOpen(event, 'toggle');
|
284 | }
|
285 | };
|
286 |
|
287 | var handleOpen = function handleOpen(event) {
|
288 | if (event.type === 'mouseenter' && onMouseEnter) {
|
289 | onMouseEnter(event);
|
290 | }
|
291 |
|
292 | if (event.type === 'focus' && onFocus) {
|
293 | onFocus(event);
|
294 | }
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | clearTimeout(eventTimer.current);
|
300 |
|
301 | if (onOpen && !open) {
|
302 | event.persist();
|
303 |
|
304 | eventTimer.current = setTimeout(function () {
|
305 | var eventMap = {
|
306 | focus: 'focus',
|
307 | mouseenter: 'mouseEnter'
|
308 | };
|
309 | onOpen(event, eventMap[event.type]);
|
310 | });
|
311 | }
|
312 | };
|
313 |
|
314 |
|
315 | var id = ariaLabel.replace(/^[^a-z]+|[^\w:.-]+/gi, '');
|
316 | var allItems = React.Children.toArray(childrenProp).filter(function (child) {
|
317 | if (process.env.NODE_ENV !== 'production') {
|
318 | if ((0, _reactIs.isFragment)(child)) {
|
319 | console.error(["Material-UI: The SpeedDial component doesn't accept a Fragment as a child.", 'Consider providing an array instead.'].join('\n'));
|
320 | }
|
321 | }
|
322 |
|
323 | return React.isValidElement(child);
|
324 | });
|
325 | var children = allItems.map(function (child, index) {
|
326 | var _child$props$FabProps = child.props.FabProps;
|
327 | _child$props$FabProps = _child$props$FabProps === void 0 ? {} : _child$props$FabProps;
|
328 | var origButtonRef = _child$props$FabProps.ref,
|
329 | ChildFabProps = (0, _objectWithoutProperties2.default)(_child$props$FabProps, ["ref"]);
|
330 | return React.cloneElement(child, {
|
331 | FabProps: (0, _extends2.default)({}, ChildFabProps, {
|
332 | ref: createHandleSpeedDialActionButtonRef(index, origButtonRef)
|
333 | }),
|
334 | delay: 30 * (open ? index : allItems.length - index),
|
335 | open: open,
|
336 | id: "".concat(id, "-action-").concat(index)
|
337 | });
|
338 | });
|
339 | return React.createElement("div", (0, _extends2.default)({
|
340 | className: (0, _clsx.default)(classes.root, classes["direction".concat((0, _utils.capitalize)(direction))], className),
|
341 | ref: ref,
|
342 | role: "presentation",
|
343 | onKeyDown: handleKeyDown,
|
344 | onBlur: handleClose,
|
345 | onFocus: handleOpen,
|
346 | onMouseEnter: handleOpen,
|
347 | onMouseLeave: handleClose
|
348 | }, other), React.createElement(TransitionComponent, (0, _extends2.default)({
|
349 | in: !hidden,
|
350 | timeout: transitionDuration,
|
351 | unmountOnExit: true
|
352 | }, TransitionProps), React.createElement(_Fab.default, (0, _extends2.default)({
|
353 | color: "primary",
|
354 | "aria-label": ariaLabel,
|
355 | "aria-haspopup": "true",
|
356 | "aria-expanded": open,
|
357 | "aria-controls": "".concat(id, "-actions")
|
358 | }, FabProps, {
|
359 | onClick: handleClick,
|
360 | className: (0, _clsx.default)(classes.fab, FabProps.className),
|
361 | ref: handleFabRef
|
362 | }), React.isValidElement(icon) && (0, _utils.isMuiElement)(icon, ['SpeedDialIcon']) ? React.cloneElement(icon, {
|
363 | open: open
|
364 | }) : icon)), React.createElement("div", {
|
365 | id: "".concat(id, "-actions"),
|
366 | role: "menu",
|
367 | "aria-orientation": getOrientation(direction),
|
368 | className: (0, _clsx.default)(classes.actions, !open && classes.actionsClosed)
|
369 | }, children));
|
370 | });
|
371 | process.env.NODE_ENV !== "production" ? SpeedDial.propTypes = {
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 | |
378 |
|
379 |
|
380 |
|
381 | ariaLabel: _propTypes.default.string.isRequired,
|
382 |
|
383 | |
384 |
|
385 |
|
386 | children: _propTypes.default.node,
|
387 |
|
388 | |
389 |
|
390 |
|
391 |
|
392 | classes: _propTypes.default.object,
|
393 |
|
394 | |
395 |
|
396 |
|
397 | className: _propTypes.default.string,
|
398 |
|
399 | |
400 |
|
401 |
|
402 | direction: _propTypes.default.oneOf(['down', 'left', 'right', 'up']),
|
403 |
|
404 | |
405 |
|
406 |
|
407 | FabProps: _propTypes.default.object,
|
408 |
|
409 | |
410 |
|
411 |
|
412 | hidden: _propTypes.default.bool,
|
413 |
|
414 | |
415 |
|
416 |
|
417 |
|
418 | icon: _propTypes.default.node,
|
419 |
|
420 | |
421 |
|
422 |
|
423 | onBlur: _propTypes.default.func,
|
424 |
|
425 | |
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | onClose: _propTypes.default.func,
|
432 |
|
433 | |
434 |
|
435 |
|
436 | onFocus: _propTypes.default.func,
|
437 |
|
438 | |
439 |
|
440 |
|
441 | onKeyDown: _propTypes.default.func,
|
442 |
|
443 | |
444 |
|
445 |
|
446 | onMouseEnter: _propTypes.default.func,
|
447 |
|
448 | |
449 |
|
450 |
|
451 | onMouseLeave: _propTypes.default.func,
|
452 |
|
453 | |
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 | onOpen: _propTypes.default.func,
|
460 |
|
461 | |
462 |
|
463 |
|
464 | open: _propTypes.default.bool.isRequired,
|
465 |
|
466 | |
467 |
|
468 |
|
469 | openIcon: _propTypes.default.node,
|
470 |
|
471 | |
472 |
|
473 |
|
474 |
|
475 | TransitionComponent: _propTypes.default.elementType,
|
476 |
|
477 | |
478 |
|
479 |
|
480 |
|
481 | transitionDuration: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.shape({
|
482 | appear: _propTypes.default.number,
|
483 | enter: _propTypes.default.number,
|
484 | exit: _propTypes.default.number
|
485 | })]),
|
486 |
|
487 | |
488 |
|
489 |
|
490 | TransitionProps: _propTypes.default.object
|
491 | } : void 0;
|
492 |
|
493 | var _default = (0, _styles.withStyles)(styles, {
|
494 | name: 'MuiSpeedDial'
|
495 | })(SpeedDial);
|
496 |
|
497 | exports.default = _default; |
\ | No newline at end of file |