@spaced-out/ui-design-system
Version:
Sense UI components library
162 lines (161 loc) • 6.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.InlineDropdown = void 0;
var React = _interopRequireWildcard(require("react"));
var _react2 = require("@floating-ui/react");
var _hooks = require("../../hooks");
var _space = require("../../styles/variables/_space");
var _classify = require("../../utils/classify");
var _clickAway = require("../../utils/click-away");
var _mergeRefs = require("../../utils/merge-refs");
var _qa = require("../../utils/qa");
var _Button = require("../Button");
var _Icon = require("../Icon");
var _Menu = require("../Menu");
var _Tooltip = require("../Tooltip");
var _Truncate = require("../Truncate");
var _InlineDropdownModule = _interopRequireDefault(require("./InlineDropdown.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const InlineDropdown = exports.InlineDropdown = /*#__PURE__*/React.forwardRef((_ref, ref) => {
let {
anchorPosition = 'bottom-start',
size = 'medium',
onOptionSelect,
menu,
classNames,
disabled,
onMenuOpen,
onMenuClose,
children,
clickAwayRef,
elevation = 'modal',
testId,
...restButtonProps
} = _ref;
const {
x,
y,
refs,
strategy,
context
} = (0, _react2.useFloating)({
open: true,
strategy: 'absolute',
placement: anchorPosition,
whileElementsMounted: _react2.autoUpdate,
middleware: [(0, _react2.shift)(), (0, _react2.flip)(), (0, _react2.offset)(parseInt(_space.spaceXXSmall))]
});
const dropdownWidth = (0, _hooks.useReferenceElementWidth)(refs.reference?.current);
const onMenuToggle = isOpen => {
if (isOpen) {
onMenuOpen?.();
} else {
onMenuClose?.();
}
};
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_clickAway.ClickAway, {
onChange: onMenuToggle,
clickAwayRef: clickAwayRef,
children: _ref2 => {
let {
isOpen,
onOpen,
clickAway,
boundaryRef,
triggerRef
} = _ref2;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'root'
}),
className: (0, _classify.classify)(_InlineDropdownModule.default.inlineDropdownContainer, classNames?.dropdownContainer),
ref: ref,
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_Button.UnstyledButton, {
...restButtonProps,
disabled: disabled,
ref: (0, _mergeRefs.mergeRefs)([refs.setReference, triggerRef]),
onClick: e => {
e.stopPropagation();
onOpen();
},
className: (0, _classify.classify)(_InlineDropdownModule.default.inlineButton, {
[_InlineDropdownModule.default.disabled]: disabled
}, _InlineDropdownModule.default[size], classNames?.buttonWrapper),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'button'
}),
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.Truncate, {
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'text'
}),
children: children
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Icon.Icon, {
name: isOpen ? 'caret-up' : 'caret-down',
size: "small",
type: "solid",
className: (0, _classify.classify)({
[_InlineDropdownModule.default.disabled]: disabled
}),
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'icon'
})
})]
}), isOpen && menu && /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.FloatingPortal, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.FloatingFocusManager, {
modal: false,
context: context,
initialFocus: triggerRef,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _InlineDropdownModule.default.menuWrapper,
ref: (0, _mergeRefs.mergeRefs)([refs.setFloating, boundaryRef]),
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'menu-wrapper'
}),
style: {
display: 'flex',
position: strategy,
top: y ?? _space.spaceNone,
left: x ?? _space.spaceNone,
/* NOTE(Sharad): The FloatingPortal renders the menu outside the normal DOM structure,
so its parent is effectively the <body> element. This means the menu
would otherwise default to the body's width. To support fluid width,
we must manually set the dropdown width here; otherwise, it uses a fixed width.
Also, Only treat menu as non-fluid if isFluid is strictly false, since default is true in menu and undefined means fluid. */
...(menu.isFluid !== false && {
'--dropdown-width': dropdownWidth
}),
'--menu-elevation': (0, _Tooltip.getElevationValue)(elevation)
},
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Menu.Menu, {
...menu,
onSelect: (option, e) => {
onOptionSelect?.(option, e);
if (
// option.keepMenuOpenOnOptionSelect - to allow the menu persist its open stat upon option selection in normal variant
!option.keepMenuOpenOnOptionSelect && (!menu.optionsVariant || menu.optionsVariant === 'normal')) {
clickAway();
}
},
size: menu.size || 'medium',
onTabOut: clickAway,
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'menu'
})
})
})
})
})]
});
}
});
});