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.

sdk/ui: a root mounted on another element takes its clicks, and a link's press follows its href

shinyobjectzfb37428ddf9a
diff --git a/sdk/ui/src/dom/keys.ts b/sdk/ui/src/dom/keys.ts
index 666c0edc..52ff091b 100644
--- a/sdk/ui/src/dom/keys.ts
+++ b/sdk/ui/src/dom/keys.ts
@@ -114,6 +114,23 @@ export function activate(t: Element_, ev: Event_, trusted: boolean) {
if (btn instanceof HTMLButtonElement_ && btn.form) {
if (btn.type === "submit") submitForm(btn.form as HTMLFormElement_, btn);
else if (btn.type === "reset") (btn.form as HTMLFormElement_).reset();
+ } else if (btn.localName === "a" && btn.hasAttribute("href")) {
+ // a link's default action, as Chrome's: its fragment on this page (location's hashchange, which a hash router
+ // hears: raea cloud's dashboard in raea.app moved between its pages on none of its rail's links, 11-23), another
+ // page of this one's origin, or a new window for one that asks (target) or is elsewhere
+ const href = btn.getAttribute("href") ?? "";
+ const loc = (g as { location?: { hash: string; href: string; assign?: (u: string) => void } }).location;
+ if (href.startsWith("#")) {
+ if (loc) loc.hash = href;
+ } else if (href && !/^(javascript|mailto|tel):/i.test(href)) {
+ let url: URL | null = null;
+ try {
+ url = new URL(href, loc?.href ?? "http://localhost/");
+ } catch {}
+ const blank = (btn.getAttribute("target") ?? "") !== "" && btn.getAttribute("target") !== "_self";
+ if (url && !blank && loc && url.origin === new URL(loc.href).origin) loc.assign?.(url.href);
+ else if (url) (g as { open?: (u: string, t?: string) => unknown }).open?.(url.href, "_blank");
+ }
} else if (btn instanceof HTMLLabelElement_) {
// a click on a label (on it or on what's in it, its control aside) clicks its control
const c = btn.control;
diff --git a/sdk/ui/src/react.ts b/sdk/ui/src/react.ts
index 9846d472..4a96f9b1 100644
--- a/sdk/ui/src/react.ts
+++ b/sdk/ui/src/react.ts
@@ -245,6 +245,10 @@ function start(opts: RootOptions) {
}
function container(at: number, opts: RootOptions) {
const box = domMode ? dom.container(at) : { id: at };
+ // React's handlers run from its root container's listeners: the host's root has them from the start, and a root
+ // mounted on another element (raea.app's welcome and pages over the dashboard, 11-23) needs them as a portal does,
+ // or it draws and no press reaches its handlers
+ if (domMode && at !== ROOT) dom.reactContainer(box);
return reconciler.createContainer(box, ConcurrentRoot, null, false, null, opts.identifierPrefix ?? "", opts.onUncaughtError ?? uncaught, opts.onCaughtError ?? logError, opts.onRecoverableError ?? logError, () => {}, null);
}
diff --git a/sdk/ui/test/dom.test.tsx b/sdk/ui/test/dom.test.tsx
index f0ca27d4..20133b44 100644
--- a/sdk/ui/test/dom.test.tsx
+++ b/sdk/ui/test/dom.test.tsx
@@ -117,6 +117,54 @@ describe("P18 DOM surface", () => {
expect(log).toEqual(["allowed", "behind"]);
});
+ test("a link's press follows it: its fragment becomes location's hash, unless a listener prevents it", () => {
+ const host = fresh();
+ function App() {
+ const r = useRef<any>(null);
+ // markup set as HTML, as raea cloud's dashboard builds its rail in raea.app
+ useEffect(() => void (r.current.innerHTML = '<a data-testid="go" href="#/projects">Projects</a><a data-testid="stay" href="#/agents">Agents</a>'), []);
+ return <nav data-testid="root" ref={r} onClick={() => {}} />;
+ }
+ mount(<App />, 0, { dom: true });
+ flush();
+ snap(host, { root: [0, 0, 200, 100], go: [0, 0, 200, 40], stay: [0, 50, 200, 40] });
+ const was = g.location.hash;
+ host.send("root", "pointerdown", { ...press(10, 10), target: host.tid("go") });
+ host.send("root", "pointerup", { ...press(10, 10), target: host.tid("go") });
+ expect(g.location.hash).toBe("#/projects");
+ g.document.querySelector('[data-testid="stay"]').addEventListener("click", (e: any) => e.preventDefault());
+ host.send("root", "pointerdown", { ...press(10, 60), target: host.tid("stay") });
+ host.send("root", "pointerup", { ...press(10, 60), target: host.tid("stay") });
+ expect(g.location.hash).toBe("#/projects");
+ g.location.hash = was;
+ });
+
+ test("a second React root on a layer over the page (raea.app's welcome) takes its button's click", () => {
+ const host = fresh();
+ const log: string[] = [];
+ mount(<main data-testid="page" onClick={() => log.push("page")}>dashboard</main>, 0, { dom: true });
+ flush();
+ const layer = g.document.createElement("div");
+ layer.setAttribute("data-testid", "layer");
+ g.document.body.appendChild(layer);
+ flush();
+ mount(
+ <div data-testid="over" className="fixed inset-0">
+ <button data-testid="start" onClick={() => log.push("start")}>Start</button>
+ </div>,
+ layer.__wid,
+ { dom: true },
+ );
+ flush();
+ snap(host, { page: [0, 0, 800, 600], layer: [0, 0, 0, 0], over: [0, 0, 800, 600], start: [600, 500, 60, 30] });
+ const target = host.tid("start");
+ // to the nearest element up from the button that listens, as the host sends it
+ const hearer = ["start", "over", "layer", "page"].find((t) => (host as any).get(host.tid(t)).on.has("pointerdown"));
+ host.send(hearer!, "pointerdown", { ...press(620, 510), target });
+ host.send(hearer!, "pointerup", { ...press(620, 510), target });
+ expect(log).toEqual(["start"]);
+ });
+
test("a press: capture and bubble through native and React listeners in Chrome's order, then click", () => {
const host = fresh();
const log: string[] = [];