text: "use strict";
  
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.default = exports.styles = void 0;
  
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  
  var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
  
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  
  var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
  
  var _getPrototypeOf3 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
  
  var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
  
  var _react = _interopRequireDefault(require("react"));
  
  var _propTypes = _interopRequireDefault(require("prop-types"));
  
  var _reactDom = _interopRequireDefault(require("react-dom"));
  
  var _scrollbarSize = _interopRequireDefault(require("dom-helpers/util/scrollbarSize"));
  
  var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
  
  var _Popover = _interopRequireDefault(require("../Popover"));
  
  var _MenuList = _interopRequireDefault(require("../MenuList"));
  
  // @inheritedComponent Popover
  var RTL_ORIGIN = {
    vertical: 'top',
    horizontal: 'right'
  };
  var LTR_ORIGIN = {
    vertical: 'top',
    horizontal: 'left'
  };
  var styles = {
    /* Styles applied to the `Paper` component. */
    paper: {
      // specZ: The maximum height of a simple menu should be one or more rows less than the view
      // height. This ensures a tapable area outside of the simple menu with which to dismiss
      // the menu.
      maxHeight: 'calc(100% - 96px)',
      // Add iOS momentum scrolling.
      WebkitOverflowScrolling: 'touch'
    }
  };
  exports.styles = styles;
  
  var Menu =
  /*#__PURE__*/
  function (_React$Component) {
    (0, _inherits2.default)(Menu, _React$Component);
  
    function Menu() {
      var _getPrototypeOf2;
  
      var _this;
  
      (0, _classCallCheck2.default)(this, Menu);
  
      for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }
  
      _this = (0, _possibleConstructorReturn2.default)(this, (_getPrototypeOf2 = (0, _getPrototypeOf3.default)(Menu)).call.apply(_getPrototypeOf2, [this].concat(args)));
  
      _this.getContentAnchorEl = function () {
        if (_this.menuListRef.selectedItemRef) {
          return _reactDom.default.findDOMNode(_this.menuListRef.selectedItemRef);
        }
  
        return _reactDom.default.findDOMNode(_this.menuListRef).firstChild;
      };
  
      _this.focus = function () {
        if (_this.menuListRef && _this.menuListRef.selectedItemRef) {
          _reactDom.default.findDOMNode(_this.menuListRef.selectedItemRef).focus();
  
          return;
        }
  
        var menuList = _reactDom.default.findDOMNode(_this.menuListRef);
  
        if (menuList && menuList.firstChild) {
          menuList.firstChild.focus();
        }
      };
  
      _this.handleMenuListRef = function (ref) {
        _this.menuListRef = ref;
      };
  
      _this.handleEntering = function (element) {
        var _this$props = _this.props,
            disableAutoFocusItem = _this$props.disableAutoFocusItem,
            theme = _this$props.theme;
  
        var menuList = _reactDom.default.findDOMNode(_this.menuListRef); // Focus so the scroll computation of the Popover works as expected.
  
  
        if (disableAutoFocusItem !== true) {
          _this.focus();
        } // Let's ignore that piece of logic if users are already overriding the width
        // of the menu.
  
  
        if (menuList && element.clientHeight < menuList.clientHeight && !menuList.style.width) {
          var size = "".concat((0, _scrollbarSize.default)(), "px");
          menuList.style[theme.direction === 'rtl' ? 'paddingLeft' : 'paddingRight'] = size;
          menuList.style.width = "calc(100% + ".concat(size, ")");
        }
  
        if (_this.props.onEntering) {
          _this.props.onEntering(element);
        }
      };
  
      _this.handleListKeyDown = function (event) {
        if (event.key === 'Tab') {
          event.preventDefault();
  
          if (_this.props.onClose) {
            _this.props.onClose(event, 'tabKeyDown');
          }
        }
      };
  
      return _this;
    }
  
    (0, _createClass2.default)(Menu, [{
      key: "componentDidMount",
      value: function componentDidMount() {
        if (this.props.open && this.props.disableAutoFocusItem !== true) {
          this.focus();
        }
      }
    }, {
      key: "render",
      value: function render() {
        var _this$props2 = this.props,
            children = _this$props2.children,
            classes = _this$props2.classes,
            disableAutoFocusItem = _this$props2.disableAutoFocusItem,
            MenuListProps = _this$props2.MenuListProps,
            onEntering = _this$props2.onEntering,
            _this$props2$PaperPro = _this$props2.PaperProps,
            PaperProps = _this$props2$PaperPro === void 0 ? {} : _this$props2$PaperPro,
            PopoverClasses = _this$props2.PopoverClasses,
            theme = _this$props2.theme,
            other = (0, _objectWithoutProperties2.default)(_this$props2, ["children", "classes", "disableAutoFocusItem", "MenuListProps", "onEntering", "PaperProps", "PopoverClasses", "theme"]);
        return _react.default.createElement(_Popover.default, (0, _extends2.default)({
          getContentAnchorEl: this.getContentAnchorEl,
          classes: PopoverClasses,
          onEntering: this.handleEntering,
          anchorOrigin: theme.direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN,
          transformOrigin: theme.direction === 'rtl' ? RTL_ORIGIN : LTR_ORIGIN,
          PaperProps: (0, _extends2.default)({}, PaperProps, {
            classes: (0, _extends2.default)({}, PaperProps.classes, {
              root: classes.paper
            })
          })
        }, other), _react.default.createElement(_MenuList.default, (0, _extends2.default)({
          onKeyDown: this.handleListKeyDown
        }, MenuListProps, {
          ref: this.handleMenuListRef
        }), children));
      }
    }]);
    return Menu;
  }(_react.default.Component);
  
  process.env.NODE_ENV !== "production" ? Menu.propTypes = {
    /**
     * The DOM element used to set the position of the menu.
     */
    anchorEl: _propTypes.default.oneOfType([_propTypes.default.object, _propTypes.default.func]),
  
    /**
     * Menu contents, normally `MenuItem`s.
     */
    children: _propTypes.default.node,
  
    /**
     * Override or extend the styles applied to the component.
     * See [CSS API](#css-api) below for more details.
     */
    classes: _propTypes.default.object.isRequired,
  
    /**
     * If `true`, the selected / first menu item will not be auto focused.
     */
    disableAutoFocusItem: _propTypes.default.bool,
  
    /**
     * Properties applied to the [`MenuList`](/api/menu-list/) element.
     */
    MenuListProps: _propTypes.default.object,
  
    /**
     * Callback fired when the component requests to be closed.
     *
     * @param {object} event The event source of the callback
     * @param {string} reason Can be:`"escapeKeyDown"`, `"backdropClick"`, `"tabKeyDown"`
     */
    onClose: _propTypes.default.func,
  
    /**
     * Callback fired before the Menu enters.
     */
    onEnter: _propTypes.default.func,
  
    /**
     * Callback fired when the Menu has entered.
     */
    onEntered: _propTypes.default.func,
  
    /**
     * Callback fired when the Menu is entering.
     */
    onEntering: _propTypes.default.func,
  
    /**
     * Callback fired before the Menu exits.
     */
    onExit: _propTypes.default.func,
  
    /**
     * Callback fired when the Menu has exited.
     */
    onExited: _propTypes.default.func,
  
    /**
     * Callback fired when the Menu is exiting.
     */
    onExiting: _propTypes.default.func,
  
    /**
     * If `true`, the menu is visible.
     */
    open: _propTypes.default.bool.isRequired,
  
    /**
     * @ignore
     */
    PaperProps: _propTypes.default.object,
  
    /**
     * `classes` property applied to the [`Popover`](/api/popover/) element.
     */
    PopoverClasses: _propTypes.default.object,
  
    /**
     * @ignore
     */
    theme: _propTypes.default.object.isRequired,
  
    /**
     * The length of the transition in `ms`, or 'auto'
     */
    transitionDuration: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.shape({
      enter: _propTypes.default.number,
      exit: _propTypes.default.number
    }), _propTypes.default.oneOf(['auto'])])
  } : void 0;
  Menu.defaultProps = {
    disableAutoFocusItem: false,
    transitionDuration: 'auto'
  };
  
  var _default = (0, _withStyles.default)(styles, {
    name: 'MuiMenu',
    withTheme: true
  })(Menu);
  
  exports.default = _default;