Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

A React page's getComputedStyle reads a colour and a custom property from the host (sdk/ui's DOM layer)

shinyobjectz2b9a95366a5e
diff --git a/sdk/ui/src/dom/computed.ts b/sdk/ui/src/dom/computed.ts
index 24e22773..b9dda16e 100644
--- a/sdk/ui/src/dom/computed.ts
+++ b/sdk/ui/src/dom/computed.ts
@@ -115,15 +115,21 @@ export function computedOf(el: Element_): Computed {
return out;
}
+/** What only the host's cascade knows on a React page: colours and custom properties. */
+const HOST_ONLY = /^--|color$|^(fill|stroke)$/;
+
export function getComputedStyle(el: Element_, _pseudo?: string | null): any {
if (!(el instanceof Element_)) throw new TypeError("getComputedStyle: not an Element");
const c = computedOf(el);
// with the host's cascade on, each value is stylo's for the element as the JS has written it so far; this cascade's
- // answers where the host has none (an unwired element, a call outside a step)
- const host = htmlDoc && el._wired && el.__wid !== undefined ? el.__wid : undefined;
+ // answers where the host has none (an unwired element, a call outside a step). A React page (DOM mode) asks the host
+ // only for what this cascade can't know, a colour or a custom property (a class's color, a var()): those read black
+ // and empty here, where the boxes and the classes libraries read are answered without a call
+ const wid = el._wired && el.__wid !== undefined ? el.__wid : undefined;
const ask = (prop: string): string | undefined => {
- if (host === undefined) return undefined;
const name = prop.startsWith("--") ? prop : kebab(prop);
+ const host = htmlDoc || HOST_ONLY.test(name) ? wid : undefined;
+ if (host === undefined) return undefined;
try {
const v = wire.callSync<Record<string, string>>("style.computed", { id: host, names: [name] })?.[name];
return v ? v : undefined;
diff --git a/sdk/ui/test/dom.test.tsx b/sdk/ui/test/dom.test.tsx
index 35f213b2..14e7897f 100644
--- a/sdk/ui/test/dom.test.tsx
+++ b/sdk/ui/test/dom.test.tsx
@@ -4,7 +4,7 @@
// Chrome by the parity cases (sdk/ui/parity/cases/p18-dom); this is the fast check.
import { beforeAll, describe, expect, test } from "bun:test";
import { useEffect, useRef, useState } from "react";
-import { install, layout } from "../src/dom";
+import { getComputedStyle, install, layout } from "../src/dom";
import { createPortal, flushSync, mount } from "../src/react";
import { flush } from "../src/wire";
import { fresh, snap } from "./host";
@@ -423,3 +423,44 @@ describe("video and audio play on the host's player (runtime/dom media.rs, raea-
expect(seen).toEqual(["volumechange:0.4"]);
});
});
+
+// getComputedStyle on a React page: the boxes and the classes libraries read come from the page's own cascade, with
+// no host call; a colour or a custom property, which that cascade can't know (a class's color, a var()), is the
+// host's (stylo's style.computed), as an HTML page's every value is
+describe("P18 DOM surface: computed style", () => {
+ test("a React page asks the host for a colour and a custom property, never for display or a size", () => {
+ const host = fresh();
+ let el: any;
+ mount(<span data-testid="gold" className="block text-gold" ref={(e: any) => (el = e)} />, 0, { dom: true });
+ snap(host, { gold: [0, 0, 40, 20] });
+ const asked: { name: string; args: any }[] = [];
+ const was = g.__ui_sync;
+ g.__ui_sync = (name: string, args: string) => {
+ const a = JSON.parse(args);
+ asked.push({ name, args: a });
+ const v: Record<string, string> = { color: "rgb(240, 180, 40)", "background-color": "rgba(0, 0, 0, 0)", "--accent": "#5b8cff" };
+ return JSON.stringify({ ok: { [a.names[0]]: v[a.names[0]] ?? "" } });
+ };
+ let got: { display: string; askedForDisplay: number; color: string; accent: string; background: string };
+ try {
+ const c = getComputedStyle(el);
+ const display = c.display;
+ got = { display, askedForDisplay: asked.length, color: c.color, accent: c.getPropertyValue("--accent"), background: c.backgroundColor };
+ } finally {
+ g.__ui_sync = was;
+ }
+ expect(got).toEqual({ display: "block", askedForDisplay: 0, color: "rgb(240, 180, 40)", accent: "#5b8cff", background: "rgba(0, 0, 0, 0)" });
+ expect(asked.map((a) => [a.name, a.args.id === el.__wid, a.args.names[0]])).toEqual([
+ ["style.computed", true, "color"],
+ ["style.computed", true, "--accent"],
+ ["style.computed", true, "background-color"],
+ ]);
+ });
+
+ test("with no host to ask, a colour is the page cascade's, as before", () => {
+ fresh();
+ let el: any;
+ mount(<span style={{ color: "red" }} ref={(e: any) => (el = e)} />, 0, { dom: true });
+ expect(getComputedStyle(el).color).toBe("red");
+ });
+});