跳至正文
获取 Pro
基础原语U.25

头像

FreeRegistry
components/linyi/avatar.tsx
@base-ui/reactclass-variance-authority
"use client";
 
import * as React from "react";
import { Avatar as BaseAvatar } from "@base-ui/react/avatar";
import { cva, type VariantProps } from "class-variance-authority";
import { cn } from "@/lib/cn";
 
export const avatarVariants = cva(
"relative inline-flex shrink-0 items-center justify-center select-none border border-line bg-surface-2 text-fg font-mono-num font-medium",
{
variants: {
size: {
xs: "size-6 text-[10px]",
sm: "size-8 text-[11px]",
md: "size-10 text-xs",
lg: "size-12 text-sm",
xl: "size-14 text-base",
},
shape: {
circle: "rounded-pill",
rounded: "rounded-well",
},
tone: {
surface: "bg-surface text-fg border-line",
well: "bg-well text-fg-secondary border-line-well",
accent: "bg-accent text-accent-fg border-accent",
},
},
defaultVariants: {
size: "md",
shape: "circle",
tone: "surface",
},
}
);
 
export const avatarStatusVariants = cva(
"absolute bottom-0 right-0 block rounded-pill ring-2 ring-surface",
{
variants: {
status: {
online: "bg-success",
busy: "bg-danger",
away: "bg-warning",
offline: "bg-fg-muted",
},
size: {
xs: "size-1.5",
sm: "size-2",
md: "size-2.5",
lg: "size-3",
xl: "size-3.5",
},
},
defaultVariants: {
status: "online",
size: "md",
},
}
);
 
export interface AvatarProps
extends Omit<React.ComponentPropsWithRef<typeof BaseAvatar.Root>, "className">,
VariantProps<typeof avatarVariants> {
className?: string;
}
 
export function Avatar({
className,
size = "md",
shape = "circle",
tone = "surface",
...props
}: AvatarProps) {
return (
<BaseAvatar.Root
className={cn(avatarVariants({ size, shape, tone }), className)}
{...props}
/>
);
}
 
export interface AvatarImageProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseAvatar.Image>,
"className"
> {
className?: string;
}
 
export function AvatarImage({ className, ...props }: AvatarImageProps) {
return (
<BaseAvatar.Image
className={cn(
"size-full rounded-[inherit] object-cover",
className
)}
{...props}
/>
);
}
 
export interface AvatarFallbackProps
extends Omit<
React.ComponentPropsWithRef<typeof BaseAvatar.Fallback>,
"className"
> {
className?: string;
}
 
export function AvatarFallback({ className, ...props }: AvatarFallbackProps) {
return (
<BaseAvatar.Fallback
className={cn(
"flex size-full items-center justify-center rounded-[inherit] uppercase tracking-tight",
className
)}
{...props}
/>
);
}
 
export interface AvatarStatusProps
extends React.ComponentPropsWithRef<"span">,
VariantProps<typeof avatarStatusVariants> {}
 
export function AvatarStatus({
className,
status = "online",
size = "md",
...props
}: AvatarStatusProps) {
return (
<span
role="img"
aria-label={`状态:${status}`}
className={cn(avatarStatusVariants({ status, size }), className)}
{...props}
/>
);
}