// // Copyright 2026 DXOS.org // import '../../theme/index.css'; import { type Meta, type StoryObj } from '@storybook/react-vite'; import React from 'react'; import { expect, waitFor, within } from 'storybook/test'; import { SIZES } from '../../sizes.ts'; import { GEOMETRY, sizeRow } from '../../testing/geometry.ts'; import { withLayout, withTheme } from '../../testing/index.ts'; import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx'; import * as Avatar from './Avatar.tsx'; /** Inline SVG, so the stories never fetch from the network. */ const PORTRAIT = `data:image/svg+xml,${encodeURIComponent( ` `, )}`; type StoryArgs = SizeArgs & Pick; /** Initials, an emoji, an icon, an image (one with a dominant-colour backdrop) and a portrait filling its host, in a row with a visible name. */ const DefaultStory = ({ size, variant, status, hue, hueVariant, fallback }: StoryArgs) => (
{fallback}
); const meta = { title: 'ui/react-ui-core/components/Avatar', render: DefaultStory, decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[32rem]' }), withTheme()], args: { size: 'md', variant: 'circle', status: 'current', hue: 'blue', hueVariant: 'fill', fallback: 'Ada Lovelace' }, argTypes: { ...SIZE_ARG_TYPES, variant: { control: 'inline-radio', options: ['circle', 'square'] }, status: { control: 'select', options: [undefined, 'active', 'inactive', 'current', 'internal', 'error', 'warning'], }, hueVariant: { control: 'inline-radio', options: ['fill', 'surface', 'transparent'] }, }, parameters: { layout: 'centered' }, } satisfies Meta; export default meta; type Story = StoryObj; export const Default: Story = {}; /** Each avatar is one block across at every size; the fallback shows initials until an image loads. */ export const Test: Story = { args: { allSizes: true }, play: async ({ canvasElement }) => { for (const size of SIZES) { const row = sizeRow(canvasElement, size); const canvas = within(row); const initials = canvas.getByTestId(`initials-${size}`); const rect = initials.getBoundingClientRect(); await expect(rect.width).toBeCloseTo(GEOMETRY[size].block, 0); await expect(rect.height).toBeCloseTo(GEOMETRY[size].block, 0); // Named by the visible text beside it; the fallback shows the name's initials. await expect(canvas.getByRole('img', { name: 'Ada Lovelace' })).toBe(initials); await expect(initials).toHaveTextContent('AL'); await expect(canvas.getByTestId(`emoji-${size}`)).toHaveTextContent('🦊'); // The hue fills the frame and the status draws a ring. await expect(getComputedStyle(initials).backgroundColor).not.toBe('rgba(0, 0, 0, 0)'); await expect(getComputedStyle(initials, '::after').boxShadow).not.toBe('none'); // Once the image loads the fallback is hidden. const image = canvas.getByTestId(`image-${size}`); await waitFor(() => expect(image.querySelector('[data-part="image"]')).toHaveAttribute('data-state', 'visible')); const fallback = image.querySelector('[data-part="fallback"]'); await waitFor(() => expect(fallback).not.toBeVisible()); // `backdrop='dominant'` fills behind the loaded image with its edge colour in place of the hue. const backdrop = canvas.getByTestId(`backdrop-${size}`); await waitFor(() => expect(backdrop.style.getPropertyValue('--dx-avatar-backdrop')).not.toBe('')); await expect(getComputedStyle(backdrop).backgroundColor).not.toBe(getComputedStyle(initials).backgroundColor); // A filled avatar takes its host's width as a square. const fill = canvas.getByTestId(`fill-${size}`).getBoundingClientRect(); await expect(fill.width).toBeCloseTo(80, 0); await expect(fill.height).toBeCloseTo(80, 0); } }, };