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
feat: add shift/option modifiers to spacing and position input popovers (#3924)
Oleg Isonen939a2b82dcc0
diff --git a/apps/builder/app/builder/features/style-panel/sections/position/position-control.stories.tsx b/apps/builder/app/builder/features/style-panel/sections/position/position-control.stories.tsxindex 72ae119dea..c372e316f9 100644--- a/apps/builder/app/builder/features/style-panel/sections/position/position-control.stories.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/position/position-control.stories.tsx@@ -93,20 +93,18 @@ const bigValue = {} as const;export const PositionControlComponent = () => {- const { styleInfo, setProperty, deleteProperty, createBatchUpdate } =- useStyleInfo({- left: defaultValue,- right: bigValue,- top: defaultValue,- bottom: defaultValue,- });+ const { styleInfo, deleteProperty, createBatchUpdate } = useStyleInfo({+ left: defaultValue,+ right: bigValue,+ top: defaultValue,+ bottom: defaultValue,+ });return (<Box css={{ marginLeft: 100 }}><PositionControlcreateBatchUpdate={createBatchUpdate}currentStyle={styleInfo}- setProperty={setProperty}deleteProperty={deleteProperty}/></Box>diff --git a/apps/builder/app/builder/features/style-panel/sections/position/position-control.tsx b/apps/builder/app/builder/features/style-panel/sections/position/position-control.tsxindex db7add90ba..a011d61a2c 100644--- a/apps/builder/app/builder/features/style-panel/sections/position/position-control.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/position/position-control.tsx@@ -1,11 +1,10 @@import { Grid, theme } from "@webstudio-is/design-system";import { PositionLayout, type PositionProperty } from "./position-layout";import { movementMapPosition, useKeyboardNavigation } from "../shared/keyboard";-import { useRef, useState, type ComponentProps } from "react";+import { useRef, useState } from "react";import type {CreateBatchUpdate,DeleteProperty,- SetProperty,} from "../../shared/use-style-data";import { getStyleSource, type StyleInfo } from "../../shared/style-info";import { getPositionModifiersGroup, useScrub } from "../shared/scrub";@@ -21,12 +20,10 @@ const Cell = ({onHover,isPopoverOpen,onPopoverClose,- onChange,createBatchUpdate,}: {isPopoverOpen: boolean;onPopoverClose: () => void;- onChange: ComponentProps<typeof InputPopover>["onChange"];scrubStatus: ReturnType<typeof useScrub>;currentStyle: StyleInfo;property: PositionProperty;@@ -50,8 +47,8 @@ const Cell = ({value={finalValue}isOpen={isPopoverOpen}property={property}- onChange={onChange}onClose={onPopoverClose}+ createBatchUpdate={createBatchUpdate}/><PositionTooltipproperty={property}@@ -87,7 +84,6 @@ type HoverTarget = {};type PositionControlProps = {- setProperty: SetProperty;deleteProperty: DeleteProperty;createBatchUpdate: CreateBatchUpdate;currentStyle: StyleInfo;@@ -97,7 +93,6 @@ export const PositionControl = ({createBatchUpdate,currentStyle,deleteProperty,- setProperty,}: PositionControlProps) => {const [hoverTarget, setHoverTarget] = useState<HoverTarget>();@@ -192,13 +187,6 @@ export const PositionControl = ({layoutRef.current?.focus();}}}- onChange={(update, options) => {- if (update.operation === "set") {- setProperty(update.property)(update.value, options);- } else {- deleteProperty(update.property, options);- }- }}createBatchUpdate={createBatchUpdate}/>)}diff --git a/apps/builder/app/builder/features/style-panel/sections/position/position.tsx b/apps/builder/app/builder/features/style-panel/sections/position/position.tsxindex 81b91745c3..d5de8a5384 100644--- a/apps/builder/app/builder/features/style-panel/sections/position/position.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/position/position.tsx@@ -101,7 +101,6 @@ export const Section = ({<Grid gap={3} columns={2}><PositionControlcurrentStyle={currentStyle}- setProperty={setProperty}deleteProperty={deleteProperty}createBatchUpdate={createBatchUpdate}/>diff --git a/apps/builder/app/builder/features/style-panel/sections/shared/input-popover.tsx b/apps/builder/app/builder/features/style-panel/sections/shared/input-popover.tsxindex c5e70ad14c..3dfe42a16f 100644--- a/apps/builder/app/builder/features/style-panel/sections/shared/input-popover.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/shared/input-popover.tsx@@ -6,40 +6,65 @@ import {PopoverTrigger,PopoverContent,} from "@webstudio-is/design-system";-import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";+import type { StyleValue } from "@webstudio-is/css-engine";import {CssValueInput,type IntermediateStyleValue,} from "../../shared/css-value-input";import type { StyleSource } from "../../shared/style-info";import type {+ CreateBatchUpdate,StyleUpdate,StyleUpdateOptions,} from "../../shared/use-style-data";import { theme } from "@webstudio-is/design-system";+import { getPositionModifiersGroup, getSpaceModifiersGroup } from "./scrub";+import type { SpaceStyleProperty } from "../space/types";+import type { PositionProperty } from "../position/position-layout";const slideUpAndFade = keyframes({"0%": { opacity: 0, transform: "scale(0.8)" },"100%": { opacity: 1, transform: "scale(1)" },});+// We need to differentiate between marginTop and top for example.+const isSpace = (property: string) => {+ return property.startsWith("margin") || property.startsWith("padding");+};+const Input = ({styleSource,value,property,- onChange,onClosePopover,+ createBatchUpdate,}: {styleSource: StyleSource;- property: StyleProperty;+ property: SpaceStyleProperty | PositionProperty;value: StyleValue;- onChange: (update: StyleUpdate, options: StyleUpdateOptions) => void;onClosePopover: () => void;+ createBatchUpdate: CreateBatchUpdate;}) => {const [intermediateValue, setIntermediateValue] = useState<StyleValue | IntermediateStyleValue>();+ const onChange = (+ updates: Array<StyleUpdate>,+ 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 (<CssValueInputstyleSource={styleSource}@@ -50,38 +75,47 @@ const Input = ({setIntermediateValue(styleValue);if (styleValue === undefined) {- onChange({ operation: "delete", property }, { isEphemeral: true });+ onChange([{ operation: "delete", property }], { isEphemeral: true });return;}if (styleValue.type !== "intermediate") {- onChange(- { operation: "set", property, value: styleValue },- { isEphemeral: true }- );+ onChange([{ operation: "set", property, value: styleValue }], {+ isEphemeral: true,+ });}}}onHighlight={(styleValue) => {if (styleValue === undefined) {- onChange({ operation: "delete", property }, { isEphemeral: true });+ onChange([{ operation: "delete", property }], { isEphemeral: true });return;}- onChange(- { operation: "set", property, value: styleValue },- { isEphemeral: true }- );+ onChange([{ operation: "set", property, value: styleValue }], {+ isEphemeral: true,+ });}}- onChangeComplete={({ value, reason }) => {+ onChangeComplete={({ value, type, altKey, shiftKey }) => {+ const updates: Array<StyleUpdate> = [];+ const options = { isEphemeral: false };+ const modifiers = { shiftKey, altKey };+ const properties = isSpace(property)+ ? getSpaceModifiersGroup(property as SpaceStyleProperty, modifiers)+ : getPositionModifiersGroup(property as PositionProperty, modifiers);+setIntermediateValue(undefined);- onChange({ operation: "set", property, value }, { isEphemeral: false });- if (reason === "blur" || reason === "enter") {+ properties.forEach((property) => {+ updates.push({ operation: "set", property, value });+ });+ onChange(updates, options);++ if (type === "blur" || type === "enter") {onClosePopover();}}}onAbort={() => {- onChange({ operation: "delete", property }, { isEphemeral: true });+ onChange([{ operation: "delete", property }], { isEphemeral: true });}}/>);@@ -108,14 +142,13 @@ export const InputPopover = ({styleSource,property,value,- onChange,isOpen,onClose,-}: {- styleSource: StyleSource;- property: StyleProperty;- value: StyleValue;- onChange: ComponentProps<typeof Input>["onChange"];+ createBatchUpdate,+}: Pick<+ ComponentProps<typeof Input>,+ "styleSource" | "property" | "value" | "createBatchUpdate"+> & {isOpen: boolean;onClose: () => void;}) => {@@ -136,7 +169,7 @@ export const InputPopover = ({styleSource={styleSource}value={value}property={property}- onChange={onChange}+ createBatchUpdate={createBatchUpdate}onClosePopover={onClose}/></PopoverContentStyled>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.tsxindex 35c42ad4dd..0065823256 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,4 +1,4 @@-import { type ComponentProps, useState, useRef } from "react";+import { useState, useRef } from "react";import type { SectionProps } from "../shared/section";import { SpaceLayout } from "./layout";import { ValueText } from "../shared/value-text";@@ -10,13 +10,11 @@ import { SpaceTooltip } from "./tooltip";import { getStyleSource } from "../../shared/style-info";import { CollapsibleSection } from "../../shared/collapsible-section";import { movementMapSpace, useKeyboardNavigation } from "../shared/keyboard";-import type { CreateBatchUpdate } from "../../shared/use-style-data";const Cell = ({isPopoverOpen,onPopoverClose,- onChange,onHover,property,scrubStatus,@@ -25,7 +23,6 @@ const Cell = ({}: {isPopoverOpen: boolean;onPopoverClose: () => void;- onChange: ComponentProps<typeof InputPopover>["onChange"];onHover: (target: HoverTarget | undefined) => void;property: SpaceStyleProperty;scrubStatus: ReturnType<typeof useScrub>;@@ -39,7 +36,7 @@ const Cell = ({// for TypeScriptif (finalValue === undefined) {- return null;+ return;}return (@@ -49,8 +46,8 @@ const Cell = ({value={finalValue}isOpen={isPopoverOpen}property={property}- onChange={onChange}onClose={onPopoverClose}+ createBatchUpdate={createBatchUpdate}/><SpaceTooltipproperty={property}@@ -83,7 +80,6 @@ const Cell = ({export { spaceProperties as properties };export const Section = ({- setProperty,deleteProperty,createBatchUpdate,currentStyle,@@ -168,13 +164,6 @@ export const Section = ({layoutRef.current?.focus();}}}- onChange={(update, options) => {- if (update.operation === "set") {- setProperty(update.property)(update.value, options);- } else {- deleteProperty(update.property, options);- }- }}onHover={handleHover}property={property}scrubStatus={scrubStatus}diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsxindex b95fa665ec..9cfa21022f 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx@@ -228,7 +228,11 @@ const useHandleKeyDown =ignoreEnter: boolean;ignoreUpDownNumeric: boolean;value: CssValueInputValue;- onChange: (value: CssValueInputValue) => void;+ onChange: (event: {+ value: CssValueInputValue;+ altKey: boolean;+ shiftKey: boolean;+ }) => void;onKeyDown: KeyboardEventHandler<HTMLInputElement>;}) =>(event: KeyboardEvent<HTMLInputElement>) => {@@ -236,11 +240,12 @@ const useHandleKeyDown =// Underlying select like `unitSelect` can already prevent an event like up/down buttonsreturn;}+ const meta = { altKey: event.altKey, shiftKey: event.shiftKey };// Do not prevent downshift behaviour on item selectif (ignoreEnter === false) {if (event.key === "Enter") {- onChange(value);+ onChange({ value, ...meta });}}@@ -255,9 +260,12 @@ const useHandleKeyDown =value.type === "unit" ? value.value : Number(value.value.trim());onChange({- type: "unit",- value: handleNumericInputArrowKeys(inputValue, event),- unit: value.unit,+ value: {+ type: "unit",+ value: handleNumericInputArrowKeys(inputValue, event),+ unit: value.unit,+ },+ ...meta,});// Prevent Downshift from opening menu on arrow up/downreturn;@@ -274,12 +282,15 @@ export type IntermediateStyleValue = {export type CssValueInputValue = StyleValue | IntermediateStyleValue;-export type ChangeReason =- | "enter"- | "blur"- | "unit-select"- | "keyword-select"- | "scrub-end";+type Modifiers = {+ altKey: boolean;+ shiftKey: boolean;+};++type ChangeCompleteEvent = {+ type: "enter" | "blur" | "scrub-end" | "unit-select" | "keyword-select";+ value: StyleValue;+} & Modifiers;type CssValueInputProps = Pick<ComponentProps<typeof InputField>,@@ -301,10 +312,7 @@ type CssValueInputProps = Pick<*/keywords?: Array<KeywordValue>;onChange: (value: CssValueInputValue | undefined) => void;- onChangeComplete: (event: {- value: StyleValue;- reason: ChangeReason;- }) => void;+ onChangeComplete: (event: ChangeCompleteEvent) => void;onHighlight: (value: StyleValue | undefined) => void;onAbort: () => void;icon?: ReactNode;@@ -398,11 +406,16 @@ export const CssValueInput = ({};const onChangeComplete = (- value: CssValueInputValue,- reason: ChangeReason+ event: {+ type: ChangeCompleteEvent["type"];+ value: CssValueInputValue;+ } & Partial<Modifiers>) => {+ const { value } = event;+ const defaultProps = { altKey: false, shiftKey: false };+if (value.type !== "intermediate" && value.type !== "invalid") {- props.onChangeComplete({ value, reason });+ props.onChangeComplete({ ...defaultProps, ...event, value });return;}@@ -414,8 +427,9 @@ export const CssValueInput = ({}props.onChangeComplete({+ ...defaultProps,+ ...event,value: parsedValue,- reason,});};@@ -439,7 +453,7 @@ export const CssValueInput = ({onChange(inputValue);},onItemSelect: (value) => {- onChangeComplete(value, "keyword-select");+ onChangeComplete({ value, type: "keyword-select" });},onItemHighlight: (value) => {if (value == null) {@@ -463,7 +477,7 @@ export const CssValueInput = ({value,onChange: (unitOrKeyword) => {if (unitOrKeyword.type === "keyword") {- onChangeComplete(unitOrKeyword, "unit-select");+ onChangeComplete({ value: unitOrKeyword, type: "unit-select" });return;}@@ -475,7 +489,7 @@ export const CssValueInput = ({value.type === "intermediate" &&Number.isNaN(Number.parseFloat(value.value)) === false) {- onChangeComplete({ ...value, unit }, "unit-select");+ onChangeComplete({ value: { ...value, unit }, type: "unit-select" });return;}@@ -489,14 +503,14 @@ export const CssValueInput = ({unit);- onChangeComplete(- {+ onChangeComplete({+ value: {type: "unit",value: Number.parseFloat(convertedValue.toFixed(2)),unit,},- "unit-select"- );+ type: "unit-select",+ });return;}@@ -519,25 +533,25 @@ export const CssValueInput = ({unit);- onChangeComplete(- {+ onChangeComplete({+ value: {type: "unit",value: Number.parseFloat(convertedValue.toFixed(2)),unit,},- "unit-select"- );+ type: "unit-select",+ });return;}- onChangeComplete(- {+ onChangeComplete({+ value: {type: "intermediate",value: toValue(value),unit,},- "unit-select"- );+ type: "unit-select",+ });},onCloseAutoFocus(event) {// We don't want to focus the unit trigger when closing the select (no matter if unit was selected, clicked outside or esc was pressed)@@ -555,7 +569,7 @@ export const CssValueInput = ({value,property,onChange: props.onChange,- onChangeComplete: (value) => onChangeComplete(value, "scrub-end"),+ onChangeComplete: (value) => onChangeComplete({ value, type: "scrub-end" }),shouldHandleEvent,});@@ -567,7 +581,7 @@ export const CssValueInput = ({**/const callOnCompleteIfIntermediateValueExists = useDebouncedCallback(() => {if (props.intermediateValue !== undefined) {- onChangeComplete(value, "blur");+ onChangeComplete({ value, type: "blur" });}});@@ -593,7 +607,7 @@ export const CssValueInput = ({onAbort();return;}- onChangeComplete(value, "blur");+ onChangeComplete({ value, type: "blur" });};const handleKeyDown = useHandleKeyDown({@@ -603,7 +617,7 @@ export const CssValueInput = ({isUnitsOpen || (isOpen && !menuProps.empty && highlightedIndex !== -1),// Do not change the number value on the arrow up/down if any menu is openedignoreUpDownNumeric: isUnitsOpen || isOpen,- onChange: (value) => onChangeComplete(value, "enter"),+ onChange: (event) => onChangeComplete({ ...event, type: "enter" }),value,onKeyDown: inputProps.onKeyDown,});diff --git a/packages/design-system/src/components/primitives/numeric-gesture-control.ts b/packages/design-system/src/components/primitives/numeric-gesture-control.tsindex 37ce712636..69b51987a2 100644--- a/packages/design-system/src/components/primitives/numeric-gesture-control.ts+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.ts@@ -20,11 +20,14 @@ export type NumericScrubDirection = "horizontal" | "vertical";export type NumericScrubValue = number;-export type NumericScrubCallback = (event: {+export type NumericScrubEvent = {+ type: "scrubend" | "scrubbing";target: HTMLElement | SVGElement;value: NumericScrubValue;preventDefault: () => void;-}) => void;+};++type NumericScrubCallback = (event: NumericScrubEvent) => void;export type NumericScrubOptions = {inverse?: boolean;@@ -144,6 +147,7 @@ export const numericScrubControl = (cleanup();if (shouldComponentUpdate) {onValueChange?.({+ type: "scrubend",target: targetNode,value: state.value,preventDefault: () => event.preventDefault(),@@ -198,6 +202,7 @@ export const numericScrubControl = (onStatusChange?.("scrubbing");}onValueInput?.({+ type: "scrubbing",target: targetNode,value: state.value,preventDefault: () => event.preventDefault(),