Skip to content

Add expandAll/collapseAll to diffs#349

Open
atamarzban wants to merge 3 commits intopierrecomputer:mainfrom
atamarzban:ata/diffs-expand-collapse-all
Open

Add expandAll/collapseAll to diffs#349
atamarzban wants to merge 3 commits intopierrecomputer:mainfrom
atamarzban:ata/diffs-expand-collapse-all

Conversation

@atamarzban
Copy link

Summary

  • Adds programmatic expandAll() and collapseAll() methods to FileDiff, VirtualizedFileDiff, and the React useFileDiffInstance hook for controlling collapsed "N unmodified lines" sections
  • Adds a text toggle button ("Expand all" / "Collapse all") in the file header next to the filename — visible by default, opt-out via disableExpandAllButton: true
  • Button is automatically hidden when expandUnchanged or disableFileHeader is set

API

Vanilla JS:

const diff = new FileDiff(options);
diff.expandAll();   // expand all collapsed sections
diff.collapseAll(); // collapse everything back

React:

const { ref, expandAll, collapseAll } = useFileDiffInstance(options);

Disable the built-in button:

const diff = new FileDiff({ disableExpandAllButton: true, ...rest });

🤖 Generated with Claude Code

atamarzban and others added 3 commits February 24, 2026 16:37
Adds an `allExpanded` flag to the renderer that, when set, passes
`expandedHunks: true` to `iterateOverDiff` — expanding all collapsed
"N unmodified lines" sections without mutating the per-hunk map.

New methods: `expandAll()`, `collapseAll()`, `isAllExpanded()`.
State is reset on `cleanUp()` and `recycle()`.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds public `expandAll()` and `collapseAll()` methods to `FileDiff`,
`VirtualizedFileDiff`, and the React `useFileDiffInstance` hook.

Also adds a text toggle button ("Expand all" / "Collapse all") in the
file header, next to the filename. The button is visible by default and
can be disabled with `disableExpandAllButton: true`. It is automatically
hidden when `expandUnchanged` or `disableFileHeader` is set.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Adds 7 unit tests covering expand, collapse, split mode, round-trip,
and equivalence with the `expandUnchanged` option.

Updates existing snapshots to reflect the new expand-all-button slot
in the file header HAST.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 24, 2026

@atamarzban is attempting to deploy a commit to the Pierre Computer Company Team on Vercel.

A member of the Team first needs to authorize it.

@atamarzban
Copy link
Author

@amadeus Would appreciate a review! 🙏

@amadeus
Copy link
Member

amadeus commented Feb 25, 2026

Question on this -- shouldn't you be able to accomplish this functionality already with toggling the expandUnchanged prop? Or is there something about that API that doesn't work that I'm not seeing.

It's not the cleanest, but right now in the VanillaJS api you could accomplish it with:

const diff = new FileDiff(options);
diff.setOptions({ ...diff.options, expandUnchanged: true })
diff.setOptions({ ...diff.options, expandUnchanged: false })

// and in react, you would pass in the prop change to options:

<FileDiff
  // ...
  options={{ expandUnchanged: true }}
/>
<FileDiff
  // ...
  options={{ expandUnchanged: false }}
/>

@amadeus
Copy link
Member

amadeus commented Feb 25, 2026

Also, sorry for the delays on this, been traveling a bunch lately so catching up when i can.

@atamarzban
Copy link
Author

@amadeus thx for the taking a look! setOptions with expandUnchanged does cover the basic expand/collapse toggle. The main thing this PR adds is the built-in header button (next to the filename, similar to GitHub). Since it lives inside the shadow DOM, it’s not something consumers can easily wire up themselves, so having it built-in felt worthwhile.

There’s also a subtle behavioral difference: collapseAll() clears individually-expanded hunks too, so if someone manually expanded a few sections and then hits “Collapse all”, they get a clean reset. Toggling expandUnchanged back to false would leave those individual expansions intact.

That said, the button doesn’t strictly need the new imperative API. It could toggle expandUnchanged internally. The separate methods just make the wiring cleaner. Happy to simplify if you’d prefer a different approach.

@mdo mdo added the diffs label Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants