Skip to content

Studio: key the modal pointer-lock rules off attributes that never sit above the thread - #17006

Merged
danielhanchen merged 3 commits into
mainfrom
studio-menu-pointer-lock
Nov 7, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-menu-pointer-lock

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

On long chats in WebKit (the macOS app's WKWebView, Safari), each modal menu or dialog open and close costs a thread-sized style pass. The cause is not where it first looks.

Radix writes pointer-events: none on <body> while a modal layer is open. pointer-events is inherited, but :where(.aui-thread-viewport) { pointer-events: auto } (#16902) already stops the inheritance at the thread scroller. What still scales with the chat are the four rules that depend on the lock (#16902, #16961), which were keyed on the body's inline style:

  • body[style*="pointer-events: none"] .aui-thread-root::after (thread cover)
  • body[style*="pointer-events: none"] .chat-artifact-pop-surface .side-pane-lock-cover::after (side pane cover)
  • body[style*="pointer-events: none"] :is(.pointer-events-auto, .sidebar-row-action):not(...) (page restorations)
  • :is([role="dialog"], ...)[style*="pointer-events: none"] .pointer-events-auto (lower layers)

When the [style*=...] match on an element flips, WebKit walks and re-matches every element under it. On <body> that includes the whole thread. The lower-layer rule triggers the same walk for the body flip, because WebKit only looks at the attribute part of that compound.

Forced style + layout flush per write, Playwright WebKit with the macOS UA, 150 turns (55.8k elements, 55.0k of them in the thread), median of 5:

Side effect 1 turn 150 turns
body.style.pointerEvents = "none", as shipped 24-28 ms 59-107 ms
Same, with the four [style*=...] rules deleted 19-23 ms 17-25 ms
Inline pointer-events: none on the thread scroller's parent, with the rules 1 ms 34-47 ms
Same, rules deleted 1 ms 0 ms
Any attribute on <html> used as an ancestor in a rule (even [x] .one-element) 1-5 ms 27-46 ms
The same attribute used only on the element it styles 0 ms 0-1 ms

In real menus at 150 turns the write accounted for 49-96 ms of each open and 48-72 ms of each close (timed between two forced flushes). With the write stubbed out, the longest frame of a menu open dropped from 255-441 ms to 123-200 ms (command palette, search, composer, chat options, Tools). Chromium pays 9-12 ms per write at both lengths, and the long-chat gate's lock check reads 5.3 ms at 40 turns and 20.6 ms at 200 turns on main.

Fix

Radix's write stays as it is, so everything that reads the body lock still sees it: tooltip-modal-layer.ts, the inherited none, the Playwright checks. Only the CSS rules move to cheaper keys.

  • src/lib/modal-pointer-lock.ts (installed in main.tsx next to the other WebKit guards) watches the body's style attribute. While the lock is up it mirrors it onto attributes that never sit above a thread:
    • data-modal-lock-thread on each .aui-thread-root. Only used on the element it styles (the cover).
    • data-modal-lock-path on each ancestor of a thread root. Only used on the element it styles.
    • data-modal-lock-region on every other child of those ancestors. Together these are the rest of the page, split into subtrees that hold no thread. Descendant rules key on this, as they did on body before.
    • data-modal-lock-lower on a dialog, alert dialog, menu or listbox whose inline pointer-events Radix set to none because a stacked modal layer opened above it.
  • index.css: the same four rules, keyed on those attributes. Same declarations, same exclusions. A thread root that is not found (say, one mounted inside a region while a modal is open) still gets its cover through [data-modal-lock-region] .aui-thread-root::after.
  • While locked, children added to a thread root's ancestors are picked up by a childList observer on just those elements. Anything mounted deeper lands inside a region (covered by the descendant rules) or inside a thread root (excluded, as before). Lower layers are tracked by a style attribute observer that only runs while the lock is up, plus a scan of the regions when the lock goes up (for a non-modal menu that turns lower in the same commit). Unlocking clears every mark and disconnects both observers. One watcher per document; installing it again does nothing.
  • The long-chat gate page installs the watcher too, so its lock check times the rules the app actually uses.

Results

Same host, same build pair, servers on one machine. The host was heavily loaded (load average about 130 on 36 cores), so timings are noisy. The model picker does not take the body lock, so it serves as the noise reference: up to about 55 ms there.

Forced flush per body lock write (median of 5-7, 2-4 sessions per arm):

WebKit 1 turn WebKit 150 turns Chromium 1 turn Chromium 150 turns
Before 23-28 ms 59-107 ms 9-12 ms 9 ms
After (includes the marks) 25-37 ms 28-48 ms 12 ms 10-12 ms

Long-chat gate lock check (Chromium, CDP style recalc per open or close): 5.3 ms at 40 turns and 20.6 ms at 200 turns before, 0.8 ms and 1.0 ms after.

Real menus, 150 turns, base and head interleaved, 3 reps x 3 cycles. Each cell is the longest frame in ms, as a median with the range of per-rep medians in brackets:

Surface WebKit before WebKit after Chromium before Chromium after
Tools menu open 321 [320-371] 236 [203-290] 67 50
Tools menu close 159 [146-173] 120 [114-147] 50 33
Command palette open 404 [257-476] 326 [269-344] 167 150
Command palette close 221 [208-225] 198 [198-291] 117 117
Search chats open 446 [379-492] 394 [358-413] 150 150
Chat options open 340 [319-361] 293 [285-312] 83 83
Chat options close 177 [141-234] 164 [159-173] 50 33
Rename chat open 261 [230-290] 222 [194-236] 84 117
Rename chat close 260 [240-382] 184 [152-210] 83 83
Composer menu open 329 [327-460] 379 [235-406] 100 100
Composer menu close 153 [131-160] 133 [126-134] 50 50
Settings open 518 [482-522] 513 [509-557] 233 233
Settings close 200 [189-201] 197 [175-202] 167 133
Model picker open (no body lock) 397 [392-400] 341 [329-401] 117 117

The clear wins are the ones above the noise band: Tools menu open (-85 ms), command palette open (-78 ms) and rename close (-76 ms). The rest move by about one write's worth or sit inside the noise. Settings is dominated by other work.

In Chromium, elements restyled per open or close go up by 0-37 (the roughly 32 elements that get an attribute; for example Tools open 630 -> 663, close 479 -> 515). Style ms stays the same or drops (Settings close 33 -> 19 ms, chat options open 34 -> 25 ms).

What remains in WebKit is the inherited write itself: 17-25 ms, flat from 1 to 150 turns, on the roughly 750 elements outside the thread. Removing it would mean replacing Radix's body lock with a blocker element. That changes hit-testing for window chrome that sits at z-40 to z-120 and is meant to stay live (.modal-pe-live: navbar, titlebar, combobox, toasts), so it is not done here.

Behaviour

Behaviour walk at 150 turns, base vs head, in Chromium and in WebKit with the macOS UA. It compares a 792-point elementFromPoint grid, the computed pointer-events of every restoration (.pointer-events-auto, .sidebar-row-action, .modal-pe-live, the thread scroller), the cover pseudo-elements, and the body and layer state, in 14 states: idle; Tools menu; Tools submenu; chat options; Settings; Settings with its select open; Settings after the select; after Settings; browser pane; browser pane under the Tools menu; Library; Library under its Filter menu; Library after. Result: identical in both engines.

  • An outside click on another chat's sidebar row closes the menu and does not navigate.
  • Wheel over the thread or the sidebar under the Tools menu, and over the thread under Settings, moves them 0 px in Chromium. With no modal open it scrolls normally.
  • Sidebar row actions and the Library toolbar restoration are none under a modal and auto without one.
  • The browser pane under a menu hits its cover.
  • The submenu item and the select option are hit.
  • The Settings tab under its open select is not hit.
  • A .pointer-events-auto element inside Settings reads none while its select is open (the lower-layer rule) and auto otherwise.
  • No marks are left after any close.

Tests

  • New tests/modal-pointer-lock.test.ts, which fails on main:
    • no [style*=...] selector remains in index.css;
    • the thread and path attributes are only ever used on the element they style;
    • with a fake DOM, the lock splits the page so that every element outside the thread roots is in exactly one region or on the path, and no message is marked;
    • children mounted next to the thread while the lock is up join a region;
    • stacked layers are marked lower and lifted again, including a layer that was already lower when the lock went up;
    • unlocking clears everything and leaves only the body observer;
    • a second install is a no-op.
  • modal-lock-thread-cover.test.ts and webkit-modal-page-restyle.test.ts now follow the new selectors.
  • tests/studio/playwright_keyboard_shortcuts.py: 232/232 on base and head (Chromium + WebKit). tests/studio/playwright_find_in_page.py: 452/452 on base and head.
  • Long-chat gate (tests/studio/playwright_long_thread_gate.py): PASS on head, with and without the watcher on its page.
  • Full frontend suite: 15,121 pass. The one failure is the KaTeX stylesheet Vite test, which also fails on main with a symlinked node_modules. node scripts/typecheck.mjs --tests is clean.

Not covered

  • Real Safari and WKWebView on macOS. All WebKit numbers come from Playwright WebKit (WebKitGTK) with the macOS UA. Windows WebView2 was not run.
  • Wheel under a modal in local WebKit is not decisive. After any wheel, with or without a modal, the 150-turn thread jumps by 24-27k px in both arms, so that check only shows the two arms agree.
  • The Skills list under its actions menu (the case the lower-layer rule was written for) was not driven in the app, because this test home lists no skills. It is covered by the synthetic restoration inside Settings under its select, and by the unit test.
  • There was no A/A timing run on this loaded host. The model picker, which takes no body lock, is the noise reference instead.
  • Touch devices (iPad Safari).

…t above the thread

Radix writes pointer-events: none on <body> for every modal menu and dialog. The rules that
depend on it (thread and side pane covers, inert page and lower-layer restorations) were keyed
on that inline style, so each open and close made WebKit walk and re-match every element under
<body>, the whole thread included: about 45 ms per write at 150 turns, on top of the flat
inherited restyle the thread scroller already stops.

src/lib/modal-pointer-lock.ts keeps Radix's write and mirrors the lock onto attributes: the
thread roots, their ancestors (subject-only rules), every other subtree of the page (descendant
rules, as before), and dialogs or menus a stacked layer turned into a lower layer. index.css keys
the same four rules on those, so a toggle only reaches the elements they style.
…tsx does

The gate's lock check toggles the body lock; without the watcher the modal rules never engaged
there, so a costly rule keyed on the new attributes would have gone unseen.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T10:21:05.024163Z e84e41c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: e84e41ce61

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit be2e825 into main Nov 7, 2026
@danielhanchen
danielhanchen deleted the studio-menu-pointer-lock branch November 7, 2026 12:48
iam-knoit pushed a commit to SynhalaAI/unsloth that referenced this pull request Nov 9, 2026
…i#17284)

## Problem

On macOS WebKit, opening or closing a menu or dialog still cost far more
in a long chat than in a short one (Round 11 real runners: composer menu
about 200 to 250 ms at 150 turns against 72 to 100 ms at 1 turn;
Settings 390 to 560 ms against 232 to 306 ms). unslothai#16902 and unslothai#17006 removed
the whole-thread restyles that Radix's body `pointer-events` lock caused
in Chromium; WebKit still paid for the scroll lock.

## Cause

Every modal open and close sets or clears `body[data-scroll-locked]`,
and WebKit re-ran style and layout over the whole page for it:

1. `hub.css` still had `body[data-scroll-locked] .hub-modal-pe-guard`. A
body attribute before a descendant combinator makes WebKit re-check
every element under `<body>`, every message included. unslothai#17006 removed
this pattern from `index.css`, but its test only scanned `index.css`.
2. The lock rule flips `<body>` from `overflow: visible; position:
static` to `hidden; relative`, which re-lays out the whole long page in
WebKit.

WebKit microbenchmark (Playwright WebKit, Mac UA, one lock on and off
with a forced style and layout flush, median of 5): 3 ms at 1 turn; 77
to 121 ms at 150 turns on main; 22 to 33 ms with the hub rule removed; 0
ms with the hub rule removed and the body overflow and position preset.

## Change

- `hub.css`: the body-keyed rule is removed. Every `.hub-modal-pe-guard`
element also carries `pointer-events-auto`, which unslothai#17006's region-keyed
rule already sets to `none` behind a modal (and the lower-layer rule
covers stacked dialogs). Checked in Chromium and WebKit: the guard reads
`auto` at rest, `none` with Settings open and `auto` after close, on
main and here alike.
- `index.css`: `@media screen { body { overflow: hidden; position:
relative; } }`, so the lock changes nothing on `<body>`. Print keeps
document flow. Desktop already sets `html.tauri body { overflow: hidden
}`. No route scrolls the document itself: on main, every route at
1440x900 and 900x560 has the document's scroll height equal to the
viewport (pages scroll their own containers).
- `tests/modal-pointer-lock.test.ts` now scans every `src/**/*.css` and
fails on `data-scroll-locked`, `aria-hidden` or `data-aria-hidden`
before a combinator (it names the hub rule on main).
`tests/webkit-modal-page-restyle.test.ts` pins the screen preset.

## Results

Real macos-15 Safari WebKit (two runners, main twice as A/A, arms
rotated, 3 cycles each), open / close ms at 150 turns, main / main (A/A)
/ this PR:

| Surface | solid | Glass |
| --- | --- | --- |
| Composer menu open | 228 / 246 / 198 | 314 / 347 / 198 |
| Composer menu close | 262 / 304 / 218 | 309 / 314 / 218 |
| Command palette close | 315 / 348 / 290 | 382 / 396 / 322 |
| Search chats close | 352 / 364 / 318 | 406 / 415 / 333 |
| Model picker open | 259 / 310 / 306 | 300 / 330 / 227 |
| Settings close | 364 / 414 / 384 | 465 / 455 / 388 |
| Settings open | 404 / 436 / 404 | 510 / 618 / 543 |

Closes improve on almost every surface beyond the A/A spread, and opens
do for the composer menu and, with Glass, the model picker and search.
Settings and shortcuts opens stay within noise: their cost is mostly the
dialog mount. Long chats still cost more than short ones on macOS WebKit
(composer menu about 200 ms against 72 to 100 ms at 1 turn); the
remaining growth is not from the attribute toggles measured here.

No visible change. Frontend suite 15,326 pass, `tsc` clean, full
long-chat gate passes.

---------

Co-authored-by: Nilay Yadav <[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.

1 participant