From 7daff34c21ff4d13fbe7ff7a68387fe2ddddb591 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Mon, 26 Dec 2022 19:49:15 +0300 Subject: [PATCH] Pass property to control (#700) We might not need to pass style config everywhere and instead we can pass only property. In another PR will remove passing style configs everywhere. --- .../style-panel/controls/color/color-control.tsx | 8 ++++---- .../controls/font-family/font-family-control.tsx | 6 +++--- .../controls/font-weight/font-weight-control.tsx | 6 +++--- .../style-panel/controls/image/image-control.tsx | 6 +++--- .../style-panel/controls/menu/menu-control.tsx | 15 +++++++-------- .../controls/select/select-control.tsx | 5 +++-- .../style-panel/controls/text/text-control.tsx | 13 +++++++------ .../sections/flex-child/flex-child.tsx | 15 ++++++--------- .../style-panel/sections/layout/layout.tsx | 3 ++- .../features/style-panel/style-sections.tsx | 11 +++++++---- .../features/style-panel/style-settings.tsx | 9 +++++---- 11 files changed, 50 insertions(+), 47 deletions(-) diff --git a/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx b/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx index 4bc47ae958..058f0fb1d2 100644 --- a/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx +++ b/apps/designer/app/designer/features/style-panel/controls/color/color-control.tsx @@ -5,11 +5,11 @@ import { ColorPicker } from "../../shared/color-picker"; import { colord } from "colord"; export const ColorControl = ({ + property, currentStyle, setProperty, - styleConfig, }: ControlProps) => { - let value = currentStyle[styleConfig.property] ?? { + let value = currentStyle[property] ?? { // provide default value to avoid control hiding // when value is recomputed type: "rgb" as const, @@ -19,7 +19,7 @@ export const ColorControl = ({ alpha: 0, }; - const setValue = setProperty(styleConfig.property); + const setValue = setProperty(property); if (value.type !== "rgb") { // Support previously set colors @@ -50,7 +50,7 @@ export const ColorControl = ({ return ( { setValue(value, { isEphemeral: true }); diff --git a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx index e27431fe68..8ccacfcb68 100644 --- a/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx +++ b/apps/designer/app/designer/features/style-panel/controls/font-family/font-family-control.tsx @@ -6,14 +6,14 @@ import { useState } from "react"; import { toValue } from "@webstudio-is/css-engine"; export const FontFamilyControl = ({ + property, currentStyle, setProperty, - styleConfig, }: ControlProps) => { - const value = currentStyle[styleConfig.property]; + const value = currentStyle[property]; const [isOpen, setIsOpen] = useState(false); - const setValue = setProperty(styleConfig.property); + const setValue = setProperty(property); return ( { - const fontWeight = currentStyle[styleConfig.property]; + const fontWeight = currentStyle[property]; // We need the font family to determine which font weights are available const fontFamily = currentStyle.fontFamily; @@ -84,7 +84,7 @@ export const FontWeightControl = ({ toValue(fontWeight) ); - const setValue = setProperty(styleConfig.property); + const setValue = setProperty(property); return ( { - const value = currentStyle[styleConfig.property]; + const value = currentStyle[property]; - const setValue = setProperty(styleConfig.property); + const setValue = setProperty(property); const [intermediateValue, setIntermediateValue] = useState< StyleValue | IntermediateStyleValue @@ -27,7 +28,7 @@ export const TextControl = ({ ({ @@ -38,7 +39,7 @@ export const TextControl = ({ setIntermediateValue(styleValue); if (styleValue === undefined) { - deleteProperty(styleConfig.property, { isEphemeral: true }); + deleteProperty(property, { isEphemeral: true }); return; } @@ -50,7 +51,7 @@ export const TextControl = ({ if (styleValue !== undefined) { setValue(styleValue, { isEphemeral: true }); } else { - deleteProperty(styleConfig.property, { isEphemeral: true }); + deleteProperty(property, { isEphemeral: true }); } }} onChangeComplete={({ value }) => { @@ -58,7 +59,7 @@ export const TextControl = ({ setIntermediateValue(undefined); }} onAbort={() => { - deleteProperty(styleConfig.property, { isEphemeral: true }); + deleteProperty(property, { isEphemeral: true }); }} /> diff --git a/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx b/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx index ae4945ebeb..5c53347460 100644 --- a/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx +++ b/apps/designer/app/designer/features/style-panel/sections/flex-child/flex-child.tsx @@ -30,7 +30,7 @@ export const FlexChildSection = (props: RenderCategoryProps) => { }; const FlexChildSectionAlign = (props: RenderCategoryProps) => { - const { setProperty, deleteProperty, currentStyle, sectionStyle } = props; + const { setProperty, deleteProperty, currentStyle } = props; const setAlignSelf = setProperty("alignSelf"); return ( @@ -41,7 +41,7 @@ const FlexChildSectionAlign = (props: RenderCategoryProps) => { onReset={() => deleteProperty("alignSelf")} /> setAlignSelf(value)} value={toValue(currentStyle.alignSelf)} items={[ @@ -82,7 +82,7 @@ const FlexChildSectionAlign = (props: RenderCategoryProps) => { }; const FlexChildSectionSizing = (props: RenderCategoryProps) => { - const { createBatchUpdate, currentStyle, sectionStyle } = props; + const { createBatchUpdate, currentStyle } = props; const setSizing = createBatchUpdate(); return ( @@ -97,10 +97,7 @@ const FlexChildSectionSizing = (props: RenderCategoryProps) => { }} /> { switch (value) { case "none": { @@ -204,7 +201,7 @@ const FlexChildSectionSizingPopover = ({ }; const FlexChildSectionOrder = (props: RenderCategoryProps) => { - const { deleteProperty, setProperty, currentStyle, sectionStyle } = props; + const { deleteProperty, setProperty, currentStyle } = props; const setOrder = setProperty("order"); return ( @@ -215,7 +212,7 @@ const FlexChildSectionOrder = (props: RenderCategoryProps) => { onReset={() => deleteProperty("order")} /> { switch (value) { case "0": 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.tsx index c6ed754723..ab0925090a 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 @@ -36,7 +36,7 @@ const LayoutSectionFlex = ({ alignContent, ].some((config) => config !== undefined); - const flexWrapValue = flexWrap.currentStyle[flexWrap.styleConfig.property]; + const flexWrapValue = flexWrap.currentStyle.flexWrap; // From design: Notice that the align-content icon button is not visible by default. // This property only applies when flex-wrap is set to "wrap". @@ -161,6 +161,7 @@ export const LayoutSection = ({ onReset={() => deleteProperty("display")} /> { const Control = controls[styleConfig.control]; - const { property } = styleConfig; if (!Control) { return null; } @@ -69,11 +71,12 @@ export const renderProperty = ({ return ( deleteProperty(styleConfig.property)} + onReset={() => deleteProperty(property)} /> = []; for (const styleConfig of styleConfigs) { - const isInCategory = categoryProperties.includes(styleConfig.property); + const { property } = styleConfig; + const isInCategory = categoryProperties.includes(property); // We don't want to filter out inapplicable styles if user wants to apply them anyway const isApplicable = isSearchMode ? true @@ -125,6 +126,7 @@ export const StyleSettings = ({ const isRendered = didRender(category, styleConfig); const element = { ...rest, + property, setProperty, deleteProperty, currentStyle, @@ -132,15 +134,14 @@ export const StyleSettings = ({ category, }; - sectionStyle[styleConfig.property] = element; + sectionStyle[property] = element; // @todo remove isRendered once spacing section is converted to a section if (isInCategory && isApplicable && isRendered === false) { // We are making a separate array of properties which come after the "moreFrom" // so we can make them collapsable if ( - (styleConfig.property === moreFrom || - moreStyleConfigsByCategory.length !== 0) && + (property === moreFrom || moreStyleConfigsByCategory.length !== 0) && isSearchMode === false ) { moreStyleConfigsByCategory.push(element);