UNPKG

@nex-ui/react

Version:

🎉 A beautiful, modern, and reliable React component library.

111 lines (108 loc) • 3.59 kB
import { jsx, jsxs } from 'react/jsx-runtime'; import { useMemo } from 'react'; import { isArray } from '@nex-ui/utils'; import { AvatarGroupProvider } from './AvatarGroupContext.mjs'; import { Avatar } from './Avatar.mjs'; import { useDefaultProps } from '../utils/useDefaultProps.mjs'; import { useStyles } from '../utils/useStyles.mjs'; import { useSlot } from '../utils/useSlot.mjs'; import { useNexUI } from '../provider/Context.mjs'; import { composeClasses } from '../utils/composeClasses.mjs'; import { avatarGroupRecipe } from '../../theme/recipes/avatar.mjs'; import { getUtilityClass } from '../utils/getUtilityClass.mjs'; const useSlotClasses = (ownerState)=>{ const { prefix } = useNexUI(); const { classes } = ownerState; return useMemo(()=>{ const avatarGroupRoot = `${prefix}-avatar-group`; const slots = { root: [ 'root' ], surplus: [ 'surplus' ] }; return composeClasses(slots, getUtilityClass(avatarGroupRoot), classes); }, [ classes, prefix ]); }; const AvatarGroup = (inProps)=>{ const props = useDefaultProps({ name: 'AvatarGroup', props: inProps }); const { children, total, slotProps, spacing, renderSurplus, size = 'md', color = 'gray', outlined = false, radius = size, max: maxProp = 5, ...remainingProps } = props; const max = Math.max(1, maxProp); const ownerState = { ...props, size, color, outlined, radius, max }; const classes = useSlotClasses(ownerState); const style = useStyles({ ownerState, name: 'AvatarGroup', recipe: avatarGroupRecipe }); const [AvatarGroupRoot, getAvatarGroupRootProps] = useSlot({ style, ownerState, elementType: 'div', externalForwardedProps: remainingProps, classNames: classes.root, additionalProps: { style: { [`--avatar-group-spacing`]: spacing !== undefined ? `${spacing}px` : undefined } } }); const [AvatarSurplus, getAvatarSurplusProps] = useSlot({ ownerState, elementType: Avatar, externalSlotProps: slotProps?.surplus, classNames: classes.surplus, shouldForwardComponent: false }); const ctx = useMemo(()=>({ size, color, outlined, radius }), [ color, outlined, radius, size ]); const childrenLength = isArray(children) ? children.length : 1; if (childrenLength === 1 && (total === undefined || total < 2)) { return children; } const lastAvatar = Math.min(childrenLength, max); const totalAvatars = total ?? childrenLength; const extraAvatars = Math.max(0, totalAvatars - lastAvatar); return /*#__PURE__*/ jsx(AvatarGroupRoot, { ...getAvatarGroupRootProps(), children: /*#__PURE__*/ jsxs(AvatarGroupProvider, { value: ctx, children: [ isArray(children) ? children.slice(0, lastAvatar) : children, !!extraAvatars && (renderSurplus ? renderSurplus(extraAvatars) : /*#__PURE__*/ jsxs(AvatarSurplus, { ...getAvatarSurplusProps(), children: [ "+", extraAvatars ] })) ] }) }); }; AvatarGroup.displayName = 'AvatarGroup'; export { AvatarGroup };