// // Copyright 2026 DXOS.org // import '../../theme/index.css'; import { type Meta, type StoryObj } from '@storybook/react-vite'; import React from 'react'; import { expect, waitFor } from 'storybook/test'; import { random } from '@dxos/random'; import { byTestId, expectScoped, realHover } from '../../testing/geometry.ts'; import { withLayout, withTheme } from '../../testing/index.ts'; import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx'; import { Block } from '../Block/Block.tsx'; import { Container } from '../Container/Container.tsx'; import * as Icon from '../Icon/Icon.tsx'; import { Input } from '../Input/Input.tsx'; import * as Tag from '../Tag/Tag.tsx'; import * as Typography from '../Typography/Typography.tsx'; import * as ScrollArea from './ScrollArea.tsx'; random.seed(123); const PARAGRAPHS = Array.from({ length: 12 }, () => random.lorem.paragraph()); const Header = ({ testId, children }: { testId: string; children: string }) => (
{children}
); type PaneProps = Pick & { prefix: string }; /** A header over a scroll pane whose rows share its rails; `prefix` keeps test ids unique. */ const Pane = ({ prefix, mode, width, native }: PaneProps) => (
Header
{PARAGRAPHS.map((paragraph, index) => ( {paragraph} ))}
); type StoryArgs = SizeArgs & Pick; const TAGS = Array.from({ length: 16 }, (_, index) => `Tag ${index + 1}`); type StripProps = Pick & { prefix: string }; /** A horizontal strip of Tags wider than its pane. */ const Strip = ({ prefix, ...props }: StripProps) => (
{TAGS.map((tag) => ( {tag} ))}
); /** * An overlay (or `mode`/`width`) pane beside a native one, then a horizontal strip that snaps and hides its thumb * until hovered, and one with no scrollbar at all. */ const DefaultStory = ({ size, mode, width }: StoryArgs) => ( <>
); const meta = { title: 'ui/react-ui-core/components/ScrollArea', render: DefaultStory, decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[56rem]' }), withTheme()], args: { size: 'md' }, argTypes: SIZE_ARG_TYPES, parameters: { layout: 'centered' }, } satisfies Meta; export default meta; type Story = StoryObj; const rect = (root: HTMLElement, testId: string) => byTestId(root, testId).getBoundingClientRect(); /** Rows in the scroll pane share the header's rails, and the content track ends where the header's does. */ const assertAligned = async (root: HTMLElement, prefix: string, { railEnd = true } = {}) => { const at = (testId: string) => rect(root, `${prefix}-${testId}`); await expect(at('row-rail-start').left).toBeCloseTo(at('header-rail-start').left, 0); if (railEnd) { await expect(at('row-rail-end').right).toBeCloseTo(at('header-rail-end').right, 0); await expect(at('paragraph').right).toBeCloseTo(at('header-content').right, 0); } await expect(at('paragraph').left).toBeCloseTo(at('header-content').left, 0); const viewport = byTestId(root, `${prefix}-viewport`); await expect(viewport.scrollHeight).toBeGreaterThan(viewport.clientHeight); }; export const Default: Story = {}; /** * Overlay thumbs sit in the end gutter, so the content track is not narrowed, and the thumb follows the scroll; a * native bar reserves its width out of the end track, so rail-end Blocks cannot also align (finding 4). A horizontal * pane scrolls sideways only, snaps when asked, and its `autoHide` thumb runs along the bottom, showing only while the * pointer is over it; `scrollbars={false}` shows no bar. */ export const Test: Story = { args: { allSizes: true }, play: async ({ canvasElement }) => { await assertAligned(canvasElement, 'overlay-md'); const viewport = byTestId(canvasElement, 'overlay-md-viewport'); const root = byTestId(canvasElement, 'overlay-md-root'); await expect(viewport).toHaveClass('dx-scroll-viewport'); await expect(root).toHaveAttribute('data-mode', 'overlay'); await expect(getComputedStyle(viewport).scrollbarWidth).toBe('none'); const thumb = await waitFor(() => { const element = root.querySelector(':scope > .absolute'); if (!element) { throw new Error('missing thumb'); } return element; }); const top = thumb.getBoundingClientRect().top; viewport.scrollTop = viewport.scrollHeight; await waitFor(() => expect(thumb.getBoundingClientRect().top).toBeGreaterThan(top)); await expect(thumb.getBoundingClientRect().right).toBeCloseTo(root.getBoundingClientRect().right, 0); await expectScoped(canvasElement); await assertAligned(canvasElement, 'native-md', { railEnd: false }); await expect(byTestId(canvasElement, 'native-md-root')).toHaveAttribute('data-mode', 'reserve'); await expect(byTestId(canvasElement, 'native-md-viewport')).toHaveAttribute('data-native'); await expect(byTestId(canvasElement, 'native-md-root').querySelector(':scope > .absolute')).toBeNull(); const strip = byTestId(canvasElement, 'strip-md-root'); const stripViewport = byTestId(canvasElement, 'strip-md-viewport'); await expect(stripViewport.scrollWidth).toBeGreaterThan(stripViewport.clientWidth); await expect(getComputedStyle(stripViewport).overflowY).toBe('hidden'); await expect(getComputedStyle(stripViewport).scrollSnapType).toBe('x mandatory'); // Contained only along its own axis, so a vertical swipe over the strip scrolls whatever encloses it. await expect(getComputedStyle(stripViewport).overscrollBehaviorX).toBe('contain'); await expect(getComputedStyle(stripViewport).overscrollBehaviorY).toBe('auto'); await expect(getComputedStyle(viewport).overscrollBehaviorY).toBe('contain'); const stripThumb = await waitFor(() => { const element = strip.querySelector(':scope > .absolute'); if (!element) { throw new Error('missing horizontal thumb'); } return element; }); // Along the bottom edge, inset by the thumb's padding. const stripGap = strip.getBoundingClientRect().bottom - stripThumb.getBoundingClientRect().bottom; await expect(stripGap >= 0 && stripGap <= 4, `thumb gap ${stripGap}`).toBe(true); await expect(stripThumb.getBoundingClientRect().width).toBeLessThan(strip.getBoundingClientRect().width); await expect(getComputedStyle(stripThumb).opacity).toBe('0'); await realHover(strip); await waitFor(() => expect(getComputedStyle(stripThumb).opacity).toBe('1')); const bare = byTestId(canvasElement, 'bare-md-root'); await expect(bare.querySelector(':scope > .absolute')).toBeNull(); await expect(getComputedStyle(byTestId(canvasElement, 'bare-md-viewport')).scrollbarWidth).toBe('none'); }, };