Skip to content

Add auto-collapsed React code blocks for large code snippets. #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2025

Conversation

rezrah
Copy link
Collaborator

@rezrah rezrah commented Jun 25, 2025

Towards primer/brand#1062

Adds auto collapsed React code block pane similar to primer.style/product (with some minor differences).

  • Only enabled on jsx live code fences
  • Auto-collapses code blocks that exceed a certain height
  • Visual parity with product counterpart
  • Simpler implementation, which doesn't require a separate previewCode and fullCode to determine what shows in collapsed and original states respectively.

🔗 preview

Screen.Recording.2025-06-25.at.12.06.41.mov
Screenshot 2025-06-25 at 11 37 23 Screenshot 2025-06-25 at 11 37 16

@Copilot Copilot AI review requested due to automatic review settings June 25, 2025 11:11
@rezrah rezrah requested a review from a team as a code owner June 25, 2025 11:11
@rezrah rezrah requested a review from danielguillan June 25, 2025 11:11
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds automatic collapsing for large live JSX/TSX code blocks in the ReactCodeBlock component, matching Primer’s product style.

  • Introduces height measurement, collapse/expand state, and a toggle button with smooth scrolling on collapse.
  • Updates CSS for collapsed state styling and collapse button appearance.
  • Renames the demo section and marks the example as jsx live in the MD file.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
packages/theme/components/layout/code-block/ReactCodeBlock.tsx Added refs, effects, unique IDs, and collapse/expand logic
packages/theme/components/layout/code-block/ReactCodeBlock.module.css Styled the collapsed editor, wrapper, and collapse button
packages/site/content/content-example/tabbed-component/react.mdx Renamed section and updated code fence to jsx live
.changeset/bright-parrots-smash.md Added changeset entry for the auto-collapse feature
Comments suppressed due to low confidence (2)

packages/theme/components/layout/code-block/ReactCodeBlock.tsx:50

  • There are no unit tests covering the collapse/expand logic; adding tests to simulate editor heights above and below COLLAPSE_HEIGHT would help ensure this behavior remains stable.
  }, [code, isCodePaneCollapsed])

packages/site/content/content-example/tabbed-component/react.mdx:98

  • [nitpick] The example uses multiple identical <Animate> blocks to simulate a long code snippet; consider consolidating them within a single code fence or clarifying intention to avoid confusion.
  <Animate animate="fade-in">

Copy link
Contributor

@danielguillan danielguillan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! 🚀

@rezrah rezrah merged commit 4c76c4f into main Jun 25, 2025
8 checks passed
@primer-css primer-css mentioned this pull request Jun 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants