From 17da4ab9690106e6a38091377d35b95450935157 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 10 Dec 2022 12:05:40 +0300 Subject: [PATCH] Add proprety reset button in popover Ref https://github.com/webstudio-is/webstudio-designer/issues/29 --- apps/designer/app/canvas/shared/styles.ts | 6 + .../sections/flex-child/flex-child.tsx | 35 ++++++ .../style-panel/sections/layout/layout.tsx | 5 + .../sections/typography/typography.tsx | 100 +++++++++++++-- .../style-panel/shared/property-name.tsx | 114 +++++++++++++----- .../style-panel/shared/use-style-data.ts | 16 ++- .../features/style-panel/style-sections.tsx | 16 +-- packages/css-engine/src/core/rules.ts | 8 ++ .../shared/tree-utils/set-instance-style.ts | 6 +- 9 files changed, 252 insertions(+), 54 deletions(-) diff --git a/apps/designer/app/canvas/shared/styles.ts b/apps/designer/app/canvas/shared/styles.ts index dfa0f3b9191..aae05f9c3eb 100644 --- a/apps/designer/app/canvas/shared/styles.ts +++ b/apps/designer/app/canvas/shared/styles.ts @@ -180,6 +180,12 @@ export const useCssRules = ({ for (property in dynamicStyle) { rule.styleMap.set(property, dynamicStyle[property]); } + // delete previously rendered properties when reset + for (const property of rule.styleMap.keys()) { + if (dynamicStyle[property] == null) { + rule.styleMap.delete(property); + } + } } cssEngine.render(); }, [id, cssRules]); 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 a22b77acbb0..824b16b6c65 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 @@ -38,6 +38,11 @@ const FlexChildSectionAlign = (props: RenderCategoryProps) => { { + sectionStyle.alignSelf.setProperty( + sectionStyle.alignSelf?.styleConfig.property + )({ type: "unset", value: "" }); + }} /> { sectionStyle.flexShrink?.styleConfig.property, ]} label="Sizing" + onReset={() => { + setSizing.setProperty("flexGrow")({ type: "unset", value: "" }); + setSizing.setProperty("flexShrink")({ type: "unset", value: "" }); + setSizing.publish(); + }} /> { + sectionStyle.flexBasis.setProperty( + sectionStyle.flexBasis?.styleConfig.property + )({ type: "unset", value: "" }); + }} /> @@ -176,6 +191,11 @@ const FlexChildSectionSizingPopover = ({ { + sectionStyle.flexGrow.setProperty( + sectionStyle.flexGrow?.styleConfig.property + )({ type: "unset", value: "" }); + }} /> @@ -183,6 +203,11 @@ const FlexChildSectionSizingPopover = ({ { + sectionStyle.flexShrink.setProperty( + sectionStyle.flexShrink?.styleConfig.property + )({ type: "unset", value: "" }); + }} /> @@ -205,6 +230,11 @@ const FlexChildSectionOrder = (props: RenderCategoryProps) => { { + sectionStyle.order.setProperty( + sectionStyle.order?.styleConfig.property + )({ type: "unset", value: "" }); + }} /> { { + sectionStyle.order.setProperty( + sectionStyle.order?.styleConfig.property + )({ type: "unset", value: "" }); + }} /> 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 17aba833038..0cb79dd7675 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 @@ -61,6 +61,11 @@ const LayoutSectionFlex = ({ { + sectionStyle.display.setProperty( + sectionStyle.display.styleConfig.property + )({ type: "unset", value: "" }); + }} /> diff --git a/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx b/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx index 67c941d281d..f9d33ad26e2 100644 --- a/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx +++ b/apps/designer/app/designer/features/style-panel/sections/typography/typography.tsx @@ -55,15 +55,39 @@ export const TypographySectionFont = (props: RenderCategoryProps) => { }} > - + { + sectionStyle.fontFamily.setProperty( + sectionStyle.fontFamily.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> - + { + sectionStyle.fontWeight.setProperty( + sectionStyle.fontWeight.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> - + { + sectionStyle.color.setProperty( + sectionStyle.color.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> @@ -81,15 +105,39 @@ export const TypographySectionSizing = (props: RenderCategoryProps) => { }} > - + { + sectionStyle.fontSize.setProperty( + sectionStyle.fontSize.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> - + { + sectionStyle.lineHeight.setProperty( + sectionStyle.lineHeight.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> - + { + sectionStyle.letterSpacing.setProperty( + sectionStyle.letterSpacing.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> @@ -235,11 +283,27 @@ export const TypographySectionAdvancedPopover = ( content={ - + { + sectionStyle.whiteSpace.setProperty( + sectionStyle.whiteSpace.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> - + { + sectionStyle.direction.setProperty( + sectionStyle.direction.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> setDirection(value)} @@ -259,7 +323,15 @@ export const TypographySectionAdvancedPopover = ( /> - + { + sectionStyle.hyphens.setProperty( + sectionStyle.hyphens.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> setHyphens(value)} @@ -279,7 +351,15 @@ export const TypographySectionAdvancedPopover = ( /> - + { + sectionStyle.textOverflow.setProperty( + sectionStyle.textOverflow.styleConfig.property + )({ type: "unset", value: "" }); + }} + /> setTextOverflow(value)} diff --git a/apps/designer/app/designer/features/style-panel/shared/property-name.tsx b/apps/designer/app/designer/features/style-panel/shared/property-name.tsx index 260911c56ab..48abc2b8696 100644 --- a/apps/designer/app/designer/features/style-panel/shared/property-name.tsx +++ b/apps/designer/app/designer/features/style-panel/shared/property-name.tsx @@ -1,39 +1,89 @@ -import { Flex, Label, Tooltip } from "@webstudio-is/design-system"; +import { useState } from "react"; +import type { StyleProperty } from "@webstudio-is/css-data"; +import { + Button, + Flex, + Box, + Text, + Label, + Tooltip, + Popover, + PopoverContent, + PopoverPortal, + PopoverTrigger, + Separator, +} from "@webstudio-is/design-system"; +import { UndoIcon } from "@webstudio-is/icons"; import { useIsFromCurrentBreakpoint } from "./use-is-from-current-breakpoint"; -import { propertyNameColorForSelectedBreakpoint } from "./constants"; -import type { PropertyProps } from "../style-sections"; -export const PropertyName = ({ property, label, css }: PropertyProps) => { +type PropertyProps = { + property: StyleProperty | StyleProperty[]; + label: string; + onReset: () => void; +}; + +export const PropertyName = ({ property, label, onReset }: PropertyProps) => { const isCurrentBreakpoint = useIsFromCurrentBreakpoint(property); + const [isOpen, setIsOpen] = useState(false); + const isPopoverEnabled = isCurrentBreakpoint; + + const labelElement = ( + + ); + + if (isPopoverEnabled) { + return ( + + + + {labelElement} + + + setIsOpen(false)}> + + + + + + + Resetting will revert to initial or inherited value + + + + + + + ); + } + return ( - - + - - - + {labelElement} + + ); }; -PropertyName.displayName = "PropertyName"; diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts index 3ea42e34faf..57d5e644fbb 100644 --- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts +++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts @@ -129,7 +129,13 @@ export const useStyleData = ({ } if (options.isEphemeral === false) { - setCurrentStyle({ ...currentStyle, [property]: nextValue }); + if (nextValue.type === "unset") { + const newStyle = { ...currentStyle }; + delete newStyle[property]; + setCurrentStyle(newStyle); + } else { + setCurrentStyle({ ...currentStyle, [property]: nextValue }); + } } }; }; @@ -169,8 +175,12 @@ export const useStyleData = ({ publishUpdates("update", updates); const nextStyle = updates.reduce( (currentStyle, { property, value }) => { - // @todo - currentStyle[property] = value; + if (value.type === "unset") { + delete currentStyle[property]; + } else { + // @todo + currentStyle[property] = value; + } return currentStyle; }, { ...currentStyle } diff --git a/apps/designer/app/designer/features/style-panel/style-sections.tsx b/apps/designer/app/designer/features/style-panel/style-sections.tsx index d97d770f76d..fb9b34f0928 100644 --- a/apps/designer/app/designer/features/style-panel/style-sections.tsx +++ b/apps/designer/app/designer/features/style-panel/style-sections.tsx @@ -2,7 +2,7 @@ import { Grid, type CSS } from "@webstudio-is/design-system"; import { toValue } from "@webstudio-is/css-engine"; import type { StyleConfig } from "./shared/configs"; import type { Category } from "@webstudio-is/react-sdk"; -import type { Style, StyleProperty } from "@webstudio-is/css-data"; +import type { Style } from "@webstudio-is/css-data"; import type { SetProperty, CreateBatchUpdate } from "./shared/use-style-data"; import type { InheritedStyle } from "./shared/get-inherited-style"; import * as controls from "./controls"; @@ -21,12 +21,6 @@ import { } from "./sections"; import { PropertyName } from "./shared/property-name"; -export type PropertyProps = { - property: StyleProperty | StyleProperty[]; - label: string; - css?: CSS; -}; - export type ControlProps = { setProperty: SetProperty; currentStyle: Style; @@ -71,7 +65,13 @@ export const renderProperty = ({ return ( - + { + setProperty(styleConfig.property)({ type: "unset", value: "" }); + }} + />