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: Animations imporovements (#5487)

Oleg Isonen04390ecea5b6
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx
index bb14e767ff..f549bacd69 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx
+++ b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-panel-content.tsx
@@ -16,12 +16,12 @@ import { keywordValues } from "@webstudio-is/css-data";
import type {
DurationUnitValue,
RangeUnitValue,
+ IterationsUnitValue,
ScrollAnimation,
ViewAnimation,
} from "@webstudio-is/sdk";
import {
durationUnitValueSchema,
- RANGE_UNITS,
rangeUnitValueSchema,
scrollAnimationSchema,
viewAnimationSchema,
@@ -110,64 +110,83 @@ const scrollTimelineRangeName = {
end: "Distance from the bottom of the scroll container where animation ends",
};
-const unitOptions = RANGE_UNITS.map((unit) => ({
- id: unit,
- label: unit,
- type: "unit" as const,
-}));
+type ValidatedCssValueInputProps<T> = Omit<
+ React.ComponentProps<typeof CssValueInput>,
+ | "onChange"
+ | "onChangeComplete"
+ | "onHighlight"
+ | "onAbort"
+ | "onReset"
+ | "intermediateValue"
+ | "styleSource"
+ | "getOptions"
+ | "placeholder"
+> & {
+ onValidate: (
+ styleValue: StyleValue,
+ rawValue: string
+ ) => { success: true; data: T | undefined } | { success: false };
+ onChange: (value: T | undefined, isEphemeral: boolean) => void;
+ onHighlight?: (value: T | undefined, isEphemeral: boolean) => void;
+ styleSource?: React.ComponentProps<typeof CssValueInput>["styleSource"];
+ getOptions?: React.ComponentProps<typeof CssValueInput>["getOptions"];
+ placeholder?: React.ComponentProps<typeof CssValueInput>["placeholder"];
+};
-const RangeValueInput = ({
- value,
+/**
+ * Generic wrapper component for CssValueInput with validation
+ */
+const ValidatedCssValueInput = <T,>({
+ onValidate,
onChange,
- disabled,
-}: {
- value: RangeUnitValue;
- disabled?: boolean;
- onChange: ((value: undefined, isEphemeral: true) => void) &
- ((value: RangeUnitValue, isEphemeral: boolean) => void);
-}) => {
+ onHighlight,
+ styleSource = "default",
+ getOptions = () => $availableUnitVariables.get(),
+ placeholder = "auto",
+ ...cssValueInputProps
+}: ValidatedCssValueInputProps<T>) => {
const [intermediateValue, setIntermediateValue] = useState<
StyleValue | IntermediateStyleValue
>();
-
return (
<CssValueInput
- disabled={disabled}
- styleSource="default"
- value={value}
- /* marginLeft to allow negative values */
- property={"margin-left"}
- unitOptions={unitOptions}
+ {...cssValueInputProps}
+ styleSource={styleSource}
+ getOptions={getOptions}
+ placeholder={placeholder}
intermediateValue={intermediateValue}
onChange={(styleValue) => {
setIntermediateValue(styleValue);
-
- if (styleValue?.type !== "intermediate") {
- const parsedValue = rangeUnitValueSchema.safeParse(styleValue);
- if (parsedValue.success) {
- onChange(parsedValue.data, true);
- return;
- }
- }
-
- onChange(undefined, true);
- }}
- getOptions={() => $availableUnitVariables.get()}
- onHighlight={() => {
- /* Nothing to Highlight */
}}
+ onHighlight={
+ onHighlight
+ ? (styleValue) => {
+ if (styleValue === undefined) {
+ onHighlight(undefined, true);
+ return;
+ }
+ const rawValue = toValue(styleValue);
+ const result = onValidate(styleValue, rawValue);
+ if (result.success) {
+ onHighlight(result.data, true);
+ }
+ }
+ : () => {}
+ }
onChangeComplete={(event) => {
- const parsedValue = rangeUnitValueSchema.safeParse(event.value);
+ onChange(undefined, true);
+ const rawValue = toValue(event.value);
+ const result = onValidate(event.value, rawValue);
- if (parsedValue.success) {
- onChange(parsedValue.data, false);
+ if (result.success) {
+ onChange(result.data, false);
setIntermediateValue(undefined);
return;
}
setIntermediateValue({
type: "invalid",
- value: toValue(event.value),
+ value: rawValue,
});
}}
onAbort={() => {
@@ -175,64 +194,63 @@ const RangeValueInput = ({
}}
onReset={() => {
setIntermediateValue(undefined);
- onChange(undefined, true);
+ onChange(undefined, false);
}}
/>
);
};
+const RangeValueInput = ({
+ value,
+ onChange,
+ disabled,
+}: {
+ value: RangeUnitValue;
+ disabled?: boolean;
+ onChange: (value: RangeUnitValue | undefined, isEphemeral: boolean) => void;
+}) => (
+ <ValidatedCssValueInput
+ value={value}
+ property="margin-left" /* allows negative values */
+ disabled={disabled}
+ onValidate={(styleValue) => {
+ const parsedValue = rangeUnitValueSchema.safeParse(styleValue);
+ return parsedValue.success
+ ? { success: true, data: parsedValue.data }
+ : { success: false };
+ }}
+ onChange={onChange}
+ />
+);
+
const EasingInput = ({
value,
onChange,
}: {
value: string | undefined;
onChange: (value: string | undefined, isEphemeral: boolean) => void;
-}) => {
- const [intermediateValue, setIntermediateValue] = useState<
- StyleValue | IntermediateStyleValue
- >();
-
- return (
- <CssValueInput
- styleSource="default"
- value={
- value === undefined
- ? { type: "keyword", value: "ease" }
- : { type: "unparsed", value }
- }
- getOptions={() => [
- ...keywordValues["animation-timing-function"]
- .filter((value) => !cssWideKeywords.has(value))
- .map((value) => ({
- type: "keyword" as const,
- value,
- })),
- ...$availableUnitVariables.get(),
- ]}
- property="animation-timing-function"
- intermediateValue={intermediateValue}
- onChange={(styleValue) => {
- setIntermediateValue(styleValue);
- /* @todo: allow to change some ephemeral property to see the result in action */
- }}
- onHighlight={(value) => {
- onChange(toValue(value), true);
- }}
- onChangeComplete={(event) => {
- onChange(toValue(event.value), false);
- setIntermediateValue(undefined);
- }}
- onAbort={() => {
- onChange(undefined, true);
- }}
- onReset={() => {
- setIntermediateValue(undefined);
- onChange(undefined, true);
- }}
- />
- );
-};
-
+}) => (
+ <ValidatedCssValueInput
+ value={
+ value === undefined
+ ? { type: "keyword", value: "ease" }
+ : { type: "unparsed", value }
+ }
+ property="animation-timing-function"
+ getOptions={() => [
+ ...keywordValues["animation-timing-function"]
+ .filter((value) => !cssWideKeywords.has(value))
+ .map((value) => ({
+ type: "keyword" as const,
+ value,
+ })),
+ ...$availableUnitVariables.get(),
+ ]}
+ onValidate={(_, rawValue) => ({ success: true, data: rawValue })}
+ onChange={onChange}
+ onHighlight={onChange}
+ />
+);
const DurationInput = ({
value,
onChange,
@@ -242,51 +260,68 @@ const DurationInput = ({
value: DurationUnitValue | undefined,
isEphemeral: boolean
) => void;
+}) => (
+ <ValidatedCssValueInput
+ value={value}
+ property="animation-duration"
+ onValidate={(styleValue, rawValue) => {
+ if (rawValue.toLowerCase() === "auto") {
+ return { success: true, data: undefined };
+ }
+ const parsedValue = durationUnitValueSchema.safeParse(styleValue);
+ return parsedValue.success
+ ? { success: true, data: parsedValue.data }
+ : { success: false };
+ }}
+ onChange={onChange}
+ />
+);
+
+const IterationsInput = ({
+ value,
+ onChange,
+ disabled,
+}: {
+ value: IterationsUnitValue | undefined;
+ onChange: (
+ value: IterationsUnitValue | undefined,
+ isEphemeral: boolean
+ ) => void;
+ disabled?: boolean;
}) => {
- const [intermediateValue, setIntermediateValue] = useState<
- StyleValue | IntermediateStyleValue
- >();
+ const styleValue: StyleValue | undefined =
+ value === undefined
+ ? { type: "unit", value: 1, unit: "number" }
+ : value === "infinite"
+ ? { type: "keyword", value: "infinite" }
+ : { type: "unit", value, unit: "number" };
return (
- <CssValueInput
- styleSource="default"
- value={value}
- placeholder="auto"
- property="animation-duration"
- intermediateValue={intermediateValue}
- onChange={(styleValue) => {
- setIntermediateValue(styleValue);
- }}
- getOptions={() => $availableUnitVariables.get()}
- onHighlight={() => {}}
- onChangeComplete={(event) => {
- const value = durationUnitValueSchema.safeParse(event.value);
- onChange(undefined, true);
- // allow user to reset with initial value
- if (toValue(event.value).toLowerCase() === "auto") {
- onChange(undefined, false);
- setIntermediateValue(undefined);
- return;
+ <ValidatedCssValueInput
+ value={styleValue}
+ placeholder="1"
+ property="animation-iteration-count"
+ disabled={disabled}
+ getOptions={() => [
+ { type: "keyword" as const, value: "infinite" },
+ ...$availableUnitVariables.get(),
+ ]}
+ onValidate={(styleValue, rawValue) => {
+ if (rawValue === "auto") {
+ return { success: true, data: undefined };
}
- if (value.success) {
- onChange(value.data, false);
- setIntermediateValue(undefined);
- return;
+ if (rawValue === "infinite") {
+ return { success: true, data: "infinite" as const };
}
-
- setIntermediateValue({
- type: "invalid",
- value: toValue(event.value),
- });
- }}
- onAbort={() => {
- onChange(undefined, true);
- }}
- onReset={() => {
- setIntermediateValue(undefined);
- onChange(undefined, false);
- onChange(undefined, true);
+ if (styleValue.type === "unit" && styleValue.unit === "number") {
+ const numValue = styleValue.value;
+ if (typeof numValue === "number" && numValue > 0) {
+ return { success: true, data: numValue };
+ }
+ }
+ return { success: false };
}}
+ onChange={onChange}
/>
);
};
@@ -437,6 +472,8 @@ export const AnimationPanelContent = ({
const timelineRangeNames = Object.keys(timelineRangeDescriptions);
const isRangeEndEnabled = value.timing.duration === undefined;
+ const isRangeStartEnabled = value.timing.delay === undefined;
+ const isIterationsEnabled = value.timing.duration !== undefined;
const animationSchema = isScrollAnimation
? scrollAnimationSchema
@@ -577,12 +614,7 @@ export const AnimationPanelContent = ({
/>
</Grid>
- <Grid
- gap={1}
- css={{
- paddingInline: theme.panel.paddingInline,
- }}
- >
+ <Grid gap="2" css={{ paddingInline: theme.panel.paddingInline }}>
<Grid
css={{
gridTemplateColumns: "1.1fr 2fr",
@@ -769,6 +801,7 @@ export const AnimationPanelContent = ({
>
<ToggleGroupButton
key={toggleValue}
+ disabled={!isRangeStartEnabled}
value={toggleValue}
onClick={() => {
setIsAdvancedRangeStart(false);
@@ -796,6 +829,7 @@ export const AnimationPanelContent = ({
<Tooltip content="Set custom range">
<ToggleGroupButton
+ disabled={!isRangeStartEnabled}
onClick={() => {
setIsAdvancedRangeStart(true);
}}
@@ -816,6 +850,7 @@ export const AnimationPanelContent = ({
gap={2}
>
<Select
+ disabled={!isRangeStartEnabled}
options={timelineRangeNames}
getLabel={humanizeString}
value={value.timing.rangeStart?.[0] ?? timelineRangeNames[0]!}
@@ -871,6 +906,7 @@ export const AnimationPanelContent = ({
}}
/>
<RangeValueInput
+ disabled={!isRangeStartEnabled}
value={
value.timing.rangeStart?.[1] ?? {
type: "unit",
@@ -904,31 +940,86 @@ export const AnimationPanelContent = ({
/>
</Grid>
)}
+ </Grid>
+ <Grid gap="2" columns="3">
+ <Box>
+ <FieldLabel description="Sets a fixed duration instead of using range end.">
+ Duration
+ </FieldLabel>
+ <DurationInput
+ value={value.timing.duration}
+ onChange={(duration, isEphemeral) => {
+ if (duration === undefined && isEphemeral) {
+ handleChange(undefined, true);
+ return;
+ }
- <FieldLabel description="Sets a fixed duration instead of using range end.">
- Duration
- </FieldLabel>
+ handleChange(
+ {
+ ...value,
+ timing: {
+ ...value.timing,
+ duration,
+ },
+ },
+ isEphemeral
+ );
+ }}
+ />
+ </Box>
+
+ <Box>
+ <FieldLabel description="Sets a fixed delay before the animation starts instead of using range start.">
+ Delay
+ </FieldLabel>
+ <DurationInput
+ value={value.timing.delay}
+ onChange={(delay, isEphemeral) => {
+ if (delay === undefined && isEphemeral) {
+ handleChange(undefined, true);
+ return;
+ }
- <DurationInput
- value={value.timing.duration}
- onChange={(duration, isEphemeral) => {
- if (duration === undefined && isEphemeral) {
- handleChange(undefined, true);
- return;
- }
+ handleChange(
+ {
+ ...value,
+ timing: {
+ ...value.timing,
+ delay,
+ },
+ },
+ isEphemeral
+ );
+ }}
+ />
+ </Box>
+
+ <Box>
+ <FieldLabel description="Number of times the animation should repeat. Use 'infinite' for continuous loop. Requires duration to be set.">
+ Iterations
+ </FieldLabel>
+ <IterationsInput
+ disabled={!isIterationsEnabled}
+ value={value.timing.iterations}
+ onChange={(iterations, isEphemeral) => {
+ if (iterations === undefined && isEphemeral) {
+ handleChange(undefined, true);
+ return;
+ }
- handleChange(
- {
- ...value,
- timing: {
- ...value.timing,
- duration,
+ handleChange(
+ {
+ ...value,
+ timing: {
+ ...value.timing,
+ iterations,
+ },
},
- },
- isEphemeral
- );
- }}
- />
+ isEphemeral
+ );
+ }}
+ />
+ </Box>
</Grid>
</Grid>
diff --git a/packages/sdk-components-animation/private-src b/packages/sdk-components-animation/private-src
index 527f1e6074..8e802355e2 160000
--- a/packages/sdk-components-animation/private-src
+++ b/packages/sdk-components-animation/private-src
@@ -1 +1 @@
-Subproject commit 527f1e6074734eb44025d0d7a946d1eba28dda14
+Subproject commit 8e802355e264690386042c871d11f1f81d0df466
diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts
index 6ea6e89aab..0ffa639bbd 100644
--- a/packages/sdk/src/index.ts
+++ b/packages/sdk/src/index.ts
@@ -40,6 +40,7 @@ export type {
ViewAnimation,
InsetUnitValue,
DurationUnitValue,
+ IterationsUnitValue,
TimeUnit,
} from "./schema/animation-schema";
diff --git a/packages/sdk/src/schema/animation-schema.ts b/packages/sdk/src/schema/animation-schema.ts
index 82d1c00368..ccc537f16e 100644
--- a/packages/sdk/src/schema/animation-schema.ts
+++ b/packages/sdk/src/schema/animation-schema.ts
@@ -90,6 +90,8 @@ export const durationUnitValueSchema = z.union([
}),
]);
+const iterationsUnitValueSchema = z.union([z.number(), z.literal("infinite")]);
+
// view-timeline-inset
export const insetUnitValueSchema = z.union([
rangeUnitValueSchema,
@@ -121,6 +123,8 @@ export const keyframeEffectOptionsSchema = z.object({
])
.optional(), // FillMode
duration: durationUnitValueSchema.optional(),
+ delay: durationUnitValueSchema.optional(),
+ iterations: iterationsUnitValueSchema.optional(),
});
// Scroll Named Range
@@ -235,6 +239,7 @@ export const isRangeUnit = (
export type RangeUnit = z.infer<typeof rangeUnitSchema>;
export type RangeUnitValue = z.infer<typeof rangeUnitValueSchema>;
export type DurationUnitValue = z.infer<typeof durationUnitValueSchema>;
+export type IterationsUnitValue = z.infer<typeof iterationsUnitValueSchema>;
export type TimeUnit = z.infer<typeof timeUnitSchema>;
export type KeyframeStyles = z.infer<typeof keyframeStylesSchema>;
export type AnimationKeyframe = z.infer<typeof animationKeyframeSchema>;