Skip to content

Keep sidebar resize off the app-root style path - #1992

Merged
SawyerHood merged 2 commits into
mainfrom
bb/improve-large-thread-sidebar-resizing-thr_q6rsehnvv7
Aug 20, 2026
Merged

SawyerHood merged 2 commits into
mainfrom
bb/improve-large-thread-sidebar-resizing-thr_q6rsehnvv7

Conversation

@SawyerHood

Copy link
Copy Markdown
Collaborator

What was wrong

Dragging the left sidebar in a long thread was slow (DevTools profile: 2.2 s of style recalculation, ~50 ms per frame, vs 78 ms of layout). Three things in AppLayout each forced a restyle of the whole document:

  1. Per frame: the live width was written as --sidebar-width on the app root (SidebarProvider). A custom property is inherited, so a change on the root invalidates every descendant, including the timeline. Cost scales with thread size (~20–45 µs per element).
  2. At mousedown/mouseup: body.style.userSelect and body.style.cursor (both inherited) → two more full restyles each.
  3. At mousedown/mouseup: IframeDragGuardOverlay mounted as a sibling before the app root. Inserting/removing a node invalidates every following sibling's subtree → one more full restyle each.

Items 1–2 date from the initial sidebar implementation; item 3 came with the iframe guard (#57). Nothing regressed recently; it only becomes visible in large threads.

What changed

  • ui/sidebar.tsx: new SidebarWidthContext and a width prop on SidebarProvider. Sidebar writes --sidebar-width directly on the two elements that read it (gap and panel), not on the wrapper.
  • ui/theme.css: @property --sidebar-width { inherits: false }, so a change restyles only those two elements.
  • layout/AppLayout.tsx: the live width flows through a non-persisted sidebarLiveWidthAtom; only the bridge and Sidebar re-render per frame (flushSync keeps the browser-view bounds sync measuring the updated DOM). Removed body cursor/user-select: mousedown preventDefault already blocks selection, and the overlay now carries the resize cursor. The overlay mounts after the app root.
  • lib/iframe-drag-guard.tsx: required cursor prop, placement note. ThreadSecondaryPanel moves its overlay after the panel content for the same reason.

How you verified

Repro: dev app with pnpm seed:perf -- --projects 2 --threads 20 --events 60000, open the 9k-event thread in headless Chromium, scroll to the top until ~20k DOM elements are mounted, run a 60-step scripted drag on the resize handle under a CDP trace.

Style recalcs Recalc total Max per recalc Elements/recalc Layout total
Before 50 19,444 ms 419 ms 19,423 1,087 ms
After 2 5 ms 2.9 ms 31 199 ms

Manual checks in the browser: width tracks the drag, commits to localStorage and survives reload, collapse/expand still works, no text selection during drag, overlay shows col-resize.

Tests (fail before, pass after): AppLayout.sidebar-resize.test.tsx (drag writes only gap/panel, never root or body; overlay mounts after the root with the cursor), sidebar.test.tsx (width lands on gap/panel, not the wrapper), theme.test.ts (@property non-inherited). pnpm exec turbo run typecheck lint --filter=@bb/app passes; 110 test files / 888 tests pass in the touched areas.

AGENT GENERATED: by Claude Opus 5

SawyerHood and others added 2 commits August 20, 2026 02:45
Resizing the left sidebar wrote --sidebar-width on the app root, set
cursor/user-select on body, and mounted the drag-guard overlay before the
app root. Each is an inherited-property or sibling change that restyles
every element in the document, so a drag cost 50-400 ms per frame in a
long thread.

The width now flows through a SidebarWidthContext and is written only on
the sidebar gap and panel, with --sidebar-width registered non-inherited;
the overlay carries the cursor and mounts after its siblings.

Co-Authored-By: Claude <[email protected]>
@SawyerHood
SawyerHood merged commit e31208e into main Aug 20, 2026
12 checks passed
@SawyerHood
SawyerHood deleted the bb/improve-large-thread-sidebar-resizing-thr_q6rsehnvv7 branch August 20, 2026 02:54
ymichael added a commit that referenced this pull request Aug 20, 2026
## What was wrong


[`SidebarSplitDivider`](https://github.com/get-bb/bb/blob/6be45053be92f964895038fc374a45e759087479/apps/app/src/components/secondary-panel/SidebarSplitContainer.tsx#L679-L757),
introduced by [#1601](#1601), wrote
both `cursor` and `user-select` to `document.body` at drag start and
cleared them at drag finish. Both properties are inherited, so each
write invalidated styles across the full app tree. That repeats the root
cause measured in [#1992](#1992), where
inherited body/root mutations cost hundreds of milliseconds per
recalculation on a roughly 20k-element long-thread DOM. The divider also
had no unmount cleanup for its native pointer listeners or temporary
flex values.

## What changed

- Keep the active split-divider cursor in local `SidebarSplitContainer`
state instead of mutating `document.body` or the document root.
- Mount the existing `IframeDragGuardOverlay` after the split content,
with `col-resize` or `row-resize` for the active divider. This keeps the
drag captured across iframe/browser-backed pane content without
inserting a guard before a large sibling subtree.
- Continue relying on the divider's existing pointer-down
`preventDefault()` to prevent text selection, matching #1992.
- Cancel the in-flight resize on divider unmount: remove native pointer
listeners, release capture when held, clear drag chrome, and restore
uncommitted flex values.
- Remove the now-unused global body cursor helpers, while preserving the
panel-library takeover used by main.tsx.

This is an app-only change with no host-daemon protocol or other wire
changes. It reuses the existing drag guard and straightforward component
state; it does not introduce another split framework or alter the split
layout model.

## How you verified

The focused regression coverage failed before the production change and
passed after it. Before the fix, the new assertions observed `cursor:
col-resize; user-select: none` on `body` during pointer-down and a stuck
`data-dragging="true"` state after unmount.

- `pnpm exec turbo run test --filter=@bb/app --force --
src/components/secondary-panel/SidebarSplitContainer.test.tsx
src/components/layout/AppLayout.sidebar-resize.test.tsx` — 2 files, 20
tests passed.
- `pnpm exec turbo run build typecheck lint --filter=@bb/app` — build
and typecheck passed; lint passed with 0 errors and 156 existing
warnings.
- `git diff --check` — passed.

Coverage proves that split drag does not change body/document-root
inline styles, pointer-down is default-prevented, the guard mounts after
split content with both row and column cursors, pointer-up/cancel remove
it, and unmount cancels the drag and removes its native listeners.

## Relationship to #1992 and #2059

This applies #1992's measured performance fix to the internal
right-panel divider path that #1992 did not cover. Draft
[#2059](#2059) fixes the separate
mirrored split-tree correctness problem. Its current head (`4aca1e08`)
still contains both body mutations, so it does not supersede this fix.
The changes overlap only in `SidebarSplitContainer.tsx` and its test
because #2059 rewrites that component; if #2059 lands first, the rebase
should keep its single-tree rendering while retaining this PR's
container-local cursor state, final drag guard, and divider cleanup.

> AGENT GENERATED: by GPT-5.6-Sol
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