// // 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 { hues } from '@dxos/ui-types'; import { translations } from '#translations'; import { type Size, SIZES } from '../../sizes.ts'; import { byTestId, centreY, expectScoped, sizeRow } from '../../testing/geometry.ts'; import { withLayout, withTheme } from '../../testing/index.ts'; import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx'; import { Button } from '../Button/Button.tsx'; import { Container } from '../Container/Container.tsx'; import { Group } from '../Group/Group.tsx'; import * as Tag from './Tag.tsx'; /** Label text (one step below the body) per size, in px. */ const LABEL_FONT: Record = { xs: 12, sm: 12, md: 14, lg: 16, xl: 18 }; const VALENCES: Tag.TagHue[] = ['neutral', 'info', 'success', 'warning', 'error']; /** Clickable, deletable, and clickable and deletable tags, counting their clicks and restoring deleted ones. */ const InteractiveTags = ({ size }: { size?: Size }) => { const [clicks, setClicks] = useState(0); const [deleted, setDeleted] = useState([]); const remove = (name: string) => () => setDeleted((current) => [...current, name]); const shown = (name: string) => !deleted.includes(name); return ( setClicks((count) => count + 1)} data-testid={`clickable-${size}`}> Filter {shown('design') && ( Design )} {shown('bug') && ( setClicks((count) => count + 1)} onDelete={remove('bug')} data-testid={`both-${size}`} > Bug )} {clicks} ); }; /** A row of tags centred in a block row, clickable and deletable tags, then every valence and hue. */ const DefaultStory = ({ size }: SizeArgs) => ( <> Release Draft {[...VALENCES, ...hues].map((hue) => ( {hue} ))} ); const meta = { title: 'ui/react-ui-core/components/Tag', render: DefaultStory, decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[48rem]' }), withTheme()], args: { size: 'md' }, argTypes: SIZE_ARG_TYPES, parameters: { layout: 'centered', translations }, } satisfies Meta; export default meta; type Story = StoryObj; export const Default: Story = {}; /** * A tag is its label's line box tall, with no block inset, centred in its row, in the size's label text; each hue maps * to ui-theme's surface/fg tokens, and valences share the current Tag's hues (error is rose). A plain tag is a span; an * `onClick` tag is a button (click and Enter); an `onDelete` tag has a trailing "Remove " button that fits the * pill without changing its height and fires only `onDelete`; a clickable tag also deletes on Backspace; no button * nests in another. */ export const Test: Story = { args: { allSizes: true }, play: async ({ canvasElement }) => { for (const size of SIZES) { const tag = byTestId(canvasElement, `tag-${size}`); const row = byTestId(canvasElement, `row-${size}`); const rect = tag.getBoundingClientRect(); await expect(rect.height, size).toBeCloseTo(parseFloat(getComputedStyle(tag).lineHeight), 0); await expect(centreY(rect), size).toBeCloseTo(centreY(row.getBoundingClientRect()), 0); await expect(parseFloat(getComputedStyle(tag).fontSize), size).toBe(LABEL_FONT[size]); } await expectScoped(canvasElement); const canvas = within(sizeRow(canvasElement, 'md')); const background = (hue: string) => getComputedStyle(byTestId(canvasElement, `hue-${hue}-md`)).backgroundColor; const colours = new Set(hues.map(background)); await expect(colours.size).toBe(hues.length); await expect(background('error')).toBe(background('rose')); await expect(background('warning')).toBe(background('amber')); await expect(background('success')).toBe(background('emerald')); await expect(background('info')).toBe(background('cyan')); await expect(background('neutral')).not.toBe(background('red')); await expect(canvas.getByText('red')).toHaveAttribute('data-hue', 'red'); // Interactive tags keep the plain tag's height; the × fits inside the pill. for (const size of SIZES) { const plain = byTestId(canvasElement, `tag-${size}`).getBoundingClientRect(); for (const testId of [`clickable-${size}`, `deletable-${size}`, `both-${size}`]) { const tag = byTestId(canvasElement, testId); const rect = tag.getBoundingClientRect(); await expect(rect.height, testId).toBeCloseTo(plain.height, 0); const cross = tag.querySelector('[data-part="delete-trigger"]')?.getBoundingClientRect(); if (cross) { await expect(cross.top, `${testId} ×`).toBeGreaterThanOrEqual(rect.top - 0.5); await expect(cross.bottom).toBeLessThanOrEqual(rect.bottom + 0.5); await expect(cross.right).toBeLessThanOrEqual(rect.right + 0.5); } } } await expect(canvasElement.querySelector('button button')).toBeNull(); await expect(byTestId(canvasElement, 'tag-md').tagName).toBe('SPAN'); const clicks = byTestId(canvasElement, 'clicks-md'); const clickable = canvas.getByRole('button', { name: 'Filter' }); await expect(clickable).toBe(byTestId(canvasElement, 'clickable-md')); await userEvent.click(clickable); await waitFor(() => expect(clicks).toHaveTextContent('1')); clickable.focus(); await userEvent.keyboard('{Enter}'); await waitFor(() => expect(clicks).toHaveTextContent('2')); // The × fires onDelete only. await userEvent.click(canvas.getByRole('button', { name: 'Remove Design' })); await waitFor(() => expect(canvas.queryByTestId('deletable-md')).toBeNull()); await expect(clicks).toHaveTextContent('2'); // Clicking and deleting a clickable, deletable tag: its text is one button, its × a sibling. const bug = canvas.getByRole('button', { name: 'Bug' }); await expect(bug.closest('[data-part="root"]')).toBe(byTestId(canvasElement, 'both-md')); await userEvent.click(bug); await waitFor(() => expect(clicks).toHaveTextContent('3')); await userEvent.click(canvas.getByRole('button', { name: 'Remove Bug' })); await waitFor(() => expect(canvas.queryByTestId('both-md')).toBeNull()); await expect(clicks).toHaveTextContent('3'); // Backspace on the focused tag deletes it. await userEvent.click(byTestId(canvasElement, 'reset-md')); const restored = await canvas.findByRole('button', { name: 'Bug' }); restored.focus(); await userEvent.keyboard('{Backspace}'); await waitFor(() => expect(canvas.queryByTestId('both-md')).toBeNull()); await expect(clicks).toHaveTextContent('3'); }, };