@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
102 lines (101 loc) • 5.79 kB
JavaScript
'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); }
var _excluded = ["className", "style", "avatars", "size", "grid", "cornerShape", "avatarShape"];
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 _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; }
import { cva } from 'class-variance-authority';
import { memo, useMemo } from 'react';
import Avatar from "../Avatar";
import Block from "../Block";
import Grid from "../Grid";
import { useStyles } from "./style";
import { jsx as _jsx } from "react/jsx-runtime";
var GroupAvatar = /*#__PURE__*/memo(function (_ref) {
var className = _ref.className,
style = _ref.style,
_ref$avatars = _ref.avatars,
avatars = _ref$avatars === void 0 ? [] : _ref$avatars,
_ref$size = _ref.size,
size = _ref$size === void 0 ? 32 : _ref$size,
_ref$grid = _ref.grid,
grid = _ref$grid === void 0 ? 2 : _ref$grid,
_ref$cornerShape = _ref.cornerShape,
cornerShape = _ref$cornerShape === void 0 ? 'squircle' : _ref$cornerShape,
_ref$avatarShape = _ref.avatarShape,
avatarShape = _ref$avatarShape === void 0 ? 'circle' : _ref$avatarShape,
rest = _objectWithoutProperties(_ref, _excluded);
var _useStyles = useStyles(),
cx = _useStyles.cx,
styles = _useStyles.styles;
var calcSize = useMemo(function () {
var length = avatars.length;
var isAutoGrid = grid === 'auto';
var gridSize = isAutoGrid ? length > 4 ? 3 : 2 : grid;
var isCircle = cornerShape === 'circle';
var avatarSize = Math.floor(size / gridSize * (isCircle ? 0.65 : 0.75));
var gapSize = Math.floor((size - avatarSize * gridSize) / (isCircle ? 6 : 4));
return {
avatarSize: avatarSize,
gapSize: gapSize,
gridSize: gridSize,
gridWidth: avatarSize * gridSize + gapSize,
maxItemWidth: avatarSize - 1
};
}, [avatars, grid, size, cornerShape]);
var calcAvatars = useMemo(function () {
return avatars === null || avatars === void 0 ? void 0 : avatars.slice(0, calcSize.gridSize * calcSize.gridSize);
}, [avatars, calcSize.gridSize]);
var variants = useMemo(function () {
return cva(styles.root, {
defaultVariants: {
cornerShape: 'squircle'
},
/* eslint-disable sort-keys-fix/sort-keys-fix */
variants: {
cornerShape: {
circle: styles.circle,
ios: styles.ios,
sharp: styles.sharp,
smooth: styles.smooth,
square: styles.square,
squircle: styles.squircle
}
}
/* eslint-enable sort-keys-fix/sort-keys-fix */
});
}, [styles]);
return /*#__PURE__*/_jsx(Block, _objectSpread(_objectSpread({
align: 'center',
className: cx(variants({
cornerShape: cornerShape
}), className),
height: size,
justify: 'center',
style: style,
width: size
}, rest), {}, {
children: /*#__PURE__*/_jsx(Grid, {
gap: calcSize.gapSize,
maxItemWidth: 0,
rows: calcSize.gridSize,
width: calcSize.gridWidth,
children: calcAvatars.map(function (item, index) {
if (typeof item === 'string') return /*#__PURE__*/_jsx(Avatar, {
avatar: item,
shape: avatarShape,
size: calcSize.avatarSize
}, index);
return /*#__PURE__*/_jsx(Avatar, _objectSpread(_objectSpread({}, item), {}, {
shape: avatarShape,
size: calcSize.avatarSize
}), index);
})
})
}));
});
export default GroupAvatar;