-
Notifications
You must be signed in to change notification settings - Fork 6.1k
fix(web): stop auto-animate polling in the legacy sidebar #9502
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
ylcn91
wants to merge
1
commit into
pingdotgg:main
Choose a base branch
from
ylcn91:fix/sidebar-auto-animate-idle-cpu
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.
Open
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
95 changes: 95 additions & 0 deletions
95
apps/web/src/components/sidebar/useSidebarListMotion.test.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 |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { act, type ReactElement } from "react"; | ||
| import { create, type ReactTestRenderer } from "react-test-renderer"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| const { createSidebarListMotion, motions } = vi.hoisted(() => { | ||
| const motions: Array<{ | ||
| readonly parent: unknown; | ||
| readonly update: ReturnType<typeof vi.fn>; | ||
| readonly dispose: ReturnType<typeof vi.fn>; | ||
| }> = []; | ||
| return { | ||
| motions, | ||
| createSidebarListMotion: vi.fn((parent: unknown) => { | ||
| const motion = { parent, update: vi.fn(), dispose: vi.fn() }; | ||
| motions.push(motion); | ||
| return motion; | ||
| }), | ||
| }; | ||
| }); | ||
| vi.mock("../Sidebar.motion", () => ({ createSidebarListMotion })); | ||
|
|
||
| import { useSidebarListMotion } from "./useSidebarListMotion"; | ||
|
|
||
| function List({ orderKey, nodeKey = "list" }: { orderKey: string; nodeKey?: string }) { | ||
| return <ul key={nodeKey} ref={useSidebarListMotion(orderKey)} />; | ||
| } | ||
|
|
||
| let renderer: ReactTestRenderer | null = null; | ||
| const nodes: object[] = []; | ||
|
|
||
| function render(element: ReactElement) { | ||
| act(() => { | ||
| if (renderer) { | ||
| renderer.update(element); | ||
| return; | ||
| } | ||
| renderer = create(element, { | ||
| createNodeMock: () => { | ||
| const node = {}; | ||
| nodes.push(node); | ||
| return node; | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); | ||
| motions.length = 0; | ||
| nodes.length = 0; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| act(() => renderer?.unmount()); | ||
| renderer = null; | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("useSidebarListMotion", () => { | ||
| it("runs one motion pass per row change and nothing while idle", () => { | ||
| render(<List orderKey="a,b" />); | ||
| expect(motions).toHaveLength(1); | ||
| const motion = motions[0]!; | ||
| expect(motion.parent).toBe(nodes[0]); | ||
| // Mounting records the baseline without animating. | ||
| expect(motion.update.mock.calls[0]).toEqual([false]); | ||
|
|
||
| const passes = motion.update.mock.calls.length; | ||
| render(<List orderKey="a,b" />); | ||
| expect(motion.update).toHaveBeenCalledTimes(passes); | ||
|
|
||
| render(<List orderKey="b,a" />); | ||
| expect(motion.update).toHaveBeenCalledTimes(passes + 1); | ||
| expect(motion.update).toHaveBeenLastCalledWith(true); | ||
|
|
||
| // Emptying the list resets the baseline instead of fading rows out. | ||
| render(<List orderKey="" />); | ||
| expect(motion.update).toHaveBeenLastCalledWith(false); | ||
| expect(motion.dispose).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("disposes the motion as soon as its list detaches", () => { | ||
| render(<List orderKey="a" />); | ||
| render(<List orderKey="a" nodeKey="replacement" />); | ||
| expect(motions).toHaveLength(2); | ||
| expect(motions[0]!.dispose).toHaveBeenCalledOnce(); | ||
| expect(motions[1]!.parent).toBe(nodes[1]); | ||
| expect(motions[1]!.dispose).not.toHaveBeenCalled(); | ||
|
|
||
| act(() => renderer!.unmount()); | ||
| renderer = null; | ||
| expect(motions[1]!.dispose).toHaveBeenCalledOnce(); | ||
| expect(motions).toHaveLength(2); | ||
| }); | ||
| }); |
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,25 @@ | ||
| import { useCallback, useLayoutEffect, useRef } from "react"; | ||
|
|
||
| import { createSidebarListMotion } from "../Sidebar.motion"; | ||
|
|
||
| /** | ||
| * Animates a legacy sidebar list's rows into place with the default sidebar's | ||
| * motion. `orderKey` names the rendered rows in order, empty when there are | ||
| * none: a change runs one motion pass after the commit, and an idle list does | ||
| * no work at all. The returned ref callback owns the motion and disposes it as | ||
| * soon as the list detaches. | ||
| */ | ||
| export function useSidebarListMotion(orderKey: string) { | ||
| const motionRef = useRef<ReturnType<typeof createSidebarListMotion> | null>(null); | ||
| const attach = useCallback((node: HTMLUListElement | null) => { | ||
| motionRef.current?.dispose(); | ||
| motionRef.current = node === null ? null : createSidebarListMotion(node); | ||
| motionRef.current?.update(false); | ||
| }, []); | ||
| useLayoutEffect(() => { | ||
| // An emptied list has nothing left to show a fade in, so it only resets | ||
| // its baseline; that also keeps a collapse from cloning every row. | ||
| motionRef.current?.update(orderKey !== ""); | ||
| }, [orderKey]); | ||
| return attach; | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🌐 Web query:
According to the official React documentation, does useLayoutEffect run after the initial component commit when its dependency array contains an initial value?💡 Result:
According to the official React documentation, useLayoutEffect runs synchronously after React performs DOM mutations but before the browser repaints the screen [1][2][3]. Regarding your specific question about the timing relative to the initial component commit: useLayoutEffect does indeed run after the initial component commit [3]. During the "commit" phase, React applies the changes to the DOM; useLayoutEffect is then executed synchronously after those mutations have been applied [2][4][3]. Because it fires after the DOM has been updated but before the browser repaints, it allows you to perform measurements or make further DOM updates synchronously, preventing the user from seeing any visual inconsistencies that might occur if the browser were to paint before your effect logic ran [1][2][3]. This behavior holds true regardless of whether a dependency array is provided; if you include dependencies, the effect will run after the initial mount and subsequently whenever those dependencies change [3].
Citations:
🤖 get_repo_knowledge executed:
get_repo_knowledge pingdotgg/t3code /tmp/coderabbit-repo-knowledge/pingdotgg-t3code-ca3faa49/architecture /tmp/coderabbit-repo-knowledge/pingdotgg-t3code-ca3faa49/learningsLength of output: 27054
🏁 Script executed:
Repository: pingdotgg/t3code
Length of output: 10220
🏁 Script executed:
Repository: pingdotgg/t3code
Length of output: 321
🏁 Script executed:
Repository: pingdotgg/t3code
Length of output: 12906
Skip the animated pass after the initial baseline.
For a non-empty
orderKey, the ref callback callsupdate(false)when the list attaches. The initialuseLayoutEffectthen callsupdate(true)after the initial DOM commit. This animates the initial list despite the baseline-only mount behavior.Track the baseline
orderKeyand skip the effect when it matches. Assert that mounting makes exactly oneupdate(false)call.🤖 Prompt for AI Agents