// // Copyright 2026 DXOS.org // import '../../theme/index.css'; import { type Meta, type StoryObj } from '@storybook/react-vite'; import React, { useState } from 'react'; import { expect, userEvent, waitFor, within } from 'storybook/test'; import { 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 Typography from '../Typography/Typography.tsx'; import { Image } from './Image.tsx'; /** Inline SVG, so the stories never fetch from the network. */ const LANDSCAPE = `data:image/svg+xml,${encodeURIComponent( ` `, )}`; /** A wide red strip with a centred blue mark, so a square `contain` frame letterboxes it in its red corners. */ const STRIP = `data:image/svg+xml,${encodeURIComponent( ` `, )}`; /** Transparent at its edges (a cut-out), so no dominant colour is sampled. */ const CUTOUT = `data:image/svg+xml,${encodeURIComponent( ` `, )}`; /** A malformed data URI fails to decode without any network request. */ const BROKEN = 'data:image/png;base64,AAAA'; /** Cover, contain, broken and square frames, a clickable image, and `backdrop='dominant'` letterboxes. */ const DefaultStory = ({ size }: SizeArgs) => { const [clicks, setClicks] = useState(0); return (
Mountains at dusk Mountains, contained Missing photo Mountains, square Open mountains setClicks((count) => count + 1)} data-testid={`clickable-${size}`} /> Opened {clicks} Red strip Cut-out
); }; const meta = { title: 'ui/react-ui-core/components/Image', render: DefaultStory, decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[40rem]' }), withTheme()], args: { size: 'md' }, argTypes: SIZE_ARG_TYPES, parameters: { layout: 'centered' }, } satisfies Meta; export default meta; type Story = StoryObj; export const Default: Story = {}; /** * Frames keep their ratio, loaded images fill them, and a broken source shows the fallback icon. With `onClick` the * frame is a button named by its `alt`, activated by click, Enter and Space, with a focus ring. `backdrop='dominant'` * fills the letterbox with the image's corner colour, slightly darkened; an image transparent at its edges leaves the * host surface. */ export const Test: Story = { args: { allSizes: true }, play: async ({ canvasElement }) => { const canvas = within(sizeRow(canvasElement, 'md')); for (const [testId, ratio] of [ ['cover-md', 16 / 9], ['contain-md', 1], ['square-md', 1], ] as const) { const frame = canvas.getByTestId(testId); await waitFor(() => expect(frame).toHaveAttribute('data-status', 'loaded')); const box = frame.getBoundingClientRect(); await expect(box.width / box.height, testId).toBeCloseTo(ratio, 1); const img = within(frame).getByRole('img').getBoundingClientRect(); await expect(img.width).toBeCloseTo(box.width, 0); await expect(img.height).toBeCloseTo(box.height, 0); } await expect(canvas.getByRole('img', { name: 'Mountains at dusk' })).toHaveAttribute('loading', 'lazy'); const broken = canvas.getByTestId('broken-md'); await waitFor(() => expect(broken).toHaveAttribute('data-status', 'error')); const icon = within(broken).getByRole('img', { name: 'Missing photo' }); await expect(icon.tagName.toLowerCase()).toBe('svg'); // The icon is centred in the frame. const frame = broken.getBoundingClientRect(); const glyph = icon.getBoundingClientRect(); await expect(glyph.left + glyph.width / 2).toBeCloseTo(frame.left + frame.width / 2, 0); await expect(glyph.top + glyph.height / 2).toBeCloseTo(frame.top + frame.height / 2, 0); const clickable = canvas.getByRole('button', { name: 'Open mountains' }); await expect(clickable).toBe(canvas.getByTestId('clickable-md')); await userEvent.click(clickable); await waitFor(() => expect(canvas.getByTestId('clicks-md')).toHaveTextContent('Opened 1')); await userEvent.keyboard('{Enter}'); await waitFor(() => expect(canvas.getByTestId('clicks-md')).toHaveTextContent('Opened 2')); await userEvent.keyboard(' '); await waitFor(() => expect(canvas.getByTestId('clicks-md')).toHaveTextContent('Opened 3')); await userEvent.tab({ shift: true }); await userEvent.tab(); await expect(clickable).toHaveFocus(); await expect(getComputedStyle(clickable).outlineStyle).toBe('solid'); await expect(canvas.getByTestId('cover-md')).not.toHaveAttribute('role'); const backdrop = canvas.getByTestId('backdrop-md'); await waitFor(() => expect(backdrop).toHaveAttribute('data-status', 'loaded')); await waitFor(() => expect(getComputedStyle(backdrop).backgroundColor).toBe('rgb(242, 0, 0)')); const cutout = canvas.getByTestId('cutout-md'); await waitFor(() => expect(cutout).toHaveAttribute('data-status', 'loaded')); await expect(cutout.style.getPropertyValue('--dx-image-backdrop')).toBe(''); await expect(getComputedStyle(cutout).backgroundColor).toBe('rgba(0, 0, 0, 0)'); }, };