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: fix css vars search (#5494)

Oleg Isonen46c964d29282
diff --git a/apps/builder/app/builder/features/command-panel/groups/components-group.tsx b/apps/builder/app/builder/features/command-panel/groups/components-group.tsx
index 57f9aa709c..e410059553 100644
--- a/apps/builder/app/builder/features/command-panel/groups/components-group.tsx
+++ b/apps/builder/app/builder/features/command-panel/groups/components-group.tsx
@@ -69,7 +69,7 @@ export const $componentOptions = computed(
continue;
}
}
- const label = getInstanceLabel({ component: name }, meta);
+ const label = getInstanceLabel({ component: name });
componentOptions.push({
terms: ["components", label, category],
type: "component",
@@ -90,7 +90,7 @@ export const $componentOptions = computed(
const label =
meta.label ??
componentMeta?.label ??
- getInstanceLabel({ component: name }, meta);
+ getInstanceLabel({ component: name });
componentOptions.push({
terms: ["components", label, meta.category],
type: "component",
diff --git a/apps/builder/app/builder/features/command-panel/groups/css-variables-group.tsx b/apps/builder/app/builder/features/command-panel/groups/css-variables-group.tsx
index 4cc7d782b5..2e0cd31e23 100644
--- a/apps/builder/app/builder/features/command-panel/groups/css-variables-group.tsx
+++ b/apps/builder/app/builder/features/command-panel/groups/css-variables-group.tsx
@@ -29,8 +29,6 @@ import {
import type { BaseOption } from "../shared/types";
import { formatUsageCount, getUsageSearchTerms } from "../shared/usage-utils";
import { getInstanceLabel } from "~/builder/shared/instance-label";
-import { $instances } from "~/shared/nano-states";
-import { $registeredComponentMetas } from "~/shared/nano-states";
export type CssVariableOption = BaseOption & {
type: "cssVariable";
@@ -40,32 +38,20 @@ export type CssVariableOption = BaseOption & {
};
export const $cssVariableOptions = computed(
- [
- $cssVariableDefinitionsByVariable,
- $usedCssVariablesInInstances,
- $instances,
- $registeredComponentMetas,
- ],
- (definitionsByVariable, usedVariablesInInstances, instances, metas) => {
+ [$cssVariableDefinitionsByVariable, $usedCssVariablesInInstances],
+ (definitionsByVariable, usedVariablesInInstances) => {
const cssVariableOptions: CssVariableOption[] = [];
// Create options for each defined CSS variable on each instance
for (const [property, instanceIds] of definitionsByVariable) {
for (const instanceId of instanceIds) {
- const instance = instances.get(instanceId);
- if (!instance) {
- continue;
- }
- const meta = metas.get(instance.component);
- const instanceLabel = getInstanceLabel(instance, meta);
-
const usages = usedVariablesInInstances.get(property) ?? 0;
cssVariableOptions.push({
terms: [
"css variables",
property,
property.slice(2), // Include name without --
- instanceLabel,
+ getInstanceLabel(instanceId),
...getUsageSearchTerms(usages),
],
type: "cssVariable",
@@ -102,8 +88,6 @@ export const CssVariablesGroup = ({
}) => {
const action = useSelectedAction();
const resetActionIndex = useResetActionIndex();
- const instances = useStore($instances);
- const metas = useStore($registeredComponentMetas);
const [variableDialog, setVariableDialog] = useState<
{ action: "rename" | "delete"; property: string } | undefined
>();
@@ -115,16 +99,10 @@ export const CssVariablesGroup = ({
heading={<CommandGroupHeading>CSS Variables</CommandGroupHeading>}
actions={["select", "find usages", "rename", "delete"]}
>
- {options.map(({ property, instanceId, usages }) => {
- const instance = instances.get(instanceId);
- const meta = instance ? metas.get(instance.component) : undefined;
- const instanceLabel = instance
- ? getInstanceLabel(instance, meta)
- : "";
-
+ {options.map(({ property, instanceId, usages, terms }) => {
return (
<CommandItem
- keywords={["test"]}
+ keywords={terms}
key={`${property}-${instanceId}`}
// preserve selected state when rerender
value={`${property}-${instanceId}`}
@@ -153,7 +131,7 @@ export const CssVariablesGroup = ({
</Text>
</Text>
<Text as="span" color="moreSubtle">
- {instanceLabel}
+ {getInstanceLabel(instanceId)}
</Text>
</CommandItem>
);
diff --git a/apps/builder/app/builder/features/command-panel/groups/data-variables-group.tsx b/apps/builder/app/builder/features/command-panel/groups/data-variables-group.tsx
index efc7daad52..fb75e8fe67 100644
--- a/apps/builder/app/builder/features/command-panel/groups/data-variables-group.tsx
+++ b/apps/builder/app/builder/features/command-panel/groups/data-variables-group.tsx
@@ -1,6 +1,5 @@
import { useState } from "react";
import { computed } from "nanostores";
-import { useStore } from "@nanostores/react";
import {
CommandGroup,
CommandGroupHeading,
@@ -10,7 +9,7 @@ import {
useSelectedAction,
useResetActionIndex,
} from "@webstudio-is/design-system";
-import { $dataSources, $instances } from "~/shared/nano-states";
+import { $dataSources } from "~/shared/nano-states";
import {
$commandContent,
closeCommandPanel,
@@ -27,7 +26,6 @@ import {
import type { BaseOption } from "../shared/types";
import { formatUsageCount, getUsageSearchTerms } from "../shared/usage-utils";
import { getInstanceLabel } from "~/builder/shared/instance-label";
-import { $registeredComponentMetas } from "~/shared/nano-states";
export type DataVariableOption = BaseOption & {
type: "dataVariable";
@@ -38,13 +36,8 @@ export type DataVariableOption = BaseOption & {
};
export const $dataVariableOptions = computed(
- [
- $dataSources,
- $instances,
- $usedVariablesInInstances,
- $registeredComponentMetas,
- ],
- (dataSources, instances, usedInInstances, metas) => {
+ [$dataSources, $usedVariablesInInstances],
+ (dataSources, usedInInstances) => {
const dataVariableOptions: DataVariableOption[] = [];
for (const dataSource of dataSources.values()) {
@@ -52,27 +45,22 @@ export const $dataVariableOptions = computed(
dataSource.type === "variable" &&
dataSource.scopeInstanceId !== undefined
) {
- const instance = instances.get(dataSource.scopeInstanceId);
- if (instance) {
- const meta = metas.get(instance.component);
- const instanceLabel = getInstanceLabel(instance, meta);
- const usages = usedInInstances.get(dataSource.id)?.size ?? 0;
- dataVariableOptions.push({
- terms: [
- "variable",
- "variables",
- "data",
- dataSource.name,
- instanceLabel,
- ...getUsageSearchTerms(usages),
- ],
- type: "dataVariable",
- id: dataSource.id,
- name: dataSource.name,
- instanceId: dataSource.scopeInstanceId,
- usages,
- });
- }
+ const usages = usedInInstances.get(dataSource.id)?.size ?? 0;
+ dataVariableOptions.push({
+ terms: [
+ "variable",
+ "variables",
+ "data",
+ dataSource.name,
+ getInstanceLabel(dataSource.scopeInstanceId) ?? "Unused",
+ ...getUsageSearchTerms(usages),
+ ],
+ type: "dataVariable",
+ id: dataSource.id,
+ name: dataSource.name,
+ instanceId: dataSource.scopeInstanceId,
+ usages,
+ });
}
}
@@ -102,8 +90,6 @@ export const DataVariablesGroup = ({
}) => {
const action = useSelectedAction();
const resetActionIndex = useResetActionIndex();
- const instances = useStore($instances);
- const metas = useStore($registeredComponentMetas);
const [variableDialog, setVariableDialog] = useState<
(DataVariableOption & { action: "rename" | "delete" }) | undefined
>();
@@ -116,14 +102,9 @@ export const DataVariablesGroup = ({
actions={["select", "find usages", "rename", "delete"]}
>
{options.map((option) => {
- const instance = instances.get(option.instanceId);
- const meta = instance ? metas.get(instance.component) : undefined;
- const instanceLabel = instance
- ? getInstanceLabel(instance, meta)
- : "";
-
return (
<CommandItem
+ keywords={option.terms}
key={option.id}
value={option.id}
onSelect={() => {
@@ -151,7 +132,7 @@ export const DataVariablesGroup = ({
</Text>
</Text>
<Text as="span" color="moreSubtle">
- {instanceLabel}
+ {getInstanceLabel(option.instanceId)}
</Text>
</CommandItem>
);
diff --git a/apps/builder/app/builder/features/command-panel/shared/instance-list.tsx b/apps/builder/app/builder/features/command-panel/shared/instance-list.tsx
index 0f0577d735..804802c48c 100644
--- a/apps/builder/app/builder/features/command-panel/shared/instance-list.tsx
+++ b/apps/builder/app/builder/features/command-panel/shared/instance-list.tsx
@@ -42,7 +42,7 @@ export const InstanceList = ({ instanceIds, onSelect }: InstanceListProps) => {
const meta = metas.get(instance?.component ?? "");
if (instance && meta) {
usedInInstances.push({
- label: getInstanceLabel(instance, meta),
+ label: getInstanceLabel(instance),
id: instance.id,
});
}
diff --git a/apps/builder/app/builder/features/components/components.tsx b/apps/builder/app/builder/features/components/components.tsx
index 70a69eea55..139e73d76e 100644
--- a/apps/builder/app/builder/features/components/components.tsx
+++ b/apps/builder/app/builder/features/components/components.tsx
@@ -69,7 +69,7 @@ const $metas = computed(
name,
category: componentMeta.category ?? "hidden",
order: componentMeta.order,
- label: getInstanceLabel({ component: name }, componentMeta),
+ label: getInstanceLabel({ component: name }),
description: componentMeta.description,
firstInstance: { component: name },
});
@@ -84,7 +84,7 @@ const $metas = computed(
label:
templateMeta.label ??
componentMeta?.label ??
- getInstanceLabel({ component: name }, templateMeta),
+ getInstanceLabel({ component: name }),
description: templateMeta.description,
icon: templateMeta.icon,
firstInstance: templateMeta.template.instances[0],
diff --git a/apps/builder/app/builder/features/components/use-draggable.tsx b/apps/builder/app/builder/features/components/use-draggable.tsx
index 7e5545fc6f..1b5b0ccaf4 100644
--- a/apps/builder/app/builder/features/components/use-draggable.tsx
+++ b/apps/builder/app/builder/features/components/use-draggable.tsx
@@ -12,7 +12,10 @@ import {
} from "@webstudio-is/design-system";
import { useSubscribe, type Publish } from "~/shared/pubsub";
import { $canvasRect, $scale } from "~/builder/shared/nano-states";
-import { InstanceIcon, InstanceLabel } from "~/builder/shared/instance-label";
+import {
+ InstanceIcon,
+ getInstanceLabel,
+} from "~/builder/shared/instance-label";
const DragLayer = ({
component,
@@ -32,7 +35,7 @@ const DragLayer = ({
}}
>
<ComponentCard
- label={<InstanceLabel instance={{ component }} />}
+ label={getInstanceLabel({ component })}
icon={<InstanceIcon size="auto" instance={{ component }} />}
style={{
transform: `translate3d(${point.x}px, ${point.y}px, 0)`,
diff --git a/apps/builder/app/builder/features/footer/breadcrumbs.tsx b/apps/builder/app/builder/features/footer/breadcrumbs.tsx
index 72272380b7..b8df155354 100644
--- a/apps/builder/app/builder/features/footer/breadcrumbs.tsx
+++ b/apps/builder/app/builder/features/footer/breadcrumbs.tsx
@@ -4,7 +4,7 @@ import { ChevronRightIcon } from "@webstudio-is/icons";
import { theme, Button, Flex, Text } from "@webstudio-is/design-system";
import { $textEditingInstanceSelector } from "~/shared/nano-states";
import { $selectedInstancePath, selectInstance } from "~/shared/awareness";
-import { InstanceLabel } from "~/builder/shared/instance-label";
+import { getInstanceLabel } from "~/builder/shared/instance-label";
export const Breadcrumbs = () => {
const instancePath = useStore($selectedInstancePath);
@@ -29,7 +29,7 @@ export const Breadcrumbs = () => {
$textEditingInstanceSelector.set(undefined);
}}
>
- <InstanceLabel instance={instance} />
+ {getInstanceLabel(instance)}
</Button>
{/* hide the last one */}
{index < instancePath.length - 1 && <ChevronRightIcon />}
diff --git a/apps/builder/app/builder/features/navigator/navigator-tree.tsx b/apps/builder/app/builder/features/navigator/navigator-tree.tsx
index b446ab863d..9aba538b4a 100644
--- a/apps/builder/app/builder/features/navigator/navigator-tree.tsx
+++ b/apps/builder/app/builder/features/navigator/navigator-tree.tsx
@@ -29,7 +29,6 @@ import {
blockTemplateComponent,
descendantComponent,
type Instance,
- type WsComponentMeta,
} from "@webstudio-is/sdk";
import { animationCanPlayOnCanvasProperty } from "@webstudio-is/sdk/runtime";
import {
@@ -402,19 +401,17 @@ const EditableTreeNodeLabel = styled("div", {
});
const TreeNodeContent = ({
- meta,
instance,
isEditing,
onIsEditingChange,
}: {
- meta: WsComponentMeta;
instance: Instance;
isEditing: boolean;
onIsEditingChange: (isEditing: boolean) => void;
}) => {
const editableRef = useRef<HTMLDivElement | null>(null);
- const label = getInstanceLabel(instance, meta);
+ const label = getInstanceLabel(instance);
const { ref, handlers } = useContentEditable({
value: label,
isEditable: true,
@@ -785,7 +782,6 @@ export const NavigatorTree = () => {
}
>
<TreeNodeContent
- meta={meta}
instance={item.instance}
isEditing={
item.selector.join() === editingItemSelector?.join()
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/animation/subject-select.tsx b/apps/builder/app/builder/features/settings-panel/props-section/animation/subject-select.tsx
index eb8b3890aa..1eb94e1d7c 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/animation/subject-select.tsx
+++ b/apps/builder/app/builder/features/settings-panel/props-section/animation/subject-select.tsx
@@ -62,7 +62,7 @@ const initSubjects = () => {
subjects.push({
value,
- label: getInstanceLabel(instance, meta),
+ label: getInstanceLabel(instance),
isTimelineExists,
instanceId,
selector,
diff --git a/apps/builder/app/builder/features/settings-panel/settings-section.tsx b/apps/builder/app/builder/features/settings-panel/settings-section.tsx
index 189a4b20ef..53dcae248a 100644
--- a/apps/builder/app/builder/features/settings-panel/settings-section.tsx
+++ b/apps/builder/app/builder/features/settings-panel/settings-section.tsx
@@ -2,7 +2,7 @@ import { useId } from "react";
import { useStore } from "@nanostores/react";
import type { Instance } from "@webstudio-is/sdk";
import { InputField } from "@webstudio-is/design-system";
-import { $instances, $registeredComponentMetas } from "~/shared/nano-states";
+import { $instances } from "~/shared/nano-states";
import { HorizontalLayout, Label, Row, useLocalValue } from "./shared";
import { serverSyncStore } from "~/shared/sync";
import { $selectedInstance } from "~/shared/awareness";
@@ -19,7 +19,6 @@ const saveLabel = (label: string, selectedInstance: Instance) => {
export const SettingsSection = () => {
const selectedInstance = useStore($selectedInstance);
- const metas = useStore($registeredComponentMetas);
const id = useId();
const localValue = useLocalValue(selectedInstance?.label ?? "", (value) => {
if (selectedInstance) {
@@ -31,8 +30,7 @@ export const SettingsSection = () => {
return;
}
- const meta = metas.get(selectedInstance.component);
- const placeholder = getInstanceLabel(selectedInstance, meta);
+ const placeholder = getInstanceLabel(selectedInstance);
return (
<Row>
diff --git a/apps/builder/app/builder/features/style-panel/property-label.tsx b/apps/builder/app/builder/features/style-panel/property-label.tsx
index 751830eb68..e57229b34f 100644
--- a/apps/builder/app/builder/features/style-panel/property-label.tsx
+++ b/apps/builder/app/builder/features/style-panel/property-label.tsx
@@ -128,7 +128,7 @@ export const PropertyInfo = ({
);
}
if (instance && meta) {
- instanceSet.add(getInstanceLabel(instance, meta));
+ instanceSet.add(getInstanceLabel(instance));
}
}
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-instance-outline.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-instance-outline.tsx
index cd779e5215..2790f85079 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-instance-outline.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/outline/block-instance-outline.tsx
@@ -106,7 +106,7 @@ export const TemplatesMenu = ({
const menuItems = templates?.map(([template, templateSelector]) => ({
id: template.id,
icon: <InstanceIcon instance={{ component: template.component }} />,
- title: getInstanceLabel(template, undefined),
+ title: getInstanceLabel(template),
value: templateSelector,
}));
diff --git a/apps/builder/app/builder/features/workspace/canvas-tools/outline/label.tsx b/apps/builder/app/builder/features/workspace/canvas-tools/outline/label.tsx
index b68c1fc155..c68eea3bb1 100644
--- a/apps/builder/app/builder/features/workspace/canvas-tools/outline/label.tsx
+++ b/apps/builder/app/builder/features/workspace/canvas-tools/outline/label.tsx
@@ -2,7 +2,10 @@ import { useCallback, useState } from "react";
import { styled, type Rect } from "@webstudio-is/design-system";
import type { Instance } from "@webstudio-is/sdk";
import { theme } from "@webstudio-is/design-system";
-import { InstanceIcon, InstanceLabel } from "~/builder/shared/instance-label";
+import {
+ InstanceIcon,
+ getInstanceLabel,
+} from "~/builder/shared/instance-label";
type LabelPosition = "top" | "inside" | "bottom";
type LabelRefCallback = (element: HTMLElement | null) => void;
@@ -99,7 +102,7 @@ export const Label = ({ instance, instanceRect, variant }: LabelProps) => {
return (
<LabelContainer position={position} variant={variant} ref={labelRef}>
<InstanceIcon size="1em" instance={instance} />
- <InstanceLabel instance={instance} />
+ {getInstanceLabel(instance)}
</LabelContainer>
);
};
diff --git a/apps/builder/app/builder/inspector.tsx b/apps/builder/app/builder/inspector.tsx
index 590c8a2bd0..144168677f 100644
--- a/apps/builder/app/builder/inspector.tsx
+++ b/apps/builder/app/builder/inspector.tsx
@@ -34,14 +34,14 @@ import {
$selectedPage,
} from "~/shared/awareness";
import { $styleSourceInputElement } from "./shared/commands";
-import { InstanceIcon, InstanceLabel } from "./shared/instance-label";
+import { InstanceIcon, getInstanceLabel } from "./shared/instance-label";
const InstanceInfo = ({ instance }: { instance: Instance }) => {
return (
<Flex shrink={false} gap="1" align="center">
<InstanceIcon instance={instance} />
<Text truncate variant="labelsSentenceCase">
- <InstanceLabel instance={instance} />
+ {getInstanceLabel(instance)}
</Text>
</Flex>
);
diff --git a/apps/builder/app/builder/shared/commands.ts b/apps/builder/app/builder/shared/commands.ts
index 1ed2c998d9..b5f5eeba8e 100644
--- a/apps/builder/app/builder/shared/commands.ts
+++ b/apps/builder/app/builder/shared/commands.ts
@@ -199,7 +199,7 @@ export const wrapIn = (component: string, tag?: string) => {
instanceSelector: newInstanceSelector,
});
if (isSatisfying === false) {
- const label = getInstanceLabel({ component, tag }, {});
+ const label = getInstanceLabel({ component, tag });
toast.error(`Cannot wrap in ${label}`);
throw Error("Abort transaction");
}
@@ -255,7 +255,7 @@ export const replaceWith = (component: string, tag?: string) => {
instanceSelector: selectedInstanceSelector,
});
if (isSatisfying === false) {
- const label = getInstanceLabel({ component, tag }, {});
+ const label = getInstanceLabel({ component, tag });
toast.error(`Cannot replace with ${label}`);
throw Error("Abort transaction");
}
diff --git a/apps/builder/app/builder/shared/instance-label.tsx b/apps/builder/app/builder/shared/instance-label.tsx
index 2765c85c8c..07b1b2be87 100644
--- a/apps/builder/app/builder/shared/instance-label.tsx
+++ b/apps/builder/app/builder/shared/instance-label.tsx
@@ -28,9 +28,10 @@ import {
import {
elementComponent,
parseComponentName,
+ ROOT_INSTANCE_ID,
type Instance,
} from "@webstudio-is/sdk";
-import { $registeredComponentMetas } from "~/shared/nano-states";
+import { $registeredComponentMetas, $instances } from "~/shared/nano-states";
import { humanizeString } from "~/shared/string-utils";
const htmlIcons: Record<string, undefined | string> = {
@@ -105,20 +106,32 @@ const getLabelFromComponentName = (component: Instance["component"]) => {
};
export const getInstanceLabel = (
- instance: InstanceLike,
- meta: undefined | { label?: string }
-) => {
- if (instance.label) {
- return instance.label;
+ instanceOrInstanceId: InstanceLike | string
+): string => {
+ if (typeof instanceOrInstanceId === "string") {
+ if (instanceOrInstanceId === ROOT_INSTANCE_ID) {
+ return "Root";
+ }
+ const instance = $instances.get().get(instanceOrInstanceId);
+ if (instance) {
+ return getInstanceLabel(instance);
+ }
+ return "Unknown";
}
- if (instance.component === elementComponent && instance.tag) {
- return `<${instance.tag}>`;
- }
- return meta?.label || getLabelFromComponentName(instance.component);
-};
-export const InstanceLabel = ({ instance }: { instance: InstanceLike }) => {
- const metas = useStore($registeredComponentMetas);
- const meta = metas.get(instance.component);
- return getInstanceLabel(instance, meta);
+ if (instanceOrInstanceId.label) {
+ return instanceOrInstanceId.label;
+ }
+ if (
+ instanceOrInstanceId.component === elementComponent &&
+ instanceOrInstanceId.tag
+ ) {
+ return `<${instanceOrInstanceId.tag}>`;
+ }
+ const meta = $registeredComponentMetas
+ .get()
+ .get(instanceOrInstanceId.component);
+ return (
+ meta?.label || getLabelFromComponentName(instanceOrInstanceId.component)
+ );
};
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 36d214788c..6b3af0f2a6 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -1348,7 +1348,7 @@ export const findClosestInsertable = (
fragment,
onError: (message) => {
const component = fragment.instances[0].component;
- const label = getInstanceLabel({ component }, {});
+ const label = getInstanceLabel({ component });
toast.warn(message || `"${label}" has no place here`);
},
});