Skip to content

Commit 0c9df81

Browse files
authored
feat(DiffViewer): one collapsible prop configures the file disclosure, as on Banner (#13699)
DiffViewer's file disclosure takes one named feature prop, `collapsible?: boolean | CollapsibleConfig`, in place of `isCollapsible`, `isCollapsed`, `defaultIsCollapsed` and `onCollapsedChange`. The config is the shared one Banner and Collapsible take (`defaultIsOpen`, `isOpen`, `onOpenChange`), and the shared `useCollapsible` hook holds the state. Omitted or `true` keeps today's default: collapsible, starting open. No behavior or visual change.
1 parent 9b0a228 commit 0c9df81

5 files changed

Lines changed: 62 additions & 68 deletions

File tree

‎apps/sandbox/src/app/(fullscreen)/pages/diffviewer-perf/page.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ const BenchmarkFile = memo(function BenchmarkFile({
190190
file={file}
191191
viewMode={viewMode}
192192
language={language}
193-
isCollapsed={isCollapsed}
194-
onCollapsedChange={next => onToggle(path, next)}
193+
collapsible={{
194+
isOpen: !isCollapsed,
195+
onOpenChange: isOpen => onToggle(path, !isOpen),
196+
}}
195197
/>
196198
);
197199
});

‎apps/storybook/stories/DiffViewer.stories.tsx‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,18 @@ export const LargeChange: Story = {
285285
export const ViewedToggle: Story = {
286286
render: args => {
287287
const [isViewed, setIsViewed] = useState(false);
288-
const [isCollapsed, setIsCollapsed] = useState(false);
288+
const [isOpen, setIsOpen] = useState(true);
289289
return (
290290
<DiffViewer
291291
{...args}
292-
isCollapsed={isCollapsed}
293-
onCollapsedChange={setIsCollapsed}
292+
collapsible={{isOpen, onOpenChange: setIsOpen}}
294293
headerEnd={
295294
<CheckboxInput
296295
label="Viewed"
297296
value={isViewed}
298297
onChange={checked => {
299298
setIsViewed(checked);
300-
setIsCollapsed(checked);
299+
setIsOpen(!checked);
301300
}}
302301
/>
303302
}

‎packages/lab/src/DiffViewer/DiffViewer.doc.mjs‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const docs = {
4545
{
4646
guidance: true,
4747
description:
48-
'Put file-level actions such as a "Viewed" checkbox in headerEnd, and drive isCollapsed from the same state if viewed files should fold away.',
48+
'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.',
4949
},
5050
{
5151
guidance: false,
@@ -157,29 +157,12 @@ export const docs = {
157157
default: 'true',
158158
},
159159
{
160-
name: 'isCollapsible',
161-
type: 'boolean',
160+
name: 'collapsible',
161+
type: 'boolean | {defaultIsOpen?: boolean; isOpen?: boolean; onOpenChange?: (isOpen: boolean) => void}',
162162
description:
163-
'Make the file header a disclosure that hides and shows the hunks. Applies when the file has a path to name the control.',
163+
'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.',
164164
default: 'true',
165165
},
166-
{
167-
name: 'isCollapsed',
168-
type: 'boolean',
169-
description: 'Controlled collapsed state. Pair with onCollapsedChange.',
170-
},
171-
{
172-
name: 'defaultIsCollapsed',
173-
type: 'boolean',
174-
description: 'Initial collapsed state when uncontrolled.',
175-
default: 'false',
176-
},
177-
{
178-
name: 'onCollapsedChange',
179-
type: '(isCollapsed: boolean) => void',
180-
description:
181-
'Called with the next collapsed state when the header is toggled.',
182-
},
183166
{
184167
name: 'headerEnd',
185168
type: 'ReactNode',

‎packages/lab/src/DiffViewer/DiffViewer.test.tsx‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ describe('DiffViewer', () => {
198198

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

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

216216
await user.click(toggle);
217-
expect(onCollapsedChange).toHaveBeenLastCalledWith(false);
217+
expect(onOpenChange).toHaveBeenLastCalledWith(true);
218+
expect(container.querySelector('table')).not.toBeNull();
219+
});
220+
221+
it('starts collapsed with defaultIsOpen false', async () => {
222+
const user = userEvent.setup();
223+
const {container} = render(
224+
<DiffViewer file={FILE} collapsible={{defaultIsOpen: false}} />,
225+
);
226+
const toggle = screen.getByRole('button', {name: 'src/greet.ts'});
227+
expect(toggle).toHaveAttribute('aria-expanded', 'false');
228+
expect(container.querySelector('table')).toBeNull();
229+
230+
await user.click(toggle);
231+
expect(toggle).toHaveAttribute('aria-expanded', 'true');
218232
expect(container.querySelector('table')).not.toBeNull();
219233
});
220234

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

248-
it('follows a controlled collapsed state', async () => {
262+
it('follows a controlled open state', async () => {
249263
const user = userEvent.setup();
250-
const onCollapsedChange = vi.fn();
264+
const onOpenChange = vi.fn();
251265
const {container} = render(
252-
<DiffViewer
253-
file={FILE}
254-
isCollapsed
255-
onCollapsedChange={onCollapsedChange}
256-
/>,
266+
<DiffViewer file={FILE} collapsible={{isOpen: false, onOpenChange}} />,
257267
);
258268
await user.click(screen.getByRole('button', {name: 'src/greet.ts'}));
259-
expect(onCollapsedChange).toHaveBeenCalledWith(false);
269+
expect(onOpenChange).toHaveBeenCalledWith(true);
260270
// Still collapsed: the caller owns the state.
261271
expect(container.querySelector('table')).toBeNull();
262272
});
263273

264274
it('renders the path as plain text when not collapsible', () => {
265-
render(<DiffViewer file={FILE} isCollapsible={false} />);
275+
const {container} = render(<DiffViewer file={FILE} collapsible={false} />);
266276
expect(
267277
screen.queryByRole('button', {name: 'src/greet.ts'}),
268278
).not.toBeInTheDocument();
269279
expect(screen.getByText('src/greet.ts')).toBeInTheDocument();
280+
expect(container.querySelector('table')).not.toBeNull();
270281
});
271282

272283
it('renders annotation rows under the lines they belong to', () => {

‎packages/lab/src/DiffViewer/DiffViewer.tsx‎

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* @file DiffViewer.tsx
55
* @input Uses React, StyleX, theme tokens, the shared CodeBlock tokenizer and
66
* syntax-theme slots (incl. the diff row slots), useScrollableArea,
7-
* Token, Icon, Button, VisuallyHidden, and the i18n translator
7+
* useCollapsible, Token, Icon, Button, VisuallyHidden, and the i18n
8+
* translator
89
* @output Exports DiffViewer component, DiffViewerProps, DiffViewerViewMode,
910
* DiffViewerChangeIndicator, DiffViewerInlineHighlight
1011
* @position Lab implementation; read-only, syntax-highlighted file diff for
@@ -61,6 +62,10 @@ import {
6162
type SyntaxToken,
6263
type TokenLine,
6364
} from '@astryxdesign/core/CodeBlock';
65+
import {
66+
useCollapsible,
67+
type CollapsibleConfig,
68+
} from '@astryxdesign/core/Collapsible';
6469
import {
6570
useScrollableArea,
6671
type ScrollableElementProps,
@@ -185,20 +190,21 @@ export interface DiffViewerProps extends BaseProps<HTMLDivElement> {
185190
*/
186191
hasChangeBackground?: boolean;
187192
/**
188-
* Make the file header a disclosure that hides and shows the hunks. Needs a
189-
* file path to name the control.
193+
* Whether the file header is a disclosure that hides and shows the hunks.
194+
* Needs a file path to name the control.
195+
*
196+
* - omitted / `true` — collapsible, starts open
197+
* - `{defaultIsOpen: false}` — collapsible, starts collapsed
198+
* - `{isOpen, onOpenChange}` — controlled by the caller
199+
* - `false` — not collapsible: the hunks always show, and the path is
200+
* plain text
201+
*
202+
* Takes the shared `CollapsibleConfig`, so the disclosure is configured
203+
* exactly like `Collapsible`'s and Banner's.
204+
*
190205
* @default true
191206
*/
192-
isCollapsible?: boolean;
193-
/** Controlled collapsed state. Pair with `onCollapsedChange`. */
194-
isCollapsed?: boolean;
195-
/**
196-
* Initial collapsed state when uncontrolled.
197-
* @default false
198-
*/
199-
defaultIsCollapsed?: boolean;
200-
/** Called with the next collapsed state when the header is toggled. */
201-
onCollapsedChange?: (isCollapsed: boolean) => void;
207+
collapsible?: boolean | CollapsibleConfig;
202208
/**
203209
* Content at the end of the file header, e.g. a "Viewed" checkbox or a menu
204210
* of file actions.
@@ -1302,10 +1308,7 @@ export function DiffViewer({
13021308
changeIndicator = 'classic',
13031309
inlineHighlight = 'word',
13041310
hasChangeBackground = true,
1305-
isCollapsible = true,
1306-
isCollapsed: isCollapsedProp,
1307-
defaultIsCollapsed = false,
1308-
onCollapsedChange,
1311+
collapsible = true,
13091312
headerEnd,
13101313
renderLineAnnotation,
13111314
onLineNumberClick,
@@ -1333,17 +1336,13 @@ export function DiffViewer({
13331336
})
13341337
: path;
13351338

1336-
const [uncontrolledCollapsed, setUncontrolledCollapsed] =
1337-
useState(defaultIsCollapsed);
1339+
const {
1340+
isEnabled: isCollapsible,
1341+
isOpen,
1342+
toggle: toggleOpen,
1343+
} = useCollapsible({isCollapsible: collapsible});
13381344
const canCollapse = isCollapsible && displayPath != null;
1339-
const isCollapsed = canCollapse && (isCollapsedProp ?? uncontrolledCollapsed);
1340-
const toggleCollapsed = () => {
1341-
const next = !isCollapsed;
1342-
if (isCollapsedProp === undefined) {
1343-
setUncontrolledCollapsed(next);
1344-
}
1345-
onCollapsedChange?.(next);
1346-
};
1345+
const isCollapsed = canCollapse && !isOpen;
13471346

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

0 commit comments

Comments
 (0)