@material-ui/core
Version:
React components that implement Google's Material Design.
300 lines (258 loc) • 10.1 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import * as React from 'react';
import PropTypes from 'prop-types';
import PopperJs from 'popper.js';
import { chainPropTypes, refType, HTMLElementType } from '@material-ui/utils';
import { useTheme } from '@material-ui/styles';
import Portal from '../Portal';
import createChainedFunction from '../utils/createChainedFunction';
import setRef from '../utils/setRef';
import useForkRef from '../utils/useForkRef';
function flipPlacement(placement, theme) {
const direction = theme && theme.direction || 'ltr';
if (direction === 'ltr') {
return placement;
}
switch (placement) {
case 'bottom-end':
return 'bottom-start';
case 'bottom-start':
return 'bottom-end';
case 'top-end':
return 'top-start';
case 'top-start':
return 'top-end';
default:
return placement;
}
}
function getAnchorEl(anchorEl) {
return typeof anchorEl === 'function' ? anchorEl() : anchorEl;
}
const useEnhancedEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
const defaultPopperOptions = {};
/**
* Poppers rely on the 3rd party library [Popper.js](https://popper.js.org/docs/v1/) for positioning.
*/
const Popper = /*#__PURE__*/React.forwardRef(function Popper(props, ref) {
const {
anchorEl,
children,
container,
disablePortal = false,
keepMounted = false,
modifiers,
open,
placement: initialPlacement = 'bottom',
popperOptions = defaultPopperOptions,
popperRef: popperRefProp,
style,
transition = false
} = props,
other = _objectWithoutPropertiesLoose(props, ["anchorEl", "children", "container", "disablePortal", "keepMounted", "modifiers", "open", "placement", "popperOptions", "popperRef", "style", "transition"]);
const tooltipRef = React.useRef(null);
const ownRef = useForkRef(tooltipRef, ref);
const popperRef = React.useRef(null);
const handlePopperRef = useForkRef(popperRef, popperRefProp);
const handlePopperRefRef = React.useRef(handlePopperRef);
useEnhancedEffect(() => {
handlePopperRefRef.current = handlePopperRef;
}, [handlePopperRef]);
React.useImperativeHandle(popperRefProp, () => popperRef.current, []);
const [exited, setExited] = React.useState(true);
const theme = useTheme();
const rtlPlacement = flipPlacement(initialPlacement, theme);
/**
* placement initialized from prop but can change during lifetime if modifiers.flip.
* modifiers.flip is essentially a flip for controlled/uncontrolled behavior
*/
const [placement, setPlacement] = React.useState(rtlPlacement);
React.useEffect(() => {
if (popperRef.current) {
popperRef.current.update();
}
});
const handleOpen = React.useCallback(() => {
if (!tooltipRef.current || !anchorEl || !open) {
return;
}
if (popperRef.current) {
popperRef.current.destroy();
handlePopperRefRef.current(null);
}
const handlePopperUpdate = data => {
setPlacement(data.placement);
};
const resolvedAnchorEl = getAnchorEl(anchorEl);
if (process.env.NODE_ENV !== 'production') {
if (resolvedAnchorEl && resolvedAnchorEl.nodeType === 1) {
const box = resolvedAnchorEl.getBoundingClientRect();
if (process.env.NODE_ENV !== 'test' && box.top === 0 && box.left === 0 && box.right === 0 && box.bottom === 0) {
console.warn(['Material-UI: The `anchorEl` prop provided to the component is invalid.', 'The anchor element should be part of the document layout.', "Make sure the element is present in the document or that it's not display none."].join('\n'));
}
}
}
const popper = new PopperJs(getAnchorEl(anchorEl), tooltipRef.current, _extends({
placement: rtlPlacement
}, popperOptions, {
modifiers: _extends({}, disablePortal ? {} : {
// It's using scrollParent by default, we can use the viewport when using a portal.
preventOverflow: {
boundariesElement: 'window'
}
}, modifiers, popperOptions.modifiers),
// We could have been using a custom modifier like react-popper is doing.
// But it seems this is the best public API for this use case.
onCreate: createChainedFunction(handlePopperUpdate, popperOptions.onCreate),
onUpdate: createChainedFunction(handlePopperUpdate, popperOptions.onUpdate)
}));
handlePopperRefRef.current(popper);
}, [anchorEl, disablePortal, modifiers, open, rtlPlacement, popperOptions]);
const handleRef = React.useCallback(node => {
setRef(ownRef, node);
handleOpen();
}, [ownRef, handleOpen]);
const handleEnter = () => {
setExited(false);
};
const handleClose = () => {
if (!popperRef.current) {
return;
}
popperRef.current.destroy();
handlePopperRefRef.current(null);
};
const handleExited = () => {
setExited(true);
handleClose();
};
React.useEffect(() => {
return () => {
handleClose();
};
}, []);
React.useEffect(() => {
if (!open && !transition) {
// Otherwise handleExited will call this.
handleClose();
}
}, [open, transition]);
if (!keepMounted && !open && (!transition || exited)) {
return null;
}
const childProps = {
placement
};
if (transition) {
childProps.TransitionProps = {
in: open,
onEnter: handleEnter,
onExited: handleExited
};
}
return /*#__PURE__*/React.createElement(Portal, {
disablePortal: disablePortal,
container: container
}, /*#__PURE__*/React.createElement("div", _extends({
ref: handleRef,
role: "tooltip"
}, other, {
style: _extends({
// Prevents scroll issue, waiting for Popper.js to add this style once initiated.
position: 'fixed',
// Fix Popper.js display issue
top: 0,
left: 0,
display: !open && keepMounted && !transition ? 'none' : null
}, style)
}), typeof children === 'function' ? children(childProps) : children));
});
process.env.NODE_ENV !== "production" ? Popper.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the d.ts file and run "yarn proptypes" |
// ----------------------------------------------------------------------
/**
* A HTML element, [referenceObject](https://popper.js.org/docs/v1/#referenceObject),
* or a function that returns either.
* It's used to set the position of the popper.
* The return value will passed as the reference object of the Popper instance.
*/
anchorEl: chainPropTypes(PropTypes.oneOfType([HTMLElementType, PropTypes.object, PropTypes.func]), props => {
if (props.open) {
const resolvedAnchorEl = getAnchorEl(props.anchorEl);
if (resolvedAnchorEl && resolvedAnchorEl.nodeType === 1) {
const box = resolvedAnchorEl.getBoundingClientRect();
if (process.env.NODE_ENV !== 'test' && box.top === 0 && box.left === 0 && box.right === 0 && box.bottom === 0) {
return new Error(['Material-UI: The `anchorEl` prop provided to the component is invalid.', 'The anchor element should be part of the document layout.', "Make sure the element is present in the document or that it's not display none."].join('\n'));
}
} else if (!resolvedAnchorEl || typeof resolvedAnchorEl.clientWidth !== 'number' || typeof resolvedAnchorEl.clientHeight !== 'number' || typeof resolvedAnchorEl.getBoundingClientRect !== 'function') {
return new Error(['Material-UI: The `anchorEl` prop provided to the component is invalid.', 'It should be an HTML element instance or a referenceObject ', '(https://popper.js.org/docs/v1/#referenceObject).'].join('\n'));
}
}
return null;
}),
/**
* Popper render function or node.
*/
children: PropTypes
/* @typescript-to-proptypes-ignore */
.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
/**
* A HTML element, component instance, or function that returns either.
* The `container` will have the portal children appended to it.
*
* By default, it uses the body of the top-level document object,
* so it's simply `document.body` most of the time.
*/
container: PropTypes
/* @typescript-to-proptypes-ignore */
.oneOfType([HTMLElementType, PropTypes.instanceOf(React.Component), PropTypes.func]),
/**
* Disable the portal behavior.
* The children stay within it's parent DOM hierarchy.
*/
disablePortal: PropTypes.bool,
/**
* Always keep the children in the DOM.
* This prop can be useful in SEO situation or
* when you want to maximize the responsiveness of the Popper.
*/
keepMounted: PropTypes.bool,
/**
* Popper.js is based on a "plugin-like" architecture,
* most of its features are fully encapsulated "modifiers".
*
* A modifier is a function that is called each time Popper.js needs to
* compute the position of the popper.
* For this reason, modifiers should be very performant to avoid bottlenecks.
* To learn how to create a modifier, [read the modifiers documentation](https://popper.js.org/docs/v1/#modifiers).
*/
modifiers: PropTypes.object,
/**
* If `true`, the popper is visible.
*/
open: PropTypes.bool.isRequired,
/**
* Popper placement.
*/
placement: PropTypes.oneOf(['bottom-end', 'bottom-start', 'bottom', 'left-end', 'left-start', 'left', 'right-end', 'right-start', 'right', 'top-end', 'top-start', 'top']),
/**
* Options provided to the [`popper.js`](https://popper.js.org/docs/v1/) instance.
*/
popperOptions: PropTypes.object,
/**
* A ref that points to the used popper instance.
*/
popperRef: refType,
/**
* @ignore
*/
style: PropTypes.object,
/**
* Help supporting a react-transition-group/Transition component.
*/
transition: PropTypes.bool
} : void 0;
export default Popper;