//
// 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 { Flex } from '../../layout/Flex/Flex.tsx';
import { withLayout, withTheme } from '../../testing/index.ts';
import * as Typography from '../Typography/Typography.tsx';
import * as Splitter from './Splitter.tsx';
type StoryArgs = Pick<
Splitter.RootProps,
'orientation' | 'anchor' | 'mode' | 'resizable' | 'defaultSize' | 'minSize' | 'transition'
>;
const Pane = ({ label }: { label: string }) => (
{label}
);
/** A controlled splitter, as the deck drives it: the size (rem) is the app's state and the seam reports drags. */
const DefaultStory = ({ defaultSize = 12, ...args }: StoryArgs) => {
const [size, setSize] = useState(defaultSize);
return (
{size.toFixed(2)}rem
);
};
const meta = {
title: 'ui/react-ui-core/components/Splitter',
render: DefaultStory,
decorators: [withLayout({ layout: 'column' }), withTheme()],
args: { orientation: 'horizontal', anchor: 'start', mode: 'split', resizable: true, defaultSize: 12, minSize: 6 },
argTypes: {
orientation: { control: 'inline-radio', options: ['horizontal', 'vertical'] },
anchor: { control: 'inline-radio', options: ['start', 'end'] },
mode: { control: 'inline-radio', options: ['start', 'split', 'end'] },
},
parameters: { layout: 'centered' },
} satisfies Meta;
export default meta;
type Story = StoryObj;
export const Default: Story = {};
const part = (canvasElement: HTMLElement, name: string) => {
const element = canvasElement.querySelector(`[data-scope="splitter"][data-part="${name}"]`);
if (!element) {
throw new Error(`No splitter ${name}`);
}
return element;
};
const panelsOf = (canvasElement: HTMLElement) => [
...canvasElement.querySelectorAll('[data-scope="splitter"][data-part="panel"]'),
];
/** The anchored pane holds its rem, the seam takes no width, and the keyboard resize round-trips through the size. */
export const Test: Story = {
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const rem = parseFloat(getComputedStyle(document.documentElement).fontSize);
const root = part(canvasElement, 'root');
const trigger = part(canvasElement, 'resize-trigger');
const panels = panelsOf(canvasElement);
const widths = () => panels.map((panel) => panel.getBoundingClientRect().width);
await waitFor(() => expect(Math.round(widths()[0] / rem)).toBe(12));
await expect(Math.round(widths()[0] + widths()[1])).toBe(Math.round(root.getBoundingClientRect().width));
// A separator between side-by-side panes is a vertical line.
await expect(trigger).toHaveAttribute('role', 'separator');
await expect(trigger).toHaveAttribute('aria-orientation', 'vertical');
await expect(trigger.getBoundingClientRect().width).toBeCloseTo(7, 0);
trigger.focus();
await userEvent.keyboard('{ArrowRight}');
await waitFor(() => expect(canvas.getByTestId('size')).not.toHaveTextContent('12.00rem'));
const reported = () => parseFloat(canvas.getByTestId('size').textContent ?? '');
await waitFor(() => expect(Math.abs(widths()[0] / rem - reported())).toBeLessThan(0.05));
},
};
/** `mode` collapses to one pane; a non-resizable splitter renders no seam. */
export const Collapsed: Story = {
args: { mode: 'start', resizable: false, orientation: 'vertical', transition: 0 },
play: async ({ canvasElement }) => {
const root = part(canvasElement, 'root');
const panels = panelsOf(canvasElement);
await expect(canvasElement.querySelector('[data-part="resize-trigger"]')).toBeNull();
await waitFor(() => expect(panels[1].getBoundingClientRect().height).toBe(0));
// The root's inner height: a host's divider can border the root itself.
await expect(panels[0].getBoundingClientRect().height).toBeCloseTo(root.clientHeight, 0);
},
};