@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
158 lines (157 loc) • 8.26 kB
JavaScript
'use client';
var _excluded = ["children"];
var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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; }
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
import { useSize } from 'ahooks';
import { createStyles } from 'antd-style';
import { ChevronDown, ChevronUp } from 'lucide-react';
import { rgba } from 'polished';
import { forwardRef, useCallback, useEffect, useRef, useState } from 'react';
import { Flexbox } from 'react-layout-kit';
import ActionIcon from "../../ActionIcon";
import ChatInputAreaInner from "../../chat/ChatInputArea/ChatInputAreaInner";
import MobileSafeArea from "../MobileSafeArea";
import { jsxs as _jsxs } from "react/jsx-runtime";
import { jsx as _jsx } from "react/jsx-runtime";
var useStyles = createStyles(function (_ref) {
var css = _ref.css,
token = _ref.token;
return {
container: css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n flex: none;\n padding-block: 12px;\n background: ", ";\n border-block-start: 1px solid ", ";\n "])), token.colorFillQuaternary, rgba(token.colorBorder, 0.25)),
expand: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n position: absolute;\n height: 100%;\n "]))),
expandButton: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n position: absolute;\n inset-inline-start: 14px;\n "]))),
expandTextArea: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n flex: 1;\n "]))),
inner: css(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n height: inherit;\n padding-block: 0;\n padding-inline: 8px;\n "])))
};
});
var MobileChatInputArea = /*#__PURE__*/forwardRef(function (_ref2, ref) {
var className = _ref2.className,
style = _ref2.style,
topAddons = _ref2.topAddons,
textAreaLeftAddons = _ref2.textAreaLeftAddons,
textAreaRightAddons = _ref2.textAreaRightAddons,
bottomAddons = _ref2.bottomAddons,
_ref2$expand = _ref2.expand,
expand = _ref2$expand === void 0 ? false : _ref2$expand,
setExpand = _ref2.setExpand,
onSend = _ref2.onSend,
onInput = _ref2.onInput,
loading = _ref2.loading,
value = _ref2.value,
safeArea = _ref2.safeArea;
var containerRef = useRef(null);
var _useStyles = useStyles(),
cx = _useStyles.cx,
styles = _useStyles.styles;
var size = useSize(containerRef);
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
showFullscreen = _useState2[0],
setShowFullscreen = _useState2[1];
var _useState3 = useState(false),
_useState4 = _slicedToArray(_useState3, 2),
isFocused = _useState4[0],
setIsFocused = _useState4[1];
useEffect(function () {
if (!(size !== null && size !== void 0 && size.height)) return;
setShowFullscreen(size.height > 72);
}, [size]);
var InnerContainer = useCallback(function (_ref3) {
var children = _ref3.children,
r = _objectWithoutProperties(_ref3, _excluded);
return expand ? /*#__PURE__*/_jsxs(Flexbox, {
className: styles.inner,
gap: 8,
children: [/*#__PURE__*/_jsxs(Flexbox, {
gap: 8,
horizontal: true,
justify: 'flex-end',
children: [r.textAreaLeftAddons, r.textAreaRightAddons]
}), children, r.topAddons, r.bottomAddons]
}) : /*#__PURE__*/_jsxs(Flexbox, {
align: 'flex-end',
className: styles.inner,
gap: 8,
horizontal: true,
children: [r.textAreaLeftAddons, children, r.textAreaRightAddons]
});
}, [expand, loading]);
var showAddons = !expand && !isFocused;
return /*#__PURE__*/_jsxs(Flexbox, {
className: cx(styles.container, expand && styles.expand, className),
gap: 12,
style: style,
children: [topAddons && /*#__PURE__*/_jsx(Flexbox, {
style: showAddons ? {} : {
display: 'none'
},
children: topAddons
}), /*#__PURE__*/_jsxs(Flexbox, {
className: cx(expand && styles.expand),
ref: containerRef,
style: {
position: 'relative'
},
children: [showFullscreen && /*#__PURE__*/_jsx(ActionIcon, {
active: true,
className: styles.expandButton,
icon: expand ? ChevronDown : ChevronUp,
onClick: function onClick() {
return setExpand === null || setExpand === void 0 ? void 0 : setExpand(!expand);
},
size: {
blockSize: 24,
borderRadius: '50%',
fontSize: 14
},
style: expand ? {
top: 6
} : {}
}), /*#__PURE__*/_jsx(InnerContainer, {
bottomAddons: bottomAddons,
textAreaLeftAddons: textAreaLeftAddons,
textAreaRightAddons: textAreaRightAddons,
topAddons: topAddons,
children: /*#__PURE__*/_jsx(ChatInputAreaInner, {
autoSize: expand ? false : {
maxRows: 6,
minRows: 0
},
className: cx(expand && styles.expandTextArea),
loading: loading,
onBlur: function onBlur() {
return setIsFocused(false);
},
onFocus: function onFocus() {
return setIsFocused(true);
},
onInput: onInput,
onSend: onSend,
ref: ref,
style: {
height: 36,
paddingBlock: 6
},
type: expand ? 'pure' : 'block',
value: value
})
})]
}), bottomAddons && /*#__PURE__*/_jsx(Flexbox, {
style: showAddons ? {} : {
display: 'none'
},
children: bottomAddons
}), safeArea && !isFocused && /*#__PURE__*/_jsx(MobileSafeArea, {
position: 'bottom'
})]
});
});
export default MobileChatInputArea;