Skip to content

Close the mobile sidebar in the commit that shows the destination - #5870

Merged
ymichael merged 1 commit into
mainfrom
bb/fix-mobile-thread-navigation-jank-thr_5dnbsnp9bq
Oct 20, 2026
Merged

ymichael merged 1 commit into
mainfrom
bb/fix-mobile-thread-navigation-jank-thr_5dnbsnp9bq

Conversation

@ymichael

@ymichael ymichael commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Human comments

What was wrong

On a phone, tapping a sidebar destination (New thread, a thread row, a plugin panel, back/forward) started the drawer's 220 ms slide immediately, while react-router rendered the destination as a transition. The slide revealed the old page first. When the slide settled, the drawer ran flushSync(setOpenMobile(false)), a sync update that interrupted the still-pending navigation render, so the new page appeared only after the drawer was already gone. The navigation render itself was also slower than it needed to be:

  • The compact new-thread page mounted every recent thread at once (79 rows, about 1,430 elements, in a list 4,818 px tall, with about 4 rows visible).
  • useComposer() built its useSyncExternalStore snapshot from a ref that the plugin flips while it renders (by reading composer.text). React's end-of-render tearing check therefore saw a changed snapshot, threw the concurrent render away, and redid the whole tree synchronously.

What changed

  • components/ui/sidebar.tsx: closeMobileSidebar sets the closing state inside startTransition, so it shares the lane of a navigation started in the same event and commits with it. The slide starts in a layout effect of that commit. Closes that are not part of a navigation (backdrop, Escape, trigger) commit on the next frame.
  • lib/app-route-history.ts, HistoryCommandHandlers.tsx, SidebarHistoryNavigationControls.tsx: goBack/goForward take an optional callback that fires on the popstate that lands the traversal. The drawer closes there, in the same commit as the route.
  • views/RootComposeMobileRecents.tsx: renders one screenful of rows in the navigation commit and the rest in a deferred render (useDeferredValue(true, false), which defers the first mount only).
  • lib/plugin-sdk-hooks.ts: useComposer() snapshots read the draft and selection directly. The "only re-render plugins that read the text" behaviour from Keep composer plugin consumers off the typing render path #4655 moved into the subscription callbacks.

How you verified

Real data from the local bb, a 390×844 phone viewport, tapping New thread from the open drawer on a 7,900-event thread:

main this PR
iOS Simulator Safari (iPhone 16 Pro, iOS 18.4, 5 runs): new page committed 126–184 ms (median 132) 67–73 ms
iOS Simulator Safari: new page first painted 201–306 ms (median 260), after the drawer has already revealed the old page 138–143 ms, revealed by the slide
Headless Chrome 1× (4 runs): committed 87–100 ms 53–66 ms
Headless Chrome 2× CPU slowdown (4 runs): committed 162–174 ms 90–92 ms

On main, the slide starts at 1–11 ms. With this PR it starts in the commit that shows the new page. A CPU profile of the branch shows no tearing re-render, and the forced layout on the new-thread page dropped from about 26 ms to about 5 ms at 1×.

Tests, each of which fails on main and passes here:

  • sidebar.test.tsx: while a navigation is still suspended, the drawer must not start sliding. It slides in the commit that shows the destination.
  • plugin-slot-mounts.test.tsx: mounting a plugin that reads composer.text inside a transition renders the rest of the page once (main renders it twice).
  • HistoryCommandHandlers.test.tsx now runs on BrowserRouter, so back/forward close the drawer on the real popstate.

pnpm exec turbo run typecheck lint test --filter=@bb/app passes: 6,113 tests, with no new lint findings.

🤖 Generated with Claude Code

AGENT GENERATED

…nation

Tapping a sidebar destination on a phone slid the drawer closed at once,
while react-router rendered the destination as a transition. The 220 ms
settle then forced a sync update that interrupted that render, so the
drawer revealed the old page before the new one appeared.

- The mobile drawer close is now a transition, so it commits in the same
  frame as the navigation it accompanies, and the slide starts in a layout
  effect of that commit. Back/forward close on the popstate that lands the
  traversal.
- The compact new-thread page renders one screenful of recent threads in
  the navigation commit and the rest in a deferred render.
- useComposer() keeps its useSyncExternalStore snapshots pure and gates
  re-renders in the subscription instead. Reading composer.text during
  render no longer changes the snapshot, which forced React to throw away
  and synchronously redo every concurrent render that mounted the plugin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael force-pushed the bb/fix-mobile-thread-navigation-jank-thr_5dnbsnp9bq branch from dc33848 to 0a1fcb3 Compare October 20, 2026 17:16
@ymichael
ymichael merged commit 90d275a into main Oct 20, 2026
28 checks passed
@ymichael
ymichael deleted the bb/fix-mobile-thread-navigation-jank-thr_5dnbsnp9bq branch October 20, 2026 18:33
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.

1 participant