UNPKG

@procore/core-react

Version:
224 lines (218 loc) • 11.2 kB
var _excluded = ["restoreFocus"], _excluded2 = ["children", "className", "arrow", "loading", "open", "focus", "variant"], _excluded3 = ["afterHide", "afterShow", "beforeHide", "beforeShow", "children", "error", "footer", "i18nScope", "icon", "label", "onKeyDown", "onSearch", "onSelect", "placement", "restoreFocusOnHide", "variant", "hasTabbableElements"]; function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import { CaretDown, CaretUp, EllipsisVertical } from '@procore/core-icons/dist'; import classNames from 'classnames'; import React from 'react'; import { MenuImperative, useMenuImperativeControlNavigation } from '../MenuImperative/MenuImperative'; import { OverlayTrigger, useOverlayTriggerContext } from '../OverlayTrigger/OverlayTrigger'; import { useI18nContext } from '../_hooks/I18n'; import { addSubcomponents } from '../_utils/addSubcomponents'; import { mergeRefs } from '../_utils/mergeRefs'; import { StyledDropdownButton, StyledDropdownMenu, StyledDropdownSpinner } from './Dropdown.styles'; function noop() {} function isFunction(obj) { return typeof obj === 'function'; } function isOpen(_ref) { var _ref$open = _ref.open, open = _ref$open === void 0 ? false : _ref$open; return open; } var Item = /*#__PURE__*/React.forwardRef(function Item(_ref2, ref) { var _ref2$restoreFocus = _ref2.restoreFocus, restoreFocus = _ref2$restoreFocus === void 0 ? false : _ref2$restoreFocus, props = _objectWithoutProperties(_ref2, _excluded); return /*#__PURE__*/React.createElement(MenuImperative.Item, _extends({ ref: ref, restoreFocus: restoreFocus }, props)); }); export var DropdownButton = /*#__PURE__*/React.forwardRef(function DropdownButton(_ref3, ref) { var children = _ref3.children, className = _ref3.className, _ref3$arrow = _ref3.arrow, arrow = _ref3$arrow === void 0 ? false : _ref3$arrow, _ref3$loading = _ref3.loading, loading = _ref3$loading === void 0 ? false : _ref3$loading, _ref3$open = _ref3.open, open = _ref3$open === void 0 ? false : _ref3$open, _ref3$focus = _ref3.focus, focus = _ref3$focus === void 0 ? false : _ref3$focus, _ref3$variant = _ref3.variant, variant = _ref3$variant === void 0 ? 'secondary' : _ref3$variant, props = _objectWithoutProperties(_ref3, _excluded2); var caret = arrow && (open ? /*#__PURE__*/React.createElement(CaretUp, { size: "sm" }) : /*#__PURE__*/React.createElement(CaretDown, { size: "sm" })); var icon = loading ? /*#__PURE__*/React.createElement(StyledDropdownSpinner, { size: "xs", variant: variant === 'primary' ? 'light' : 'default' }) : caret; return /*#__PURE__*/React.createElement(StyledDropdownButton, _extends({}, props, { className: classNames(className, { focus: isFunction(focus) ? focus({ open: open }) : focus }), "aria-busy": loading || undefined, iconRight: icon, $loading: loading, variant: variant, ref: ref }), children); }); var DropdownMenu = /*#__PURE__*/React.forwardRef(function DropdownMenu(_ref4, ref) { var children = _ref4.children, footer = _ref4.footer, i18nScope = _ref4.i18nScope, menuRef = _ref4.menuRef, onSearch_ = _ref4.onSearch, _ref4$onSelect = _ref4.onSelect, onSelect_ = _ref4$onSelect === void 0 ? noop : _ref4$onSelect, id = _ref4.id; var overlayTriggerContext = useOverlayTriggerContext(); var _React$useState = React.useState(''), _React$useState2 = _slicedToArray(_React$useState, 2), search = _React$useState2[0], setSearch = _React$useState2[1]; var _useMenuImperativeCon = useMenuImperativeControlNavigation(menuRef, Boolean(onSearch_), { menuId: id }), menuProps = _useMenuImperativeCon.menuProps, menuNavigationTriggerProps = _useMenuImperativeCon.menuNavigationTriggerProps; function onSelect(selection) { overlayTriggerContext.hide({ restoreFocus: selection.restoreFocus }); onSelect_(selection); } function onSearch(e) { setSearch(e.target.value); onSearch_ === null || onSearch_ === void 0 ? void 0 : onSearch_(e); } function onKeyDown(e) { // This is disconnected from the OverlayTrigger closing on Escape. // Closing a dropdown with Escape will not close a Modal, because stopP. // It is called only once because the menu component is unmounted. // The next Escape will propagate. if (e.key === 'Escape' || e.key === 'Esc') { // only let parent listeners (modal, overlay) know when empty e.stopPropagation(); } } return /*#__PURE__*/React.createElement(StyledDropdownMenu, { ref: ref, shadowStrength: 2 }, /*#__PURE__*/React.createElement(MenuImperative, _extends({}, menuProps, { ref: menuRef, onKeyDown: onKeyDown, onSelect: onSelect, role: "menu" }), onSearch_ && /*#__PURE__*/React.createElement(MenuImperative.Search, _extends({}, menuNavigationTriggerProps, { onChange: onSearch, i18nScope: i18nScope })), /*#__PURE__*/React.createElement(MenuImperative.Options, null, children), footer && /*#__PURE__*/React.createElement(MenuImperative.Footer, null, footer))); }); var Dropdown_ = /*#__PURE__*/React.forwardRef(function Dropdown(_ref5, ref) { var afterHide_ = _ref5.afterHide, afterShow = _ref5.afterShow, beforeHide = _ref5.beforeHide, beforeShow = _ref5.beforeShow, children = _ref5.children, error = _ref5.error, footer = _ref5.footer, _ref5$i18nScope = _ref5.i18nScope, i18nScope = _ref5$i18nScope === void 0 ? 'core.dropdown' : _ref5$i18nScope, icon = _ref5.icon, label = _ref5.label, _ref5$onKeyDown = _ref5.onKeyDown, onKeyDown = _ref5$onKeyDown === void 0 ? noop : _ref5$onKeyDown, onSearch = _ref5.onSearch, _ref5$onSelect = _ref5.onSelect, onSelect = _ref5$onSelect === void 0 ? noop : _ref5$onSelect, _ref5$placement = _ref5.placement, placement = _ref5$placement === void 0 ? 'bottom-left' : _ref5$placement, _ref5$restoreFocusOnH = _ref5.restoreFocusOnHide, restoreFocusOnHide = _ref5$restoreFocusOnH === void 0 ? false : _ref5$restoreFocusOnH, _ref5$variant = _ref5.variant, variant = _ref5$variant === void 0 ? 'secondary' : _ref5$variant, _ref5$hasTabbableElem = _ref5.hasTabbableElements, hasTabbableElements = _ref5$hasTabbableElem === void 0 ? false : _ref5$hasTabbableElem, props = _objectWithoutProperties(_ref5, _excluded3); var menuRef = React.useRef(null); var buttonRef = React.useRef(null); var isDialog = Boolean(onSearch) || hasTabbableElements; function afterHide(e) { // @ts-expect-error if ((e === null || e === void 0 ? void 0 : e.key) === 'Escape' || e !== null && e !== void 0 && e.restoreFocus) { var _buttonRef$current; (_buttonRef$current = buttonRef.current) === null || _buttonRef$current === void 0 ? void 0 : _buttonRef$current.focus(); } afterHide_ === null || afterHide_ === void 0 ? void 0 : afterHide_(e); onSearch === null || onSearch === void 0 ? void 0 : onSearch({ target: { value: '' } }); } var I18n = useI18nContext(); var ariaLabel = props['aria-label'] || label || I18n.t("core.dropdown.".concat(props.loading ? 'loading' : 'moreOptions')); return /*#__PURE__*/React.createElement(OverlayTrigger, { role: isDialog ? 'dialog' : 'menu', passA11yPropsToOverlay: !isDialog, autoFocus: true, afterHide: afterHide, afterShow: afterShow, beforeHide: beforeHide, beforeShow: beforeShow, overlay: /*#__PURE__*/React.createElement(DropdownMenu, { footer: footer, i18nScope: i18nScope, menuRef: menuRef, onSearch: onSearch, onSelect: onSelect }, children), placement: placement, ref: mergeRefs(ref, buttonRef), restoreFocusOnHide: restoreFocusOnHide }, /*#__PURE__*/React.createElement(DropdownButton, _extends({}, props, { "aria-label": ariaLabel, arrow: label !== undefined, focus: onSearch ? false : isOpen, icon: props.loading && !label ? undefined : !icon && !label ? /*#__PURE__*/React.createElement(EllipsisVertical, null) : icon, onKeyDown: onKeyDown, variant: variant }), label)); }); export var useDropdownContext = useOverlayTriggerContext; export function DropdownConsumer(_ref6) { var children = _ref6.children; return children(useDropdownContext()); } Dropdown_.displayName = 'Dropdown'; Item.displayName = 'Dropdown.Item'; /** Dropdowns display a list of actions in a menu that opens and closes. We use dropdowns most commonly to trigger an action or to redirect the user to a new page or modal. @since 10.19.0 @see [Storybook](https://stories.core.procore.com/?path=/story/core-react_demos-dropdown--demo) @see [Design Guidelines](https://design.procore.com/dropdown) */ export var Dropdown = addSubcomponents({ Item: Item, Group: MenuImperative.Group }, Dropdown_); //# sourceMappingURL=Dropdown.js.map