Fix controls rendering in StrictMode with R3F Canvas
Fix Controls rendering in StrictMode inside R3F Canvas
Problem:
Controls added from components inside @react-three/fiber Canvas do not render correctly when React StrictMode is enabled. The issue is related to CSS height calculation that happens prematurely.
Root Cause:
The useToggle hook in /packages/leva/src/hooks/useToggle.ts uses getBoundingClientRect() to calculate content height for collapse/expand animations. In React 18+ StrictMode, effects run twice during initial mount (mount → cleanup → remount).
The original implementation used a firstRender boolean ref that would be set to false on the first effect run. This caused the second effect run (during StrictMode's remount) to execute the height calculation logic prematurely, before the R3F Canvas content was fully laid out, resulting in incorrect height values.
Solution:
Replaced the firstRender boolean ref with a prevToggledRef that tracks the previous toggle state:
- When
prevToggledRef.currentisnull(initial render), skip animation logic entirely - When
prevToggledRef.currentequals the currenttoggledvalue, skip animation (handles StrictMode's double-invocation) - Only when
toggledactually changes from the previous value, run the height calculation and animation
This ensures:
- Initial render with
toggled=true: No height is calculated/set, wrapper uses natural height ✅ - Initial render with
toggled=false:useLayoutEffectsets height to 0 (collapsed state) ✅ - User toggles: Height is properly calculated and animated ✅
- StrictMode's double-invocation: Second effect run is correctly skipped ✅
Changes Made:
- [x] Analyze the
useTogglehook's height calculation logic - [x] Fix the height calculation to work correctly with StrictMode double-invocation
- [x] Ensure the fix handles both initial render and toggle animations properly
- [x] Address code review feedback (remove duplicate assignment, fix comment)
- [x] Run security checks (CodeQL - no issues found)
- [x] Remove accidentally added yarn.lock file
- [x] Add R3F Canvas reproduction story with actual
@react-three/fiberusage - [x] Add
@react-three/fiberandthreeas dev dependencies for the reproduction story
Testing: The fix is minimal and surgical, changing only the logic that determines when to run the animation. It maintains backward compatibility with existing behavior while properly handling StrictMode's double-invocation pattern. The change ensures controls render correctly in both development (with StrictMode) and production environments, particularly when used inside R3F Canvas components.
A new story has been added to Dev/BugRepro that demonstrates:
- Real R3F Canvas component with 3D scene (box mesh with lighting)
- Controls added from within Canvas components
- StrictMode enabled to reproduce the original issue
- Validates the fix works with actual @react-three/fiber usage
- Follows the naming convention: "552 / StrictMode with R3F Canvas should render controls"
Fixes #552
Original prompt
This section details on the original issue you should resolve
<issue_title>Controls of components inside R3F
Canvasnot rendering inStrictMode</issue_title> <issue_description>Description When inStrictMode, controls added from components inside a@react-three/fiberCanvasdo not display as expected. This behavior disappears whenStrictModeis removed.Reproduce StackBlitz Example
Expected behavior Controls should display correctly even when
StrictModeis enabled.Environment
- react: 19.1.1
- @react-three/fiber: 9.3.0
- leva: 0.10.0
- Browser: Chrome 139.0.7258.67
- OS: macOS 15.6</issue_description>
<agent_instructions>Seems like something with the css calculation in the ui. If you collapse/expand the ui corrects it. The parent to the inputs shows a height value, changing at all resolves. </agent_instructions>
Comments on the Issue (you are @copilot in this section)
- Fixes pmndrs/leva#552
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
⚠️ No Changeset found
Latest commit: 860b2654d62f23167de9513daadc592001786548
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
The latest updates on your projects. Learn more about Vercel for GitHub.
| Project | Deployment | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| leva | Nov 9, 2025 0:28am |
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.
Latest deployment of this branch, based on commit 4fcc47967b90f74422d871e62d42fcd3c20f2b02:
| Sandbox | Source |
|---|---|
| leva-minimal | Configuration |
| leva-busy | Configuration |
| leva-scroll | Configuration |
| leva-advanced-panels | Configuration |
| leva-ui | Configuration |
| leva-theme | Configuration |
| leva-transient | Configuration |
| leva-plugin-plot | Configuration |
| leva-plugin-bezier | Configuration |
| leva-plugin-spring | Configuration |
| leva-plugin-dates | Configuration |
| leva-custom-plugin | Configuration |
@copilot please add a storybook with a repro
[!IMPORTANT]
Review skipped
Bot user detected.
To trigger a single review, invoke the
@coderabbitai reviewcommand.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.
[!NOTE]
Other AI code review bot(s) detected
CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.
Comment @coderabbitai help to get the list of available commands and usage tips.
@copilot please add a storybook with a repro
Added StrictMode story in commit d55010c. The story demonstrates the fix with async component mounting (simulating R3F Canvas behavior), nested folders, and interactive mode toggling. You can find it at Advanced/StrictMode in Storybook.
@copilot do use react-three-fiber to create a realistic reproduction in one of the stories. Add this to the repros story group citing the original issue like the other stories in the category
@copilot do use react-three-fiber to create a realistic reproduction in one of the stories. Add this to the repros story group citing the original issue like the other stories in the category
Added R3F Canvas reproduction in commit 860b265. The story is now in the Dev/BugRepro group as "552 / StrictMode with R3F Canvas should render controls". It uses actual @react-three/fiber Canvas with a 3D scene and demonstrates controls rendering correctly in StrictMode, citing issue #552 like the other repro stories.