dumi-theme-lobehub
Version:
dumi-theme-lobehub is a documentation site theme package designed for dumi2. It provides a more beautiful and user-friendly development and reading experience based on @lobehub/ui
248 lines (247 loc) • 12.2 kB
JavaScript
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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(arr) { if (Array.isArray(arr)) return arr; }
import { FloatingFocusManager, FloatingOverlay, FloatingPortal, autoUpdate, flip, inner, offset, shift, size, useClick, useDismiss, useFloating, useInnerOffset, useInteractions, useListNavigation, useRole, useTypeahead } from '@floating-ui/react';
import { memo, useEffect, useRef, useState } from 'react';
import useControlledState from 'use-merge-value';
import SelectItem from "./SelectItem";
import { useStyles } from "./style";
import { jsx as _jsx } from "react/jsx-runtime";
import { Fragment as _Fragment } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var NativeSelect = /*#__PURE__*/memo(function (_ref) {
var _ref$options = _ref.options,
options = _ref$options === void 0 ? [] : _ref$options,
value = _ref.value,
prefixCls = _ref.prefixCls,
onChange = _ref.onChange,
renderValue = _ref.renderValue,
renderItem = _ref.renderItem,
style = _ref.style;
var cls = prefixCls !== null && prefixCls !== void 0 ? prefixCls : 'native-select';
var _useControlledState = useControlledState(0, {
onChange: onChange,
value: value
}),
_useControlledState2 = _slicedToArray(_useControlledState, 2),
selectedIndex = _useControlledState2[0],
setSelectedIndex = _useControlledState2[1];
var _useStyles = useStyles(cls),
styles = _useStyles.styles;
var listReference = useRef([]);
var listContentReference = useRef([]);
var overflowReference = useRef(null);
var allowSelectReference = useRef(false);
var allowMouseUpReference = useRef(true);
var selectTimeoutReference = useRef(null);
var scrollReference = useRef(null);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
open = _useState2[0],
setOpen = _useState2[1];
var _useState3 = useState(null),
_useState4 = _slicedToArray(_useState3, 2),
activeIndex = _useState4[0],
setActiveIndex = _useState4[1];
var _useState5 = useState(false),
_useState6 = _slicedToArray(_useState5, 2),
fallback = _useState6[0],
setFallback = _useState6[1];
var _useState7 = useState(0),
_useState8 = _slicedToArray(_useState7, 2),
innerOffset = _useState8[0],
setInnerOffset = _useState8[1];
var _useState9 = useState(false),
_useState10 = _slicedToArray(_useState9, 2),
touch = _useState10[0],
setTouch = _useState10[1];
var _useState11 = useState(false),
_useState12 = _slicedToArray(_useState11, 2),
blockSelection = _useState12[0],
setBlockSelection = _useState12[1];
if (!open) {
if (innerOffset !== 0) setInnerOffset(0);
if (fallback) setFallback(false);
if (blockSelection) setBlockSelection(false);
}
var _useFloating = useFloating({
middleware: fallback ? [offset(5), touch ? shift({
crossAxis: true,
padding: 10
}) : flip({
padding: 10
}), size({
apply: function apply(_ref2) {
var _scrollReference$curr, _scrollReference$curr2;
var availableHeight = _ref2.availableHeight;
Object.assign((_scrollReference$curr = (_scrollReference$curr2 = scrollReference.current) === null || _scrollReference$curr2 === void 0 ? void 0 : _scrollReference$curr2.style) !== null && _scrollReference$curr !== void 0 ? _scrollReference$curr : {}, {
maxHeight: "".concat(availableHeight, "px")
});
},
padding: 10
})] : [inner({
index: selectedIndex,
listRef: listReference,
minItemsVisible: touch ? 8 : 4,
offset: innerOffset,
onFallbackChange: setFallback,
overflowRef: overflowReference,
padding: 10,
referenceOverflowThreshold: 20,
scrollRef: scrollReference
}), offset({
crossAxis: -4
})],
onOpenChange: setOpen,
open: open,
placement: 'bottom-start',
whileElementsMounted: autoUpdate
}),
x = _useFloating.x,
y = _useFloating.y,
strategy = _useFloating.strategy,
refs = _useFloating.refs,
context = _useFloating.context;
var _useInteractions = useInteractions([useClick(context, {
event: 'mousedown'
}), useDismiss(context), useRole(context, {
role: 'listbox'
}), useInnerOffset(context, {
enabled: !fallback,
onChange: setInnerOffset,
overflowRef: overflowReference,
scrollRef: scrollReference
}), useListNavigation(context, {
activeIndex: activeIndex,
listRef: listReference,
onNavigate: setActiveIndex,
selectedIndex: selectedIndex
}), useTypeahead(context, {
activeIndex: activeIndex,
listRef: listContentReference,
onMatch: open ? setActiveIndex : setSelectedIndex
})]),
getReferenceProps = _useInteractions.getReferenceProps,
getFloatingProps = _useInteractions.getFloatingProps,
getItemProps = _useInteractions.getItemProps;
useEffect(function () {
if (open) {
selectTimeoutReference.current = setTimeout(function () {
allowSelectReference.current = true;
}, 300);
return function () {
clearTimeout(selectTimeoutReference.current);
};
}
allowSelectReference.current = false;
allowMouseUpReference.current = true;
}, [open]);
var _ref3 = options[selectedIndex] || {},
label = _ref3.label;
return /*#__PURE__*/_jsxs(_Fragment, {
children: [/*#__PURE__*/_jsx("button", _objectSpread(_objectSpread({
"aria-label": 'selected-item',
className: styles.button,
ref: refs.setReference,
style: style,
type: 'button'
}, getReferenceProps({
onPointerMove: function onPointerMove(_ref4) {
var pointerType = _ref4.pointerType;
if (pointerType === 'mouse') {
setTouch(false);
}
},
onTouchStart: function onTouchStart() {
setTouch(true);
}
})), {}, {
children: renderValue ? renderValue(selectedIndex) : label
})), /*#__PURE__*/_jsx(FloatingPortal, {
children: open && /*#__PURE__*/_jsx(FloatingOverlay, {
lockScroll: !touch,
style: {
zIndex: 3000
},
children: /*#__PURE__*/_jsx(FloatingFocusManager, {
context: context,
initialFocus: -1,
modal: false,
children: /*#__PURE__*/_jsx("div", {
ref: refs.setFloating,
style: {
left: x !== null && x !== void 0 ? x : 0,
position: strategy,
top: y !== null && y !== void 0 ? y : 0
},
children: /*#__PURE__*/_jsx("div", _objectSpread(_objectSpread({
className: styles.container,
ref: scrollReference,
style: {
overflowY: 'auto'
}
}, getFloatingProps({
onContextMenu: function onContextMenu(e) {
e.preventDefault();
}
})), {}, {
children: options.map(function (item, index) {
return /*#__PURE__*/_jsx(SelectItem, _objectSpread({
disabled: blockSelection,
isActive: index === activeIndex,
isSelected: index === selectedIndex,
label: renderItem ? renderItem(item, index) : item.label,
prefixCls: cls,
ref: function ref(node) {
listReference.current[index] = node;
listContentReference.current[index] = item.label;
},
value: item.value
}, getItemProps({
onClick: function onClick() {
if (allowSelectReference.current) {
setSelectedIndex(index);
setOpen(false);
}
},
onKeyDown: function onKeyDown() {
allowSelectReference.current = true;
},
onMouseUp: function onMouseUp() {
if (!allowMouseUpReference.current) {
return;
}
if (allowSelectReference.current) {
setSelectedIndex(index);
setOpen(false);
}
// On touch devices, prevent the element from
// immediately closing `onClick` by deferring it
clearTimeout(selectTimeoutReference.current);
selectTimeoutReference.current = setTimeout(function () {
allowSelectReference.current = true;
});
},
onTouchStart: function onTouchStart() {
allowSelectReference.current = true;
allowMouseUpReference.current = false;
}
})), item.value);
})
}))
})
})
})
})]
});
});
export default NativeSelect;