Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: stop polling focus while message menus are open (#12323)
Michael Han4700dbc86f0e
diff --git a/studio/frontend/src/components/assistant-ui/use-action-bar-focus-reveal.ts b/studio/frontend/src/components/assistant-ui/use-action-bar-focus-reveal.tsindex a2c22944e4b..83e9ffbea00 100644--- a/studio/frontend/src/components/assistant-ui/use-action-bar-focus-reveal.ts+++ b/studio/frontend/src/components/assistant-ui/use-action-bar-focus-reveal.ts@@ -35,6 +35,7 @@ export function useActionBarFocusReveal() {const rootRef = useRef<HTMLDivElement | null>(null);const focusWithinRef = useRef(false);const clearFrameRef = useRef<number | null>(null);+ const popupObserverRef = useRef<MutationObserver | null>(null);// The More menu is portaled OUTSIDE the message, so focus entering it looks like a blur.// Its own interaction lock keeps the bar mounted meanwhile, but the trigger this hook has to@@ -66,23 +67,16 @@ export function useActionBarFocusReveal() {cancelAnimationFrame(clearFrameRef.current);clearFrameRef.current = null;}+ popupObserverRef.current?.disconnect();+ popupObserverRef.current = null;}, []);- /**- * Decide, a frame from now, whether focus has really left, and keep asking until it has.- *- * Deferred rather than read off `relatedTarget`: that is null both for focus going to the- * browser chrome and for focus entering a portal, and it says nothing at all when the- * focused element is REMOVED, which is how a menu closes and which Chrome reports with no- * focusout event whatsoever. Reading `document.activeElement` a frame later answers all of- * them. Clearing late costs a frame of a mounted bar; clearing early destroys the element- * the user is on, so late is the safe direction.- */+ /** Defer clearing until focus settles; observe popup closure instead of polling. */const scheduleClear = useCallback((restart: boolean) => {- if (clearFrameRef.current !== null) {+ if (clearFrameRef.current !== null || popupObserverRef.current !== null) {if (!restart) return;- cancelAnimationFrame(clearFrameRef.current);+ cancelPendingClear();}const decide = () => {clearFrameRef.current = null;@@ -90,12 +84,32 @@ export function useActionBarFocusReveal() {if (!el || !focusWithinRef.current) return;const active = document.activeElement;if (active && el.contains(active)) return;- if (openPopupTrigger()) {- // Focus is in this message's own portaled menu, whose interaction lock is holding- // the bar open anyway. Deciding now would be wrong and deciding never would pin the- // bar open for good, so ask again next frame; the loop lasts only as long as the- // menu is open on this one message.- clearFrameRef.current = requestAnimationFrame(decide);+ const trigger = openPopupTrigger();+ if (trigger) {+ // Watch closure or removal without waking for streamed content.+ const observer = new MutationObserver(() => {+ if (+ el.contains(trigger) &&+ trigger.getAttribute("aria-expanded") === "true"+ )+ return;+ observer.disconnect();+ popupObserverRef.current = null;+ clearFrameRef.current = requestAnimationFrame(decide);+ });+ popupObserverRef.current = observer;+ observer.observe(trigger, {+ attributes: true,+ attributeFilter: ["aria-expanded"],+ });+ for (+ let parent = trigger.parentElement;+ parent;+ parent = parent.parentElement+ ) {+ observer.observe(parent, { childList: true });+ if (parent === el) break;+ }return;}focusWithinRef.current = false;@@ -105,7 +119,7 @@ export function useActionBarFocusReveal() {};clearFrameRef.current = requestAnimationFrame(decide);},- [aui, openPopupTrigger],+ [aui, openPopupTrigger, cancelPendingClear],);// onFocus/onBlur on a container are focusin/focusout in React, so they give focus-within.@@ -114,10 +128,7 @@ export function useActionBarFocusReveal() {const el = rootRef.current;const target = event.target as Node | null;if (el && target && !el.contains(target)) {- // React bubbles focus events out of PORTALS along the React tree, so this is this- // message's own menu, rendered into document.body. Focus is not in the subtree, so do- // not cancel the watchdog -- the menu will take focus with it when it unmounts, and- // that removal fires no focusout to wake us up again.+ // Portaled focus must keep the pending popup-close check.scheduleClear(false);return;}diff --git a/studio/frontend/tests/fixtures/user-message-time/main.tsx b/studio/frontend/tests/fixtures/user-message-time/main.tsxindex 8632cc36af9..2aee525278f 100644--- a/studio/frontend/tests/fixtures/user-message-time/main.tsx+++ b/studio/frontend/tests/fixtures/user-message-time/main.tsx@@ -4,6 +4,8 @@// Real runtime, focus handling, footer, timestamp and stylesheet. Only message// data and the unrelated action handlers are fixtures; no backend is needed.import {+ ActionBarMorePrimitive,+ ActionBarPrimitive,AssistantRuntimeProvider,MessagePrimitive,ThreadPrimitive,@@ -101,6 +103,33 @@ function Assistant() {</MessagePrimitive.Root>);}+function PopupAssistant() {+ const reveal = useActionBarFocusReveal();+ return (+ <MessagePrimitive.Root+ className="aui-assistant-message-root"+ tabIndex={0}+ {...reveal}+ >+ <MessagePrimitive.Parts />+ <ActionBarPrimitive.Root+ autohide="always"+ className="aui-assistant-action-bar-root"+ >+ <ActionBarMorePrimitive.Root modal={false}>+ <ActionBarMorePrimitive.Trigger>More</ActionBarMorePrimitive.Trigger>+ <ActionBarMorePrimitive.Content+ onCloseAutoFocus={(event) => event.preventDefault()}+ >+ <ActionBarMorePrimitive.Item>+ Menu action+ </ActionBarMorePrimitive.Item>+ </ActionBarMorePrimitive.Content>+ </ActionBarMorePrimitive.Root>+ </ActionBarPrimitive.Root>+ </MessagePrimitive.Root>+ );+}declare global {interface Window {messageTimeFixture: {@@ -148,7 +177,12 @@ function App() {className="aui-thread-viewport min-w-0 overflow-auto px-5"><ThreadPrimitive.Messages- components={{ UserMessage: User, AssistantMessage: Assistant }}+ components={{+ UserMessage: User,+ AssistantMessage: params.has("popup")+ ? PopupAssistant+ : Assistant,+ }}/></ThreadPrimitive.Viewport></ThreadPrimitive.Root>diff --git a/tests/studio/playwright_user_message_time.py b/tests/studio/playwright_user_message_time.pyindex 3fb6efa5cf1..5add11e617b 100644--- a/tests/studio/playwright_user_message_time.py+++ b/tests/studio/playwright_user_message_time.py@@ -121,6 +121,28 @@ async def check(url):await page.keyboard.press("Shift+Tab")assert await delete.evaluate("(e) => e === document.activeElement")+ # Keep open menus idle; release hover on close without restoring focus.+ await page.goto(url + "?popup")+ await page.locator(".aui-assistant-message-root").focus()+ await page.get_by_role("button", name = "More", exact = True).click()+ await page.get_by_role("menuitem", name = "Menu action").wait_for()+ await page.mouse.move(799, 640)+ await paint(page)+ await page.evaluate("""async () => {+ const root = document.querySelector('.aui-assistant-message-root');+ const original = root.querySelector.bind(root);+ window.popupQueries = 0;+ root.querySelector = (...args) => {+ window.popupQueries++;+ return original(...args);+ };+ for (let i = 0; i < 8; i++) await new Promise(requestAnimationFrame);+ }""")+ assert await page.evaluate("window.popupQueries") == 0+ await page.keyboard.press("Escape")+ await paint(page)+ assert await page.get_by_role("button", name = "More", exact = True).count() == 0+print("Keyboard passed; checking layout", flush = True)# Long dates, translations and font scaling must fit the viewport;# Copy/Edit/Fork/Delete and branch targets must retain their size.