-
Notifications
You must be signed in to change notification settings - Fork 874
Fix theme media query listener cleanup #1066
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
PeterDaveHello
merged 1 commit into
ChatGPTBox-dev:master
from
PeterDaveHello:fix/theme-media-query-listener-cleanup
Sep 11, 2026
+82
−3
Merged
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import assert from 'node:assert/strict' | ||
| import { afterEach, test } from 'node:test' | ||
| import { JSDOM } from 'jsdom' | ||
| import { createElement } from 'react' | ||
| import { render, unmountComponentAtNode } from 'react-dom' | ||
| import { act } from 'preact/test-utils' | ||
| import { useWindowTheme } from '../../../src/hooks/use-window-theme.mjs' | ||
|
|
||
| const globalNames = ['window', 'document', 'Node'] | ||
| const originalDescriptors = new Map( | ||
| globalNames.map((name) => [name, Object.getOwnPropertyDescriptor(globalThis, name)]), | ||
| ) | ||
| let dom | ||
|
|
||
| const ThemeProbe = () => { | ||
| useWindowTheme() | ||
| return null | ||
| } | ||
|
|
||
| const setDOM = () => { | ||
| dom = new JSDOM('<!doctype html><div id="root"></div>') | ||
|
|
||
| for (const name of globalNames) { | ||
| Object.defineProperty(globalThis, name, { | ||
| value: dom.window[name], | ||
| configurable: true, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| dom?.window.close() | ||
| dom = undefined | ||
|
|
||
| for (const [name, descriptor] of originalDescriptors) { | ||
| if (descriptor) { | ||
| Object.defineProperty(globalThis, name, descriptor) | ||
| } else { | ||
| delete globalThis[name] | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| test('useWindowTheme removes the listener from the MediaQueryList that registered it', () => { | ||
| setDOM() | ||
| const mediaQueries = [] | ||
|
|
||
| window.matchMedia = () => { | ||
| const listeners = new Set() | ||
| const mediaQuery = { | ||
| matches: false, | ||
| addEventListener(type, listener) { | ||
| assert.equal(type, 'change') | ||
| listeners.add(listener) | ||
| }, | ||
| removeEventListener(type, listener) { | ||
| assert.equal(type, 'change') | ||
| listeners.delete(listener) | ||
| }, | ||
| listenerCount() { | ||
| return listeners.size | ||
| }, | ||
| } | ||
| mediaQueries.push(mediaQuery) | ||
| return mediaQuery | ||
| } | ||
|
|
||
| const container = document.querySelector('#root') | ||
| act(() => render(createElement(ThemeProbe), container)) | ||
|
|
||
| const subscribedMediaQuery = mediaQueries.find( | ||
| (mediaQuery) => mediaQuery.listenerCount() === 1, | ||
| ) | ||
| assert.ok(subscribedMediaQuery) | ||
|
|
||
| act(() => unmountComponentAtNode(container)) | ||
|
|
||
| assert.equal(subscribedMediaQuery.listenerCount(), 0) | ||
| }) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🤖 get_repo_knowledge executed:
get_repo_knowledge ChatGPTBox-dev/chatGPTBox /tmp/coderabbit-repo-knowledge/chatgptbox-dev-chatgptbox-458a1ad5/learnings /tmp/coderabbit-repo-knowledge/chatgptbox-dev-chatgptbox-458a1ad5/conventionsLength of output: 5760
🏁 Script executed:
Repository: ChatGPTBox-dev/chatGPTBox
Length of output: 15397
🏁 Script executed:
Repository: ChatGPTBox-dev/chatGPTBox
Length of output: 7113
🏁 Script executed:
Repository: ChatGPTBox-dev/chatGPTBox
Length of output: 10091
🏁 Script executed:
Repository: ChatGPTBox-dev/chatGPTBox
Length of output: 876
🌐 Web query:
Preact 10.22.1 unmountComponentAtNode preact export official documentation💡 Result:
In Preact, unmountComponentAtNode is not part of the core library, but it is provided as an export within preact/compat [1][2]. This utility is designed to support React compatibility, and in Preact, it functions by calling render(null, container) to clear the component tree and trigger appropriate lifecycle methods [3][4]. For standard Preact applications, you generally do not need a special unmount function. To unmount a component tree from a DOM node, you can simply call the render function with null as the first argument [3][5][4]: import { render } from 'preact'; render(null, document.querySelector('
#app')); This approach is the recommended, idiomatic way to destroy a root Preact node, as it ensures lifecycle methods like componentWillUnmount are properly executed for the component tree [3][5][4]. The preact/compat implementation of unmountComponentAtNode essentially performs this exact operation [3][4].Citations:
Use Preact’s renderer for mounting and unmounting.
react-domresolves to@preact/compat@17.1.2, and the configured React rules reject these deprecated imports. ImportcreateElementandrenderfrompreact, then unmount withrender(null, container)inside the existingactcalls.🧰 Tools
🪛 ESLint
[error] 5-5: ReactDOM.render is deprecated since React 18.0.0, use createRoot instead, see https://reactjs.org/link/switch-to-createroot
(react/no-deprecated)
[error] 5-5: ReactDOM.unmountComponentAtNode is deprecated since React 18.0.0, use root.unmount instead, see https://reactjs.org/link/switch-to-createroot
(react/no-deprecated)
🤖 Prompt for AI Agents
Sources: Coding guidelines, Linters/SAST tools