User Card

A person's avatar, name and one line about them

User Card shows a person: an avatar, their name and one line about them, in a Card. It replaces the UserCard package export, which is deprecated and removed in 0.15.0.

Ada Lovelaceada@example.com

Installation

Install the dependencies.

npm install @kushagradhawan/kookie-ui

Copy this file into components/blocks/user-card. Keep the folder names, since blocks import each other by relative path.

components/blocks/user-card/user-card.tsx
import React from 'react';
import { Avatar, Card, Flex, Text } from '@kushagradhawan/kookie-ui';

type UserCardSize = '1' | '2' | '3' | '4';

interface UserCardProps extends Omit<React.ComponentPropsWithoutRef<typeof Card>, 'children' | 'size'> {
  /** The person's name. */
  name: string;
  /** A second line, such as a role or an email. */
  description?: string;
  /** Avatar image URL. */
  src?: string;
  /** Initials or an icon shown when there is no image. */
  fallback: NonNullable<React.ReactNode>;
  /** Card padding, avatar and text size. Default `2`. */
  size?: UserCardSize;
}

// Avatar size, name size, description size for each card size.
const SIZES = {
  '1': ['1', '1', '1'],
  '2': ['2', '2', '1'],
  '3': ['3', '3', '2'],
  '4': ['4', '4', '3'],
} as const;

export const UserCard = React.forwardRef<HTMLDivElement, UserCardProps>(({ name, description, src, fallback, size = '2', ...props }, ref) => {
  const [avatarSize, nameSize, descriptionSize] = SIZES[size];
  return (
    <Card size={size} {...props} ref={ref}>
      <Flex align="center" gap="4">
        <Avatar size={avatarSize} src={src} fallback={fallback} />
        <Flex direction="column" minWidth="0">
          <Text size={nameSize} weight="medium" truncate>
            {name}
          </Text>
          {description ? (
            <Text size={descriptionSize} emphasis="medium" truncate>
              {description}
            </Text>
          ) : null}
        </Flex>
      </Flex>
    </Card>
  );
});
UserCard.displayName = 'UserCard';

Usage

import { UserCard } from '@/components/blocks/user-card/user-card';
 
<UserCard name="Ada Lovelace" description="ada@example.com" fallback="AL" />

Props

PropTypeDescription
namestringThe person's name
descriptionstringA second line, such as a role or an email
srcstringAvatar image URL
fallbackReactNodeShown when there is no image
size'1' | '2' | '3' | '4'Card padding, avatar and text size. Default '2'

Every other prop goes to the Card, so variant and asChild work as they do on Card.