Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ const BenchmarkFile = memo(function BenchmarkFile({
file={file}
viewMode={viewMode}
language={language}
isCollapsed={isCollapsed}
onCollapsedChange={next => onToggle(path, next)}
collapsible={{
isOpen: !isCollapsed,
onOpenChange: isOpen => onToggle(path, !isOpen),
}}
/>
);
});
Expand Down
7 changes: 3 additions & 4 deletions apps/storybook/stories/DiffViewer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,18 @@ export const LargeChange: Story = {
export const ViewedToggle: Story = {
render: args => {
const [isViewed, setIsViewed] = useState(false);
const [isCollapsed, setIsCollapsed] = useState(false);
const [isOpen, setIsOpen] = useState(true);
return (
<DiffViewer
{...args}
isCollapsed={isCollapsed}
onCollapsedChange={setIsCollapsed}
collapsible={{isOpen, onOpenChange: setIsOpen}}
headerEnd={
<CheckboxInput
label="Viewed"
value={isViewed}
onChange={checked => {
setIsViewed(checked);
setIsCollapsed(checked);
setIsOpen(!checked);
}}
/>
}
Expand Down
25 changes: 4 additions & 21 deletions packages/lab/src/DiffViewer/DiffViewer.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const docs = {
{
guidance: true,
description:
'Put file-level actions such as a "Viewed" checkbox in headerEnd, and drive isCollapsed from the same state if viewed files should fold away.',
'Put file-level actions such as a "Viewed" checkbox in headerEnd, and drive collapsible={{isOpen, onOpenChange}} from the same state if viewed files should fold away.',
},
{
guidance: false,
Expand Down Expand Up @@ -157,29 +157,12 @@ export const docs = {
default: 'true',
},
{
name: 'isCollapsible',
type: 'boolean',
name: 'collapsible',
type: 'boolean | {defaultIsOpen?: boolean; isOpen?: boolean; onOpenChange?: (isOpen: boolean) => void}',
description:
'Make the file header a disclosure that hides and shows the hunks. Applies when the file has a path to name the control.',
'Whether the file header is a disclosure that hides and shows the hunks. Applies when the file has a path to name the control. On by default, starting open. `false` opts out: the hunks always show and the path is plain text. `{defaultIsOpen: false}` starts collapsed; `{isOpen, onOpenChange}` is controlled. Takes the same CollapsibleConfig as Collapsible.',
default: 'true',
},
{
name: 'isCollapsed',
type: 'boolean',
description: 'Controlled collapsed state. Pair with onCollapsedChange.',
},
{
name: 'defaultIsCollapsed',
type: 'boolean',
description: 'Initial collapsed state when uncontrolled.',
default: 'false',
},
{
name: 'onCollapsedChange',
type: '(isCollapsed: boolean) => void',
description:
'Called with the next collapsed state when the header is toggled.',
},
{
name: 'headerEnd',
type: 'ReactNode',
Expand Down
37 changes: 24 additions & 13 deletions packages/lab/src/DiffViewer/DiffViewer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ describe('DiffViewer', () => {

it('collapses and expands from the header', async () => {
const user = userEvent.setup();
const onCollapsedChange = vi.fn();
const onOpenChange = vi.fn();
const {container} = render(
<DiffViewer file={FILE} onCollapsedChange={onCollapsedChange} />,
<DiffViewer file={FILE} collapsible={{onOpenChange}} />,
);
const toggle = screen.getByRole('button', {name: 'src/greet.ts'});
expect(toggle).toHaveAttribute('aria-expanded', 'true');
Expand All @@ -209,12 +209,26 @@ describe('DiffViewer', () => {
expect(container.querySelector(`[id="${regionId}"]`)).not.toBeNull();

await user.click(toggle);
expect(onCollapsedChange).toHaveBeenLastCalledWith(true);
expect(onOpenChange).toHaveBeenLastCalledWith(false);
expect(toggle).toHaveAttribute('aria-expanded', 'false');
expect(container.querySelector('table')).toBeNull();

await user.click(toggle);
expect(onCollapsedChange).toHaveBeenLastCalledWith(false);
expect(onOpenChange).toHaveBeenLastCalledWith(true);
expect(container.querySelector('table')).not.toBeNull();
});

it('starts collapsed with defaultIsOpen false', async () => {
const user = userEvent.setup();
const {container} = render(
<DiffViewer file={FILE} collapsible={{defaultIsOpen: false}} />,
);
const toggle = screen.getByRole('button', {name: 'src/greet.ts'});
expect(toggle).toHaveAttribute('aria-expanded', 'false');
expect(container.querySelector('table')).toBeNull();

await user.click(toggle);
expect(toggle).toHaveAttribute('aria-expanded', 'true');
expect(container.querySelector('table')).not.toBeNull();
});

Expand Down Expand Up @@ -245,28 +259,25 @@ describe('DiffViewer', () => {
expect(glyph.getAttribute('class')).not.toBe(expanded.glyph);
});

it('follows a controlled collapsed state', async () => {
it('follows a controlled open state', async () => {
const user = userEvent.setup();
const onCollapsedChange = vi.fn();
const onOpenChange = vi.fn();
const {container} = render(
<DiffViewer
file={FILE}
isCollapsed
onCollapsedChange={onCollapsedChange}
/>,
<DiffViewer file={FILE} collapsible={{isOpen: false, onOpenChange}} />,
);
await user.click(screen.getByRole('button', {name: 'src/greet.ts'}));
expect(onCollapsedChange).toHaveBeenCalledWith(false);
expect(onOpenChange).toHaveBeenCalledWith(true);
// Still collapsed: the caller owns the state.
expect(container.querySelector('table')).toBeNull();
});

it('renders the path as plain text when not collapsible', () => {
render(<DiffViewer file={FILE} isCollapsible={false} />);
const {container} = render(<DiffViewer file={FILE} collapsible={false} />);
expect(
screen.queryByRole('button', {name: 'src/greet.ts'}),
).not.toBeInTheDocument();
expect(screen.getByText('src/greet.ts')).toBeInTheDocument();
expect(container.querySelector('table')).not.toBeNull();
});

it('renders annotation rows under the lines they belong to', () => {
Expand Down
55 changes: 27 additions & 28 deletions packages/lab/src/DiffViewer/DiffViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @file DiffViewer.tsx
* @input Uses React, StyleX, theme tokens, the shared CodeBlock tokenizer and
* syntax-theme slots (incl. the diff row slots), useScrollableArea,
* Token, Icon, Button, VisuallyHidden, and the i18n translator
* useCollapsible, Token, Icon, Button, VisuallyHidden, and the i18n
* translator
* @output Exports DiffViewer component, DiffViewerProps, DiffViewerViewMode,
* DiffViewerChangeIndicator, DiffViewerInlineHighlight
* @position Lab implementation; read-only, syntax-highlighted file diff for
Expand Down Expand Up @@ -61,6 +62,10 @@ import {
type SyntaxToken,
type TokenLine,
} from '@astryxdesign/core/CodeBlock';
import {
useCollapsible,
type CollapsibleConfig,
} from '@astryxdesign/core/Collapsible';
import {
useScrollableArea,
type ScrollableElementProps,
Expand Down Expand Up @@ -185,20 +190,21 @@ export interface DiffViewerProps extends BaseProps<HTMLDivElement> {
*/
hasChangeBackground?: boolean;
/**
* Make the file header a disclosure that hides and shows the hunks. Needs a
* file path to name the control.
* Whether the file header is a disclosure that hides and shows the hunks.
* Needs a file path to name the control.
*
* - omitted / `true` — collapsible, starts open
* - `{defaultIsOpen: false}` — collapsible, starts collapsed
* - `{isOpen, onOpenChange}` — controlled by the caller
* - `false` — not collapsible: the hunks always show, and the path is
* plain text
*
* Takes the shared `CollapsibleConfig`, so the disclosure is configured
* exactly like `Collapsible`'s and Banner's.
*
* @default true
*/
isCollapsible?: boolean;
/** Controlled collapsed state. Pair with `onCollapsedChange`. */
isCollapsed?: boolean;
/**
* Initial collapsed state when uncontrolled.
* @default false
*/
defaultIsCollapsed?: boolean;
/** Called with the next collapsed state when the header is toggled. */
onCollapsedChange?: (isCollapsed: boolean) => void;
collapsible?: boolean | CollapsibleConfig;
/**
* Content at the end of the file header, e.g. a "Viewed" checkbox or a menu
* of file actions.
Expand Down Expand Up @@ -1302,10 +1308,7 @@ export function DiffViewer({
changeIndicator = 'classic',
inlineHighlight = 'word',
hasChangeBackground = true,
isCollapsible = true,
isCollapsed: isCollapsedProp,
defaultIsCollapsed = false,
onCollapsedChange,
collapsible = true,
headerEnd,
renderLineAnnotation,
onLineNumberClick,
Expand Down Expand Up @@ -1333,17 +1336,13 @@ export function DiffViewer({
})
: path;

const [uncontrolledCollapsed, setUncontrolledCollapsed] =
useState(defaultIsCollapsed);
const {
isEnabled: isCollapsible,
isOpen,
toggle: toggleOpen,
} = useCollapsible({isCollapsible: collapsible});
const canCollapse = isCollapsible && displayPath != null;
const isCollapsed = canCollapse && (isCollapsedProp ?? uncontrolledCollapsed);
const toggleCollapsed = () => {
const next = !isCollapsed;
if (isCollapsedProp === undefined) {
setUncontrolledCollapsed(next);
}
onCollapsedChange?.(next);
};
const isCollapsed = canCollapse && !isOpen;

let unfoldedChangedLines = 0;
let hasReplacedBlock = false;
Expand Down Expand Up @@ -2073,7 +2072,7 @@ export function DiffViewer({
type="button"
aria-expanded={!isCollapsed}
aria-controls={bodyId}
onClick={toggleCollapsed}
onClick={toggleOpen}
{...focusOutlineProps.focusVisible(styles.toggle)}>
{/* The collapsed chevron points toward the inline end, so it
mirrors in RTL. The glyph's own transform holds the rotation,
Expand Down
Loading