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
Access style config by property (#702)
Bogdan Chadkin983fb650de7a
diff --git a/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx b/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsxindex 76e79dc44c..02257074cc 100644--- a/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/menu/menu-control.tsx@@ -1,15 +1,16 @@import { IconButtonWithMenu } from "@webstudio-is/design-system";import { useIsFromCurrentBreakpoint } from "../../shared/use-is-from-current-breakpoint";import type { ControlProps } from "../../style-sections";-import { iconConfigs } from "../../shared/configs";+import { iconConfigs, styleConfigByName } from "../../shared/configs";import { toValue } from "@webstudio-is/css-engine";export const MenuControl = ({property,+ items: passedItems,currentStyle,setProperty,- styleConfig,}: ControlProps) => {+ const { label } = styleConfigByName[property];const value = currentStyle[property];const isFromCurrentBreakpoint = useIsFromCurrentBreakpoint(property);@@ -31,7 +32,7 @@ export const MenuControl = ({: 0}deg)`,};- const items = styleConfig.items+ const items = passedItems.map((item) => {const ItemIcon = iconProps[item.name];return {@@ -43,7 +44,7 @@ export const MenuControl = ({return (<IconButtonWithMenuicon={items.find(({ name }) => name === currentValue)?.icon}- label={styleConfig.label}+ label={label}items={items}value={String(currentValue)}isActive={isFromCurrentBreakpoint}diff --git a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsxindex eeacdb60ca..49d89ff499 100644--- a/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/select/select-control.tsx@@ -6,7 +6,7 @@ export const SelectControl = ({property,currentStyle,setProperty,- styleConfig,+ items,}: ControlProps) => {const value = currentStyle[property];@@ -17,7 +17,7 @@ export const SelectControl = ({// show empty field instead of radix placeholder// like css value input doesplaceholder=""- options={styleConfig.items.map(({ label }) => label)}+ options={items.map(({ label }) => label)}value={toValue(value)}onChange={setValue}/>diff --git a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsxindex 9c1521b2dd..4ecb3d2e14 100644--- a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx+++ b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx@@ -6,15 +6,17 @@ import { ControlProps } from "../../style-sections";import type { StyleValue } from "@webstudio-is/css-data";import { useState } from "react";import { Box, EnhancedTooltip } from "@webstudio-is/design-system";+import { styleConfigByName } from "../../shared/configs";export const TextControl = ({property,+ items,currentStyle,setProperty,deleteProperty,- styleConfig,icon,}: ControlProps & { icon?: JSX.Element }) => {+ const { label } = styleConfigByName[property];const value = currentStyle[property];const setValue = setProperty(property);@@ -24,14 +26,14 @@ export const TextControl = ({>();return (- <EnhancedTooltip content={styleConfig.label}>+ <EnhancedTooltip content={label}><Box><CssValueInputicon={icon}property={property}value={value}intermediateValue={intermediateValue}- keywords={styleConfig.items.map((item) => ({+ keywords={items.map((item) => ({type: "keyword",value: item.name,}))}diff --git a/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx b/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsxindex ab0925090a..da522eaa3f 100644--- a/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx+++ b/apps/designer/app/designer/features/style-panel/sections/layout/layout.tsx@@ -7,6 +7,7 @@ import { renderProperty } from "../../style-sections";import { MenuControl, SelectControl, TextControl } from "../../controls";import { PropertyName } from "../../shared/property-name";import { ColumnGapIcon, RowGapIcon } from "@webstudio-is/icons";+import { styleConfigByName } from "../../shared/configs";const LayoutSectionFlex = ({currentStyle,@@ -63,26 +64,18 @@ const LayoutSectionFlex = ({<Box css={{ gridArea: "grid" }}><FlexGrid currentStyle={currentStyle} batchUpdate={batchUpdate} /></Box>- {flexDirection?.styleConfig && (- <Box css={{ gridArea: "flexDirection" }}>- <MenuControl {...flexDirection} />- </Box>- )}- {flexWrap?.styleConfig && (- <Box css={{ gridArea: "flexWrap" }}>- <MenuControl {...flexWrap} />- </Box>- )}- {alignItems?.styleConfig && (- <Box css={{ gridArea: "alignItems" }}>- <MenuControl {...alignItems} />- </Box>- )}- {justifyContent?.styleConfig && (- <Box css={{ gridArea: "justifyContent" }}>- <MenuControl {...justifyContent} />- </Box>- )}+ <Box css={{ gridArea: "flexDirection" }}>+ <MenuControl {...flexDirection} />+ </Box>+ <Box css={{ gridArea: "flexWrap" }}>+ <MenuControl {...flexWrap} />+ </Box>+ <Box css={{ gridArea: "alignItems" }}>+ <MenuControl {...alignItems} />+ </Box>+ <Box css={{ gridArea: "justifyContent" }}>+ <MenuControl {...justifyContent} />+ </Box>{alignContent && showAlignContent && (<Box css={{ gridArea: "alignContent" }}><MenuControl {...alignContent} />@@ -101,25 +94,19 @@ const LayoutSectionFlex = ({alignItems: "center",}}>- {columnGap?.styleConfig && (- <Box css={{ gridArea: "columnGap" }}>- <TextControl icon={<ColumnGapIcon />} {...columnGap} />- </Box>- )}- {rowGap?.styleConfig && columnGap?.styleConfig && (- <Box css={{ gridArea: "lock", px: "$spacing$3" }}>- <Lock- pairedKeys={["columnGap", "rowGap"]}- currentStyle={currentStyle}- batchUpdate={batchUpdate}- />- </Box>- )}- {rowGap?.styleConfig && (- <Box css={{ gridArea: "rowGap" }}>- <TextControl icon={<RowGapIcon />} {...rowGap} />- </Box>- )}+ <Box css={{ gridArea: "columnGap" }}>+ <TextControl icon={<ColumnGapIcon />} {...columnGap} />+ </Box>+ <Box css={{ gridArea: "lock", px: "$spacing$3" }}>+ <Lock+ pairedKeys={["columnGap", "rowGap"]}+ currentStyle={currentStyle}+ batchUpdate={batchUpdate}+ />+ </Box>+ <Box css={{ gridArea: "rowGap" }}>+ <TextControl icon={<RowGapIcon />} {...rowGap} />+ </Box></Grid></Flex>);@@ -150,32 +137,27 @@ export const LayoutSection = ({const displayValue = toValue(currentStyle.display);const { display } = sectionStyle;+ const { label, items } = styleConfigByName.display;return (<>- {display?.styleConfig && (- <Grid css={{ gridTemplateColumns: "4fr 6fr" }}>- <PropertyName- property="display"- label={display.styleConfig.label}- onReset={() => deleteProperty("display")}- />- <SelectControl- property="display"- category={display.category}- currentStyle={display.currentStyle}- setProperty={display.setProperty}- deleteProperty={display.deleteProperty}- // show only important values first and hide others with scroll- styleConfig={{- ...display.styleConfig,- items: display.styleConfig.items- .filter((item) => orderedDisplayValues.includes(item.name))- .sort(compareDisplayValues),- }}- />- </Grid>- )}+ <Grid css={{ gridTemplateColumns: "4fr 6fr" }}>+ <PropertyName+ property="display"+ label={label}+ onReset={() => deleteProperty("display")}+ />+ <SelectControl+ property="display"+ currentStyle={display.currentStyle}+ setProperty={display.setProperty}+ deleteProperty={display.deleteProperty}+ // show only important values first and hide others with scroll+ items={items+ .filter((item) => orderedDisplayValues.includes(item.name))+ .sort(compareDisplayValues)}+ />+ </Grid>{displayValue === "flex" || displayValue === "inline-flex" ? (<LayoutSectionFlex@@ -187,9 +169,7 @@ export const LayoutSection = ({) : (styleConfigsByCategory.map((entry) =>// exclude display already rendered above- entry.styleConfig.property === "display"- ? null- : renderProperty(entry)+ entry.property === "display" ? null : renderProperty(entry)))}</>diff --git a/apps/designer/app/designer/features/style-panel/shared/configs.ts b/apps/designer/app/designer/features/style-panel/shared/configs.tsindex 43d679b8bf..31eec8c669 100644--- a/apps/designer/app/designer/features/style-panel/shared/configs.ts+++ b/apps/designer/app/designer/features/style-panel/shared/configs.ts@@ -1,5 +1,6 @@import { categories, type Category } from "@webstudio-is/react-sdk";import type { StyleProperty, AppliesTo } from "@webstudio-is/css-data";+import { keywordValues, properties } from "@webstudio-is/css-data";import { humanizeString } from "~/shared/string-utils";import {IconRecords,@@ -32,7 +33,6 @@ import {RowGapIcon,ColumnGapIcon,} from "@webstudio-is/icons";-import { keywordValues, properties } from "@webstudio-is/css-data";import type * as Controls from "../controls";type BaseStyleConfig = {@@ -75,7 +75,7 @@ const getControl = (property: StyleProperty): Control => {const createStyleConfigs = () => {// We have same properties in different categories: alignSelf is in grid children and flex children// but this list has to contain only unique props- const map: { [prop in Property]?: StyleConfig } = {};+ const styleConfigByName = {} as { [prop in StyleProperty]: StyleConfig };let category: Category;@@ -87,7 +87,7 @@ const createStyleConfigs = () => {const keywords = keywordValues[property] || [];const label = humanizeString(property);- map[property] = {+ styleConfigByName[property] = {label,property,appliesTo: properties[property].appliesTo,@@ -100,10 +100,10 @@ const createStyleConfigs = () => {}}- return Object.values(map);+ return { styleConfigs: Object.values(styleConfigByName), styleConfigByName };};-export const styleConfigs: Array<StyleConfig> = createStyleConfigs();+export const { styleConfigs, styleConfigByName } = createStyleConfigs();export const iconConfigs: IconRecords = {// layoutdiff --git a/apps/designer/app/designer/features/style-panel/style-sections.tsx b/apps/designer/app/designer/features/style-panel/style-sections.tsxindex 8be6007059..5efe847f5d 100644--- a/apps/designer/app/designer/features/style-panel/style-sections.tsx+++ b/apps/designer/app/designer/features/style-panel/style-sections.tsx@@ -1,6 +1,6 @@import { Grid } from "@webstudio-is/design-system";import { toValue } from "@webstudio-is/css-engine";-import type { StyleConfig } from "./shared/configs";+import { styleConfigByName } from "./shared/configs";import type { Category } from "@webstudio-is/react-sdk";import type { Style, StyleProperty } from "@webstudio-is/css-data";import type {@@ -26,11 +26,10 @@ import { PropertyName } from "./shared/property-name";export type ControlProps = {property: StyleProperty;+ items: Array<{ label: string; name: string }>;+ currentStyle: Style;setProperty: SetProperty;deleteProperty: DeleteProperty;- currentStyle: Style;- styleConfig: StyleConfig;- category: Category;};export type RenderCategoryProps = {@@ -48,10 +47,10 @@ export type RenderCategoryProps = {export type RenderPropertyProps = {property: StyleProperty;+ items: Array<{ label: string; name: string }>;setProperty: SetProperty;deleteProperty: DeleteProperty;currentStyle: Style;- styleConfig: StyleConfig;category: Category;};@@ -60,10 +59,10 @@ export const renderProperty = ({currentStyle,setProperty,deleteProperty,- styleConfig,category,}: RenderPropertyProps) => {- const Control = controls[styleConfig.control];+ const { label, control, items } = styleConfigByName[property];+ const Control = controls[control];if (!Control) {return null;}@@ -72,16 +71,15 @@ export const renderProperty = ({<Grid key={category + property} css={{ gridTemplateColumns: "4fr 6fr" }}><PropertyNameproperty={property}- label={styleConfig.label}+ label={label}onReset={() => deleteProperty(property)}/><Controlproperty={property}+ items={items}currentStyle={currentStyle}setProperty={setProperty}deleteProperty={deleteProperty}- styleConfig={styleConfig}- category={category}/></Grid>);diff --git a/apps/designer/app/designer/features/style-panel/style-settings.tsx b/apps/designer/app/designer/features/style-panel/style-settings.tsxindex 12ffd7e2f5..e89aaf550e 100644--- a/apps/designer/app/designer/features/style-panel/style-settings.tsx+++ b/apps/designer/app/designer/features/style-panel/style-settings.tsx@@ -3,7 +3,11 @@ import { categories, type Category } from "@webstudio-is/react-sdk";import type { CssRule, Style, StyleProperty } from "@webstudio-is/css-data";import { toValue } from "@webstudio-is/css-engine";-import { type StyleConfig, styleConfigs } from "./shared/configs";+import {+ type StyleConfig,+ styleConfigs,+ styleConfigByName,+} from "./shared/configs";import { CollapsibleSection } from "~/designer/shared/inspector";import {renderCategory,@@ -19,7 +23,10 @@ import { type SelectedInstanceData } from "@webstudio-is/project";import { type RenderPropertyProps } from "./style-sections";// Finds a property/value by using any available form: property, label, value-const filterProperties = (properties: Array<string>, search: string) => {+const filterProperties = (+ properties: ReadonlyArray<StyleProperty>,+ search: string+) => {const searchParts = search.split(" ").map((part) => part.trim());const includes = (property: string) => {if (property.toLowerCase().includes(search)) {@@ -34,21 +41,17 @@ const filterProperties = (properties: Array<string>, search: string) => {);};return properties.filter((property) => {- for (const styleConfig of styleConfigs) {- if (styleConfig.property !== property) {- continue;- }- if (includes(styleConfig.property)) {- return true;- }- if (includes(styleConfig.label)) {+ const { label, items } = styleConfigByName[property];+ if (includes(property)) {+ return true;+ }+ if (includes(label)) {+ return true;+ }+ for (const item of items) {+ if (includes(item.name) || includes(item.label)) {return true;}- for (const item of styleConfig.items) {- if (includes(item.name) || includes(item.label)) {- return true;- }- }}return false;});@@ -76,7 +79,7 @@ const appliesTo = (styleConfig: StyleConfig, currentStyle: Style): boolean => {return true;};-const didRender = (category: Category, { property }: StyleConfig): boolean => {+const didRender = (category: Category, property: StyleProperty): boolean => {// We only want to render the first thing in spacing since the widget will be the way to set all margin and paddingif (category === "spacing" && property !== categories.spacing.properties[0]) {return true;@@ -108,7 +111,7 @@ export const StyleSettings = ({for (category in categories) {// @todo seems like properties are the exact strings and styleConfig.property is not?const categoryProperties = filterProperties(- categories[category].properties as unknown as Array<string>,+ categories[category].properties,search);const { moreFrom } = categories[category];@@ -123,14 +126,14 @@ export const StyleSettings = ({const isApplicable = isSearchMode? true: appliesTo(styleConfig, currentStyle);- const isRendered = didRender(category, styleConfig);+ const isRendered = didRender(category, property);const element = {...rest,property,+ items: styleConfig.items,setProperty,deleteProperty,currentStyle,- styleConfig,category,};