diff --git a/apps/builder/app/builder/features/style-panel/sections/position/inset-control.tsx b/apps/builder/app/builder/features/style-panel/sections/position/inset-control.tsx index d2031def152a..13d5fcd1c718 100644 --- a/apps/builder/app/builder/features/style-panel/sections/position/inset-control.tsx +++ b/apps/builder/app/builder/features/style-panel/sections/position/inset-control.tsx @@ -48,7 +48,6 @@ const Cell = ({ isOpen={isPopoverOpen} property={property} onClose={onPopoverClose} - createBatchUpdate={createBatchUpdate} /> void; - createBatchUpdate: CreateBatchUpdate; }) => { const [intermediateValue, setIntermediateValue] = useState< StyleValue | IntermediateStyleValue >(); - const onChange = ( - updates: Array, - options: StyleUpdateOptions - ) => { - const batch = createBatchUpdate(); - for (const update of updates) { - if (update.operation === "set") { - batch.setProperty(update.property)(update.value); - } - if (update.operation === "delete") { - batch.deleteProperty(update.property); - } - } - batch.publish(options); - }; - return ( $availableVariables.get()} onChange={(styleValue) => { setIntermediateValue(styleValue); - if (styleValue === undefined) { - onChange([{ operation: "delete", property }], { isEphemeral: true }); + const batch = createBatchUpdate(); + batch.deleteProperty(property); + batch.publish({ isEphemeral: true }); return; } - if (styleValue.type !== "intermediate") { - onChange([{ operation: "set", property, value: styleValue }], { - isEphemeral: true, - }); + const batch = createBatchUpdate(); + batch.setProperty(property)(styleValue); + batch.publish({ isEphemeral: true }); } }} onHighlight={(styleValue) => { if (styleValue === undefined) { - onChange([{ operation: "delete", property }], { isEphemeral: true }); + const batch = createBatchUpdate(); + batch.deleteProperty(property); + batch.publish({ isEphemeral: true }); return; } - - onChange([{ operation: "set", property, value: styleValue }], { - isEphemeral: true, - }); + const batch = createBatchUpdate(); + batch.setProperty(property)(styleValue); + batch.publish({ isEphemeral: true }); }} onChangeComplete={({ value, type, altKey, shiftKey }) => { - const updates: Array = []; - const options = { isEphemeral: false }; + const batch = createBatchUpdate(); const modifiers = { shiftKey, altKey }; const properties = isSpace(property) ? getSpaceModifiersGroup(property as SpaceStyleProperty, modifiers) : getInsetModifiersGroup(property as InsetProperty, modifiers); - setIntermediateValue(undefined); - - properties.forEach((property) => { - updates.push({ operation: "set", property, value }); - }); - onChange(updates, options); - + for (const property of properties) { + batch.setProperty(property)(value); + } + batch.publish({ isEphemeral: false }); if (type === "blur" || type === "enter") { onClosePopover(); } }} onAbort={() => { - onChange([{ operation: "delete", property }], { isEphemeral: true }); + const batch = createBatchUpdate(); + batch.deleteProperty(property); + batch.publish({ isEphemeral: true }); }} /> ); @@ -144,11 +123,10 @@ export const InputPopover = ({ value, isOpen, onClose, - createBatchUpdate, -}: Pick< - ComponentProps, - "styleSource" | "property" | "value" | "createBatchUpdate" -> & { +}: { + styleSource: StyleSource; + property: SpaceStyleProperty | InsetProperty; + value: StyleValue; isOpen: boolean; onClose: () => void; }) => { @@ -169,7 +147,6 @@ export const InputPopover = ({ styleSource={styleSource} value={value} property={property} - createBatchUpdate={createBatchUpdate} onClosePopover={onClose} /> diff --git a/apps/builder/app/builder/features/style-panel/sections/space/space.tsx b/apps/builder/app/builder/features/style-panel/sections/space/space.tsx index 6b1dc348c3a5..7214d217eab9 100644 --- a/apps/builder/app/builder/features/style-panel/sections/space/space.tsx +++ b/apps/builder/app/builder/features/style-panel/sections/space/space.tsx @@ -1,5 +1,4 @@ import { useState, useRef } from "react"; -import type { SectionProps } from "../shared/section"; import { SpaceLayout } from "./layout"; import { ValueText } from "../shared/value-text"; import { getSpaceModifiersGroup, useScrub } from "../shared/scrub"; @@ -7,10 +6,10 @@ import { spaceProperties } from "./properties"; import type { SpaceStyleProperty, HoverTarget } from "./types"; import { InputPopover } from "../shared/input-popover"; import { SpaceTooltip } from "./tooltip"; -import { getStyleSource } from "../../shared/style-info"; import { StyleSection } from "../../shared/style-section"; import { movementMapSpace, useKeyboardNavigation } from "../shared/keyboard"; -import type { CreateBatchUpdate } from "../../shared/use-style-data"; +import { useComputedStyleDecl, useComputedStyles } from "../../shared/model"; +import { createBatchUpdate, deleteProperty } from "../../shared/use-style-data"; const Cell = ({ isPopoverOpen, @@ -18,43 +17,28 @@ const Cell = ({ onHover, property, scrubStatus, - currentStyle, - createBatchUpdate, }: { isPopoverOpen: boolean; onPopoverClose: () => void; onHover: (target: HoverTarget | undefined) => void; property: SpaceStyleProperty; scrubStatus: ReturnType; - currentStyle: SectionProps["currentStyle"]; - createBatchUpdate: CreateBatchUpdate; }) => { - const styleInfo = currentStyle[property]; + const styleDecl = useComputedStyleDecl(property); const finalValue = - (scrubStatus.isActive && scrubStatus.values[property]) || styleInfo?.value; - const styleSource = getStyleSource(styleInfo); - - // for TypeScript - if (finalValue === undefined) { - return; - } + (scrubStatus.isActive && scrubStatus.values[property]) || + styleDecl.cascadedValue; return ( <> - + onHover({ property, element: event.currentTarget }) } @@ -79,18 +63,17 @@ const Cell = ({ export { spaceProperties as properties }; -export const Section = ({ - deleteProperty, - createBatchUpdate, - currentStyle, -}: SectionProps) => { +export const Section = () => { + const styles = useComputedStyles(spaceProperties); const [hoverTarget, setHoverTarget] = useState(); const scrubStatus = useScrub({ value: hoverTarget === undefined ? undefined - : currentStyle[hoverTarget.property]?.value, + : styles.find( + (styleDecl) => styleDecl.property === hoverTarget.property + )?.usedValue, target: hoverTarget, getModifiersGroup: getSpaceModifiersGroup, onChange: (values, options) => { @@ -163,8 +146,6 @@ export const Section = ({ onHover={handleHover} property={property} scrubStatus={scrubStatus} - currentStyle={currentStyle} - createBatchUpdate={createBatchUpdate} /> )} /> diff --git a/apps/builder/app/builder/features/style-panel/sections/space/tooltip.tsx b/apps/builder/app/builder/features/style-panel/sections/space/tooltip.tsx index 42c844b9bdeb..3cea1a41187a 100644 --- a/apps/builder/app/builder/features/style-panel/sections/space/tooltip.tsx +++ b/apps/builder/app/builder/features/style-panel/sections/space/tooltip.tsx @@ -1,10 +1,11 @@ -import type { SpaceStyleProperty } from "./types"; -import { PropertyTooltip } from "../../shared/property-name"; -import type { StyleInfo } from "../../shared/style-info"; import { useState, type ReactElement } from "react"; import { useModifierKeys } from "../../shared/modifier-keys"; import { getSpaceModifiersGroup } from "../shared/scrub"; -import type { CreateBatchUpdate } from "../../shared/use-style-data"; +import { createBatchUpdate } from "../../shared/use-style-data"; +import { Tooltip } from "@webstudio-is/design-system"; +import { PropertyInfo } from "../../property-label"; +import { useComputedStyles } from "../../shared/model"; +import type { SpaceStyleProperty } from "./types"; const sides = { paddingTop: "top", @@ -77,22 +78,19 @@ const isSameUnorderedArrays = ( export const SpaceTooltip = ({ property, - style, children, - createBatchUpdate, preventOpen, }: { property: SpaceStyleProperty; - style: StyleInfo; children: ReactElement; - createBatchUpdate: CreateBatchUpdate; preventOpen: boolean; }) => { - const [open, setOpen] = useState(false); + const [isOpen, setIsOpen] = useState(false); const modifiers = useModifierKeys(); const properties = [...getSpaceModifiersGroup(property, modifiers)]; + const styles = useComputedStyles(properties); const propertyContent = propertyContents.find((propertyContent) => isSameUnorderedArrays(propertyContent.properties, properties) @@ -102,27 +100,47 @@ export const SpaceTooltip = ({ if (preventOpen && value === true) { return; } - setOpen(value); + setIsOpen(value); + }; + + const resetProperties = () => { + const batch = createBatchUpdate(); + for (const property of properties) { + batch.deleteProperty(property); + } + batch.publish(); }; return ( - { - const batch = createBatchUpdate(); - for (const property of properties) { - batch.deleteProperty(property); - } - batch.publish(); + // prevent closing tooltip on content click + onPointerDown={(event) => event.preventDefault()} + triggerProps={{ + onClick: (event) => { + if (event.altKey) { + event.preventDefault(); + resetProperties(); + return; + } + }, }} + content={ + { + resetProperties(); + handleOpenChange(false); + }} + /> + } > + {/* @todo show tooltip on focus */}
{children}
-
+ ); };