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.

Support remote label for next style sources (#1590)

Bogdan Chadkin09e32b60fffd
diff --git a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
index 8f9834feae..081872af28 100644
--- a/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/property-name.tsx
@@ -4,6 +4,7 @@ import type { StyleProperty } from "@webstudio-is/css-data";
import { toValue } from "@webstudio-is/css-engine";
import {
theme,
+ rawTheme,
Button,
Flex,
Box,
@@ -16,7 +17,7 @@ import {
PopoverContent,
Text,
} from "@webstudio-is/design-system";
-import { UndoIcon } from "@webstudio-is/icons";
+import { AlertIcon, UndoIcon } from "@webstudio-is/icons";
import {
breakpointsStore,
instancesStore,
@@ -62,6 +63,24 @@ const PropertyPopoverContent = ({
{properties.map((property) => {
const styleValueInfo = style[property];
+ if (styleValueInfo?.nextSource) {
+ const { value, styleSourceId } = styleValueInfo.nextSource;
+ const styleSource = styleSources.get(styleSourceId);
+ let name: undefined | string = undefined;
+ if (styleSource?.type === "local") {
+ name = "local style source";
+ }
+ if (styleSource?.type === "token") {
+ name = `"${styleSource.name}" token`;
+ }
+ return (
+ <Text key={property} color="subtle">
+ The value of {property} is overriden by {toValue(value)} from{" "}
+ {name}
+ </Text>
+ );
+ }
+
if (styleValueInfo?.previousSource) {
const { value, styleSourceId } = styleValueInfo.previousSource;
const styleSource = styleSources.get(styleSourceId);
@@ -117,9 +136,11 @@ const PropertyPopoverContent = ({
<Box css={{ px: theme.spacing[4], py: theme.spacing[3] }}>
{properties.map((property) => {
const styleValueInfo = style[property];
+ const source =
+ styleValueInfo?.nextSource ?? styleValueInfo?.previousSource;
- if (styleValueInfo?.previousSource) {
- const { styleSourceId } = styleValueInfo.previousSource;
+ if (source) {
+ const { styleSourceId } = source;
const styleSource = styleSources.get(styleSourceId);
let name: undefined | string = undefined;
if (styleSource?.type === "local") {
@@ -179,16 +200,25 @@ export const PropertyName = ({
const [isOpen, setIsOpen] = useState(false);
const isPopoverEnabled = styleSource === "local" || styleSource === "remote";
- const labelElement =
- typeof label === "string" ? (
- <Flex shrink>
+ const hasStyleSourceConflict = properties.some((property) => {
+ const info = style[property];
+ return info?.nextSource && info.local;
+ });
+
+ const labelElement = (
+ <Flex shrink gap={1}>
+ {typeof label === "string" ? (
<Label color={styleSource} truncate>
{label}
</Label>
- </Flex>
- ) : (
- label
- );
+ ) : (
+ label
+ )}
+ {hasStyleSourceConflict && (
+ <AlertIcon fill={rawTheme.colors.foregroundDestructive} />
+ )}
+ </Flex>
+ );
if (isPopoverEnabled) {
return (
diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts
index 006b05dc86..3fc5558c1e 100644
--- a/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.test.ts
@@ -10,6 +10,7 @@ import {
getCascadedBreakpointIds,
getCascadedInfo,
getInheritedInfo,
+ getNextSourceInfo,
getPreviousSourceInfo,
} from "./style-info";
@@ -273,3 +274,62 @@ test("compute styles from previous sources", () => {
}
`);
});
+
+test("compute styles from next sources", () => {
+ const styleSourceSelections = new Map([
+ ["3", { instanceId: "3", values: ["1", "2", "3", "4"] }],
+ ]);
+ const selectedStyleSourceSelector = {
+ styleSourceId: "3",
+ };
+ const stylesByInstanceId = new Map<Instance["id"], StylesList>([
+ [
+ "3",
+ [
+ {
+ breakpointId: "bp",
+ styleSourceId: "1",
+ property: "width",
+ value: { type: "unit", value: 100, unit: "px" },
+ },
+ {
+ breakpointId: "bp",
+ styleSourceId: "2",
+ property: "width",
+ value: { type: "unit", value: 200, unit: "px" },
+ },
+ {
+ breakpointId: "bp",
+ styleSourceId: "3",
+ property: "width",
+ value: { type: "unit", value: 300, unit: "px" },
+ },
+ {
+ breakpointId: "bp",
+ styleSourceId: "4",
+ property: "width",
+ value: { type: "unit", value: 400, unit: "px" },
+ },
+ ],
+ ],
+ ]);
+ expect(
+ getNextSourceInfo(
+ styleSourceSelections,
+ stylesByInstanceId,
+ selectedInstanceSelector,
+ selectedStyleSourceSelector
+ )
+ ).toMatchInlineSnapshot(`
+ {
+ "width": {
+ "styleSourceId": "4",
+ "value": {
+ "type": "unit",
+ "unit": "px",
+ "value": 400,
+ },
+ },
+ }
+ `);
+});
diff --git a/apps/builder/app/builder/features/style-panel/shared/style-info.ts b/apps/builder/app/builder/features/style-panel/shared/style-info.ts
index 4478f8b015..3d4d1e75f3 100644
--- a/apps/builder/app/builder/features/style-panel/shared/style-info.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/style-info.ts
@@ -50,19 +50,20 @@ type InheritedProperties = {
[property in StyleProperty]?: InheritedValueInfo;
};
-type PreviousSourceValueInfo = {
+type SourceValueInfo = {
styleSourceId: string;
value: StyleValue;
};
-type PreviousSourceProperties = {
- [property in StyleProperty]?: PreviousSourceValueInfo;
+type SourceProperties = {
+ [property in StyleProperty]?: SourceValueInfo;
};
export type StyleValueInfo = {
value: StyleValue;
local?: StyleValue;
- previousSource?: PreviousSourceValueInfo;
+ previousSource?: SourceValueInfo;
+ nextSource?: SourceValueInfo;
cascaded?: CascadedValueInfo;
inherited?: InheritedValueInfo;
preset?: StyleValue;
@@ -99,6 +100,7 @@ export const getStyleSource = (
for (const info of styleValueInfos) {
if (
info?.previousSource !== undefined ||
+ info?.nextSource !== undefined ||
info?.cascaded !== undefined ||
info?.inherited !== undefined
) {
@@ -295,7 +297,7 @@ export const getPreviousSourceInfo = (
selectedInstanceSelector: InstanceSelector,
selectedStyleSourceSelector: StyleSourceSelector
) => {
- const previousSourceStyle: PreviousSourceProperties = {};
+ const previousSourceStyle: SourceProperties = {};
const [selectedInstanceId] = selectedInstanceSelector;
const { styleSourceId } = selectedStyleSourceSelector;
const styleSourceSelection = styleSourceSelections.get(selectedInstanceId);
@@ -319,6 +321,36 @@ export const getPreviousSourceInfo = (
return previousSourceStyle;
};
+export const getNextSourceInfo = (
+ styleSourceSelections: StyleSourceSelections,
+ stylesByInstanceId: Map<Instance["id"], StyleDecl[]>,
+ selectedInstanceSelector: InstanceSelector,
+ selectedStyleSourceSelector: StyleSourceSelector
+) => {
+ const nextSourceStyle: SourceProperties = {};
+ const [selectedInstanceId] = selectedInstanceSelector;
+ const { styleSourceId } = selectedStyleSourceSelector;
+ const styleSourceSelection = styleSourceSelections.get(selectedInstanceId);
+ const instanceStyles = stylesByInstanceId.get(selectedInstanceId);
+ if (styleSourceSelection === undefined || instanceStyles === undefined) {
+ return nextSourceStyle;
+ }
+ const nextSourceIds = styleSourceSelection.values.slice(
+ // exclude current style source
+ styleSourceSelection.values.indexOf(styleSourceId) + 1
+ );
+ // expect instance styles to be ordered
+ for (const styleDecl of instanceStyles) {
+ if (nextSourceIds.includes(styleDecl.styleSourceId)) {
+ nextSourceStyle[styleDecl.property] = {
+ styleSourceId: styleDecl.styleSourceId,
+ value: styleDecl.value,
+ };
+ }
+ }
+ return nextSourceStyle;
+};
+
/**
* combine all local, cascaded, inherited and browser styles
*/
@@ -419,6 +451,26 @@ export const useStyleInfo = () => {
selectedOrLastStyleSourceSelector,
]);
+ const nextSourceInfo = useMemo(() => {
+ if (
+ selectedInstanceSelector === undefined ||
+ selectedOrLastStyleSourceSelector === undefined
+ ) {
+ return {};
+ }
+ return getNextSourceInfo(
+ styleSourceSelections,
+ stylesByInstanceId,
+ selectedInstanceSelector,
+ selectedOrLastStyleSourceSelector
+ );
+ }, [
+ styleSourceSelections,
+ stylesByInstanceId,
+ selectedInstanceSelector,
+ selectedOrLastStyleSourceSelector,
+ ]);
+
const presetStyle = useMemo(() => {
const selectedInstanceId = selectedInstanceSelector?.[0];
if (selectedInstanceId === undefined) {
@@ -464,9 +516,11 @@ export const useStyleInfo = () => {
const inherited = inheritedInfo[property];
const cascaded = cascadedInfo[property];
const previousSource = previousSourceInfo[property];
+ const nextSource = nextSourceInfo[property];
const local = selectedStyle?.[property];
const value =
local ??
+ nextSource?.value ??
previousSource?.value ??
cascaded?.value ??
inherited?.value ??
@@ -477,6 +531,7 @@ export const useStyleInfo = () => {
styleInfoData[property] = {
value,
local,
+ nextSource,
previousSource,
cascaded,
inherited,
@@ -487,6 +542,7 @@ export const useStyleInfo = () => {
styleInfoData[property] = {
value,
local,
+ nextSource,
previousSource,
cascaded,
inherited,
@@ -502,6 +558,7 @@ export const useStyleInfo = () => {
presetStyle,
inheritedInfo,
cascadedInfo,
+ nextSourceInfo,
previousSourceInfo,
selectedStyle,
]);
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.tsx
index 57014b312f..ae7c3102ba 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
@@ -329,7 +329,7 @@ const renderMenuItems = (props: {
return;
}
return (
- <>
+ <Fragment key={currentCategory}>
<DropdownMenuSeparator />
<DropdownMenuLabel>
{humanizeString(currentCategory)}
@@ -366,7 +366,7 @@ const renderMenuItems = (props: {
{label}
</DropdownMenuItem>
))}
- </>
+ </Fragment>
);
})}
</>