Skip to content

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.

style panel: set values on enter, calculate math expressions in inputs

Oleg Isonen49187a07c483
diff --git a/app/designer/features/style-panel/lib/autocomplete.tsx b/app/designer/features/style-panel/lib/autocomplete.tsx
index 5ec3d0c515..d894057c7d 100644
--- a/app/designer/features/style-panel/lib/autocomplete.tsx
+++ b/app/designer/features/style-panel/lib/autocomplete.tsx
@@ -38,6 +38,7 @@ export const Autocomplete = ({
...rest
}: AutocompleteProps) => {
const [debouncedValue, setDebouncedValue] = useState<string>();
+ const [currentValue, setCurrentValue] = useState<string>(value);
const [isOpen, setIsOpen] = useState<boolean>();
const inputRef = useRef<HTMLInputElement>(null);
const contentRef = useRef<HTMLDivElement>();
@@ -50,8 +51,18 @@ export const Autocomplete = ({
// so that when we press ESC we can restore it.
previousValueRef.current = value;
}
+
+ setCurrentValue(value);
}, [value, isOpen]);
+ // When Enter was pressed, we set current value to "pending"
+ // and then update it from props.value
+ useEffect(() => {
+ if (currentValue === "pending") {
+ setCurrentValue(value);
+ }
+ }, [value, currentValue]);
+
const [, cancelDebounce] = useDebounce(
() => {
if (debouncedValue !== undefined) {
@@ -131,7 +142,7 @@ export const Autocomplete = ({
<MenuAnchor asChild>
<TextField
{...rest}
- value={value}
+ value={currentValue}
ref={inputRef}
autoComplete="off"
onClick={() => {
@@ -142,9 +153,7 @@ export const Autocomplete = ({
if (isOpen === undefined) open();
}}
onChange={(event) => {
- const value = event.target.value.trim();
- select(value);
- open();
+ setCurrentValue(event.target.value);
}}
onKeyDown={(event) => {
switch (event.key) {
@@ -154,7 +163,8 @@ export const Autocomplete = ({
break;
case "Enter": {
event.preventDefault();
- select(items[currentIndex]?.label);
+ setCurrentValue("pending");
+ select(currentValue);
isOpen ? close() : open();
break;
}
diff --git a/app/designer/features/style-panel/lib/spacing-widget.tsx b/app/designer/features/style-panel/lib/spacing-widget.tsx
index 01e9c320b8..5f7d23c2c3 100644
--- a/app/designer/features/style-panel/lib/spacing-widget.tsx
+++ b/app/designer/features/style-panel/lib/spacing-widget.tsx
@@ -1,6 +1,7 @@
import { Box } from "~/shared/design-system";
import { toCss, type StyleProperty, type StyleValue } from "@webstudio-is/sdk";
import { SetProperty } from "../use-style-data";
+import { useEffect, useState } from "react";
type SpacingSingularStyle = { [property in SpacingProperty]?: StyleValue };
@@ -95,6 +96,38 @@ const styles = {
},
};
+type TextFieldProps = {
+ property: string;
+ value: string | undefined;
+ onEnter: (value: string) => void;
+};
+
+const TextField = ({ property, value, onEnter }: TextFieldProps) => {
+ const [currentValue, setCurrentValue] = useState<string>(value ?? "");
+
+ useEffect(() => {
+ setCurrentValue(value ?? "");
+ }, [value]);
+
+ return (
+ <Box
+ as="input"
+ name={property}
+ aria-label={`${property} edit`}
+ value={currentValue}
+ onKeyDown={(event: React.KeyboardEvent<HTMLInputElement>) => {
+ if (event.key === "Enter") {
+ onEnter(currentValue);
+ }
+ }}
+ onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
+ setCurrentValue(event.target.value);
+ }}
+ css={styles.input}
+ />
+ );
+};
+
type SpacingWidgetProps = {
setProperty: SetProperty;
values: SpacingStyles;
@@ -125,18 +158,15 @@ export const SpacingWidget = ({ setProperty, values }: SpacingWidgetProps) => {
gridArea: grid.margin[property],
}}
>
- <Box
- as="input"
- name={property}
- aria-label={`${property} edit`}
+ <TextField
+ property={property}
value={margins[property]?.toString()}
- onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
+ onEnter={(value: string) => {
updateSpacing({
- value: event.target.value,
+ value,
property,
- })
- }
- css={styles.input}
+ });
+ }}
/>
</Box>
))}
@@ -150,18 +180,15 @@ export const SpacingWidget = ({ setProperty, values }: SpacingWidgetProps) => {
gridArea: grid.padding[property],
}}
>
- <Box
- as="input"
- name={property}
- aria-label={`${property} edit`}
+ <TextField
+ property={property}
value={paddings[property]?.toString()}
- onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
+ onEnter={(value: string) => {
updateSpacing({
- value: event.target.value,
+ value,
property,
- })
- }
- css={styles.input}
+ });
+ }}
/>
</Box>
))}
diff --git a/app/designer/features/style-panel/parse-value.ts b/app/designer/features/style-panel/parse-value.ts
index a0a3d43a17..912d0edc4a 100644
--- a/app/designer/features/style-panel/parse-value.ts
+++ b/app/designer/features/style-panel/parse-value.ts
@@ -22,6 +22,33 @@ const isValid = (property: string, value: string): boolean => {
return true;
};
+// wtf?
+// eslint-disable-next-line no-useless-escape
+const mathRegex = /[\+\-\*\/]/;
+
+// - 2+2px
+// - 2*2
+const evaluate = (input: string, parsedUnit: [Unit] | null) => {
+ const parsed = parseFloat(input);
+ // If its not a number, it can't be a math expression.
+ if (isNaN(parsed)) return parsed;
+
+ // It's a math expression
+ if (mathRegex.test(input)) {
+ // Get rid of the unit
+ if (parsedUnit !== null) {
+ input = input.replace(parsedUnit[0], "");
+ }
+ try {
+ return eval(`(${input})`);
+ } catch (err) {
+ return parsed;
+ }
+ }
+
+ return parsed;
+};
+
// Helper to let user input:
// - 10
// - 10p
@@ -43,7 +70,7 @@ export const parseValue = (
}
const parsedUnit = unitRegex.exec(input) as [Unit] | null;
- const number = parseFloat(input);
+ const number = evaluate(input, parsedUnit);
// If we get a unit but there is no number - we assume its an accidental
// unit match and its a keyword value.
@@ -57,11 +84,6 @@ export const parseValue = (
return invalidValue;
}
- // User is in the middle of typing a unit eg. 10p
- if (parsedUnit === null && input.length > String(number).length) {
- return invalidValue;
- }
-
const previousValue = style[property];
// If user didn't enter a unit, use the previous known unit otherwise fallback to px.
const defaultUnit: Unit =
@@ -69,7 +91,13 @@ export const parseValue = (
? previousValue.unit
: "number";
const [unit] = parsedUnit || [defaultUnit];
- if (isValid(property, input) || isValid(property, input + unit)) {
+
+ if (
+ isValid(property, input) ||
+ isValid(property, input + unit) ||
+ isValid(property, number) ||
+ isValid(property, number + unit)
+ ) {
return {
type: "unit",
unit,