Skip to content

Custom widgets and Custom Dashboards read the app's mode, variant and palette - #23

Open
gustavo-aguiar wants to merge 13 commits into
mainfrom
feat/appearance
Open

gustavo-aguiar wants to merge 13 commits into
mainfrom
feat/appearance

Conversation

@gustavo-aguiar

@gustavo-aguiar gustavo-aguiar commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Feature · Custom widgets and Custom Dashboards read the app's mode, variant and palette

A custom widget cannot tell whether TagoRUN shows Light or Dark: the host sends no mode and pins the iframe to color-scheme: normal, so an application built from custom widgets shows white pages next to a dark sidebar (tago-io/issues#1233). This PR is the SDK side: typed access to the appearance the host now sends, and an opt-in that lets the widget's page follow the app's color scheme.

  • Widget appearance · TagoIO.onSyncAppearance, useAppearance()
    { mode, variant, palette } at start and on every change, with no reload. mode is light or dark; with System it is what the OS resolves to. It lives in its own appearance state, so a theme switch leaves userInformation and onSyncUserInformation alone. On a host that predates this it never arrives, and useAppearance() stays null.
  • Color-scheme opt-in · ready({ features: ["color-scheme"] })
    The SDK sets color-scheme on the page root and reports it to the host, which gives the iframe the same scheme. light-dark(), prefers-color-scheme, system colors and form controls follow the app, and a page that paints no background stays transparent. Without the opt-in nothing changes.
  • Custom Dashboard appearance · TagoDashboard.appearance, useDashboardAppearance()
    Reads variant and palette from dashboard:theme; both read default on an older host. A theme listener and an appearance listener both see the new values.
  • Variants are different in TagoRUN and Admin · package READMEs
    In Admin, variant is one of Admin's four variants. In a TagoRUN app it is default or the ID of a variant the builder named. palette is the base palette both share.
  • Isolated callbacks · TagoIO.on*
    A callback that throws is logged, and the callbacks after it still run, once per change. Before, it stopped onStart, onRealtime and onError until the next change.
How a widget follows the app's colors

Read the mode and apply your own colors. This works with or without the opt-in.

window.TagoIO.onSyncAppearance(function (appearance) {
  // appearance = { mode: "dark", variant: "default", palette: "default" }
  document.documentElement.dataset.mode = appearance.mode;
});
window.TagoIO.ready();
body { background: #ffffff; color: #14161a; }
[data-mode="dark"] body { background: #14161a; color: #f2f3f5; }
const appearance = useAppearance(); // null until the first one arrives
const dark = appearance?.mode === "dark";

Let the iframe follow the app. Opt in, then style with light-dark(). The SDK sets color-scheme on the root for you, so do not declare one yourself.

window.TagoIO.ready({ features: ["color-scheme"] });
// React: <TagoIOProvider readyOptions={{ features: ["color-scheme"] }}>
body {
  background: light-dark(#ffffff, #14161a);
  color: light-dark(#14161a, #f2f3f5);
}

@media (prefers-color-scheme) inside the widget follows the app too, live switches included, in Chrome and Safari. A widget that loads the optional custom-widget.css overrides its white body and light form fields, as the JS README shows.

Custom Dashboards read { mode, variant, palette } from TagoDashboard.appearance (get, subscribe) or useDashboardAppearance().

Branch on palette when only the colors matter: in a TagoRUN app, variant is the ID of a variant the builder named.

Why

The host sends a later change as its own appearance message rather than a second userInformation: widgets run one-time setup in onSyncUserInformation, and an older SDK ignores a message it does not know. userInformation therefore arrives once after each ready(), as before, and WidgetState.appearance keeps the last appearance the host sent.

The host switches the iframe's scheme only after the page reports the scheme it put on its root. When the iframe and its root disagree, the browser paints an opaque canvas in the root's scheme, so a host that switched first would flash the widget in the old color. An opted-in loaded message carries the scheme already on the root and a per-page ID. On pagehide the SDK takes its report back with that ID: Chrome delivers a message from a leaving page with no source, so the host matches it by page ID and origin. A page the browser restores from its back-forward cache reports its scheme again.

The scheme on the root belongs to the page, not to one store. A store that replaces another, after a provider remount or Fast Refresh, starts from the scheme on the root, and clears it when it does not opt in. The page takes its report back as it leaves even after destroy(). The page ID is made on first use, because Cloudflare Workers refuse random values at module load. A ready() on a destroyed store does nothing until initialize().

Limits that stay:

  • Live mode switch · opted-in widgets
    The transparent areas can show the canvas in the new color for one frame while the widget repaints.

Test plan

  • SDK gates · vp run build, vp check, vp run check:types, vp test run
    46 files and 662 tests pass, and so does CI. The committed packages/js/dist matches a fresh build.
  • Appearance state · color-scheme.test.ts, use-appearance.test.tsx
    A later appearance message updates the appearance and leaves userInformation as the handshake sent it. A repeat keeps the same object, and a message without one keeps the last.
  • Root ownership · color-scheme.test.ts
    A store that replaces another starts from the scheme on the root; the page takes it back as it leaves after destroy(); the page ID is made on the first opt-in; a ready() after destroy() posts nothing and leaves the root alone; an as const options object type-checks.
  • JS adapter · custom-widget.test.ts
    A throwing callback is logged and the rest still run once per change. A theme switch does not run onSyncUserInformation again. TagoIO.ready(null) posts a bare loaded, as it always has.
  • Dashboard · appearance.test.ts, use-dashboard-appearance.test.tsx
    The appearance starts from ?theme= on the URL, and inside either listener both values are new.
  • Live switch in Admin · account menu, with the matching host change
    Switching to Dark, Eye Comfort, then back to Light and Default: onSyncUserInformation ran once, and onSyncAppearance ran at start and after each switch. The opted-in iframe and root followed every switch; the widget without the opt-in stayed normal.
  • prefers-color-scheme in the iframe · Chrome and WebKit, no media emulation
    A cross-origin iframe on dark reads dark under a light preference, one on light reads light under a dark one, and switching the iframe's scheme fires change inside. Playwright's default light emulation forces every frame, which is why an earlier run read the OS.
  • TagoRUN in Dark, OS on Light · Chrome, with the matching Admin host change
    The opted-in widget's root and iframe are dark with a transparent background. The widget without the opt-in keeps normal and its own white card. With the host on main, nothing arrives.
  • Load and live switch · account menu, Light and Dark
    On load the opted-in iframe is never shown at normal. On a switch the widget root flips 4 to 16 ms before the iframe.
  • Admin · Dark with Eye Comfort
    dark · eye-comfort · eye-comfort arrives, and the opt-in behaves as in TagoRUN.
  • Custom Dashboard in TagoRUN · Dark with a named variant
    dashboard:theme carries { theme, variant, palette } on ready and on a live switch, and TagoDashboard.appearance reads it.
  • Leaving page · navigation to a page without the SDK
    The host puts the iframe back to normal.
  • Stock stylesheet override · custom-widget.min.css in Chrome
    With the root on dark, the stylesheet alone keeps a white body and light fields. With the README override, the body is transparent with white text, and the fields are dark.

Screenshots

Before: TagoRUN in Dark, host on main After: TagoRUN in Dark
Both probe widgets white, appearance waiting Opted-in probe dark, the other keeps its white card
Admin in Dark with Eye Comfort Custom Dashboard in TagoRUN with a named variant
Opted-in probe dark, the other white, both read dark eye-comfort Dashboard probe reads dark, the variant ID and high-contrast

Risk (CIA)

Likelihood: 🟢 Low | Impact: 🟢 Low | Exposure: 🟢 Low

Related

  • Host side · tago-io/admin#5565

Refs tago-io/issues#1233

A theme subscriber read the old appearance, because the theme store
notified before the appearance changed. Both values now change first.
- A theme switch reaches the widget as its own `appearance` message.
  `userInformation` stays a handshake-only message, so
  `onSyncUserInformation` and effects on `useUserInformation()` do not
  run again. The store keeps the appearance in its own state, and a
  message without one keeps the last, as the root does.
- The scheme the SDK wrote on the root is page state. A store that
  replaces another starts from it, a successor without the opt-in
  clears it, and the page takes it back as it leaves even after its
  store is destroyed.
- The page ID is made on first use: some server runtimes refuse random
  values while a module loads.
- The JS adapter runs each widget callback on its own and moves on
  before running them, so one that throws no longer blocks the others
  or repeats. `TagoIO.ready(null)` posts a bare `loaded` again.

This branch has not been deployed

No deployments
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