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
highlight the property name with blue color when value comes from the current breakpoint
Oleg Isonen5e85dca6861f
diff --git a/app/canvas/shared/instance.ts b/app/canvas/shared/instance.tsindex 29ffc2b652..1f540019fb 100644--- a/app/canvas/shared/instance.ts+++ b/app/canvas/shared/instance.ts@@ -1,4 +1,4 @@-import { useEffect } from "react";+import { useEffect, useMemo } from "react";import ObjectId from "bson-objectid";import {type InstanceProps,@@ -122,6 +122,11 @@ export const usePublishSelectedInstance = ({const [instance] = useSelectedInstance();const [selectedElement] = useSelectedElement();const [allUserProps] = useAllUserProps();+ const browserStyle = useMemo(+ () => getBrowserStyle(selectedElement),+ [selectedElement]+ );+useEffect(() => {// Unselects the instance by `undefined`let payload;@@ -139,7 +144,7 @@ export const usePublishSelectedInstance = ({id: instance.id,component: instance.component,cssRules: instance.cssRules,- browserStyle: getBrowserStyle(selectedElement),+ browserStyle,props,};}@@ -148,7 +153,7 @@ export const usePublishSelectedInstance = ({type: "selectInstance",payload,});- }, [instance, selectedElement, allUserProps, treeId]);+ }, [instance, allUserProps, treeId, browserStyle]);};export const usePublishRootInstance = () => {diff --git a/app/canvas/shared/style.ts b/app/canvas/shared/style.tsindex 8d4f186a32..f0a56a02b4 100644--- a/app/canvas/shared/style.ts+++ b/app/canvas/shared/style.ts@@ -1,21 +1,31 @@import store from "immerhin";import { type StyleUpdates } from "~/shared/component";-import { setInstanceStyleMutable } from "~/shared/tree-utils";+import { findInstanceById, setInstanceStyleMutable } from "~/shared/tree-utils";import { rootInstanceContainer, useSelectedInstance } from "./nano-values";import { useSubscribe } from "./pubsub";export const useUpdateStyle = () => {- const [selectedInstance] = useSelectedInstance();-+ const [selectedInstance, setSelectedInstance] = useSelectedInstance();useSubscribe<"updateStyle", StyleUpdates>("updateStyle",({ id, updates, breakpoint }) => {+ // Only update styles if they match the selected instance+ // It can potentially happen that we selected a difference instance right after we changed the style in style panel.+ if (id !== selectedInstance?.id) return;+store.createTransaction([rootInstanceContainer], (rootInstance) => {- if (rootInstance === undefined || id !== selectedInstance?.id) {+ if (rootInstance === undefined) {return;}setInstanceStyleMutable(rootInstance, id, updates, breakpoint);});++ if (rootInstanceContainer.value === undefined) return;+ const instance = findInstanceById(rootInstanceContainer.value, id);+ if (instance === undefined) return;+ // We need to set new version of the selected instance after a style update,+ // so that anything that depends on instance.cssRules gets updated too.+ setSelectedInstance(instance);});};diff --git a/app/designer/features/style-panel/lib/color-picker.tsx b/app/designer/features/style-panel/lib/color-picker.tsxindex 144abb4014..e3fb77baa5 100644--- a/app/designer/features/style-panel/lib/color-picker.tsx+++ b/app/designer/features/style-panel/lib/color-picker.tsx@@ -19,12 +19,14 @@ type ColorPickerProps = {onChange: (value: string) => void;onChangeComplete: (value: string) => void;value: string;+ id: string;};export const ColorPicker = ({value,onChange,onChangeComplete,+ id,}: ColorPickerProps) => {const [displayColorPicker, setDisplayColorPicker] = useState(false);// Color picker will use 0 as alpha value, which will force user to set alpha every time they have to change from transparent@@ -50,6 +52,7 @@ export const ColorPicker = ({onClick={() => setDisplayColorPicker((shown) => !shown)}variant="ghost"value={value}+ id={id}/></Flex></PopoverTrigger>diff --git a/app/designer/features/style-panel/lib/constants.ts b/app/designer/features/style-panel/lib/constants.tsnew file mode 100644index 0000000000..624617ee59--- /dev/null+++ b/app/designer/features/style-panel/lib/constants.ts@@ -0,0 +1,2 @@+// When a style value is defined on a selected breakpoint, we want to highlight the property name with this color.+export const propertyNameColorForSelectedBreakpoint = "$blue11";diff --git a/app/designer/features/style-panel/lib/spacing-widget.tsx b/app/designer/features/style-panel/lib/spacing-widget.tsxindex 9e472adb12..f932d1c84f 100644--- a/app/designer/features/style-panel/lib/spacing-widget.tsx+++ b/app/designer/features/style-panel/lib/spacing-widget.tsx@@ -1,11 +1,13 @@-import { Box } from "~/shared/design-system";+import { useEffect, useState } from "react";import {toValue,type StyleProperty,type StyleValue,} from "@webstudio-is/sdk";+import { Box } from "~/shared/design-system";import { SetProperty } from "../use-style-data";-import { useEffect, useState } from "react";+import { useIsFromCurrentBreakpoint } from "./utils/use-is-from-current-breakpoint";+import { propertyNameColorForSelectedBreakpoint } from "./constants";type SpacingSingularStyle = { [property in SpacingProperty]?: StyleValue };@@ -65,6 +67,9 @@ const styles = {textAlign: "center",outline: "none",},+ inputFromCurrentBreakpoint: {+ color: propertyNameColorForSelectedBreakpoint,+ },emptySpace: {gridArea: "2 / 2 / 3 / 2",background: "$loContrast",@@ -101,13 +106,14 @@ const styles = {};type TextFieldProps = {- property: string;+ property: StyleProperty;value: string | undefined;onEnter: (value: string) => void;};const TextField = ({ property, value, onEnter }: TextFieldProps) => {const [currentValue, setCurrentValue] = useState<string>(value ?? "");+ const isFromCurrentBreakpoint = useIsFromCurrentBreakpoint(property);useEffect(() => {setCurrentValue(value ?? "");@@ -127,7 +133,11 @@ const TextField = ({ property, value, onEnter }: TextFieldProps) => {onChange={(event: React.ChangeEvent<HTMLInputElement>) => {setCurrentValue(event.target.value);}}- css={styles.input}+ css={+ isFromCurrentBreakpoint+ ? { ...styles.input, ...styles.inputFromCurrentBreakpoint }+ : styles.input+ }/>);};diff --git a/app/designer/features/style-panel/lib/utils/get-css-rule-for-breakpoint.ts b/app/designer/features/style-panel/lib/utils/get-css-rule-for-breakpoint.tsnew file mode 100644index 0000000000..e82a7427b7--- /dev/null+++ b/app/designer/features/style-panel/lib/utils/get-css-rule-for-breakpoint.ts@@ -0,0 +1,12 @@+import { type CssRule, type Breakpoint } from "@webstudio-is/sdk";++/**+ * Find a instance css rule that is set on the provided breakpoint.+ */+export const getCssRuleForBreakpoint = (+ cssRules: Array<CssRule> = [],+ breakpoint?: Breakpoint+) => {+ if (breakpoint === undefined) return;+ return cssRules.find((cssRule) => cssRule.breakpoint === breakpoint.id);+};diff --git a/app/designer/features/style-panel/lib/utils/use-is-from-current-breakpoint.ts b/app/designer/features/style-panel/lib/utils/use-is-from-current-breakpoint.tsnew file mode 100644index 0000000000..fd16f26ffa--- /dev/null+++ b/app/designer/features/style-panel/lib/utils/use-is-from-current-breakpoint.ts@@ -0,0 +1,25 @@+import { useMemo } from "react";+import { type StyleProperty } from "@webstudio-is/sdk";+import {+ useSelectedBreakpoint,+ useSelectedInstanceData,+} from "~/designer/shared/nano-values";+import { getCssRuleForBreakpoint } from "./get-css-rule-for-breakpoint";++/**+ * Identify if the provided property is defined in the instance css rule matching the selected breakpoint.+ */+export const useIsFromCurrentBreakpoint = (property: StyleProperty) => {+ const [selectedBreakpoint] = useSelectedBreakpoint();+ const [selectedInstanceData] = useSelectedInstanceData();+ const cssRule = useMemo(+ () =>+ getCssRuleForBreakpoint(+ selectedInstanceData?.cssRules,+ selectedBreakpoint+ ),+ [selectedInstanceData, selectedBreakpoint]+ );+ if (cssRule === undefined) return false;+ return property in cssRule.style;+};diff --git a/app/designer/features/style-panel/render-property.tsx b/app/designer/features/style-panel/render-property.tsxindex 8011e39891..40da35a817 100644--- a/app/designer/features/style-panel/render-property.tsx+++ b/app/designer/features/style-panel/render-property.tsx@@ -1,11 +1,11 @@import { Flex, Grid, Label, Text } from "~/shared/design-system";import type { StyleConfig } from "~/shared/style-panel-configs";import {- Style,- StyleProperty,- StyleValue,- Category,categories,+ type Style,+ type StyleProperty,+ type Category,+ type StyleValue,} from "@webstudio-is/sdk";import type { SetProperty } from "./use-style-data";import type { InheritedStyle } from "./get-inherited-style";@@ -16,6 +16,8 @@ import {type SpacingProperty,type SpacingStyles,} from "./lib/spacing-widget";+import { useIsFromCurrentBreakpoint } from "./lib/utils/use-is-from-current-breakpoint";+import { propertyNameColorForSelectedBreakpoint } from "./lib/constants";const getFinalValue = ({currentStyle,@@ -35,6 +37,45 @@ const getFinalValue = ({return currentValue;};+type PropertyProps = {+ property: StyleProperty;+ label: string;+};++const PropertyName = ({ property, label }: PropertyProps) => {+ const isCurrentBreakpoint = useIsFromCurrentBreakpoint(property);++ return (+ <Label+ css={{+ gridColumn: "1",+ color: isCurrentBreakpoint+ ? propertyNameColorForSelectedBreakpoint+ : "$hiContrast",+ }}+ variant="contrast"+ size="1"+ htmlFor={property}+ >+ {label}+ </Label>+ );+};++const Unit = ({ value }: { value: StyleValue }) => {+ if (value.type !== "unit" || value.unit === "number") return null;+ return (+ <Text+ css={{+ fontSize: "$1",+ cursor: "default",+ }}+ >+ {value.unit}+ </Text>+ );+};+type ControlProps = {setProperty: SetProperty;currentStyle: Style;@@ -60,11 +101,10 @@ const ColorField = ({return (<Grid columns={2} align="center" gapX="1">- <Label css={{ gridColumn: "1" }} size="1">- {styleConfig.label}- </Label>+ <PropertyName property={styleConfig.property} label={styleConfig.label} /><Flex align="center" css={{ gridColumn: "2/4" }} gap="1"><ColorPicker+ id={styleConfig.property}value={String(value.value)}onChange={(value) => {setValue(value, { isEphemeral: true });@@ -107,20 +147,8 @@ const Spacing = ({return <SpacingWidget setProperty={setProperty} values={styles} />;};-const Unit = ({ value }: { value: StyleValue }) => {- if (value.type !== "unit" || value.unit === "number") return null;- return (- <Text- css={{- fontSize: "$1",- cursor: "default",- }}- >- {value.unit}- </Text>- );-};-+// @todo+// This is a cursor image for drag&drop value changing on the input by dragging horizontally//const svgCursor =// '<svg width="15" height="15" viewBox="0 0 15 15" xmlns="http://www.w3.org/2000/svg"><path d="M8.00012 1.5C8.00012 1.22386 7.77626 1 7.50012 1C7.22398 1 7.00012 1.22386 7.00012 1.5V13.5C7.00012 13.7761 7.22398 14 7.50012 14C7.77626 14 8.00012 13.7761 8.00012 13.5V1.5ZM3.31812 5.818C3.49386 5.64227 3.49386 5.35734 3.31812 5.18161C3.14239 5.00587 2.85746 5.00587 2.68173 5.18161L0.681729 7.18161C0.505993 7.35734 0.505993 7.64227 0.681729 7.818L2.68173 9.818C2.85746 9.99374 3.14239 9.99374 3.31812 9.818C3.49386 9.64227 3.49386 9.35734 3.31812 9.18161L2.08632 7.9498H5.50017C5.7487 7.9498 5.95017 7.74833 5.95017 7.4998C5.95017 7.25128 5.7487 7.0498 5.50017 7.0498H2.08632L3.31812 5.818ZM12.3181 5.18161C12.1424 5.00587 11.8575 5.00587 11.6817 5.18161C11.506 5.35734 11.506 5.64227 11.6817 5.818L12.9135 7.0498H9.50017C9.25164 7.0498 9.05017 7.25128 9.05017 7.4998C9.05017 7.74833 9.25164 7.9498 9.50017 7.9498H12.9135L11.6817 9.18161C11.506 9.35734 11.506 9.64227 11.6817 9.818C11.8575 9.99374 12.1424 9.99374 12.3181 9.818L14.3181 7.818C14.4939 7.64227 14.4939 7.35734 14.3181 7.18161L12.3181 5.18161Z" fill="#fff"></path></svg>';@@ -145,11 +173,10 @@ const TextFieldWithAutocomplete = ({return (<Grid columns={2} align="center" gapX="1">- <Label css={{ gridColumn: "1" }} size="1">- {styleConfig.label}- </Label>+ <PropertyName property={styleConfig.property} label={styleConfig.label} /><Flex align="center" css={{ gridColumn: "2/4" }} gap="1"><Autocomplete+ id={styleConfig.property}items={styleConfig.items}variant="ghost"css={{diff --git a/app/designer/features/style-panel/settings.tsx b/app/designer/features/style-panel/settings.tsxindex ccce1bb380..d78de80872 100644--- a/app/designer/features/style-panel/settings.tsx+++ b/app/designer/features/style-panel/settings.tsx@@ -1,6 +1,11 @@import { useState } from "react";import hyphenate from "hyphenate-style-name";-import { categories, type Category, type Style } from "@webstudio-is/sdk";+import {+ categories,+ type CssRule,+ type Category,+ type Style,+} from "@webstudio-is/sdk";import { Box, Flex, Collapsible, Button } from "~/shared/design-system";import { TriangleRightIcon, TriangleDownIcon } from "~/shared/icons";import { styleConfigs, type StyleConfig } from "~/shared/style-panel-configs";@@ -85,6 +90,7 @@ const ShowMore = ({ styleConfigs }: { styleConfigs: Array<JSX.Element> }) => {type VisualSettingsProps = {currentStyle: Style;inheritedStyle: InheritedStyle;+ cssRule?: CssRule;setProperty: SetProperty;selectedInstanceData: SelectedInstanceData;search: string;diff --git a/app/designer/features/style-panel/style-panel.tsx b/app/designer/features/style-panel/style-panel.tsxindex 7c42483ee9..9b75f4b362 100644--- a/app/designer/features/style-panel/style-panel.tsx+++ b/app/designer/features/style-panel/style-panel.tsx@@ -21,7 +21,7 @@ export const StylePanel = ({selectedInstanceData,publish,}: StylePanelProps) => {- const [currentStyle, inheritedStyle, setProperty] = useStyleData({+ const { currentStyle, inheritedStyle, setProperty } = useStyleData({selectedInstanceData,publish,});diff --git a/app/designer/features/style-panel/use-style-data.ts b/app/designer/features/style-panel/use-style-data.tsindex 43bead2d4d..89503c08dc 100644--- a/app/designer/features/style-panel/use-style-data.ts+++ b/app/designer/features/style-panel/use-style-data.ts@@ -1,17 +1,14 @@-import { useState, useMemo, useEffect } from "react";+import { useState, useMemo, useEffect, useCallback } from "react";import type { SelectedInstanceData, StyleUpdates } from "~/shared/component";-import {- type Breakpoint,- type Style,- type StyleProperty,-} from "@webstudio-is/sdk";+import { type StyleProperty } from "@webstudio-is/sdk";import { type Publish } from "~/designer/shared/canvas-iframe";import {useRootInstance,useSelectedBreakpoint,} from "../../shared/nano-values";import { parseCssValue } from "./parse-css-value";-import { getInheritedStyle, type InheritedStyle } from "./get-inherited-style";+import { getInheritedStyle } from "./get-inherited-style";+import { getCssRuleForBreakpoint } from "./lib/utils/get-css-rule-for-breakpoint";type UseStyleData = {publish: Publish;@@ -24,36 +21,35 @@ export type SetProperty = (property: StyleProperty) => (value: string, options?: StyleUpdateOptions) => void;-const getCurrentStyle = (- {- cssRules,- browserStyle,- }: Pick<SelectedInstanceData, "cssRules" | "browserStyle"> = {- cssRules: [],- browserStyle: {},- },- breakpoint?: Breakpoint-) => {- if (breakpoint === undefined) return browserStyle;- const cssRule = cssRules.find(- (cssRule) => cssRule.breakpoint === breakpoint.id- );- if (cssRule === undefined) return browserStyle;- return {- ...browserStyle,- ...cssRule.style,- };-};-export const useStyleData = ({selectedInstanceData,publish,-}: UseStyleData): [Style | void, InheritedStyle | void, SetProperty] => {+}: UseStyleData) => {const [rootInstance] = useRootInstance();const [selectedBreakpoint] = useSelectedBreakpoint();- const [currentStyle, setCurrentStyle] = useState<Style | undefined>(- getCurrentStyle(selectedInstanceData, selectedBreakpoint)+ const cssRule = useMemo(+ () =>+ getCssRuleForBreakpoint(+ selectedInstanceData?.cssRules,+ selectedBreakpoint+ ),+ [selectedInstanceData?.cssRules, selectedBreakpoint]+ );++ const getCurrentStyle = useCallback(+ () => ({+ ...selectedInstanceData?.browserStyle,+ ...cssRule?.style,+ }),+ [selectedInstanceData, cssRule]);++ const [currentStyle, setCurrentStyle] = useState(getCurrentStyle());++ useEffect(() => {+ setCurrentStyle(getCurrentStyle());+ }, [getCurrentStyle]);+const inheritedStyle = useMemo(() => {if (currentStyle === undefined ||@@ -65,10 +61,6 @@ export const useStyleData = ({return getInheritedStyle(rootInstance, selectedInstanceData.id);}, [currentStyle, selectedInstanceData, rootInstance]);- useEffect(() => {- setCurrentStyle(getCurrentStyle(selectedInstanceData, selectedBreakpoint));- }, [selectedInstanceData, selectedBreakpoint]);-const publishUpdates = (type: "update" | "preview",updates: StyleUpdates["updates"]@@ -109,5 +101,5 @@ export const useStyleData = ({};};- return [currentStyle, inheritedStyle, setProperty];+ return { currentStyle, inheritedStyle, setProperty };};diff --git a/app/shared/tree-utils/find-instance.ts b/app/shared/tree-utils/find-instance.tsindex 4d82644c26..ac665ee792 100644--- a/app/shared/tree-utils/find-instance.ts+++ b/app/shared/tree-utils/find-instance.ts@@ -3,7 +3,7 @@ import { type Instance } from "@webstudio-is/sdk";export const findInstanceById = (instance: Instance,id: Instance["id"]-): Instance | void => {+): Instance | undefined => {if (instance.id === id) {return instance;}