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.

feat: Scrub improvements (#2904)

Oleg Isonen6887463746d8
diff --git a/apps/builder/app/builder/features/sidebar-left/panels/pages/page-settings.stories.tsx b/apps/builder/app/builder/features/sidebar-left/panels/pages/page-settings.stories.tsx
index 799039eb0c..8d1f97c8eb 100644
--- a/apps/builder/app/builder/features/sidebar-left/panels/pages/page-settings.stories.tsx
+++ b/apps/builder/app/builder/features/sidebar-left/panels/pages/page-settings.stories.tsx
@@ -9,6 +9,12 @@ import { isRoot } from "@webstudio-is/sdk";
export default {
component: PageSettings,
+ parameters: {
+ lostpixel: {
+ // this is to fix cutting off the after scroll area in the screenshot
+ waitBeforeScreenshot: 3000,
+ },
+ },
};
$isProjectSettingsOpen.set(true);
diff --git a/apps/builder/app/builder/features/style-panel/shared/color-thumb.tsx b/apps/builder/app/builder/features/style-panel/shared/color-thumb.tsx
index 6f71270061..fba5ab524b 100644
--- a/apps/builder/app/builder/features/style-panel/shared/color-thumb.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/color-thumb.tsx
@@ -1,6 +1,7 @@
import { rawTheme, Box, theme, css } from "@webstudio-is/design-system";
import { colord, type RgbaColor } from "colord";
import { forwardRef, type ElementRef, type ComponentProps } from "react";
+import { clamp } from "~/shared/math-utils";
const whiteColor = { r: 255, g: 255, b: 255, a: 1 };
const borderColorSwatch = colord(rawTheme.colors.borderColorSwatch).toRgb();
@@ -29,10 +30,6 @@ const lerpColor = (a: RgbaColor, b: RgbaColor, t: number) => {
};
};
-const clamp = (value: number, min: number, max: number) => {
- return Math.min(Math.max(value, min), max);
-};
-
const style = css({
width: theme.spacing[10],
height: theme.spacing[10],
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.tsx
index f04fbdf7be..6d76d22ccc 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
@@ -33,13 +33,31 @@ import { toValue } from "@webstudio-is/css-engine";
import { useDebouncedCallback } from "use-debounce";
import type { StyleSource } from "../style-info";
import { toPascalCase } from "../keyword-utils";
-import { isValidDeclaration } from "@webstudio-is/css-data";
+import { isValidDeclaration, properties } from "@webstudio-is/css-data";
import {
$selectedInstanceBrowserStyle,
$selectedInstanceUnitSizes,
} from "~/shared/nano-states";
import { convertUnits } from "./convert-units";
+// We need to enable scrub on properties that can have numeric value.
+const canBeNumber = (property: StyleProperty) => {
+ const { unitGroups } = properties[property as keyof typeof properties];
+ return unitGroups.length !== 0;
+};
+
+// Subjective adjust ment based on how it feels on macbook/trackpad.
+// It won't be ideal for everyone with different input devices and preferences.
+// Ideally we also need some kind of acceleration setting with 1 value.
+const scrubUnitAcceleration = new Map<Unit, number>([
+ ["rem", 1 / 16],
+ ["em", 1 / 16],
+ ["%", 1 / 10],
+ ["dvw", 1 / 10],
+ ["dvh", 1 / 10],
+ ["number", 1 / 20],
+]);
+
const useScrub = ({
value,
property,
@@ -76,14 +94,11 @@ const useScrub = ({
const inputRefCurrent = inputRef.current;
const scrubRefCurrent = scrubRef.current;
- const { current } = valueRef;
-
// Support only auto keyword to be scrubbable
if (
- (current.type !== "unit" &&
- !(current.type === "keyword" && current.value === "auto")) ||
inputRefCurrent === null ||
- scrubRefCurrent === null
+ scrubRefCurrent === null ||
+ canBeNumber(property) === false
) {
return;
}
@@ -132,6 +147,11 @@ const useScrub = ({
};
return numericScrubControl(scrubRefCurrent, {
+ getAcceleration() {
+ if (valueRef.current.type === "unit") {
+ return scrubUnitAcceleration.get(valueRef.current.unit);
+ }
+ },
// @todo: after this https://github.com/webstudio-is/webstudio/issues/564
// we can switch back on using just initial value
//
@@ -145,28 +165,18 @@ const useScrub = ({
if (valueRef.current.type === "unit") {
return valueRef.current.value;
}
-
- if (
- valueRef.current.type === "keyword" &&
- valueRef.current.value === "auto"
- ) {
- return 0;
- }
+ return 0;
},
onStart() {
- // for TS
- if (valueRef.current.type !== "unit") {
- return;
+ if (valueRef.current.type === "unit") {
+ unit = valueRef.current.unit;
}
-
- unit = valueRef.current.unit;
},
onValueInput(event) {
// Moving focus to container of the input to hide the caret
// (it makes text harder to read and may jump around as you scrub)
scrubRef.current?.setAttribute("tabindex", "-1");
scrubRef.current?.focus();
-
const value = validateValue(event.value);
onChangeRef.current(value);
@@ -183,7 +193,7 @@ const useScrub = ({
inputRef.current?.focus();
inputRef.current?.select();
},
- shouldHandleEvent: shouldHandleEvent,
+ shouldHandleEvent,
});
}, [shouldHandleEvent, property]);
diff --git a/apps/builder/app/shared/math-utils.ts b/apps/builder/app/shared/math-utils.ts
new file mode 100644
index 0000000000..0d58498e7a
--- /dev/null
+++ b/apps/builder/app/shared/math-utils.ts
@@ -0,0 +1,3 @@
+export const clamp = (value: number, min: number, max: number) => {
+ return Math.min(Math.max(value, min), max);
+};
diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.ts
index b2786b9f14..496d7098ad 100755
--- a/packages/css-data/bin/mdn-data.ts
+++ b/packages/css-data/bin/mdn-data.ts
@@ -18,7 +18,7 @@ import type {
import { popularityIndex } from "../src/popularity-index";
import * as customData from "../src/custom-data";
-const units = {
+const units: Record<customData.UnitGroup, Array<string>> = {
number: [],
// consider % as unit
percentage: ["%"],
@@ -78,7 +78,7 @@ const convertToStyleValue = (
node: CssNode,
property: string,
value: string,
- unitGroups: Set<string>
+ unitGroups: Set<customData.UnitGroup>
): undefined | UnitValue | KeywordValue | UnparsedValue => {
if (node?.type === "Identifier") {
return {
@@ -123,7 +123,7 @@ const convertToStyleValue = (
const parseInitialValue = (
property: string,
value: string,
- unitGroups: Set<string>
+ unitGroups: Set<customData.UnitGroup>
): StyleValue => {
// Our default values hardcoded because no single standard
if (property in normalizedValues) {
@@ -308,7 +308,7 @@ let property: Property;
for (property in filteredProperties) {
const config = filteredProperties[property];
// collect node types to improve parsing of css values
- const unitGroups = new Set<string>();
+ const unitGroups = new Set<customData.UnitGroup>();
walkSyntax(config.syntax, (node) => {
if (node.type === "Type") {
if (node.name === "integer" || node.name === "number") {
@@ -316,8 +316,8 @@ for (property in filteredProperties) {
return;
}
// type names match unit groups
- if (units[node.name]) {
- unitGroups.add(node.name);
+ if (node.name in units) {
+ unitGroups.add(node.name as customData.UnitGroup);
return;
}
}
diff --git a/packages/css-data/src/custom-data.ts b/packages/css-data/src/custom-data.ts
index 8c036bb3eb..8989e47959 100644
--- a/packages/css-data/src/custom-data.ts
+++ b/packages/css-data/src/custom-data.ts
@@ -1,9 +1,17 @@
import type { StyleValue } from "@webstudio-is/css-engine";
import { popularityIndex } from "./popularity-index";
+export type UnitGroup =
+ | "number"
+ | "percentage"
+ | "length"
+ | "time"
+ | "angle"
+ | "resolution";
+
// Data type used before we generate a the constants.
-type RawPropertyData = {
- unitGroups: string[];
+export type RawPropertyData = {
+ unitGroups: Array<UnitGroup>;
inherited: boolean;
initial: StyleValue;
popularity: number;
diff --git a/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx b/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx
index 51b49525f2..a02324ae0b 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.stories.tsx
@@ -9,10 +9,12 @@ const useNumericScrubControl = ({
ref,
value,
direction,
+ acceleration,
}: {
ref: RefObject<HTMLInputElement>;
value: NumericScrubValue;
direction: NumericScrubDirection;
+ acceleration: number;
}) => {
useEffect(() => {
if (ref.current === null) {
@@ -21,30 +23,38 @@ const useNumericScrubControl = ({
ref.current.value = String(value);
return numericScrubControl(ref.current, {
getInitialValue: () => value,
- direction: direction,
+ direction,
+ getAcceleration() {
+ return acceleration;
+ },
+ onValueInput: (event) => {
+ (event.target as HTMLInputElement).value = String(event.value);
+ },
onValueChange: (event) => {
event.preventDefault();
(event.target as HTMLInputElement).value = String(event.value);
(event.target as HTMLInputElement).select();
},
});
- }, [direction, value, ref]);
+ }, [direction, value, acceleration, ref]);
};
const Input = ({
value,
direction,
+ acceleration,
}: {
value: NumericScrubValue;
direction: NumericScrubDirection;
+ acceleration: number;
}) => {
const ref = useRef<HTMLInputElement | null>(null);
- useNumericScrubControl({ ref, value, direction });
+ useNumericScrubControl({ ref, value, direction, acceleration });
return <input defaultValue={value} ref={ref} />;
};
export const NumericInput = Object.assign(Input.bind({}), {
- args: { value: 0, direction: "horizontal" },
+ args: { value: 0, direction: "horizontal", acceleration: 1 },
});
export default {
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 9e19039ef8..221086095d 100644
--- a/packages/design-system/src/components/primitives/numeric-gesture-control.ts
+++ b/packages/design-system/src/components/primitives/numeric-gesture-control.ts
@@ -14,6 +14,8 @@
* });
*/
+import { clamp } from "@react-aria/utils";
+
export type NumericScrubDirection = "horizontal" | "vertical";
export type NumericScrubValue = number;
@@ -26,6 +28,7 @@ export type NumericScrubCallback = (event: {
export type NumericScrubOptions = {
inverse?: boolean;
+ getAcceleration?: () => number | undefined;
minValue?: NumericScrubValue;
maxValue?: NumericScrubValue;
getInitialValue: () => number | undefined;
@@ -45,7 +48,7 @@ export type NumericScrubOptions = {
type NumericScrubState = {
value: number;
cursor?: SVGElement;
- direction: string;
+ direction: NumericScrubDirection;
timerId?: ReturnType<typeof window.setTimeout>;
status: "idle" | "scrubbing";
};
@@ -56,17 +59,13 @@ const getValueDefault = (
{
minValue = Number.MIN_SAFE_INTEGER,
maxValue = Number.MAX_SAFE_INTEGER,
+ getAcceleration = () => 1,
}: NumericScrubOptions
) => {
// toFixed is needed to fix `1.3 - 1 = 0.30000000000000004`
- const value = Number((state.value + movement).toFixed(2));
- if (value < minValue) {
- return minValue;
- }
- if (state.value > maxValue) {
- return maxValue;
- }
- return value;
+ const acceleration = getAcceleration() ?? 1;
+ const value = Number((state.value + movement * acceleration).toFixed(2));
+ return clamp(value, minValue, maxValue);
};
export const numericScrubControl = (