|
4 | 4 | * @file DiffViewer.tsx |
5 | 5 | * @input Uses React, StyleX, theme tokens, the shared CodeBlock tokenizer and |
6 | 6 | * 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 |
8 | 9 | * @output Exports DiffViewer component, DiffViewerProps, DiffViewerViewMode, |
9 | 10 | * DiffViewerChangeIndicator, DiffViewerInlineHighlight |
10 | 11 | * @position Lab implementation; read-only, syntax-highlighted file diff for |
@@ -61,6 +62,10 @@ import { |
61 | 62 | type SyntaxToken, |
62 | 63 | type TokenLine, |
63 | 64 | } from '@astryxdesign/core/CodeBlock'; |
| 65 | +import { |
| 66 | + useCollapsible, |
| 67 | + type CollapsibleConfig, |
| 68 | +} from '@astryxdesign/core/Collapsible'; |
64 | 69 | import { |
65 | 70 | useScrollableArea, |
66 | 71 | type ScrollableElementProps, |
@@ -185,20 +190,21 @@ export interface DiffViewerProps extends BaseProps<HTMLDivElement> { |
185 | 190 | */ |
186 | 191 | hasChangeBackground?: boolean; |
187 | 192 | /** |
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 | + * |
190 | 205 | * @default true |
191 | 206 | */ |
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; |
202 | 208 | /** |
203 | 209 | * Content at the end of the file header, e.g. a "Viewed" checkbox or a menu |
204 | 210 | * of file actions. |
@@ -1302,10 +1308,7 @@ export function DiffViewer({ |
1302 | 1308 | changeIndicator = 'classic', |
1303 | 1309 | inlineHighlight = 'word', |
1304 | 1310 | hasChangeBackground = true, |
1305 | | - isCollapsible = true, |
1306 | | - isCollapsed: isCollapsedProp, |
1307 | | - defaultIsCollapsed = false, |
1308 | | - onCollapsedChange, |
| 1311 | + collapsible = true, |
1309 | 1312 | headerEnd, |
1310 | 1313 | renderLineAnnotation, |
1311 | 1314 | onLineNumberClick, |
@@ -1333,17 +1336,13 @@ export function DiffViewer({ |
1333 | 1336 | }) |
1334 | 1337 | : path; |
1335 | 1338 |
|
1336 | | - const [uncontrolledCollapsed, setUncontrolledCollapsed] = |
1337 | | - useState(defaultIsCollapsed); |
| 1339 | + const { |
| 1340 | + isEnabled: isCollapsible, |
| 1341 | + isOpen, |
| 1342 | + toggle: toggleOpen, |
| 1343 | + } = useCollapsible({isCollapsible: collapsible}); |
1338 | 1344 | 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; |
1347 | 1346 |
|
1348 | 1347 | let unfoldedChangedLines = 0; |
1349 | 1348 | let hasReplacedBlock = false; |
@@ -2073,7 +2072,7 @@ export function DiffViewer({ |
2073 | 2072 | type="button" |
2074 | 2073 | aria-expanded={!isCollapsed} |
2075 | 2074 | aria-controls={bodyId} |
2076 | | - onClick={toggleCollapsed} |
| 2075 | + onClick={toggleOpen} |
2077 | 2076 | {...focusOutlineProps.focusVisible(styles.toggle)}> |
2078 | 2077 | {/* The collapsed chevron points toward the inline end, so it |
2079 | 2078 | mirrors in RTL. The glyph's own transform holds the rotation, |
|
0 commit comments