@material-ui/lab
Version:
Laboratory for new Material-UI modules.
221 lines (196 loc) • 7.17 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import Grow from '@material-ui/core/Grow';
import Paper from '@material-ui/core/Paper';
import Popper from '@material-ui/core/Popper';
import TrapFocus from '@material-ui/core/Unstable_TrapFocus';
import { useForkRef, useEventCallback, ownerDocument } from '@material-ui/core/utils';
import { styled } from '@material-ui/core/styles';
import { jsx as _jsx } from "react/jsx-runtime";
const PickersPopperRoot = styled(Popper, {
skipSx: true
})(({
theme
}) => ({
zIndex: theme.zIndex.modal
}));
const PickersPopperPaper = styled(Paper, {
skipSx: true
})(({
styleProps
}) => _extends({
transformOrigin: 'top center',
outline: 0
}, styleProps.placement === 'top' && {
transformOrigin: 'bottom center'
}));
function clickedRootScrollbar(event, doc) {
return doc.documentElement.clientWidth < event.clientX || doc.documentElement.clientHeight < event.clientY;
}
/**
* Based on @material-ui/core/ClickAwayListener without the customization.
* We can probably strip away even more since children won't be portaled.
* @param onClickAway
* @param onClick
* @param onTouchStart
*/
function useClickAwayListener(active, onClickAway) {
const movedRef = React.useRef(false);
const syntheticEventRef = React.useRef(false);
const nodeRef = React.useRef(null);
const activatedRef = React.useRef(false);
React.useEffect(() => {
if (!active) {
return undefined;
} // Ensure that this hook is not "activated" synchronously.
// https://github.com/facebook/react/issues/20074
function armClickAwayListener() {
activatedRef.current = true;
}
document.addEventListener('mousedown', armClickAwayListener, true);
document.addEventListener('touchstart', armClickAwayListener, true);
return () => {
document.removeEventListener('mousedown', armClickAwayListener, true);
document.removeEventListener('touchstart', armClickAwayListener, true);
activatedRef.current = false;
};
}, [active]); // The handler doesn't take event.defaultPrevented into account:
//
// event.preventDefault() is meant to stop default behaviors like
// clicking a checkbox to check it, hitting a button to submit a form,
// and hitting left arrow to move the cursor in a text input etc.
// Only special HTML elements have these default behaviors.
const handleClickAway = useEventCallback(event => {
if (!activatedRef.current) {
return;
} // Given developers can stop the propagation of the synthetic event,
// we can only be confident with a positive value.
const insideReactTree = syntheticEventRef.current;
syntheticEventRef.current = false;
const doc = ownerDocument(nodeRef.current); // 1. IE11 support, which trigger the handleClickAway even after the unbind
// 2. The child might render null.
// 3. Behave like a blur listener.
if (!nodeRef.current || 'clientX' in event && clickedRootScrollbar(event, doc)) {
return;
} // Do not act if user performed touchmove
if (movedRef.current) {
movedRef.current = false;
return;
}
let insideDOM; // If not enough, can use https://github.com/DieterHolvoet/event-propagation-path/blob/master/propagationPath.js
if (event.composedPath) {
insideDOM = event.composedPath().indexOf(nodeRef.current) > -1;
} else {
insideDOM = !doc.documentElement.contains(event.target) || nodeRef.current.contains(event.target);
}
if (!insideDOM && !insideReactTree) {
onClickAway(event);
}
}); // Keep track of mouse/touch events that bubbled up through the portal.
const handleSynthetic = () => {
syntheticEventRef.current = true;
};
React.useEffect(() => {
if (active) {
const doc = ownerDocument(nodeRef.current);
const handleTouchMove = () => {
movedRef.current = true;
};
doc.addEventListener('touchstart', handleClickAway);
doc.addEventListener('touchmove', handleTouchMove);
return () => {
doc.removeEventListener('touchstart', handleClickAway);
doc.removeEventListener('touchmove', handleTouchMove);
};
}
return undefined;
}, [active, handleClickAway]);
React.useEffect(() => {
// TODO This behavior is not tested automatically
// It's unclear whether this is due to different update semantics in test (batched in act() vs discrete on click).
// Or if this is a timing related issues due to different Transition components
// Once we get rid of all the manual scheduling (e.g. setTimeout(update, 0)) we can revisit this code+test.
if (active) {
const doc = ownerDocument(nodeRef.current);
doc.addEventListener('click', handleClickAway);
return () => {
doc.removeEventListener('click', handleClickAway);
};
}
return undefined;
}, [active, handleClickAway]);
return [nodeRef, handleSynthetic, handleSynthetic];
}
const PickersPopper = props => {
const {
anchorEl,
children,
containerRef = null,
onClose,
open,
PopperProps,
role,
TransitionComponent = Grow,
TrapFocusProps
} = props;
React.useEffect(() => {
function handleKeyDown(nativeEvent) {
// IE11, Edge (prior to using Bink?) use 'Esc'
if (nativeEvent.key === 'Escape' || nativeEvent.key === 'Esc') {
onClose();
}
}
document.addEventListener('keydown', handleKeyDown);
return () => {
document.removeEventListener('keydown', handleKeyDown);
};
}, [onClose]);
const lastFocusedElementRef = React.useRef(null);
React.useEffect(() => {
if (role === 'tooltip') {
return;
}
if (open) {
lastFocusedElementRef.current = document.activeElement;
} else if (lastFocusedElementRef.current && lastFocusedElementRef.current instanceof HTMLElement) {
lastFocusedElementRef.current.focus();
}
}, [open, role]);
const [clickAwayRef, onPaperClick, onPaperTouchStart] = useClickAwayListener(open, onClose);
const paperRef = React.useRef(null);
const handleRef = useForkRef(paperRef, containerRef);
const handlePaperRef = useForkRef(handleRef, clickAwayRef);
const styleProps = props;
return /*#__PURE__*/_jsx(PickersPopperRoot, _extends({
transition: true,
role: role,
open: open,
anchorEl: anchorEl,
styleProps: styleProps
}, PopperProps, {
children: ({
TransitionProps,
placement
}) => /*#__PURE__*/_jsx(TrapFocus, _extends({
open: open,
disableAutoFocus: true,
disableEnforceFocus: role === 'tooltip',
isEnabled: () => true
}, TrapFocusProps, {
children: /*#__PURE__*/_jsx(TransitionComponent, _extends({}, TransitionProps, {
children: /*#__PURE__*/_jsx(PickersPopperPaper, {
tabIndex: -1,
elevation: 8,
ref: handlePaperRef,
onClick: onPaperClick,
onTouchStart: onPaperTouchStart,
styleProps: _extends({}, styleProps, {
placement
}),
children: children
})
}))
}))
}));
};
export default PickersPopper;