Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "@webstudio-is/design-system";
import { MinusIcon, PlusIcon } from "@webstudio-is/icons";
import type { AnimationKeyframe } from "@webstudio-is/sdk";
import { Fragment, useMemo, useState } from "react";
import { Fragment, useMemo, useRef, useState } from "react";
import {
CssValueInput,
type IntermediateStyleValue,
Expand Down Expand Up @@ -176,6 +176,18 @@ export const Keyframes = ({
}) => {
const ids = useIds(["addKeyframe"]);

// To preserve focus on children swap
const keyRefs = useRef(
Array.from({ length: keyframes.length }, (_, index) => index)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting one, I never thought one can make array like this vs Array.from(new Array(keyframes.length))

);

if (keyframes.length !== keyRefs.current.length) {
keyRefs.current = Array.from(
{ length: keyframes.length },
(_, index) => index
);
}

return (
<Grid gap={2}>
<Grid gap={1} align={"center"} css={{ gridTemplateColumns: "1fr auto" }}>
Expand All @@ -184,19 +196,20 @@ export const Keyframes = ({
</Label>
<IconButton
id={ids.addKeyframe}
onClick={() =>
onChange([...keyframes, { offset: undefined, styles: {} }])
}
onClick={() => {
onChange([...keyframes, { offset: undefined, styles: {} }]);
keyRefs.current = [...keyRefs.current, keyframes.length];
}}
>
<PlusIcon />
</IconButton>
</Grid>

{keyframes.map((value, index) => (
<Fragment key={index}>
<Fragment key={keyRefs.current[index]}>
<Separator />
<Keyframe
key={index}
key={keyRefs.current[index]}
value={value}
onChange={(newValue) => {
if (newValue === undefined) {
Expand All @@ -208,6 +221,48 @@ export const Keyframes = ({

const newValues = [...keyframes];
newValues[index] = newValue;

const { offset } = newValue;
if (offset === undefined) {
onChange(newValues);
return;
}

// Check ordering
const minLastIndex = newValues.findLastIndex(
(keyframe, keyframeIndex) =>
keyframeIndex !== index &&
keyframe.offset !== undefined &&
keyframe.offset < offset
);

const maxFirstIndex = newValues.findIndex(
(keyframe, keyframeIndex) =>
keyframeIndex !== index &&
keyframe.offset !== undefined &&
keyframe.offset > offset
);

if (index < minLastIndex) {
const tmp = newValues[index];
newValues[index] = newValues[minLastIndex];
newValues[minLastIndex] = tmp;
// swap keyrefs too
const tmpKeyRef = keyRefs.current[index];
keyRefs.current[index] = keyRefs.current[minLastIndex];
keyRefs.current[minLastIndex] = tmpKeyRef;
}

if (index > maxFirstIndex && maxFirstIndex !== -1) {
const tmp = newValues[index];
newValues[index] = newValues[maxFirstIndex];
newValues[maxFirstIndex] = tmp;
// swap keyrefs too
const tmpKeyRef = keyRefs.current[index];
keyRefs.current[index] = keyRefs.current[maxFirstIndex];
keyRefs.current[maxFirstIndex] = tmpKeyRef;
}

onChange(newValues);
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ const fillModeDescriptions: Record<
NonNullable<ViewAnimation["timing"]["fill"]>,
string
> = {
none: "No animation is applied before or after the active period",
forwards:
"The animation state is applied after the active period. Prefered for Out Animations",
both: "The animation state is applied before and after the active period. Set if unsure whether it's In or Out.",
backwards:
"The animation state is applied before the active period. Prefered for In Animations",
both: "The animation state is applied before and after the active period",
forwards:
"The animation state is applied after the active period. Prefered for Out Animations",
none: "No animation is applied before or after the active period.",
};

const fillModeNames = Object.keys(fillModeDescriptions) as NonNullable<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const animationTypes: AnimationAction["type"][] = Object.keys(
) as AnimationAction["type"][];

const defaultActionValue: AnimationAction = {
type: "scroll",
type: "view",
animations: [],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const newScrollAnimation: ScrollAnimation = {
timing: {
rangeStart: ["start", { type: "unit", value: 0, unit: "px" }],
rangeEnd: ["end", { type: "unit", value: 0, unit: "px" }],
fill: "backwards",
fill: "both",
easing: "linear",
},
keyframes: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const newViewAnimation: ViewAnimation = {
description: "Create a new animation.",

timing: {
rangeStart: ["entry", { type: "unit", value: 0, unit: "%" }],
rangeEnd: ["entry", { type: "unit", value: 100, unit: "%" }],
fill: "backwards",
rangeStart: ["cover", { type: "unit", value: 0, unit: "%" }],
rangeEnd: ["cover", { type: "unit", value: 100, unit: "%" }],
fill: "both",
easing: "linear",
},
keyframes: [
Expand Down
1 change: 1 addition & 0 deletions apps/builder/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"include": ["app", "**/*.ts", "**/*.tsx", "../../@types/**/*.d.ts"],

"compilerOptions": {
"target": "ES2023",
"types": [
"@remix-run/node",
"vite/client",
Expand Down
Loading