Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: allow tokens on the global root (#5867)

Oleg Isonen3a3b4d30a359
diff --git a/packages/project-build/src/runtime/styles.test.ts b/packages/project-build/src/runtime/styles.test.ts
index 8cf8584080..64f69c9ec3 100644
--- a/packages/project-build/src/runtime/styles.test.ts
+++ b/packages/project-build/src/runtime/styles.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, test } from "vitest";
import {
type Breakpoint,
getStyleDeclKey,
+ ROOT_INSTANCE_ID,
type Instance,
type Prop,
type StyleDecl,
@@ -1144,6 +1145,36 @@ describe("runtime style operations", () => {
]);
});
+ test("creates and attaches design tokens to the virtual root instance", () => {
+ const mutation = createAttachedDesignTokens(
+ {
+ breakpoints: runtimeBreakpoints,
+ // The global root is virtual and is intentionally not persisted here.
+ instances: new Map(),
+ styles: new Map(),
+ styleSources: sources([local("local")]),
+ styleSourceSelections: new Map(),
+ },
+ {
+ tokens: [{ name: "Primary" }],
+ instanceIds: [ROOT_INSTANCE_ID],
+ },
+ { createId }
+ );
+
+ expect(mutation.result).toEqual({ tokenIds: ["new-id"] });
+ expect(mutation.payload).toContainEqual({
+ namespace: "styleSourceSelections",
+ patches: [
+ {
+ op: "add",
+ path: [ROOT_INSTANCE_ID],
+ value: { instanceId: ROOT_INSTANCE_ID, values: ["new-id"] },
+ },
+ ],
+ });
+ });
+
test("selected style source declarations use the project base breakpoint when omitted", () => {
const instances = toMap([instance("box")]);
const styleSources = sources([token("token", "Primary")]);
diff --git a/packages/project-build/src/runtime/styles.ts b/packages/project-build/src/runtime/styles.ts
index f67a304de3..50976c1408 100644
--- a/packages/project-build/src/runtime/styles.ts
+++ b/packages/project-build/src/runtime/styles.ts
@@ -5,6 +5,7 @@ import { camelCaseProperty } from "@webstudio-is/css-data";
import { styleValue, toValue, type StyleValue } from "@webstudio-is/css-engine";
import {
getStyleDeclKey,
+ ROOT_INSTANCE_ID,
styleDecl as styleDeclSchema,
type Breakpoint,
type Breakpoints,
@@ -3056,7 +3057,13 @@ const assertStyleInstances = (
instanceIds: Iterable<Instance["id"]>
) => {
for (const instanceId of instanceIds) {
- if (instances.has(instanceId) === false) {
+ // The global root is virtual and therefore intentionally absent from the
+ // persisted instances namespace. Its style source selection is persisted
+ // and rendered as :root.
+ if (
+ instanceId !== ROOT_INSTANCE_ID &&
+ instances.has(instanceId) === false
+ ) {
return throwBuilderRuntimeError("NOT_FOUND", "Instance not found");
}
}