UNPKG

@nex-ui/react

Version:

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

112 lines (109 loc) • 3.43 kB
import { jsxs, jsx } from 'react/jsx-runtime'; import { useMemo } from 'react'; import { isArray } from '@nex-ui/utils'; import { nex } from '@nex-ui/styled'; import { AvatarGroupProvider } from './AvatarGroupContext.mjs'; import { Avatar } from './Avatar.mjs'; import { useDefaultProps } from '../utils/useDefaultProps.mjs'; import { useStyles } from '../utils/useStyles.mjs'; import { useSlotProps } from '../utils/useSlotProps.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 avatarGroupRoot = `${prefix}-avatar-group`; const { color, size, radius, outlined, classes } = ownerState; const slots = { root: [ 'root', `radius-${radius}`, `size-${size}`, `color-${color}`, outlined && 'outlined' ], surplus: [ 'surplus' ] }; const composedClasses = composeClasses(slots, getUtilityClass(avatarGroupRoot), classes); return composedClasses; }; const AvatarGroup = (inProps)=>{ const props = useDefaultProps({ name: 'AvatarGroup', props: inProps }); const { children, total, slotProps, spacing, ref, renderSurplus, as = 'div', 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, as, max }; const classes = useSlotClasses(ownerState); const styles = useStyles({ name: 'AvatarGroup', ownerState, recipe: avatarGroupRecipe }); const rootProps = useSlotProps({ ownerState, externalForwardedProps: remainingProps, sx: styles, classNames: classes.root, additionalProps: { ref, as, style: { [`--avatar-group-spacing`]: spacing !== undefined ? `${spacing}px` : undefined } } }); const surplusProps = useSlotProps({ ownerState, externalSlotProps: slotProps?.surplus, classNames: classes.surplus }); const ctx = useMemo(()=>({ size, color, outlined, radius }), [ color, outlined, radius, size ]); if (!isArray(children)) { return children; } const lastAvatar = Math.min(children.length, max); const totalAvatars = total ?? children.length; const extraAvatars = Math.max(0, totalAvatars - lastAvatar); const extraElement = renderSurplus ? renderSurplus(extraAvatars) : /*#__PURE__*/ jsxs(Avatar, { ...surplusProps, children: [ "+", extraAvatars ] }); return /*#__PURE__*/ jsx(nex.div, { ...rootProps, children: /*#__PURE__*/ jsxs(AvatarGroupProvider, { value: ctx, children: [ children.slice(0, lastAvatar), !!extraAvatars && extraElement ] }) }); }; AvatarGroup.displayName = 'AvatarGroup'; export { AvatarGroup };