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.

refactor prefix implementation, fix suffix bug

Sultan51965e3ad975
diff --git a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx
index 3adfb8e474..ed1c01dbda 100644
--- a/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx
+++ b/apps/designer/app/designer/features/style-panel/controls/text/text-control.tsx
@@ -3,7 +3,6 @@ import { getFinalValue } from "../../shared/get-final-value";
import { ControlProps } from "../../style-sections";
import { type StyleValue, toValue } from "@webstudio-is/react-sdk";
import { useState } from "react";
-import { PropertyIcon } from "../../shared/property-icon";
import { Box, Tooltip } from "@webstudio-is/design-system";
export const TextControl = ({
@@ -11,8 +10,8 @@ export const TextControl = ({
inheritedStyle,
setProperty,
styleConfig,
- prefix,
-}: ControlProps & { prefix?: JSX.Element }) => {
+ icon,
+}: ControlProps & { icon?: JSX.Element }) => {
const value = getFinalValue({
currentStyle,
inheritedStyle,
@@ -30,16 +29,7 @@ export const TextControl = ({
>
<Box>
<CssValueInput
- prefix={
- prefix && (
- <PropertyIcon
- property={styleConfig.property}
- label={styleConfig.label}
- >
- {prefix}
- </PropertyIcon>
- )
- }
+ icon={icon}
property={styleConfig.property}
value={currentValue ?? value}
keywords={styleConfig.items.map((item) => ({
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 c777b3dd3a..d4ffc884d9 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
@@ -107,7 +107,7 @@ const LayoutSectionFlex = ({
>
{columnGap?.styleConfig && (
<Box css={{ gridArea: "columnGap" }}>
- <TextControl prefix={<ColumnGapIcon />} {...columnGap} />
+ <TextControl icon={<ColumnGapIcon />} {...columnGap} />
</Box>
)}
{rowGap?.styleConfig && columnGap?.styleConfig && (
@@ -121,7 +121,7 @@ const LayoutSectionFlex = ({
)}
{rowGap?.styleConfig && (
<Box css={{ gridArea: "rowGap" }}>
- <TextControl prefix={<RowGapIcon />} {...rowGap} />
+ <TextControl icon={<RowGapIcon />} {...rowGap} />
</Box>
)}
</Grid>
diff --git a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
index 9427fcaa91..b71aca5be4 100644
--- a/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/designer/app/designer/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -20,10 +20,12 @@ import {
import {
type KeyboardEventHandler,
type KeyboardEvent,
+ useCallback,
useEffect,
useRef,
useState,
} from "react";
+import { useIsFromCurrentBreakpoint } from "../use-is-from-current-breakpoint";
import { useUnitSelect } from "./unit-select";
const unsetValue: UnsetValue = { type: "unset", value: "" };
@@ -80,10 +82,12 @@ const useScrub = ({
value,
onChange,
onChangeComplete,
+ shouldHandleEvent,
}: {
value: StyleValue;
onChange: (value: StyleValue) => void;
onChangeComplete: (value: StyleValue) => void;
+ shouldHandleEvent?: (node: EventTarget) => boolean;
}): [
React.MutableRefObject<HTMLDivElement | null>,
React.MutableRefObject<HTMLInputElement | null>,
@@ -135,10 +139,11 @@ const useScrub = ({
inputRef.current?.focus();
inputRef.current?.select();
},
+ shouldHandleEvent: shouldHandleEvent,
});
return scrub.disconnectedCallback;
- }, [type, unit]);
+ }, [type, unit, shouldHandleEvent]);
return [scrubRef, inputRef, isInputActive];
};
@@ -176,7 +181,6 @@ const useHandleKeyDown =
};
type CssValueInputProps = {
- prefix?: JSX.Element;
property: StyleProperty;
value?: StyleValue;
keywords?: Array<KeywordValue>;
@@ -218,14 +222,14 @@ type CssValueInputProps = {
*/
export const CssValueInput = ({
- prefix,
+ icon,
property,
value = unsetValue,
keywords = [],
onChange,
onChangeComplete,
onItemHighlight,
-}: CssValueInputProps) => {
+}: CssValueInputProps & { icon?: JSX.Element }) => {
const {
items,
getInputProps,
@@ -261,10 +265,15 @@ export const CssValueInput = ({
},
});
+ const shouldHandleEvent = useCallback((node) => {
+ if (suffixRef.current?.contains?.(node)) return false;
+ return true;
+ }, []);
const [scrubRef, inputRef, isInputActive] = useScrub({
value,
onChange,
onChangeComplete,
+ shouldHandleEvent,
});
const handleOnBlur: KeyboardEventHandler = (event) => {
@@ -282,17 +291,28 @@ export const CssValueInput = ({
closeMenu,
});
- const suffix =
- value.type === "keyword" ? (
- <TextFieldIconButton
- {...getToggleButtonProps()}
- state={isOpen ? "active" : undefined}
- >
- <ChevronDownIcon />
- </TextFieldIconButton>
- ) : value.type === "unit" ? (
- unitSelectElement
- ) : null;
+ const isCurrentBreakpoint = useIsFromCurrentBreakpoint(property);
+ const prefix = icon && (
+ <TextFieldIconButton state={isCurrentBreakpoint ? "highlight" : undefined}>
+ {icon}
+ </TextFieldIconButton>
+ );
+
+ const suffixRef = useRef<HTMLDivElement | null>(null);
+ const suffix = (
+ <Box ref={suffixRef}>
+ {value.type === "keyword" ? (
+ <TextFieldIconButton
+ {...getToggleButtonProps()}
+ state={isOpen ? "active" : undefined}
+ >
+ <ChevronDownIcon />
+ </TextFieldIconButton>
+ ) : value.type === "unit" ? (
+ unitSelectElement
+ ) : null}
+ </Box>
+ );
return (
<ComboboxPopper>
@@ -306,8 +326,9 @@ export const CssValueInput = ({
}}
onBlur={handleOnBlur}
onKeyDown={handleKeyDown}
+ baseRef={scrubRef}
inputRef={inputRef}
- scrubRef={scrubRef}
+ suffixRef={suffixRef}
name={property}
state={
value.type === "invalid"
diff --git a/apps/designer/app/designer/features/style-panel/shared/property-icon.tsx b/apps/designer/app/designer/features/style-panel/shared/property-icon.tsx
deleted file mode 100644
index 5efbafc31c..0000000000
--- a/apps/designer/app/designer/features/style-panel/shared/property-icon.tsx
+++ /dev/null
@@ -1,17 +0,0 @@
-import { TextFieldIconButton } from "@webstudio-is/design-system";
-import { useIsFromCurrentBreakpoint } from "./use-is-from-current-breakpoint";
-import type { PropertyProps } from "../style-sections";
-import { forwardRef } from "react";
-
-export const PropertyIcon = forwardRef<
- HTMLButtonElement,
- PropertyProps & { children: JSX.Element }
->(({ property, children }) => {
- const isCurrentBreakpoint = useIsFromCurrentBreakpoint(property);
- return (
- <TextFieldIconButton state={isCurrentBreakpoint ? "breakpoint" : undefined}>
- {children}
- </TextFieldIconButton>
- );
-});
-PropertyIcon.displayName = "PropertyIcon";
diff --git a/packages/design-system/src/components/primitives/numeric-gesture-control.ts b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
index 8110ebf3d9..d97e0d3977 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.ts
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
@@ -31,6 +31,7 @@ export type NumericScrubOptions = {
direction?: NumericScrubDirection;
onValueInput?: NumericScrubCallback;
onValueChange?: NumericScrubCallback;
+ shouldHandleEvent?: (node: EventTarget) => boolean;
};
type NumericScrubState = {
@@ -51,6 +52,7 @@ export const numericScrubControl = (
direction = "horizontal",
onValueInput,
onValueChange,
+ shouldHandleEvent,
}: NumericScrubOptions
) => {
const eventNames = ["pointerup", "pointerdown"] as const;
@@ -82,6 +84,7 @@ export const numericScrubControl = (
break;
}
case "pointerdown": {
+ if (event.target && shouldHandleEvent?.(event.target) === false) return;
// light touches don't register corresponding pointerup
if (event.pressure === 0 || event.button !== 0) break;
state.offset = offset;
diff --git a/packages/design-system/src/components/text-field.tsx b/packages/design-system/src/components/text-field.tsx
index 4107b62ffd..b27965fc01 100644
--- a/packages/design-system/src/components/text-field.tsx
+++ b/packages/design-system/src/components/text-field.tsx
@@ -55,7 +55,7 @@ export const TextFieldIconButton = styled(
color: "white",
},
},
- breakpoint: {
+ highlight: {
backgroundColor: "$blue4",
color: "$blue11",
"&:hover": {
@@ -250,7 +250,9 @@ export type TextFieldProps = Pick<
"variant" | "state" | "css"
> &
Omit<React.ComponentProps<"input">, "prefix" | "children"> & {
- scrubRef?: React.Ref<HTMLDivElement>;
+ suffixRef?: React.Ref<HTMLDivElement>;
+ prefixRef?: React.Ref<HTMLDivElement>;
+ baseRef?: React.Ref<HTMLDivElement>;
inputRef?: React.Ref<HTMLInputElement>;
prefix?: React.ReactNode;
suffix?: React.ReactNode;
@@ -262,7 +264,9 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
prefix,
css,
disabled,
- scrubRef,
+ prefixRef,
+ suffixRef,
+ baseRef,
inputRef,
state,
variant: variantProp,
@@ -307,7 +311,7 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
<TextFieldBase
{...focusWithinProps}
aria-disabled={disabled}
- ref={mergeRefs(forwardedRef, scrubRef ?? null)}
+ ref={mergeRefs(forwardedRef, baseRef ?? null)}
state={state}
variant={variant}
css={css}
@@ -324,8 +328,8 @@ export const TextField = React.forwardRef<HTMLDivElement, TextFieldProps>(
ref={mergeRefs(internalInputRef, inputRef ?? null)}
/>
- {prefix && <PrefixSlot>{prefix}</PrefixSlot>}
- {suffix && <SuffixSlot>{suffix}</SuffixSlot>}
+ {prefix && <PrefixSlot ref={prefixRef}>{prefix}</PrefixSlot>}
+ {suffix && <SuffixSlot ref={suffixRef}>{suffix}</SuffixSlot>}
</TextFieldBase>
);
}