Skip to content

Lay out the thread context card in the same frame as a resize - #7497

Merged
ymichael merged 1 commit into
get-bb:mainfrom
smsunarto:scott/context-card-same-frame-layout
Nov 16, 2026
Merged

ymichael merged 1 commit into
get-bb:mainfrom
smsunarto:scott/context-card-same-frame-layout

Conversation

@smsunarto

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

ThreadContextCard picks its layout (gutter, 312px shift, or banner) in a ResizeObserver callback with a plain setLayout. ResizeObserver runs before paint, but React renders that update in a later task, so a width change that crosses a layout band paints one frame with the old card and transcript inset. Opening or closing the right panel at 1440–1600px makes the chat jump 312px a frame late. See #7496.

What changed

  • apps/app/src/components/plugin/PluginThreadContextCard.tsx: the observer applies a changed layout with flushSync, so the card and the inset render in the frame that changed the width. When the band is unchanged it returns early, so a divider drag inside one band adds no render. The initial measurement in the layout effect is unchanged.

How you verified

  • New test in PluginThreadContextCard.test.tsx drives the ResizeObserver callback through 1200 → 900 → 1200px and asserts the card and inset update synchronously. It fails without the fix (expected '0' to be '312') and passes with it.
  • vitest run on PluginThreadContextCard.test.tsx and theme.test.ts: passed. turbo run typecheck --filter=@bb/app: passed. oxlint and oxfmt clean.
  • On pnpm dev with the experiment on and the card pinned, I recorded every resize frame while opening and closing the right panel at 1440, 1600, and 2560px: 0 mismatched frames over two runs, against one per band crossing with the fix reverted.

Fixes #7496

AGENT GENERATED

ThreadContextCard set its layout (gutter, shift, or banner) from a
ResizeObserver with a plain state update, which React renders in a later
task, after the frame that changed the width has painted. Every width
change that crossed a layout band painted one frame with the stale card
and transcript inset. Flush the update synchronously from the observer
when the band changes, so the card and inset paint with the new width.
@ymichael
ymichael merged commit bef0ed3 into get-bb:main Nov 16, 2026
29 checks passed
ymichael added a commit that referenced this pull request Nov 17, 2026
## Human comments

## What was wrong

A `ResizeObserver` callback runs after layout and before paint, but
React applies a plain `setState` from that callback in a later task,
after the browser has painted. Any layout derived from a measured size
therefore painted one frame with the old layout at the new size. #7497
(context card), #5067 and #6488 (composer prompt stack) each fixed one
instance by hand with `flushSync`. A survey found about 30 more sites
with the same pattern: TOC show/hide, header action overflow, message
action row count, "Show more" toggles, File Preview header buttons,
panel size limits, resource toolbar, overflow fades, and others across
`apps/app`, `shared-ui`, plugins and `apps/web`.

Measured in headless Chrome, crossing a width threshold 10 times:

| How the measured size reaches React | Frames painted with the old
layout |
|---|---|
| plain `setState` in the observer | 10 of 10 |
| `flushSync` | 0 |
| `useSyncExternalStore` | 0 |

## What changed

- **One path for element sizes.** `observeSharedResize` moved from
`apps/app/src/lib` to `@bb/shared-ui/lib/shared-resize-observer`, which
also makes it a component-registry item for forkable plugins. It keeps
the one-`flushSync`-per-batch writes and `settleAfterSharedResizeWrites`
that #7612 added, so React state from a resize renders before paint. It
now also:
  - accepts a list of targets and runs each registration once per batch;
  - returns a no-op where `ResizeObserver` is missing.
- **`useMeasuredValue(target, read, initial)`** in
`@bb/shared-ui/hooks/use-measured-value` for the simple ref-to-value
case. It replaces usehooks-ts `useResizeObserver` in the git plugin.
- **Lint guard.** `bb/no-raw-resize-observer` in
`scripts/oxlint-plugin.mjs` rejects `new ResizeObserver` and usehooks-ts
`useResizeObserver` outside the shared module, apart from tests and
stories. Two files are exempt in `.oxlintrc.json` because their own
callback batching only writes the DOM and doesn't set React state:
- `scroll-position-keeper.ts`: the timeline engine's own observer, which
reconciles once per batch.
  - the landing hero feed animation in `apps/web`.
- **Migration.** 58 sites across `apps/app`, `shared-ui`, 13 plugins and
`apps/web` now use the shared path. Read steps only read layout; write
steps write the DOM and set state.
- The `flushSync` workarounds in the sidebar header slot and context
card are gone.
- The composer prompt stack keeps its inner `flushSync`, because
`positionKeeper.remeasure()` must run after the new height commits.
- #7612's new call sites (timeline row heights, `useIsOverflowing`,
markdown table geometry) import the moved scheduler.
- TOC visibility and the background-commands card now measure on first
paint instead of waiting for the observer's first callback.
- `ThreadDetailHeader` keeps a narrow/wide flag instead of the pane
width, so a divider drag no longer re-renders the header every frame.
- The component registry is regenerated: a new `shared-resize-observer`
item, and expandable-text, question-form, resource-pagination and
resource-toolbar now depend on it.

No wire, CLI or user-facing configuration changes.

## How you verified

- New tests in
`packages/shared-ui/src/lib/shared-resize-observer.test.tsx`:
- state written by a resize renders before the observer callback
returns;
  - `useMeasuredValue` measures on mount and on each resize;
  - a registration with several targets runs once per batch.
  - The first two fail with the `flushSync` batching removed.
- The lint rule flags the original code of the past fixes at the exact
line each one changed: `PluginThreadContextCard.tsx:371` (#7497),
`FollowUpPromptBox.tsx:668` (#5067) and `FollowUpPromptBox.tsx:685`
(#6488).
- `pnpm exec turbo run typecheck lint --filter='...[origin/main]'`:
180/180 passed.
- `pnpm exec turbo run test` on every affected package passed after the
fixes below:
- Two `@bb/app` tests failed because their `ResizeObserver` stubs called
the callback without entries; I fixed the stubs.
  - These failed under load and passed on a rerun alone:
    - `bb-plugin-connect` `connect.test.ts` (72/72)
    - four `@bb/server` files (131/131)
    - `CommandPalette.test.tsx`
    - `marketplace-exposure.test.tsx`
- `plugin-guide.test.ts` failed because an interrupted bundling run had
left a gitignored `plugins/.bundled-stage-*` copy of navigation.
Removing it made it pass.
- Live, with `pnpm start:worktree`, the experiment on and the context
card pinned, I recorded every resize frame while opening and closing the
right panel twice at 1440, 1600 and 2560px:
  - main before #7497: 1 stale frame per band crossing;
  - this branch: 0 at every width.

> AGENT GENERATED

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

Thread context card and transcript inset lag one frame behind a width change

2 participants