UNPKG

@react-md/menu

Version:

Create menus that auto-position themselves within the viewport and adhere to the accessibility guidelines

124 lines 6.21 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { forwardRef } from "react"; import { useIcon } from "@react-md/icon"; import { defaultMenuItemRenderer, } from "./defaultMenuItemRenderer"; import { defaultMenuRenderer, } from "./defaultMenuRenderer"; import { MenuButton } from "./MenuButton"; import { useButtonVisibility } from "./useButtonVisibility"; export var DropdownMenu = forwardRef(function DropdownMenu(_a, ref) { var propOnClick = _a.onClick, propOnKeyDown = _a.onKeyDown, children = _a.children, anchor = _a.anchor, menuLabel = _a.menuLabel, menuLabelledBy = _a.menuLabelledBy, menuStyle = _a.menuStyle, menuClassName = _a.menuClassName, _b = _a.menuRenderer, menuRenderer = _b === void 0 ? defaultMenuRenderer : _b, items = _a.items, _c = _a.itemRenderer, itemRenderer = _c === void 0 ? defaultMenuItemRenderer : _c, horizontal = _a.horizontal, onVisibilityChange = _a.onVisibilityChange, portal = _a.portal, portalInto = _a.portalInto, portalIntoId = _a.portalIntoId, positionOptions = _a.positionOptions, _d = _a.defaultVisible, defaultVisible = _d === void 0 ? false : _d, closeOnScroll = _a.closeOnScroll, closeOnResize = _a.closeOnResize, propDropdownIcon = _a.dropdownIcon, _e = _a.disableDropdownIcon, disableDropdownIcon = _e === void 0 ? false : _e, props = __rest(_a, ["onClick", "onKeyDown", "children", "anchor", "menuLabel", "menuLabelledBy", "menuStyle", "menuClassName", "menuRenderer", "items", "itemRenderer", "horizontal", "onVisibilityChange", "portal", "portalInto", "portalIntoId", "positionOptions", "defaultVisible", "closeOnScroll", "closeOnResize", "dropdownIcon", "disableDropdownIcon"]); var id = props.id; var dropdownIcon = useIcon("dropdown", propDropdownIcon); var _f = useButtonVisibility({ onClick: propOnClick, onKeyDown: propOnKeyDown, defaultVisible: defaultVisible, onVisibilityChange: onVisibilityChange, }), visible = _f.visible, defaultFocus = _f.defaultFocus, onClick = _f.onClick, onKeyDown = _f.onKeyDown, hide = _f.hide; var labelledBy = menuLabelledBy; if (!menuLabel && !menuLabelledBy) { labelledBy = id; } return (React.createElement(React.Fragment, null, React.createElement(MenuButton, __assign({}, props, { ref: ref, "aria-haspopup": "menu", visible: visible, onClick: onClick, onKeyDown: onKeyDown, dropdownIcon: dropdownIcon, disableDropdownIcon: disableDropdownIcon }), children), menuRenderer({ "aria-label": menuLabel, // ok to typecast since one of these two should be a string by this // line "aria-labelledby": labelledBy, id: id + "-menu", controlId: id, style: menuStyle, className: menuClassName, anchor: anchor, positionOptions: positionOptions, closeOnScroll: closeOnScroll, closeOnResize: closeOnResize, horizontal: horizontal, visible: visible, defaultFocus: defaultFocus, onRequestClose: hide, children: items.map(function (item, i) { return itemRenderer(item, "item-" + i); }), portal: portal, portalInto: portalInto, portalIntoId: portalIntoId, }, items))); }); if (process.env.NODE_ENV !== "production") { try { var PropTypes = require("prop-types"); DropdownMenu.propTypes = { id: PropTypes.string.isRequired, defaultVisible: PropTypes.bool, menuLabel: PropTypes.string, menuLabelledBy: PropTypes.string, menuRenderer: PropTypes.func, items: PropTypes.arrayOf(PropTypes.oneOfType([ PropTypes.string, PropTypes.number, PropTypes.node, PropTypes.object, ])).isRequired, onClick: PropTypes.func, onKeyDown: PropTypes.func, children: PropTypes.node, horizontal: PropTypes.bool, portal: PropTypes.bool, portalInto: PropTypes.oneOfType([ PropTypes.string, PropTypes.func, PropTypes.object, ]), portalIntoId: PropTypes.string, anchor: PropTypes.shape({ x: PropTypes.oneOf([ "left", "right", "center", "inner-left", "inner-right", ]).isRequired, y: PropTypes.oneOf(["above", "below", "center", "top", "bottom"]) .isRequired, }), positionOptions: PropTypes.shape({ vwMargin: PropTypes.number, vhMargin: PropTypes.number, xMargin: PropTypes.number, yMargin: PropTypes.number, initialX: PropTypes.number, initialY: PropTypes.number, disableSwapping: PropTypes.bool, }), itemRenderer: PropTypes.func, dropdownIcon: PropTypes.node, disableDropdownIcon: PropTypes.bool, onVisibilityChange: PropTypes.func, closeOnScroll: PropTypes.bool, closeOnResize: PropTypes.bool, }; } catch (e) { } } //# sourceMappingURL=DropdownMenu.js.map