"use client" import type { Assign } from "@ark-ui/react" import { Avatar as ArkAvatar } from "@ark-ui/react/avatar" import { forwardRef, useMemo } from "react" import { type HTMLChakraProps, type SlotRecipeProps, type UnstyledProp, chakra, createSlotRecipeContext, useSlotRecipe, } from "../../styled-system" import { cx } from "../../utils" import { Group, type GroupProps } from "../group" //////////////////////////////////////////////////////////////////////////////////// const { withProvider, withContext, useStyles: useAvatarStyles, useClassNames, PropsProvider, } = createSlotRecipeContext({ key: "avatar" }) export { useAvatarStyles } //////////////////////////////////////////////////////////////////////////////////// export interface AvatarRootProviderBaseProps extends Assign>, UnstyledProp {} export interface AvatarRootProviderProps extends HTMLChakraProps<"div", AvatarRootProviderBaseProps> {} export const AvatarRootProvider = withProvider< HTMLDivElement, AvatarRootProviderProps >(ArkAvatar.RootProvider, "root", { forwardAsChild: true }) //////////////////////////////////////////////////////////////////////////////////// export interface AvatarRootBaseProps extends Assign>, UnstyledProp {} export interface AvatarRootProps extends HTMLChakraProps<"div", AvatarRootBaseProps> {} export const AvatarRoot = withProvider( ArkAvatar.Root, "root", { forwardAsChild: true }, ) //////////////////////////////////////////////////////////////////////////////////// export const AvatarPropsProvider = PropsProvider as React.Provider //////////////////////////////////////////////////////////////////////////////////// export interface AvatarFallbackProps extends HTMLChakraProps<"div", ArkAvatar.FallbackProps> { /** * The name to derive the initials from. * If not provided, the fallback will display a generic icon. */ name?: string | undefined } const StyledFallback = chakra(ArkAvatar.Fallback, {}, { forwardAsChild: true }) function getFallbackChildren(props: AvatarFallbackProps) { if (props.children || props.asChild) return props.children if (props.name) return getInitials(props.name) return } function getInitials(name: string) { const names = name.trim().split(" ") const firstName = names[0] != null ? names[0] : "" const lastName = names.length > 1 ? names[names.length - 1] : "" return firstName && lastName ? `${firstName.charAt(0)}${lastName.charAt(0)}` : firstName.charAt(0) } export const AvatarFallback = forwardRef( function AvatarFallback(props, ref) { const styles = useAvatarStyles() const classNames = useClassNames() const { name: _, ...rest } = props return ( {getFallbackChildren(props)} ) }, ) //////////////////////////////////////////////////////////////////////////////////// export interface AvatarImageProps extends HTMLChakraProps<"img", ArkAvatar.ImageProps>, UnstyledProp {} export const AvatarImage = withContext( ArkAvatar.Image, "image", { forwardAsChild: true, defaultProps: { draggable: "false", referrerPolicy: "no-referrer", }, }, ) //////////////////////////////////////////////////////////////////////////////////// export interface AvatarIconProps extends HTMLChakraProps<"svg"> {} export const AvatarIcon = forwardRef( function AvatarIcon(props, ref) { return ( ) }, ) //////////////////////////////////////////////////////////////////////////////////// export const AvatarContext = ArkAvatar.Context export interface AvatarStatusChangeDetails extends ArkAvatar.StatusChangeDetails {} //////////////////////////////////////////////////////////////////////////////////// export interface AvatarGroupProps extends GroupProps, SlotRecipeProps<"avatar"> {} export const AvatarGroup = forwardRef( function AvatarGroup(props, ref) { const recipe = useSlotRecipe({ key: "avatar" }) const [variantProps, localProps] = useMemo( () => recipe.splitVariantProps(props), [props, recipe], ) return ( ) }, )