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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
refactor: drop preset tokens (#4577)
Bogdan Chadkin58e614306b51
diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.test.ts b/apps/builder/app/builder/features/style-panel/style-source-section.test.tsindex 1ae48e2478..4e6373a733 100644--- a/apps/builder/app/builder/features/style-panel/style-source-section.test.ts+++ b/apps/builder/app/builder/features/style-panel/style-source-section.test.ts@@ -1,113 +1,18 @@import { enableMapSet } from "immer";import { expect, test } from "vitest";-import type { Breakpoint } from "@webstudio-is/sdk";-import type { WsComponentMeta } from "@webstudio-is/react-sdk";import { registerContainers } from "~/shared/sync";import {- $breakpoints,$instances,- $registeredComponentMetas,$selectedStyleSources,$styleSourceSelections,$styleSources,} from "~/shared/nano-states";-import {- $presetTokens,- addStyleSourceToInstance,-} from "./style-source-section";+import { addStyleSourceToInstance } from "./style-source-section";import { $awareness } from "~/shared/awareness";enableMapSet();registerContainers();-test("generate styles from preset tokens", () => {- $breakpoints.set(- new Map<Breakpoint["id"], Breakpoint>([- ["base", { id: "base", label: "Base" }],- ])- );- $registeredComponentMetas.set(- new Map<string, WsComponentMeta>([- [- "Box",- {- icon: "",- type: "container",- presetTokens: {- boxBright: {- styles: [- {- property: "color",- value: { type: "keyword", value: "black" },- },- ],- },- },- },- ],- [- "Button",- {- icon: "",- type: "container",- presetTokens: {- buttonPrimary: {- styles: [- {- property: "backgroundColor",- value: { type: "keyword", value: "black" },- },- ],- },- },- },- ],- ])- );- expect($presetTokens.get()).toEqual(- new Map([- [- "Box:boxBright",- {- component: "Box",- styleSource: {- type: "token",- id: "Box:boxBright",- name: "Box Bright",- },- styles: [- {- breakpointId: "base",- styleSourceId: "Box:boxBright",- property: "color",- value: { type: "keyword", value: "black" },- },- ],- },- ],- [- "Button:buttonPrimary",- {- component: "Button",- styleSource: {- type: "token",- id: "Button:buttonPrimary",- name: "Button Primary",- },- styles: [- {- breakpointId: "base",- styleSourceId: "Button:buttonPrimary",- property: "backgroundColor",- value: { type: "keyword", value: "black" },- },- ],- },- ],- ])- );-});-test("add style source to instance", () => {$instances.set(new Map([diff --git a/apps/builder/app/builder/features/style-panel/style-source-section.tsx b/apps/builder/app/builder/features/style-panel/style-source-section.tsxindex c68a27d9e1..6b62055608 100644--- a/apps/builder/app/builder/features/style-panel/style-source-section.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source-section.tsx@@ -27,7 +27,6 @@ import {type StyleSourceError,} from "./style-source";import {- $breakpoints,$registeredComponentMetas,$selectedInstanceStatesByStyleSourceId,$selectedInstanceStyleSources,@@ -40,9 +39,6 @@ import {} from "~/shared/nano-states";import { removeByMutable } from "~/shared/array-utils";import { cloneStyles } from "~/shared/tree-utils";-import { humanizeString } from "~/shared/string-utils";-import { isBaseBreakpoint } from "~/shared/breakpoints";-import { shallowComputed } from "~/shared/store-utils";import { serverSyncStore } from "~/shared/sync";import { $selectedInstance } from "~/shared/awareness";@@ -88,59 +84,6 @@ const getOrCreateStyleSourceSelectionMutable = (return styleSourceSelection;};-const $baseBreakpointId = computed($breakpoints, (breakpoints) => {- const breakpointValues = Array.from(breakpoints.values());- const baseBreakpoint = breakpointValues.find(isBaseBreakpoint);- return baseBreakpoint?.id;-});--// metas are rarely change so keep preset token styles computing-// in separate store-export const $presetTokens = computed(- [$registeredComponentMetas, $baseBreakpointId],- (metas, baseBreakpointId) => {- const presetTokens = new Map<- StyleSource["id"],- {- component: Instance["component"];- styleSource: StyleSourceToken;- styles: StyleDecl[];- }- >();- if (baseBreakpointId === undefined) {- return presetTokens;- }- for (const [component, meta] of metas) {- if (meta.presetTokens === undefined) {- continue;- }- for (const [name, tokenValue] of Object.entries(meta.presetTokens)) {- const styleSourceId = `${component}:${name}`;- const styles: StyleDecl[] = [];- for (const styleDecl of tokenValue.styles) {- styles.push({- breakpointId: baseBreakpointId,- styleSourceId,- state: styleDecl.state,- property: styleDecl.property,- value: styleDecl.value,- });- }- presetTokens.set(styleSourceId, {- component,- styleSource: {- type: "token",- id: styleSourceId,- name: humanizeString(name),- },- styles,- });- }- }- return presetTokens;- }-);-const addStyleSourceToInstaceMutable = (styleSourceSelections: StyleSourceSelections,styleSources: StyleSources,@@ -176,10 +119,9 @@ const createStyleSource = (id: StyleSource["id"], name: string) => {id,name,};- const presetTokens = $presetTokens.get();serverSyncStore.createTransaction(- [$styleSources, $styles, $styleSourceSelections],- (styleSources, styles, styleSourceSelections) => {+ [$styleSources, $styleSourceSelections],+ (styleSources, styleSourceSelections) => {styleSources.set(newStyleSource.id, newStyleSource);addStyleSourceToInstaceMutable(styleSourceSelections,@@ -187,13 +129,6 @@ const createStyleSource = (id: StyleSource["id"], name: string) => {instanceId,newStyleSource.id);- // populate preset token styles- const presetToken = presetTokens.get(id);- if (presetToken) {- for (const styleDecl of presetToken.styles) {- styles.set(getStyleDeclKey(styleDecl), styleDecl);- }- }});selectStyleSource(newStyleSource.id);@@ -420,51 +355,19 @@ const convertToInputItem = (};};-const $selectedInstancePresetTokens = shallowComputed(- [$selectedInstance, $presetTokens],- (selectedInstance, presetTokens) => {- const selectedInstancePresetTokens: StyleSourceToken[] = [];- if (selectedInstance === undefined) {- return selectedInstancePresetTokens;- }- for (const presetToken of presetTokens.values()) {- if (presetToken.component === selectedInstance.component) {- selectedInstancePresetTokens.push(presetToken.styleSource);- }- }- return selectedInstancePresetTokens;- }-);-/*** find all non-local and component style sources*/-const $availableStyleSources = computed(- [$styleSources, $selectedInstancePresetTokens],- (styleSources, presetTokens) => {- const availableStylesSources: StyleSourceInputItem[] = [];- for (const styleSource of styleSources.values()) {- if (styleSource.type === "local") {- continue;- }- availableStylesSources.push(convertToInputItem(styleSource, []));- }- for (const styleSource of presetTokens) {- // skip if already present in global tokens- if (styleSources.has(styleSource.id)) {- continue;- }- availableStylesSources.push({- id: styleSource.id,- label: styleSource.name,- disabled: false,- source: "componentToken",- states: [],- });+const $availableStyleSources = computed([$styleSources], (styleSources) => {+ const availableStylesSources: StyleSourceInputItem[] = [];+ for (const styleSource of styleSources.values()) {+ if (styleSource.type === "local") {+ continue;}- return availableStylesSources;+ availableStylesSources.push(convertToInputItem(styleSource, []));}-);+ return availableStylesSources;+});export const StyleSourcesSection = () => {const componentStates = useStore($componentStates);diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsxindex e71e808e47..cee45392c4 100644--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-badge.tsx@@ -21,9 +21,6 @@ export const StyleSourceBadge = styled(Text, {token: {backgroundColor: theme.colors.backgroundStyleSourceToken,},- componentToken: {- backgroundColor: theme.colors.backgroundStyleSourceToken,- },tag: {backgroundColor: theme.colors.backgroundStyleSourceTag,},diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsxindex 3399f4375e..a9c34f290c 100644--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-control.tsx@@ -109,7 +109,7 @@ const Menu = (props: MenuProps) => {);};-export type ItemSource = "token" | "componentToken" | "tag" | "local";+export type ItemSource = "token" | "tag" | "local";type EditableTextProps = {value: string;@@ -173,11 +173,6 @@ const StyleSourceContainer = styled(Box, {[menuTriggerGradientVar]:theme.colors.backgroundStyleSourceGradientToken,},- componentToken: {- backgroundColor: theme.colors.backgroundStyleSourceToken,- [menuTriggerGradientVar]:- theme.colors.backgroundStyleSourceGradientToken,- },tag: {backgroundColor: theme.colors.backgroundStyleSourceTag,[menuTriggerGradientVar]: theme.colors.backgroundStyleSourceGradientTag,@@ -239,9 +234,6 @@ const StyleSourceState = styled(Text, {token: {backgroundColor: theme.colors.backgroundStyleSourceToken,},- componentToken: {- backgroundColor: theme.colors.backgroundStyleSourceToken,- },tag: {backgroundColor: theme.colors.backgroundStyleSourceTag,},diff --git a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsxindex fd640991a8..a86bf0e478 100644--- a/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx+++ b/apps/builder/app/builder/features/style-panel/style-source/style-source-input.tsx@@ -273,7 +273,7 @@ const matchOrSuggestToCreate = (const matched = matchSorter(items, search, {keys: [itemToString],});- const order: ItemSource[] = ["token", "componentToken"];+ const order: ItemSource[] = ["token"];matched.sort((leftItem, rightItem) => {return order.indexOf(leftItem.source) - order.indexOf(rightItem.source);});@@ -470,8 +470,6 @@ export const StyleSourceInput = (setLabel("");if (item.id === newItemId) {props.onCreateItem?.(nanoid(), item.label);- } else if (item.source === "componentToken") {- props.onCreateItem?.(item.id, item.label);} else {props.onSelectAutocompleteItem?.(item);}@@ -498,7 +496,6 @@ export const StyleSourceInput = (let hasNewTokenItem = false;let hasGlobalTokenItem = false;- let hasComponentTokenItem = false;const states = props.componentStates ?? [];@@ -580,20 +577,6 @@ export const StyleSourceInput = ();}- if (- item.source === "componentToken" &&- hasComponentTokenItem === false- ) {- hasComponentTokenItem = true;- label = (- <>- {(hasNewTokenItem || hasGlobalTokenItem) && (- <ComboboxSeparator />- )}- <ComboboxLabel>Component Tokens</ComboboxLabel>- </>- );- }const { key, ...itemProps } = getItemProps({ item, index });return (<Fragment key={index}>diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.jsonindex 3b7f2a4711..a0819dd068 100644--- a/packages/react-sdk/package.json+++ b/packages/react-sdk/package.json@@ -35,8 +35,7 @@"@webstudio-is/sdk": "workspace:*","change-case": "^5.4.4","html-tags": "^4.0.0",- "nanoid": "^5.0.8",- "title-case": "^4.3.2"+ "nanoid": "^5.0.8"},"exports": {".": {diff --git a/packages/react-sdk/src/components/component-meta.ts b/packages/react-sdk/src/components/component-meta.tsindex d542e300a0..36730c4f66 100644--- a/packages/react-sdk/src/components/component-meta.ts+++ b/packages/react-sdk/src/components/component-meta.ts@@ -40,11 +40,6 @@ export const ComponentState = z.object({export type ComponentState = z.infer<typeof ComponentState>;-const ComponentToken = z.object({- variant: z.optional(z.string()),- styles: z.array(EmbedTemplateStyleDecl),-});-export const defaultStates: ComponentState[] = [{ selector: ":hover", label: "Hover" },{ selector: ":active", label: "Active" },@@ -73,7 +68,6 @@ export const WsComponentMeta = z.object({presetStyle: z.optional(z.record(z.string(), z.array(EmbedTemplateStyleDecl))),- presetTokens: z.optional(z.record(z.string(), ComponentToken)),states: z.optional(z.array(ComponentState)),template: z.optional(WsEmbedTemplate),order: z.number().optional(),diff --git a/packages/react-sdk/src/embed-template.test.tsx b/packages/react-sdk/src/embed-template.test.tsxindex 296c683306..780dad6175 100644--- a/packages/react-sdk/src/embed-template.test.tsx+++ b/packages/react-sdk/src/embed-template.test.tsx@@ -1,7 +1,6 @@import { expect, test } from "vitest";import { generateDataFromEmbedTemplate, namespaceMeta } from "./embed-template";import { showAttribute } from "./props";-import type { WsComponentMeta } from "./components/component-meta";const expectString = expect.any(String);@@ -672,93 +671,6 @@ test("generate data for embedding from instance child bound to variables", () =>});});-test("generate styles from tokens", () => {- const presetTokens: WsComponentMeta["presetTokens"] = {- box: {- styles: [- {- property: "width",- value: { type: "keyword", value: "max-content" },- },- {- property: "height",- value: { type: "keyword", value: "max-content" },- },- ],- },- boxBright: {- styles: [- {- property: "color",- value: { type: "keyword", value: "red" },- },- {- property: "backgroundColor",- value: { type: "keyword", value: "pink" },- },- ],- },- boxNone: {- styles: [- {- property: "color",- value: { type: "keyword", value: "transparent" },- },- {- property: "backgroundColor",- value: { type: "keyword", value: "transparent" },- },- ],- },- };- const { styleSourceSelections, styleSources, styles } =- generateDataFromEmbedTemplate(- [- {- type: "instance",- component: "Box",- tokens: ["box", "boxBright"],- children: [],- },- ],- new Map([["Box", { type: "container", icon: "", presetTokens }]]),- defaultBreakpointId- );- expect(styleSources).toEqual([- { id: "Box:box", name: "Box", type: "token" },- { id: "Box:boxBright", name: "Box Bright", type: "token" },- ]);- expect(styleSourceSelections).toEqual([- { instanceId: expectString, values: ["Box:box", "Box:boxBright"] },- ]);- expect(styles).toEqual([- {- breakpointId: "base",- property: "width",- styleSourceId: "Box:box",- value: { type: "keyword", value: "max-content" },- },- {- breakpointId: "base",- property: "height",- styleSourceId: "Box:box",- value: { type: "keyword", value: "max-content" },- },- {- breakpointId: "base",- property: "color",- styleSourceId: "Box:boxBright",- value: { type: "keyword", value: "red" },- },- {- breakpointId: "base",- property: "backgroundColor",- styleSourceId: "Box:boxBright",- value: { type: "keyword", value: "pink" },- },- ]);-});-test("add namespace to selected components in embed template", () => {expect(namespaceMeta(@@ -771,16 +683,10 @@ test("add namespace to selected components in embed template", () => {component: { $nin: ["Button", "Box"] },},indexWithinAncestor: "Tooltip",- presetTokens: {- base: { styles: [] },- small: { styles: [] },- large: { styles: [] },- },template: [{type: "instance",component: "Tooltip",- tokens: ["base", "small"],children: [{ type: "text", value: "Some text" },{@@ -810,16 +716,10 @@ test("add namespace to selected components in embed template", () => {component: { $nin: ["my-namespace:Button", "my-namespace:Box"] },},indexWithinAncestor: "my-namespace:Tooltip",- presetTokens: {- base: { styles: [] },- small: { styles: [] },- large: { styles: [] },- },template: [{type: "instance",component: "my-namespace:Tooltip",- tokens: ["base", "small"],children: [{ type: "text", value: "Some text" },{diff --git a/packages/react-sdk/src/embed-template.ts b/packages/react-sdk/src/embed-template.tsindex 6309871577..9d7546398d 100644--- a/packages/react-sdk/src/embed-template.ts+++ b/packages/react-sdk/src/embed-template.ts@@ -1,7 +1,5 @@import { z } from "zod";import { nanoid } from "nanoid";-import { titleCase } from "title-case";-import { noCase } from "change-case";import type { Simplify } from "type-fest";import type {Instance,@@ -117,7 +115,6 @@ export type EmbedTemplateInstance = {label?: string;variables?: Record<string, EmbedTemplateVariable>;props?: EmbedTemplateProp[];- tokens?: string[];styles?: EmbedTemplateStyleDecl[];children: Array<EmbedTemplateInstance | EmbedTemplateText | EmbedTemplateExpression@@ -132,7 +129,6 @@ export const EmbedTemplateInstance: z.ZodType<EmbedTemplateInstance> = z.lazy(label: z.optional(z.string()),variables: z.optional(z.record(z.string(), EmbedTemplateVariable)),props: z.optional(z.array(EmbedTemplateProp)),- tokens: z.optional(z.array(z.string())),styles: z.optional(z.array(EmbedTemplateStyleDecl)),children: WsEmbedTemplate,})@@ -275,34 +271,6 @@ const createInstancesFromTemplate = (const styleSourceIds: string[] = [];- // convert tokens into style sources and styles- if (item.tokens) {- const meta = metas.get(item.component);- if (meta?.presetTokens) {- for (const name of item.tokens) {- const tokenValue = meta.presetTokens[name];- if (tokenValue) {- const styleSourceId = `${item.component}:${name}`;- styleSourceIds.push(styleSourceId);- styleSources.push({- type: "token",- id: styleSourceId,- name: titleCase(noCase(name)),- });- for (const styleDecl of tokenValue.styles) {- styles.push({- breakpointId: defaultBreakpointId,- styleSourceId,- state: styleDecl.state,- property: styleDecl.property,- value: styleDecl.value,- });- }- }- }- }- }-// populate stylesif (item.styles) {const styleSourceId = generateId();diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 1a5ad24d1d..cec4214612 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1724,9 +1724,6 @@ importers:nanoid:specifier: ^5.0.8version: 5.0.8- title-case:- specifier: ^4.3.2- version: 4.3.2devDependencies:'@types/react':specifier: ^18.2.70