UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

340 lines (324 loc) 13.8 kB
'use client'; 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 { useHover } from 'ahooks'; import { ConfigProvider } from 'antd'; import { cva } from 'class-variance-authority'; import { ChevronDown, ChevronLeft, ChevronRight, ChevronUp } from 'lucide-react'; import { Resizable } from 're-resizable'; import { memo, use, useEffect, useMemo, useRef, useState } from 'react'; import { Center } from 'react-layout-kit'; import useControlledState from 'use-merge-value'; import Icon from "../Icon"; import { useStyles } from "./style"; import { reversePlacement } from "./utils"; // Constants import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var DEFAULT_HEIGHT = 180; var DEFAULT_WIDTH = 280; var DEFAULT_HEADER_HEIGHT = 0; var DEFAULT_PIN = true; var DEFAULT_MODE = 'fixed'; var DEFAULT_EXPANDABLE = true; var DEFAULT_EXPAND = true; var DEFAULT_SHOW_HANDLE_WIDE_AREA = true; var DraggablePanel = /*#__PURE__*/memo(function (_ref) { var _ref$headerHeight = _ref.headerHeight, headerHeight = _ref$headerHeight === void 0 ? DEFAULT_HEADER_HEIGHT : _ref$headerHeight, fullscreen = _ref.fullscreen, maxHeight = _ref.maxHeight, _ref$pin = _ref.pin, pin = _ref$pin === void 0 ? DEFAULT_PIN : _ref$pin, _ref$mode = _ref.mode, mode = _ref$mode === void 0 ? DEFAULT_MODE : _ref$mode, children = _ref.children, _ref$placement = _ref.placement, placement = _ref$placement === void 0 ? 'right' : _ref$placement, resize = _ref.resize, style = _ref.style, _ref$showHandleWideAr = _ref.showHandleWideArea, showHandleWideArea = _ref$showHandleWideAr === void 0 ? DEFAULT_SHOW_HANDLE_WIDE_AREA : _ref$showHandleWideAr, size = _ref.size, customizeDefaultSize = _ref.defaultSize, minWidth = _ref.minWidth, minHeight = _ref.minHeight, maxWidth = _ref.maxWidth, onSizeChange = _ref.onSizeChange, onSizeDragging = _ref.onSizeDragging, _ref$expandable = _ref.expandable, expandable = _ref$expandable === void 0 ? DEFAULT_EXPANDABLE : _ref$expandable, expand = _ref.expand, _ref$defaultExpand = _ref.defaultExpand, defaultExpand = _ref$defaultExpand === void 0 ? DEFAULT_EXPAND : _ref$defaultExpand, onExpandChange = _ref.onExpandChange, className = _ref.className, showHandleWhenCollapsed = _ref.showHandleWhenCollapsed, destroyOnClose = _ref.destroyOnClose, customStyles = _ref.styles, classNames = _ref.classNames, dir = _ref.dir; var ref = useRef(null); var isHovering = useHover(ref); var isVertical = placement === 'top' || placement === 'bottom'; // inherit direction from Ant Design ConfigProvider var _use = use(ConfigProvider.ConfigContext), antdDirection = _use.direction; var direction = dir !== null && dir !== void 0 ? dir : antdDirection; // Handle RTL direction var internalPlacement = useMemo(function () { return direction === 'rtl' && ['left', 'right'].includes(placement) ? placement === 'left' ? 'right' : 'left' : placement; }, [direction, placement]); var _useStyles = useStyles({ headerHeight: headerHeight, showHandleWideArea: showHandleWideArea }), styles = _useStyles.styles, cx = _useStyles.cx; var _useControlledState = useControlledState(defaultExpand, { onChange: onExpandChange, value: expand }), _useControlledState2 = _slicedToArray(_useControlledState, 2), isExpand = _useControlledState2[0], setIsExpand = _useControlledState2[1]; // Auto-expand on hover if not pinned useEffect(function () { if (pin) return; if (isHovering && !isExpand) { setIsExpand(true); } else if (!isHovering && isExpand) { setIsExpand(false); } }, [pin, isHovering, isExpand, setIsExpand]); var _useState = useState(true), _useState2 = _slicedToArray(_useState, 2), showExpand = _useState2[0], setShowExpand = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isResizing = _useState4[0], setIsResizing = _useState4[1]; var canResizing = resize !== false && isExpand; // Style variants for the panel var variants = useMemo(function () { return cva(styles.root, { compoundVariants: [{ class: styles.bottomFloat, mode: 'float', placement: 'bottom' }, { class: styles.topFloat, mode: 'float', placement: 'top' }, { class: styles.leftFloat, mode: 'float', placement: 'left' }, { class: styles.rightFloat, mode: 'float', placement: 'right' }], /* eslint-disable sort-keys-fix/sort-keys-fix */ variants: { placement: { top: isExpand && styles.borderBottom, right: isExpand && styles.borderLeft, bottom: isExpand && styles.borderTop, left: isExpand && styles.borderRight }, mode: { fixed: styles.fixed, float: null } } /* eslint-enable sort-keys-fix/sort-keys-fix */ }); }, [styles, isExpand]); // Style variants for the handle var handleVariants = useMemo(function () { return cva(styles.handleRoot, { variants: { placement: { bottom: styles.handleBottom, left: styles.handleLeft, right: styles.handleRight, top: styles.handleTop } } }); }, [styles]); // Style variants for the toggle button var toggleVariants = useMemo(function () { return cva(styles.toggleRoot, { variants: { placement: { bottom: styles.toggleTop, left: styles.toggleRight, right: styles.toggleLeft, top: styles.toggleBottom } } }); }, [styles]); // Configure resizing handles var resizing = useMemo(function () { return _objectSpread(_defineProperty({ bottom: false, bottomLeft: false, bottomRight: false, left: false, right: false, top: false, topLeft: false, topRight: false }, reversePlacement(internalPlacement), true), resize); }, [internalPlacement, resize]); // Calculate default size based on orientation var defaultSize = useMemo(function () { if (isVertical) return _objectSpread({ height: DEFAULT_HEIGHT, width: '100%' }, customizeDefaultSize); return _objectSpread({ height: '100%', width: DEFAULT_WIDTH }, customizeDefaultSize); }, [isVertical, customizeDefaultSize]); // Determine appropriate size props based on expand state var sizeProps = useMemo(function () { if (!isExpand) { return isVertical ? { minHeight: 0, size: { height: 0 } } : { minWidth: 0, size: { width: 0 } }; } return { defaultSize: defaultSize, maxHeight: typeof maxHeight === 'number' ? Math.max(maxHeight, 0) : maxHeight, maxWidth: typeof maxWidth === 'number' ? Math.max(maxWidth, 0) : maxWidth, minHeight: typeof minHeight === 'number' ? Math.max(minHeight, 0) : minHeight, minWidth: typeof minWidth === 'number' ? Math.max(minWidth, 0) : minWidth, size: size }; }, [isExpand, isVertical, defaultSize, maxHeight, maxWidth, minHeight, minWidth, size]); // Determine the appropriate arrow icon based on placement var Arrow = useMemo(function () { switch (internalPlacement) { case 'top': { return ChevronDown; } case 'bottom': { return ChevronUp; } case 'right': { return ChevronLeft; } case 'left': { return ChevronRight; } default: { return ChevronLeft; } } }, [internalPlacement]); // Toggle handle component var handle = /*#__PURE__*/_jsx(Center, { className: toggleVariants({ placement: internalPlacement }), style: { opacity: isExpand ? pin ? undefined : 0 : showHandleWhenCollapsed ? 1 : 0 }, children: /*#__PURE__*/_jsx(Center, { className: classNames === null || classNames === void 0 ? void 0 : classNames.handle, onClick: function onClick() { return setIsExpand(!isExpand); }, style: customStyles === null || customStyles === void 0 ? void 0 : customStyles.handle, children: /*#__PURE__*/_jsx(Icon, { className: styles.handlerIcon, icon: Arrow, size: 16, style: { transform: "rotate(".concat(isExpand ? 180 : 0, "deg)") } }) }) }); // Handle resize events var handleResize = function handleResize(_, _direction, reference_, delta) { onSizeDragging === null || onSizeDragging === void 0 || onSizeDragging(delta, { height: reference_.style.height, width: reference_.style.width }); }; var handleResizeStart = function handleResizeStart() { setIsResizing(true); setShowExpand(false); }; var handleResizeStop = function handleResizeStop(e, direction, reference_, delta) { setIsResizing(false); setShowExpand(true); onSizeChange === null || onSizeChange === void 0 || onSizeChange(delta, { height: reference_.style.height, width: reference_.style.width }); }; // Main panel content var inner = /*#__PURE__*/_jsx(Resizable, _objectSpread(_objectSpread({}, sizeProps), {}, { className: cx(styles.panel, classNames === null || classNames === void 0 ? void 0 : classNames.content), enable: canResizing ? resizing : undefined, handleClasses: canResizing ? _defineProperty({}, reversePlacement(internalPlacement), handleVariants({ placement: reversePlacement(internalPlacement) })) : {}, onResize: handleResize, onResizeStart: handleResizeStart, onResizeStop: handleResizeStop, style: _objectSpread({ transition: isResizing ? 'unset' : undefined }, style), children: children })); // For fullscreen mode, return a simpler layout if (fullscreen) { return /*#__PURE__*/_jsx("div", { className: cx(styles.fullscreen, className), children: children }); } return /*#__PURE__*/_jsxs("aside", { className: cx(variants({ mode: mode, placement: internalPlacement }), className), dir: dir, ref: ref, children: [expandable && showExpand && handle, destroyOnClose ? isExpand && inner : inner] }); }); DraggablePanel.displayName = 'DraggablePanel'; export default DraggablePanel;