glide-design-system
Version:
Glide design system is an open-source React component library. It offers numerous benefits that make them essential tools for design and development teams.
136 lines (133 loc) • 9.75 kB
JavaScript
;
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.object.assign.js");
require("core-js/modules/es.weak-map.js");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Menu = void 0;
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactDom = _interopRequireDefault(require("react-dom"));
const _excluded = ["open", "onClose", "position", "children", "anchorEl", "style", "className", "id", "name"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
/**
* Menu that wraps MenuItems
* @param {*} open A boolean flag to control the visibility of the menu. If true, the menu is displayed; if false, the menu is hidden.
* @param {*} onClose A function to be called when the menu should be closed or hidden. It is typically used to set the open prop to false when the user clicks outside the menu.
* @param {*} position The position of the menu relative to the anchorEl element
* @param {*} children The content of the menu, which can be any valid React elements or components.
* @param {*} anchorEl The element to which the menu is anchored. It can be any valid React element.
* @param {*} style An object containing custom styles to be applied to the container of the menu.
*/
const Menu = _ref => {
var _updatedPosition, _menuChildRef$current, _updatedPosition2, _updatedPosition3, _updatedPosition4, _menuChildRef$current2, _updatedPosition5;
let {
open,
onClose,
position = "bottom",
children,
anchorEl,
style,
className,
id,
name
} = _ref,
props = _objectWithoutProperties(_ref, _excluded);
const [newPosition, setPosition] = (0, _react.useState)(null);
const menuChildRef = (0, _react.useRef)(null);
(0, _react.useEffect)(() => {
if (open && anchorEl) {
if (open) {
document.body.style.overflowY = "hidden";
}
const anchorElRect = _reactDom.default.findDOMNode(anchorEl).getBoundingClientRect();
const updatedPosition = {
top: anchorElRect.top,
left: anchorElRect.left,
bottom: anchorElRect.bottom,
right: anchorElRect.right,
height: anchorElRect.height,
width: anchorElRect.width
};
setPosition(updatedPosition);
}
return () => {
document.body.style.overflowY = "";
};
}, [open, anchorEl]);
let updatedPosition = null;
const calpositionHandler = () => {
if (open && anchorEl) {
if (open) {
document.body.style.overflowY = "hidden";
}
const anchorElRect = _reactDom.default.findDOMNode(anchorEl).getBoundingClientRect();
updatedPosition = {
top: anchorElRect.top,
left: anchorElRect.left,
bottom: anchorElRect.bottom,
right: anchorElRect.right,
height: anchorElRect.height,
width: anchorElRect.width
};
}
};
calpositionHandler();
// using useState()
// const combinedStyle = {
// top:
// position === "top" || position === "topLeft" || position === "topRight"
// ? Math.abs(newPosition?.top - menuChildRef?.current?.offsetHeight - 5)
// : newPosition && newPosition?.bottom + 5,
// left:
// (position === "topRight" ||
// position === "bottomRight" ||
// position === "top" ||
// position === "bottom") &&
// newPosition
// ? newPosition?.left
// : (position === "topLeft" || position === "bottomLeft") && newPosition
// ? Math.abs(newPosition?.right - menuChildRef?.current?.offsetWidth)
// : newPosition?.left,
// ...style,
// };
//using let
const combinedStyle = _objectSpread({
top: position === "top" || position === "topLeft" || position === "topRight" ? Math.abs(((_updatedPosition = updatedPosition) === null || _updatedPosition === void 0 ? void 0 : _updatedPosition.top) - (menuChildRef === null || menuChildRef === void 0 || (_menuChildRef$current = menuChildRef.current) === null || _menuChildRef$current === void 0 ? void 0 : _menuChildRef$current.offsetHeight) - 5) : updatedPosition && ((_updatedPosition2 = updatedPosition) === null || _updatedPosition2 === void 0 ? void 0 : _updatedPosition2.bottom) + 5,
left: (position === "topRight" || position === "bottomRight" || position === "top" || position === "bottom") && updatedPosition ? (_updatedPosition3 = updatedPosition) === null || _updatedPosition3 === void 0 ? void 0 : _updatedPosition3.left : (position === "topLeft" || position === "bottomLeft") && updatedPosition ? Math.abs(((_updatedPosition4 = updatedPosition) === null || _updatedPosition4 === void 0 ? void 0 : _updatedPosition4.right) - (menuChildRef === null || menuChildRef === void 0 || (_menuChildRef$current2 = menuChildRef.current) === null || _menuChildRef$current2 === void 0 ? void 0 : _menuChildRef$current2.offsetWidth)) : (_updatedPosition5 = updatedPosition) === null || _updatedPosition5 === void 0 ? void 0 : _updatedPosition5.left
}, style);
return /*#__PURE__*/_react.default.createElement("div", {
id: id,
"data-testid": id,
name: name,
className: open ? "menuParentContainer" : "menuContainerNoEffect"
}, /*#__PURE__*/_react.default.createElement("div", {
onClick: () => {
onClose();
},
className: "menuModalContainer"
}), /*#__PURE__*/_react.default.createElement("div", _extends({
ref: menuChildRef,
className: "menuChildContainer ".concat(className ? className : ""),
style: combinedStyle
}, props), children));
};
exports.Menu = Menu;
Menu.propTypes = {
position: _propTypes.default.string,
onClose: _propTypes.default.func,
open: _propTypes.default.bool,
anchorEl: _propTypes.default.object
};