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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: transition bugs (#3730)
Bogdan Chadkinfc81392df9f6
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsxindex e0539badb2..4c835efea9 100644--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx@@ -23,6 +23,7 @@ import {} from "@webstudio-is/design-system";import {parseCssValue,+ properties,type ExtractedTransitionProperties,} from "@webstudio-is/css-data";import type {@@ -33,14 +34,7 @@ import { type IntermediateStyleValue } from "../../shared/css-value-input";import { TransitionProperty } from "./transition-property";import { TransitionTiming } from "./transition-timing";import { CssValueInputContainer } from "../../shared/css-value-input";-import {- defaultTransitionProperty,- defaultTransitionDuration,- defaultTransitionTimingFunction,- defaultTransitionDelay,- deleteTransitionLayer,- editTransitionLayer,-} from "./transition-utils";+import { deleteTransitionLayer, editTransitionLayer } from "./transition-utils";import type { StyleInfo } from "../../shared/style-info";import { parseCssFragment } from "../../shared/parse-css-fragment";import { InfoCircleIcon } from "@webstudio-is/icons";@@ -180,7 +174,7 @@ export const TransitionContent = ({}}><TransitionProperty- property={property ?? defaultTransitionProperty}+ property={property ?? properties.transitionProperty.initial}onPropertySelection={handlePropertyUpdate}/>@@ -207,7 +201,7 @@ export const TransitionContent = ({key={"transitionDuration"}property={"transitionDuration"}styleSource="local"- value={duration ?? defaultTransitionDuration}+ value={duration ?? properties.transitionDuration.initial}keywords={[]}deleteProperty={() => {}}setValue={(value, options) => {@@ -245,7 +239,7 @@ export const TransitionContent = ({property={"transitionDelay"}key={"transitionDelay"}styleSource="local"- value={delay ?? defaultTransitionDelay}+ value={delay ?? properties.transitionDelay.initial}keywords={[]}deleteProperty={() => {}}setValue={(value, options) => {@@ -262,7 +256,7 @@ export const TransitionContent = ({/><TransitionTiming- timing={timingFunction ?? defaultTransitionTimingFunction}+ timing={timingFunction ?? properties.transitionTimingFunction.initial}onTimingSelection={handlePropertyUpdate}/></Grid>diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.ts b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.tsdeleted file mode 100644index 19a88c0cec..0000000000--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.test.ts+++ /dev/null@@ -1,196 +0,0 @@-import { afterEach, describe, test, expect } from "@jest/globals";-import type { CreateBatchUpdate } from "../../shared/use-style-data";-import {- toValue,- type StyleProperty,- type StyleValue,-} from "@webstudio-is/css-engine";-import type { StyleInfo } from "../../shared/style-info";-import {- addDefaultTransitionLayer,- convertIndividualTransitionToLayers,- deleteTransitionLayer,- editTransitionLayer,- getTransitionProperties,-} from "./transition-utils";-import type { TransitionProperties } from "./transition-utils";-import type { LayersValue } from "@webstudio-is/css-engine";--let styleInfo: StyleInfo = {};-let published = false;-const deletedProperties = new Set<string>();--afterEach(() => {- styleInfo = {};- published = false;- deletedProperties.clear();-});--const createBatchUpdate: CreateBatchUpdate = () => ({- setProperty: (propertyName: StyleProperty) => (newValue: StyleValue) => {- styleInfo[propertyName] = { value: newValue, local: newValue };- },- deleteProperty: (propertyName: string) => {- deletedProperties.add(propertyName);- },- publish: (_options?: unknown) => {- published = true;- },-});--describe("transition-utils", () => {- test("convert individual transition properties into short-hand transition layers", () => {- const properties: Record<TransitionProperties, LayersValue> = {- transitionProperty: {- type: "layers",- value: [- { type: "keyword", value: "opacity" },- { type: "keyword", value: "width" },- { type: "keyword", value: "all" },- { type: "keyword", value: "height" },- ],- },- transitionDuration: {- type: "layers",- value: [- { type: "unit", value: 50, unit: "ms" },- { type: "unit", value: 0, unit: "ms" },- ],- },- transitionTimingFunction: {- type: "layers",- value: [- { type: "keyword", value: "ease" },- { type: "keyword", value: "ease-in-out" },- { type: "keyword", value: "cubic-bezier(0.25,1,0.5,1)" },- ],- },- transitionDelay: {- type: "layers",- value: [- { type: "unit", value: 100, unit: "ms" },- { type: "unit", value: 0, unit: "ms" },- ],- },- transitionBehavior: {- type: "layers",- value: [- { type: "keyword", value: "normal" },- { type: "keyword", value: "allow-discrete" },- ],- },- };-- const layers = convertIndividualTransitionToLayers(properties);- expect(toValue(layers)).toMatchInlineSnapshot(- `"opacity 50ms ease 100ms, width 0ms ease-in-out 0ms, all 50ms cubic-bezier(0.25,1,0.5,1) 100ms, height 0ms ease 0ms"`- );- });-- test("add a layer to transition long hand proeprties", () => {- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- const properties = getTransitionProperties(styleInfo);- const layers = convertIndividualTransitionToLayers(properties);-- expect(published).toBe(true);- expect(toValue(layers)).toMatchInlineSnapshot(`"opacity 0ms ease 0ms"`);- });-- test("update individual transition properties", () => {- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- editTransitionLayer({- currentStyle: styleInfo,- index: 1,- createBatchUpdate,- layers: {- type: "layers",- value: [- { type: "tuple", value: [{ type: "keyword", value: "width" }] },- ],- },- options: { isEphemeral: false },- });-- const layers = convertIndividualTransitionToLayers(- getTransitionProperties(styleInfo)- );-- expect(published).toBe(true);- expect(layers.value.length).toBe(3);- expect(toValue(layers.value[1])).toMatchInlineSnapshot(- `"width 0ms ease 0ms"`- );- expect(toValue(layers)).toMatchInlineSnapshot(- `"opacity 0ms ease 0ms, width 0ms ease 0ms, opacity 0ms ease 0ms"`- );- });-- test("delete a layer from transition properties", () => {- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle: styleInfo,- });-- editTransitionLayer({- currentStyle: styleInfo,- index: 2,- createBatchUpdate,- layers: {- type: "layers",- value: [- {- type: "tuple",- value: [- { type: "keyword", value: "width" },- { type: "keyword", value: "ease-in-out" },- ],- },- ],- },- options: { isEphemeral: false },- });-- deleteTransitionLayer({- currentStyle: styleInfo,- createBatchUpdate,- index: 1,- });-- const layers = convertIndividualTransitionToLayers(- getTransitionProperties(styleInfo)- );-- expect(published).toBe(true);- expect(toValue(layers)).toMatchInlineSnapshot(- `"opacity 0ms ease 0ms, width 0ms ease-in-out 0ms"`- );- });-});diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.tsindex e8f0779e24..97aa990139 100644--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts@@ -3,7 +3,6 @@ import {UnparsedValue,type KeywordValue,type LayersValue,- type TupleValue,type UnitValue,} from "@webstudio-is/css-engine";import type { StyleInfo } from "../../shared/style-info";@@ -13,32 +12,11 @@ import type {} from "../../shared/use-style-data";import {extractTransitionProperties,- isValidTransitionValue,+ properties,transitionLongHandProperties,} from "@webstudio-is/css-data";-export const defaultTransitionProperty: UnparsedValue = {- type: "unparsed",- value: "opacity",-};-export const defaultTransitionDuration: UnitValue = {- type: "unit",- value: 0,- unit: "ms",-};-export const defaultTransitionDelay: UnitValue = {- type: "unit",- value: 0,- unit: "ms",-};-export const defaultTransitionTimingFunction: KeywordValue = {- type: "keyword",- value: "ease",-};-export const defaultTransitionBehavior: KeywordValue = {- type: "keyword",- value: "normal",-};+export type TransitionProperty = (typeof transitionLongHandProperties)[number];export const defaultFunctions = {linear: "linear",@@ -108,13 +86,10 @@ export const findTimingFunctionFromValue = ();};-export type TransitionProperties =- (typeof transitionLongHandProperties)[number];-export const getTransitionProperties = (currentyStyle: StyleInfo-): Record<TransitionProperties, LayersValue> => {- const properties: Record<TransitionProperties, LayersValue> = {+): Record<TransitionProperty, LayersValue> => {+ const properties: Record<TransitionProperty, LayersValue> = {transitionProperty: { type: "layers", value: [] },transitionTimingFunction: { type: "layers", value: [] },transitionDelay: { type: "layers", value: [] },@@ -132,68 +107,6 @@ export const getTransitionProperties = (return properties;};-const getValueOrRepeatLast = (arr: LayersValue["value"], index: number) => {- return arr[index % arr.length];-};--export const convertIndividualTransitionToLayers = (- properties: Record<TransitionProperties, LayersValue>-) => {- const layers: { type: "layers"; value: Array<TupleValue> } = {- type: "layers",- value: [],- };-- const {- transitionProperty,- transitionDuration,- transitionDelay,- transitionTimingFunction,- transitionBehavior,- } = properties;-- // https://developer.mozilla.org/en-US/docs/Web/CSS/transition-timing-function- // You may specify multiple easing, duration and delay values;- // each one will be applied to the corresponding property as specified by the transition-property property,- // which acts as a transition-property list. If there are fewer easing, duration and transition values specified than in the transition-property list,- // the user agent must calculate which value is used by repeating the list of values until there is one for each transition property.- // If there are more values, the list is truncated to the right size. In both cases, the CSS declaration stays valid.- // And so, we take the lenght of the transiton-property as the base length of the layers-- for (let index = 0; index < transitionProperty.value.length; index++) {- const property = getValueOrRepeatLast(transitionProperty.value, index);- const duration =- getValueOrRepeatLast(transitionDuration.value, index) ??- defaultTransitionDuration;- const timingFunction =- getValueOrRepeatLast(transitionTimingFunction.value, index) ??- defaultTransitionTimingFunction;- const delay =- getValueOrRepeatLast(transitionDelay.value, index) ??- defaultTransitionDelay;- const behavior =- getValueOrRepeatLast(transitionBehavior.value, index) ??- defaultTransitionBehavior;-- if (- isValidTransitionValue(property) &&- isValidTransitionValue(duration) &&- isValidTransitionValue(timingFunction) &&- isValidTransitionValue(delay) &&- isValidTransitionValue(behavior)- ) {- const layer: TupleValue = {- type: "tuple",- value: [property, duration, timingFunction, delay],- hidden: property.hidden ?? false,- };- layers.value.push(layer);- }- }-- return layers;-};-export const deleteTransitionProperties = (props: {createBatchUpdate: CreateBatchUpdate;}) => {@@ -204,39 +117,6 @@ export const deleteTransitionProperties = (props: {batch.publish();};-export const addDefaultTransitionLayer = (props: {- createBatchUpdate: CreateBatchUpdate;- currentStyle: StyleInfo;-}) => {- const { createBatchUpdate, currentStyle } = props;- const properties = getTransitionProperties(currentStyle);- const batch = createBatchUpdate();- batch.setProperty("transitionProperty")({- type: "layers",- value: [...properties.transitionProperty.value, defaultTransitionProperty],- });- batch.setProperty("transitionTimingFunction")({- type: "layers",- value: [- ...properties.transitionTimingFunction.value,- defaultTransitionTimingFunction,- ],- });- batch.setProperty("transitionDuration")({- type: "layers",- value: [...properties.transitionDuration.value, defaultTransitionDuration],- });- batch.setProperty("transitionDelay")({- type: "layers",- value: [...properties.transitionDelay.value, defaultTransitionDelay],- });- batch.setProperty("transitionBehavior")({- type: "layers",- value: [...properties.transitionBehavior.value, defaultTransitionBehavior],- });- batch.publish();-};-export const deleteTransitionLayer = (props: {index: number;createBatchUpdate: CreateBatchUpdate;@@ -285,9 +165,9 @@ export const editTransitionLayer = (props: {const {property,- duration = defaultTransitionDuration,- timing = defaultTransitionTimingFunction,- delay = defaultTransitionDelay,+ duration = properties.transitionDuration.initial,+ timing = properties.transitionTimingFunction.initial,+ delay = properties.transitionDelay.initial,} = extractTransitionProperties(layer);// transition-property can't be undefineddiff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsxindex 6f1aa231d2..d09d5c2d21 100644--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transitions.tsx@@ -7,8 +7,10 @@ import {SectionTitleLabel,Tooltip,} from "@webstudio-is/design-system";-import { transitionLongHandProperties } from "@webstudio-is/css-data";-+import {+ properties,+ transitionLongHandProperties,+} from "@webstudio-is/css-data";import { CollapsibleSectionRoot } from "~/builder/shared/collapsible-section";import type { SectionProps } from "../shared/section";import { getDots } from "../../shared/collapsible-section";@@ -17,29 +19,33 @@ import { $selectedOrLastStyleSourceSelector } from "~/shared/nano-states";import { TransitionContent } from "./transition-content";import {deleteTransitionProperties,- addDefaultTransitionLayer,findTimingFunctionFromValue,- defaultTransitionProperty,- defaultTransitionDuration,- defaultTransitionTimingFunction,- defaultTransitionDelay,+ type TransitionProperty,} from "./transition-utils";-import {- toValue,- type StyleValue,- type StyleProperty,-} from "@webstudio-is/css-engine";+import { toValue, type LayerValueItem } from "@webstudio-is/css-engine";import { humanizeString } from "~/shared/string-utils";import { RepeatedStyle } from "../../shared/repeated-style";import { getStyleSource, type StyleInfo } from "../../shared/style-info";+import { repeatUntil } from "~/shared/array-utils";++export { transitionLongHandProperties as properties };const label = "Transitions";-export const properties = (- transitionLongHandProperties as unknown as Array<StyleProperty>-).filter((property) => property !== "transitionBehavior");-const getLayer = (value: undefined | StyleValue, index: number) =>- value?.type === "layers" ? value.value[index] : undefined;+const getTransitionLayers = (+ style: StyleInfo,+ property: TransitionProperty+) => {+ const transitionPropertiesCount =+ style.transitionProperty?.value?.type === "layers"+ ? style.transitionProperty.value.value.length+ : 0;+ const definedLayers: LayerValueItem[] =+ style[property]?.value.type === "layers"+ ? style[property].value.value+ : [properties[property].initial];+ return repeatUntil(definedLayers, transitionPropertiesCount);+};const getLayerLabel = ({style,@@ -50,25 +56,34 @@ const getLayerLabel = ({}) => {// show label without hidden replacementconst propertyLayer =- getLayer(style.transitionProperty?.value, index) ??- defaultTransitionProperty;+ getTransitionLayers(style, "transitionProperty")[index] ??+ properties.transitionProperty.initial;const property = humanizeString(toValue({ ...propertyLayer, hidden: false }));const duration = toValue(- getLayer(style.transitionDuration?.value, index) ??- defaultTransitionDuration+ getTransitionLayers(style, "transitionDuration")[index] ??+ properties.transitionDuration.initial);const timingFunctionLayer =- getLayer(style.transitionTimingFunction?.value, index) ??- defaultTransitionTimingFunction;+ getTransitionLayers(style, "transitionTimingFunction")[index] ??+ properties.transitionTimingFunction.initial;const timingFunction = toValue({ ...timingFunctionLayer, hidden: false });const humanizedTimingFunction =findTimingFunctionFromValue(timingFunction) ?? timingFunction;const delay = toValue(- getLayer(style.transitionDelay?.value, index) ?? defaultTransitionDelay+ getTransitionLayers(style, "transitionDelay")[index] ??+ properties.transitionDelay.initial);return `${property}: ${duration} ${humanizedTimingFunction} ${delay}`;};+const defaultTransitionLayers: Record<TransitionProperty, LayerValueItem> = {+ transitionProperty: { type: "unparsed", value: "opacity" },+ transitionDuration: { type: "unit", value: 200, unit: "ms" },+ transitionTimingFunction: { type: "keyword", value: "ease" },+ transitionDelay: { type: "unit", value: 0, unit: "ms" },+ transitionBehavior: { type: "keyword", value: "normal" },+};+export const Section = (props: SectionProps) => {const { currentStyle, createBatchUpdate } = props;const [isOpen, setIsOpen] = useState(true);@@ -95,7 +110,7 @@ export const Section = (props: SectionProps) => {onOpenChange={setIsOpen}trigger={<SectionTitle- dots={getDots(currentStyle, properties)}+ dots={getDots(currentStyle, transitionLongHandProperties)}suffix={<Tooltipcontent={@@ -107,12 +122,19 @@ export const Section = (props: SectionProps) => {<SectionTitleButtondisabled={isStyleInLocalState === false}prefix={<PlusIcon />}- onClick={() =>- addDefaultTransitionLayer({- createBatchUpdate,- currentStyle,- })- }+ onClick={() => {+ const batch = createBatchUpdate();+ for (const property of transitionLongHandProperties) {+ batch.setProperty(property)({+ type: "layers",+ value: [+ ...getTransitionLayers(currentStyle, property),+ defaultTransitionLayers[property],+ ],+ });+ }+ batch.publish();+ }}/></Tooltip>}@@ -121,7 +143,7 @@ export const Section = (props: SectionProps) => {title={label}style={currentStyle}description="Animate the transition between states on this instance."- properties={properties}+ properties={transitionLongHandProperties}label={<SectionTitleLabel color={sectionStyleSource}>{label}diff --git a/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx b/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsxindex 4a757c3886..1366089976 100644--- a/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx+++ b/apps/builder/app/builder/features/style-panel/shared/repeated-style.tsx@@ -23,6 +23,7 @@ import { FloatingPanel } from "~/builder/shared/floating-panel";import type { StyleInfo } from "./style-info";import type { CreateBatchUpdate } from "./use-style-data";import { ColorThumb } from "./color-thumb";+import { repeatUntil } from "~/shared/array-utils";const createLayersTransformer =({@@ -36,10 +37,20 @@ const createLayersTransformer =}) =>(transform: (value: LayersValue) => Partial<LayersValue>) => {const batch = createBatchUpdate();+ const primaryValue = style[properties[0]]?.value;+ const primaryLayersCount =+ primaryValue?.type === "layers" ? primaryValue.value.length : 0;for (const property of properties) {const value = style[property]?.value;if (value?.type === "layers") {- const newLayers = { ...value, ...transform(value) };+ const normalizedLayers: LayersValue = {+ type: "layers",+ value: repeatUntil(value.value, primaryLayersCount),+ };+ const newLayers: LayersValue = {+ ...normalizedLayers,+ ...transform(normalizedLayers),+ };// delete empty layersif (newLayers.value.length === 0) {batch.deleteProperty(property);diff --git a/apps/builder/app/shared/array-utils.test.ts b/apps/builder/app/shared/array-utils.test.tsindex 4610048c93..63862a4578 100644--- a/apps/builder/app/shared/array-utils.test.ts+++ b/apps/builder/app/shared/array-utils.test.ts@@ -1,5 +1,5 @@import { expect, test } from "@jest/globals";-import { removeByMutable } from "./array-utils";+import { removeByMutable, repeatUntil } from "./array-utils";test("removeByMutable", () => {const array = [@@ -33,3 +33,7 @@ test("removeByMutable", () => {]`);});++test("repeatUntil", () => {+ expect(repeatUntil([1, 2, 3], 5)).toEqual([1, 2, 3, 1, 2]);+});diff --git a/apps/builder/app/shared/array-utils.ts b/apps/builder/app/shared/array-utils.tsindex 2ca3ef00f7..303d19d0ee 100644--- a/apps/builder/app/shared/array-utils.ts+++ b/apps/builder/app/shared/array-utils.ts@@ -11,3 +11,11 @@ export const removeByMutable = <Item>(}}};++export const repeatUntil = <Item>(array: Item[], count: number) => {+ const repeatedArray: Item[] = [];+ for (let index = 0; index < count; index += 1) {+ repeatedArray.push(array[index % array.length]);+ }+ return repeatedArray;+};