-
-
Notifications
You must be signed in to change notification settings - Fork 676
fix(react-form-devtools): re-mount Solid component on theme change (closes #2357) #2371
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
Open
dikshit-n
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
dikshit-n:fix/2357-devtools-theme-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+188
−9
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,66 @@ | ||
| import { createReactPanel } from '@tanstack/devtools-utils/react' | ||
| import { useEffect, useRef } from 'react' | ||
| import { FormDevtoolsCore } from '@tanstack/form-devtools' | ||
|
|
||
| // type | ||
| import type { DevtoolsPanelProps } from '@tanstack/devtools-utils/react' | ||
|
|
||
| export interface FormDevtoolsReactInit extends DevtoolsPanelProps {} | ||
|
|
||
| const [FormDevtoolsPanel, FormDevtoolsPanelNoOp] = | ||
| createReactPanel(FormDevtoolsCore) | ||
| /** | ||
| * Fixed React panel wrapper for FormDevtoolsCore. | ||
| * | ||
| * Root cause of #2357 ("devtools are always light mode even if TanStackDevtools says dark"): | ||
| * The original createReactPanel hook only calls mount() once on the Solid FormDevtoolsCore | ||
| * class. When TanStack DevTools outer shell switches theme, it calls | ||
| * plugin.render(el, newTheme) which creates a new React element — but mount() is never | ||
| * called again. The Solid component receives props.theme as a plain (non-reactive) value | ||
| * and never re-renders. | ||
| * | ||
| * Fix: track the previous theme in a ref. The effect dependency is [theme] only — it | ||
| * fires only when the theme value changes, never on unrelated prop changes. The ref | ||
| * guards against the initial mount where prevThemeRef.current is undefined (matching | ||
| * an undefined theme on first render). Cleanup unmounts the old Solid instance both | ||
| * on theme change and on component teardown, releasing the Solid tree and its | ||
| * resources. | ||
| */ | ||
| export function FormDevtoolsPanel(props: DevtoolsPanelProps) { | ||
| const devToolRef = useRef<HTMLDivElement>(null) | ||
| const devtools = useRef<InstanceType<typeof FormDevtoolsCore> | null>(null) | ||
| const prevThemeRef = useRef<string | undefined>(undefined) | ||
|
|
||
| export { FormDevtoolsPanel, FormDevtoolsPanelNoOp } | ||
| // theme is passed by TanStack DevTools outer shell via props. | ||
| // We use type assertion because @tanstack/devtools types are not available | ||
| // as a direct dependency of this package. | ||
| const theme = (props as { theme?: string }).theme | ||
|
|
||
| useEffect(() => { | ||
| // Guard: skip if theme hasn't actually changed (ref was already updated | ||
| // in the prior effect run, or this is the very first render with undefined). | ||
| if (theme === prevThemeRef.current) return | ||
| prevThemeRef.current = theme | ||
|
|
||
| if (!devToolRef.current) return | ||
|
|
||
| // Create a fresh instance for the new theme. The effect's cleanup function | ||
| // unmounts whichever instance is current at teardown time — whether that | ||
| // happens because the theme changed (next effect run) or because the | ||
| // component itself unmounted. This prevents the Solid tree from being | ||
| // orphaned when the panel closes. | ||
| const instance = new FormDevtoolsCore() | ||
| devtools.current = instance | ||
| instance.mount(devToolRef.current, props) | ||
|
|
||
| return () => { | ||
| instance.unmount() | ||
| if (devtools.current === instance) { | ||
| devtools.current = null | ||
| } | ||
| } | ||
| }, [theme]) // NOTE: intentionally omits `props` — props changes on every render | ||
| // (object identity); the ref guard above handles theme-change detection. | ||
|
|
||
| return <div style={{ height: '100%' }} ref={devToolRef} /> | ||
| } | ||
|
|
||
| export function FormDevtoolsPanelNoOp(_props: DevtoolsPanelProps) { | ||
| return null as unknown as React.ReactElement | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 109 additions & 4 deletions
113
packages/react-form-devtools/tests/formDevtools.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,112 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { render } from '@testing-library/react' | ||
| import { useEffect, useRef } from 'react' | ||
|
|
||
| describe('test suite', () => { | ||
| it('should work', () => { | ||
| expect(true).toBe(true) | ||
| // Capture the most-recently-constructed FormDevtoolsCore so the test can | ||
| // assert mount/unmount call order without needing to know the prop object | ||
| // identity React passes to the effect. | ||
| const lastInstance = vi.hoisted(() => ({ current: null as null | { mount: ReturnType<typeof vi.fn>; unmount: ReturnType<typeof vi.fn> } })) | ||
|
|
||
| vi.mock('@tanstack/form-devtools', () => { | ||
| class MockFormDevtoolsCore { | ||
| mount = vi.fn() | ||
| unmount = vi.fn() | ||
| constructor() { | ||
| lastInstance.current = this | ||
| } | ||
| } | ||
| return { FormDevtoolsCore: MockFormDevtoolsCore } | ||
| }) | ||
|
|
||
| // Re-import after the mock is registered. | ||
| const { FormDevtoolsPanel } = await import('../src/FormDevtools') | ||
|
|
||
| beforeEach(() => { | ||
| lastInstance.current = null | ||
| }) | ||
|
|
||
| describe('FormDevtoolsPanel — integration with @testing-library/react + jsdom', () => { | ||
| it('mounts FormDevtoolsCore on initial render with the given theme', () => { | ||
| const { unmount } = render(<FormDevtoolsPanel theme="dark" />) | ||
|
|
||
| expect(lastInstance.current).not.toBeNull() | ||
| expect(lastInstance.current!.mount).toHaveBeenCalledTimes(1) | ||
| expect(lastInstance.current!.unmount).not.toHaveBeenCalled() | ||
| expect(lastInstance.current!.mount).toHaveBeenCalledWith( | ||
| expect.any(HTMLDivElement), | ||
| expect.objectContaining({ theme: 'dark' }), | ||
| ) | ||
|
|
||
| unmount() | ||
| }) | ||
|
|
||
| it('does NOT remount when an unrelated prop changes but theme stays the same', () => { | ||
| // Wrap in a parent that we control so we can force prop-identity changes | ||
| // without changing theme. | ||
| function Harness({ extras }: { extras: object }) { | ||
| return <FormDevtoolsPanel theme="dark" {...extras} /> | ||
| } | ||
|
|
||
| const { rerender, unmount } = render(<Harness extras={{ a: 1 }} />) | ||
| const firstInstance = lastInstance.current | ||
| expect(firstInstance?.mount).toHaveBeenCalledTimes(1) | ||
|
|
||
| // Re-render with a new prop object — same theme. | ||
| rerender(<Harness extras={{ a: 2 }} />) | ||
|
|
||
| // Same instance, no remount, no unmount. | ||
| expect(lastInstance.current).toBe(firstInstance) | ||
| expect(firstInstance!.mount).toHaveBeenCalledTimes(1) | ||
| expect(firstInstance!.unmount).not.toHaveBeenCalled() | ||
|
|
||
| unmount() | ||
| }) | ||
|
|
||
| it('unmounts the old instance and mounts a new one when theme changes', () => { | ||
| const { rerender, unmount } = render(<FormDevtoolsPanel theme="light" />) | ||
| const lightInstance = lastInstance.current | ||
| expect(lightInstance?.mount).toHaveBeenCalledTimes(1) | ||
| expect(lightInstance?.mount).toHaveBeenCalledWith( | ||
| expect.any(HTMLDivElement), | ||
| expect.objectContaining({ theme: 'light' }), | ||
| ) | ||
|
|
||
| rerender(<FormDevtoolsPanel theme="dark" />) | ||
|
|
||
| // After theme change: old instance unmounted, new instance mounted. | ||
| expect(lightInstance!.unmount).toHaveBeenCalledTimes(1) | ||
| const darkInstance = lastInstance.current | ||
| expect(darkInstance).not.toBe(lightInstance) | ||
| expect(darkInstance?.mount).toHaveBeenCalledTimes(1) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| expect(darkInstance!.mount).toHaveBeenCalledWith( | ||
| expect.any(HTMLDivElement), | ||
| expect.objectContaining({ theme: 'dark' }), | ||
| ) | ||
| expect(darkInstance!.unmount).not.toHaveBeenCalled() | ||
|
|
||
| unmount() | ||
| }) | ||
|
|
||
| it('unmounts the current FormDevtoolsCore when the panel itself unmounts', () => { | ||
| const { unmount } = render(<FormDevtoolsPanel theme="dark" />) | ||
| const instance = lastInstance.current | ||
| expect(instance?.unmount).not.toHaveBeenCalled() | ||
|
|
||
| unmount() | ||
|
|
||
| // The cleanup function on the live effect must call unmount exactly once. | ||
| expect(instance!.unmount).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('returns null from FormDevtoolsPanelNoOp without mounting any core', async () => { | ||
| const { FormDevtoolsPanelNoOp } = await import('../src/FormDevtools') | ||
| lastInstance.current = null | ||
|
|
||
| const { container, unmount } = render(<FormDevtoolsPanelNoOp theme="dark" />) | ||
|
|
||
| expect(lastInstance.current).toBeNull() | ||
| expect(container.firstChild).toBeNull() | ||
|
|
||
| unmount() | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.