Stop a bound dashboard setting rebuilding every tile

A setting driven by a flow re-renders whatever reads it on every message.
`LookProvider` and `PaletteProvider` built a fresh context value each time,
and `CanvasSurface` called its render prop again, so a value on `theme`,
`palette`, `look`, `background`, `touch` or `locked` reconciled the whole
widget tree and re-ran every chart's `setData`.

The palette is now read as roles once per set of colours, the tokens and
both context values are memoised on it, and the canvas holds the tiles it
built while the callback and the scale stay put. `WidgetBody` is memoised
too: its two props are stable while the document is, so a grid rendered for
its own reasons no longer rebuilds every chart with it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VfgHWr7AQDiLsTQVu7hpKB
This commit is contained in:
2026-09-14 14:38:59 +02:00
co-authored by Claude Opus 5
parent 265cea38d2
commit aa3e2e8b81
6 changed files with 95 additions and 23 deletions
@@ -10,17 +10,17 @@
* menu is portalled to `body` and lands outside the canvas. A surface that
* floats out of the tree re-states the tokens rather than borrowing the app's.
*/
import { createContext, useContext } from "react"
import { createContext, useContext, useMemo } from "react"
import type { DashboardDef_Output } from "@/client"
import { useTheme } from "@/components/theme-provider"
import {
type Look,
useDashboardLook,
useDashboardPalette,
useDashboardRoles,
useDashboardTheme,
} from "../../settings"
import { rolesOf, tokenStyle } from "./theme"
import { tokenStyle } from "./theme"
type Canvas = {
look: Look
@@ -30,6 +30,9 @@ type Canvas = {
style: React.CSSProperties
}
/** A dashboard that names no palette, as one object rather than one per render. */
const NO_TOKENS: React.CSSProperties = {}
const LookContext = createContext<Canvas>({
look: "material",
touch: false,
@@ -60,11 +63,18 @@ export function useCanvasRoot(
) {
const { resolvedTheme } = useTheme()
const stated = useDashboardTheme(dashboard)
const roles = rolesOf(useDashboardPalette(dashboard))
const roles = useDashboardRoles(dashboard)
const look = useDashboardLook(dashboard)
// The tokens are handed to every widget through the context below, so they
// have to outlive a render: a fresh object here is a re-render of the whole
// canvas for every message a bound setting carries.
const style = useMemo(
() => (roles ? (tokenStyle(roles) as React.CSSProperties) : NO_TOKENS),
[roles],
)
return {
className: stated || (resolvedTheme === "dark" ? "dark" : "light"),
style: (roles ? tokenStyle(roles) : {}) as React.CSSProperties,
style,
"data-look": look,
"data-touch": touch ? "" : undefined,
"data-palette": roles ? "" : undefined,
@@ -89,15 +99,15 @@ export function LookProvider({
children: React.ReactNode
}) {
const root = useCanvasRoot(dashboard, touch)
return (
<LookContext.Provider
value={{
look: root["data-look"],
touch: root["data-touch"] !== undefined,
style: root.style,
}}
>
{children}
</LookContext.Provider>
const look = root["data-look"]
const touched = root["data-touch"] !== undefined
const { style } = root
// A bound setting wakes this component on every message its flow sends, and
// what the widgets below actually react to is the value — so it changes only
// when one of the three things it carries does.
const value = useMemo(
() => ({ look, touch: touched, style }),
[look, touched, style],
)
return <LookContext.Provider value={value}>{children}</LookContext.Provider>
}
@@ -182,4 +182,19 @@ for (const [name, hexes] of [
}
}
// --- what the memo key assumes -------------------------------------------
//
// `useDashboardRoles` keeps one `Roles` per palette, keyed on the colours
// joined, so that the look and palette contexts can hand a wall panel the same
// value on every message a bound setting carries. That key is only faithful
// while roles are a function of the hexes and of nothing else.
for (const hexes of [CHOSEN, ["#0f172a"], ["#f4f1ea", "#ffffff"], []]) {
assert.deepEqual(
rolesOf([...hexes]),
rolesOf([...hexes]),
"the same colours are the same roles; the palette is the whole key",
)
}
console.log("theme: ok")