Skip to content

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.

Studio: scope the sidebar drag's custom-property writes off the thread's ancestors (#9400)

Daniel Hanae55e8e0cbd0
diff --git a/studio/frontend/src/components/tauri/window-titlebar.tsx b/studio/frontend/src/components/tauri/window-titlebar.tsx
index 271022d2edf..f2091e9ecdf 100644
--- a/studio/frontend/src/components/tauri/window-titlebar.tsx
+++ b/studio/frontend/src/components/tauri/window-titlebar.tsx
@@ -350,6 +350,11 @@ export function WindowTitlebar({
{showSidebarSurface && (
<div
data-slot="window-titlebar-decoration"
+ // Marks a consumer of --studio-sidebar-live-width. Only this and the
+ // header below read it, so PANEL_RESIZE_SCOPED_VARS_ENABLED writes
+ // the live width here instead of on the document element, where it
+ // would restyle the whole document once per drag frame.
+ data-titlebar-live-width-scope=""
aria-hidden="true"
className="pointer-events-none absolute inset-x-0 top-[var(--studio-custom-titlebar-height)] z-[45] h-3"
>
@@ -376,6 +381,7 @@ export function WindowTitlebar({
"pointer-events-none absolute inset-x-0 top-0 z-[70] h-[var(--studio-custom-titlebar-height)] select-none text-foreground",
showSidebarSurface && "bg-sidebar text-sidebar-foreground",
)}
+ data-titlebar-live-width-scope=""
aria-label="Window titlebar"
>
{showSidebarSurface && (
diff --git a/studio/frontend/src/components/ui/panel-resize-handle.tsx b/studio/frontend/src/components/ui/panel-resize-handle.tsx
index 9c1fd70b6f1..6b0b558a161 100644
--- a/studio/frontend/src/components/ui/panel-resize-handle.tsx
+++ b/studio/frontend/src/components/ui/panel-resize-handle.tsx
@@ -12,6 +12,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip"
import { getClientPlatform } from "@/components/tauri/window-titlebar"
+import { PANEL_RESIZE_SCOPED_VARS_ENABLED } from "@/components/ui/panel-resize-recalc-flags"
/** Pointer travel (px) below which a drag counts as a plain click. */
const DRAG_SLOP = 4
@@ -56,6 +57,18 @@ export type PanelResizeHandleProps = {
className?: string
/** Mirrors the live width onto :root for chrome outside the panel. */
rootVar?: string
+ /**
+ * Narrower element for `cssVar`, replacing `target()` under
+ * PANEL_RESIZE_SCOPED_VARS_ENABLED. Must hold every consumer and nothing
+ * else: the write invalidates inherited style for everything below it.
+ */
+ scopedTarget?: () => HTMLElement | null
+ /**
+ * The elements that actually read `rootVar`, replacing
+ * `document.documentElement` under PANEL_RESIZE_SCOPED_VARS_ENABLED. Empty
+ * means no consumer, so the write is skipped.
+ */
+ rootVarTargets?: () => HTMLElement[]
}
/**
@@ -86,6 +99,8 @@ export function PanelResizeHandle({
dataSlot = "panel-resize-handle",
className,
rootVar,
+ scopedTarget,
+ rootVarTargets,
}: PanelResizeHandleProps) {
const ref = React.useRef<HTMLButtonElement>(null)
const dragRef = React.useRef<DragState | null>(null)
@@ -112,11 +127,17 @@ export function PanelResizeHandle({
committedRef.current = width
}, [width])
+ // Where `rootVar` is painted, resolved once on pointer down. Always
+ // [document.documentElement] with the flag off, which is what shipped.
+ const rootTargetsRef = React.useRef<HTMLElement[]>([])
+
const paint = React.useCallback(
(value: string) => {
targetRef.current?.style.setProperty(cssVar, value)
if (rootVar) {
- document.documentElement.style.setProperty(rootVar, value)
+ for (const el of rootTargetsRef.current) {
+ el.style.setProperty(rootVar, value)
+ }
}
},
[cssVar, rootVar],
@@ -148,7 +169,10 @@ export function PanelResizeHandle({
// Hand the property back to the committed value. A commit re-renders with
// the new width; a cancel or a no-commit drag keeps DOM and store in step.
paint(`${committedRef.current}px`)
- if (rootVar) document.documentElement.style.removeProperty(rootVar)
+ if (rootVar) {
+ for (const el of rootTargetsRef.current) el.style.removeProperty(rootVar)
+ }
+ rootTargetsRef.current = []
targetRef.current?.removeAttribute("data-resizing")
document.documentElement.removeAttribute("data-panel-resizing")
targetRef.current = null
@@ -161,7 +185,15 @@ export function PanelResizeHandle({
if (event.button !== 0) return
event.preventDefault()
event.currentTarget.setPointerCapture(event.pointerId)
- targetRef.current = target()
+ // Resolved once per drag, never per frame: the write is what costs, and a
+ // DOM walk here is already how `target()` worked.
+ targetRef.current =
+ (PANEL_RESIZE_SCOPED_VARS_ENABLED && scopedTarget?.()) || target()
+ rootTargetsRef.current = !rootVar
+ ? []
+ : PANEL_RESIZE_SCOPED_VARS_ENABLED
+ ? (rootVarTargets?.() ?? [])
+ : [document.documentElement]
// Collapsed: grow from the rendered size so the edge tracks the pointer.
const start = open ? width : measure()
dragRef.current = { startX: event.clientX, startWidth: start, moved: false }
diff --git a/studio/frontend/src/components/ui/panel-resize-recalc-flags.ts b/studio/frontend/src/components/ui/panel-resize-recalc-flags.ts
new file mode 100644
index 00000000000..bbb789cb58d
--- /dev/null
+++ b/studio/frontend/src/components/ui/panel-resize-recalc-flags.ts
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+// Panel-resize style-recalc flags, for staged rollout. The wiring stays in
+// place, so flipping a flag here is the only edit needed.
+//
+// WHY. An unregistered CSS custom property inherits, so writing one marks
+// inherited style dirty for every descendant. `PanelResizeHandle` writes two of
+// them on every animation frame of a sidebar drag -- `--sidebar-width` on
+// `[data-slot="sidebar-wrapper"]` and `--studio-sidebar-live-width` on
+// `document.documentElement` -- and both are ancestors of the chat thread, so
+// each one restyles the whole document.
+//
+// Measured on the heavy-thread harness (Chromium, `UpdateLayoutTree.elementCount`
+// from a devtools trace, median of 10 writes): the unscoped write is linear in
+// thread size, 4,286 elements / 67.9 ms at a 4,000-element thread up to 37,811 /
+// 576.6 ms at 35,119, while the scoped write is flat at 1 element / 0.02 ms.
+// End to end, driving the real handle through a real pointer drag over a
+// 12,026-element document (8 frames), 12,022 elements restyled per frame and
+// 1,088.25 ms total become 12 per frame and 3.76 ms, with identical rendered
+// geometry at all 8 positions and --sidebar-width resolving the same at every
+// consumer.
+//
+// Read those milliseconds as roughly 2.7x high: they come off the vite DEV
+// server, and the built production stylesheet costs 49.1 ms against 133.3 ms
+// over the same DOM. The mechanism is not a dev artefact -- unregistered
+// against `inherits: false` is 1333x on dev and 982x on prod, and elementCount
+// is a DOM property, identical on both.
+//
+// WHY NOT `@property { inherits: false }`. That is right only for a property
+// whose sole consumer is the element written to (`--aui-scroll-stabilizer`).
+// Here it would silently break both: `--sidebar-width` is read by
+// `[data-slot="sidebar"]`, `sidebar-gap` and `sidebar-container`, and
+// `--studio-sidebar-live-width` by `window-titlebar.tsx`, all descendants of
+// the element written to, so they would resolve the initial value instead.
+// Registration is not the mechanism, INHERITANCE is: the same probe registered
+// `inherits: true` restyled all 37,817 elements in 576 ms, `inherits: false`
+// restyled 1 in 0.08 ms. So the fix moves each write DOWN into a subtree that
+// consumes it -- `--sidebar-width` to `[data-slot="sidebar"]`, which holds
+// every consumer and not the thread, and `--studio-sidebar-live-width` to the
+// titlebar roots, which do not exist on a build without a custom titlebar,
+// where the write is skipped entirely.
+//
+// NOT ADDRESSED BY THIS FLAG, measured so the next person need not:
+// `html[data-panel-resizing] *` (index.css:1604-1605) is a universal descendant
+// selector on `<html>` and restyles the whole document too, 37,818 elements /
+// 575.0 ms. It fires twice per drag rather than once per frame, so the total is
+// smaller, and it is load-bearing for cursor correctness: a separate change
+// with its own visual risk, deliberately not bundled here.
+export const PANEL_RESIZE_SCOPED_VARS_ENABLED = false;
diff --git a/studio/frontend/src/components/ui/sidebar.tsx b/studio/frontend/src/components/ui/sidebar.tsx
index 1e25cc2f2bd..f3ff6b7771a 100644
--- a/studio/frontend/src/components/ui/sidebar.tsx
+++ b/studio/frontend/src/components/ui/sidebar.tsx
@@ -28,6 +28,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip"
import { PanelResizeHandle } from "@/components/ui/panel-resize-handle"
+import { PANEL_RESIZE_SCOPED_VARS_ENABLED } from "@/components/ui/panel-resize-recalc-flags"
import { useT } from "@/i18n"
import { useIsMobile } from "@/hooks/use-mobile"
import {
@@ -189,7 +190,14 @@ function SidebarProvider({
style={
{
// The drag handle writes this same property live while resizing.
- "--sidebar-width": `${width}px`,
+ // Under PANEL_RESIZE_SCOPED_VARS_ENABLED it moves DOWN to
+ // [data-slot="sidebar"], which holds every consumer, and cannot
+ // also stay here: this wrapper is an ancestor of the chat thread,
+ // so a declaration left behind would keep restyling the thread on
+ // every render even once the drag-time write had moved.
+ ...(PANEL_RESIZE_SCOPED_VARS_ENABLED
+ ? null
+ : { "--sidebar-width": `${width}px` }),
"--sidebar-width-icon": SIDEBAR_WIDTH_ICON,
...style,
} as React.CSSProperties
@@ -221,12 +229,21 @@ function Sidebar({
collapsible?: "offcanvas" | "icon" | "none"
collapseToZero?: boolean
}) {
- const { isMobile, state, openMobile, setOpenMobile, hasPinMode, pinned } = useSidebar()
+ const { isMobile, state, openMobile, setOpenMobile, hasPinMode, pinned, width } =
+ useSidebar()
+
+ // The scoped home for --sidebar-width: every consumer (this element,
+ // sidebar-gap, sidebar-container) is inside it and the chat thread is not.
+ // Empty with the flag off, where the wrapper keeps the declaration.
+ const scopedWidthStyle = (
+ PANEL_RESIZE_SCOPED_VARS_ENABLED ? { "--sidebar-width": `${width}px` } : {}
+ ) as React.CSSProperties
if (collapsible === "none") {
return (
<div
data-slot="sidebar"
+ style={scopedWidthStyle}
className={cn(
"bg-sidebar text-sidebar-foreground flex h-full w-(--sidebar-width) flex-col",
className
@@ -281,6 +298,7 @@ function Sidebar({
data-variant={variant}
data-side={side}
data-slot="sidebar"
+ style={scopedWidthStyle}
aria-hidden={(hasPinMode && !pinned && collapseToZero) || undefined}
inert={(hasPinMode && !pinned && collapseToZero) || undefined}
>
@@ -387,6 +405,18 @@ function SidebarResizeHandle({
cssVar="--sidebar-width"
// The custom titlebar renders outside the wrapper and cannot inherit it.
rootVar="--studio-sidebar-live-width"
+ // Both used only under PANEL_RESIZE_SCOPED_VARS_ENABLED. The rail sits
+ // inside sidebar-container, so [data-slot="sidebar"] always encloses it.
+ scopedTarget={() =>
+ ref.current?.closest<HTMLElement>('[data-slot="sidebar"]') ?? null
+ }
+ // Empty on every build without a custom titlebar: nothing reads the
+ // property there, so nothing needs writing.
+ rootVarTargets={() =>
+ Array.from(
+ document.querySelectorAll<HTMLElement>("[data-titlebar-live-width-scope]"),
+ )
+ }
measure={() =>
ref.current
?.closest<HTMLElement>('[data-slot="sidebar-container"]')