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
feat: Add UI element to control transition property. (#2538)
Jaya Krishna Namburuf1adb0e426a4
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 31d1fe1fb2..95c6d93d09 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@@ -1,27 +1,53 @@-import type { InvalidValue, LayersValue } from "@webstudio-is/css-engine";+import { useMemo, useState } from "react";+import {+ toValue,+ type InvalidValue,+ type LayersValue,+ type TupleValue,+ type TupleValueItem,+} from "@webstudio-is/css-engine";import {Flex,Label,TextArea,theme,textVariants,+ Separator,+ Tooltip,+ Text,+ Grid,} from "@webstudio-is/design-system";-import { useState } from "react";-import { parseTransition } from "@webstudio-is/css-data";+import {+ extractTransitionProperties,+ parseTransition,+} from "@webstudio-is/css-data";+import { InformationIcon } from "@webstudio-is/icons";import type { CreateBatchUpdate } from "../../shared/use-style-data";-import type { IntermediateStyleValue } from "../../shared/css-value-input";+import { type IntermediateStyleValue } from "../../shared/css-value-input";+import { TransitionProperty } from "./transition-property";type TransitionContentProps = {index: number;- value: string;+ layer: TupleValue;+ transition: string;onEditLayer: (index: number, layer: LayersValue) => void;createBatchUpdate: CreateBatchUpdate;};-export const TransitionContent = (props: TransitionContentProps) => {+export const TransitionContent = ({+ layer,+ transition,+ onEditLayer,+ index,+}: TransitionContentProps) => {const [intermediateValue, setIntermediateValue] = useState<IntermediateStyleValue | InvalidValue | undefined- >();+ >({ type: "intermediate", value: transition });++ const { property, timing, delay, duration } = useMemo(() => {+ setIntermediateValue({ type: "intermediate", value: transition });+ return extractTransitionProperties(layer);+ }, [layer, transition]);const handleChange = (value: string) => {setIntermediateValue({@@ -34,6 +60,7 @@ export const TransitionContent = (props: TransitionContentProps) => {if (intermediateValue === undefined) {return;}+const layers = parseTransition(intermediateValue.value);if (layers.type === "invalid") {setIntermediateValue({@@ -43,34 +70,88 @@ export const TransitionContent = (props: TransitionContentProps) => {return;}- props.onEditLayer(props.index, layers);+ onEditLayer(index, layers);setIntermediateValue(undefined);};+ const handleOnPropertySelection = (newProperty: TupleValueItem) => {+ const value: TupleValueItem[] = [+ newProperty,+ duration,+ timing,+ delay,+ ].filter<TupleValueItem>((item): item is TupleValueItem => item !== null);++ const newLayer: TupleValue = { type: "tuple", value };++ setIntermediateValue({+ type: "intermediate",+ value: toValue(newLayer),+ });++ onEditLayer(index, { type: "layers", value: [newLayer] });+ };+return (- <Flex- direction="column"- css={{- px: theme.spacing[5],- py: theme.spacing[5],- gap: theme.spacing[3],- minWidth: theme.spacing[30],- }}- >- <Label>Code</Label>- <TextArea- rows={3}- name="description"- css={{ minHeight: theme.spacing[14], ...textVariants.mono }}- value={intermediateValue?.value ?? props.value ?? ""}- onChange={handleChange}- onKeyDown={(event) => {- if (event.key === "Enter") {- handleComplete();- event.preventDefault();- }+ <Flex direction="column">+ <Grid+ gap="2"+ css={{+ px: theme.spacing[8],+ py: theme.spacing[8],+ gridTemplateColumns: `1fr ${theme.spacing[22]}`,+ }}+ >+ <TransitionProperty+ /* Browser defaults for transition-property - all */+ property={property ?? { type: "keyword", value: "all" }}+ onPropertySelection={handleOnPropertySelection}+ />+ </Grid>+ <Separator css={{ gridColumn: "span 2" }} />+ <Flex+ direction="column"+ css={{+ px: theme.spacing[8],+ py: theme.spacing[8],+ gap: theme.spacing[3],+ minWidth: theme.spacing[30],}}- />+ >+ <Label>+ <Flex align="center" gap="1">+ Code+ <Tooltip+ variant="wrapped"+ content={+ <Text>+ Paste CSS code for a transition or part of a transition, for+ example:+ <br />+ <br />+ opacity 200ms ease;+ </Text>+ }+ >+ <InformationIcon />+ </Tooltip>+ </Flex>+ </Label>+ <TextArea+ rows={3}+ name="description"+ css={{ minHeight: theme.spacing[14], ...textVariants.mono }}+ state={intermediateValue?.type === "invalid" ? "invalid" : undefined}+ value={intermediateValue?.value ?? ""}+ onChange={handleChange}+ onKeyDown={(event) => {+ if (event.key === "Enter") {+ handleComplete();+ event.preventDefault();+ }+ }}+ />+ </Flex></Flex>);};diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-layer.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-layer.tsxindex c4c622881f..db5e438656 100644--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-layer.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-layer.tsx@@ -5,35 +5,14 @@ import {SmallIconButton,} from "@webstudio-is/design-system";import { SubtractIcon } from "@webstudio-is/icons";-import type { TupleValue } from "@webstudio-is/css-engine";import { FloatingPanel } from "~/builder/shared/floating-panel";import { TransitionContent } from "./transition-content";import type { LayerProps } from "../../style-layers-list";--const useLayer = (layer: TupleValue) => {- return useMemo(() => {- const name: string[] = [];-- for (const item of Object.values(layer.value)) {- if (item.type === "keyword") {- name.push(item.value);- }-- if (item.type === "unit") {- name.push(`${item.value}${item.unit}`);- }- }-- return {- name: name.join(" "),- transition: name.join(" "),- };- }, [layer]);-};+import { toValue } from "@webstudio-is/css-engine";export const Layer = (props: LayerProps) => {const { id, index, layer, isHighlighted, onDeleteLayer, disabled } = props;- const { name, transition } = useLayer(layer);+ const transition = useMemo(() => toValue(layer), [layer]);return (<FloatingPanel@@ -41,7 +20,8 @@ export const Layer = (props: LayerProps) => {content={<TransitionContentindex={index}- value={transition}+ layer={layer}+ transition={transition}onEditLayer={props.onEditLayer}createBatchUpdate={props.createBatchUpdate}/>@@ -51,7 +31,7 @@ export const Layer = (props: LayerProps) => {id={id}active={isHighlighted}index={index}- label={<Label truncate>{name}</Label>}+ label={<Label truncate>{transition}</Label>}hidden={disabled ?? layer?.hidden}buttons={<>diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsxnew file mode 100644index 0000000000..89c9bd8cb3--- /dev/null+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-property.tsx@@ -0,0 +1,148 @@+import { useState, useEffect } from "react";+import {+ animatableProperties,+ isAnimatableProperty,+} from "@webstudio-is/css-data";+import {+ Label,+ InputField,+ Combobox,+ ComboboxAnchor,+ useCombobox,+ ComboboxContent,+ ComboboxLabel,+ ComboboxListbox,+ ComboboxListboxItem,+ ComboboxSeparator,+ theme,+ css,+ NestedInputButton,+} from "@webstudio-is/design-system";+import type { KeywordValue, TupleValueItem } from "@webstudio-is/css-engine";++type AnimatableProperties = (typeof animatableProperties)[number];+type NameAndLabel = { name: AnimatableProperties; label?: string };+type TransitionPropertyProps = {+ property: KeywordValue;+ onPropertySelection: (property: TupleValueItem) => void;+};++const commonPropertiesSet = new Set<AnimatableProperties>([+ "all",+ "opacity",+ "margin",+ "padding",+ "border",+ "transform",+ "filter",+ "flex",+ "background-color",+]);++const filteredPropertiesSet = new Set<AnimatableProperties>(+ animatableProperties.filter((item) => commonPropertiesSet.has(item) === false)+);++const comboBoxStyles = css({ zIndex: theme.zIndices[1] });++export const TransitionProperty = ({+ property,+ onPropertySelection,+}: TransitionPropertyProps) => {+ const [inputValue, setInputValue] = useState(property?.value ?? "all");+ useEffect(() => setInputValue(property.value), [property.value]);++ const {+ items,+ isOpen,+ getComboboxProps,+ getToggleButtonProps,+ getInputProps,+ getMenuProps,+ getItemProps,+ } = useCombobox<NameAndLabel>({+ items: [+ ...Array.from(commonPropertiesSet),+ ...Array.from(filteredPropertiesSet),+ ].map((prop) => ({+ name: prop,+ label: prop,+ })),+ value: { name: inputValue as AnimatableProperties, label: inputValue },+ selectedItem: undefined,+ itemToString: (value) => value?.name ?? "",+ onItemSelect: (prop) => {+ if (isAnimatableProperty(prop.name) === false) {+ return;+ }+ setInputValue(prop.name);+ onPropertySelection({ type: "keyword", value: prop.name });+ },+ onInputChange: (value) => setInputValue(value ?? ""),+ });++ const commonProperties = items.filter(+ (item) => commonPropertiesSet.has(item.name) === true+ );++ const filteredProperties = items.filter(+ (item) => commonPropertiesSet.has(item.name) === false+ );++ const renderItem = (item: NameAndLabel, index: number) => (+ <ComboboxListboxItem+ key={item.name}+ selectable={false}+ {...getItemProps({+ item,+ index,+ })}+ >+ {item.name}+ </ComboboxListboxItem>+ );++ return (+ <>+ <Label> Property </Label>+ <Combobox>+ <div {...getComboboxProps()}>+ <ComboboxAnchor>+ <InputField+ autoFocus+ {...getInputProps({ onKeyDown: (e) => e.stopPropagation() })}+ placeholder="all"+ suffix={<NestedInputButton {...getToggleButtonProps()} />}+ />+ </ComboboxAnchor>+ <ComboboxContent+ align="end"+ sideOffset={5}+ className={comboBoxStyles()}+ >+ <ComboboxListbox {...getMenuProps()}>+ {isOpen && (+ <>+ <ComboboxLabel>Common</ComboboxLabel>+ {commonProperties.map(renderItem)}+ <ComboboxSeparator />+ {filteredProperties.map((property, index) =>+ /*+ When rendered in two different lists.+ We will have two indexes start at '0'. Which leads to+ - The same focus might be repeated when highlighted.+ - Using findIndex within getItemProps might make the focus jump around,+ as it searches the entire list for items.+ This happens because the list isn't sorted in order but is divided when rendering.+ */+ renderItem(property, commonProperties.length + index)+ )}+ </>+ )}+ </ComboboxListbox>+ </ComboboxContent>+ </div>+ </Combobox>+ </>+ );+};diff --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 3cb21a58f9..1ca39e7c4c 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@@ -59,7 +59,7 @@ export const TransitionSection = (props: RenderCategoryProps) => {addLayer(property,// Using default transition value- parseTransition("opacity 200ms ease 0s"),+ parseTransition("opacity 200ms ease"),currentStyle,props.createBatchUpdate);@@ -91,8 +91,9 @@ export const TransitionSection = (props: RenderCategoryProps) => {renderLayer={(layerProps) => {return (<Layer- key={layerProps.index}{...layerProps}+ key={layerProps.index}+ layer={layerProps.layer}disabled={isStyleInLocalState === false}/>);diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.tsindex c71dfe14d6..b2786b9f14 100755--- a/packages/css-data/bin/mdn-data.ts+++ b/packages/css-data/bin/mdn-data.ts@@ -224,6 +224,7 @@ const walkSyntax = (type FilteredProperties = { [property in Property]: Value };+const animatableProperties: string[] = [];const filteredProperties: FilteredProperties = (() => {// A list of properties we don't want to showconst ignoreProperties = ["all", "-webkit-line-clamp", "--*"];@@ -261,6 +262,14 @@ const filteredProperties: FilteredProperties = (() => {config.initial = supportedComplexProperties[property];}+ if (+ property.charAt(0) !== "-" &&+ config.animationType !== "discrete" &&+ config.animationType !== "notAnimatable"+ ) {+ animatableProperties.push(property);+ }+if (isSupportedStatus === false ||// Skipping the complex values, since we want to use the expanded once.@@ -296,7 +305,6 @@ const patchAppliesTo = (property: Property, config: Value) => {};let property: Property;-for (property in filteredProperties) {const config = filteredProperties[property];// collect node types to improve parsing of css values@@ -397,6 +405,11 @@ const keywordValues = (() => {writeToFile("units.ts", "units", units);writeToFile("properties.ts", "properties", propertiesData);writeToFile("keyword-values.ts", "keywordValues", keywordValues);+writeToFile(+ "animatable-properties.ts",+ "animatableProperties",+ animatableProperties+);let types = "";diff --git a/packages/css-data/src/__generated__/animatable-properties.ts b/packages/css-data/src/__generated__/animatable-properties.tsnew file mode 100644index 0000000000..fc6663ec2f--- /dev/null+++ b/packages/css-data/src/__generated__/animatable-properties.ts@@ -0,0 +1,237 @@+// This file was generated by pnpm mdn-data+export const animatableProperties = [+ "accent-color",+ "all",+ "aspect-ratio",+ "backdrop-filter",+ "background",+ "background-clip",+ "background-color",+ "background-origin",+ "background-position",+ "background-position-x",+ "background-position-y",+ "background-size",+ "block-size",+ "border",+ "border-block",+ "border-block-color",+ "border-block-width",+ "border-block-end",+ "border-block-end-color",+ "border-block-end-width",+ "border-block-start",+ "border-block-start-color",+ "border-block-start-width",+ "border-bottom",+ "border-bottom-color",+ "border-bottom-left-radius",+ "border-bottom-right-radius",+ "border-bottom-width",+ "border-color",+ "border-end-end-radius",+ "border-end-start-radius",+ "border-image",+ "border-image-outset",+ "border-image-slice",+ "border-image-width",+ "border-inline",+ "border-inline-end",+ "border-inline-color",+ "border-inline-width",+ "border-inline-end-color",+ "border-inline-end-width",+ "border-inline-start",+ "border-inline-start-color",+ "border-inline-start-width",+ "border-left",+ "border-left-color",+ "border-left-width",+ "border-radius",+ "border-right",+ "border-right-color",+ "border-right-width",+ "border-start-end-radius",+ "border-start-start-radius",+ "border-top",+ "border-top-color",+ "border-top-left-radius",+ "border-top-right-radius",+ "border-top-width",+ "border-width",+ "bottom",+ "box-shadow",+ "caret",+ "caret-color",+ "caret-shape",+ "clip",+ "clip-path",+ "color",+ "column-count",+ "column-gap",+ "column-rule",+ "column-rule-color",+ "column-rule-width",+ "column-width",+ "columns",+ "contain-intrinsic-size",+ "contain-intrinsic-block-size",+ "contain-intrinsic-height",+ "contain-intrinsic-inline-size",+ "contain-intrinsic-width",+ "container",+ "container-type",+ "counter-increment",+ "counter-reset",+ "counter-set",+ "filter",+ "flex",+ "flex-basis",+ "flex-flow",+ "flex-grow",+ "flex-shrink",+ "font",+ "font-variation-settings",+ "font-size",+ "font-size-adjust",+ "font-stretch",+ "font-style",+ "font-weight",+ "gap",+ "grid",+ "grid-auto-columns",+ "grid-auto-rows",+ "grid-column-gap",+ "grid-gap",+ "grid-row-gap",+ "grid-template",+ "grid-template-columns",+ "grid-template-rows",+ "height",+ "hyphenate-limit-chars",+ "initial-letter",+ "inline-size",+ "input-security",+ "inset",+ "inset-block",+ "inset-block-end",+ "inset-block-start",+ "inset-inline",+ "inset-inline-end",+ "inset-inline-start",+ "left",+ "letter-spacing",+ "line-clamp",+ "line-height",+ "line-height-step",+ "list-style",+ "margin",+ "margin-block",+ "margin-block-end",+ "margin-block-start",+ "margin-bottom",+ "margin-inline",+ "margin-inline-end",+ "margin-inline-start",+ "margin-left",+ "margin-right",+ "margin-top",+ "mask",+ "mask-border",+ "mask-position",+ "mask-size",+ "max-block-size",+ "max-height",+ "max-inline-size",+ "max-lines",+ "max-width",+ "min-block-size",+ "min-height",+ "min-inline-size",+ "min-width",+ "object-position",+ "offset",+ "offset-anchor",+ "offset-distance",+ "offset-path",+ "offset-position",+ "offset-rotate",+ "opacity",+ "order",+ "orphans",+ "outline",+ "outline-color",+ "outline-offset",+ "outline-style",+ "outline-width",+ "padding",+ "padding-block",+ "padding-block-end",+ "padding-block-start",+ "padding-bottom",+ "padding-inline",+ "padding-inline-end",+ "padding-inline-start",+ "padding-left",+ "padding-right",+ "padding-top",+ "perspective",+ "perspective-origin",+ "right",+ "rotate",+ "row-gap",+ "ruby-align",+ "ruby-merge",+ "scale",+ "scrollbar-color",+ "scrollbar-width",+ "scroll-margin",+ "scroll-margin-block",+ "scroll-margin-block-start",+ "scroll-margin-block-end",+ "scroll-margin-bottom",+ "scroll-margin-inline",+ "scroll-margin-inline-start",+ "scroll-margin-inline-end",+ "scroll-margin-left",+ "scroll-margin-right",+ "scroll-margin-top",+ "scroll-padding",+ "scroll-padding-block",+ "scroll-padding-block-start",+ "scroll-padding-block-end",+ "scroll-padding-bottom",+ "scroll-padding-inline",+ "scroll-padding-inline-start",+ "scroll-padding-inline-end",+ "scroll-padding-left",+ "scroll-padding-right",+ "scroll-padding-top",+ "scroll-snap-coordinate",+ "scroll-snap-destination",+ "scroll-timeline",+ "shape-image-threshold",+ "shape-margin",+ "shape-outside",+ "tab-size",+ "text-decoration",+ "text-decoration-color",+ "text-decoration-thickness",+ "text-emphasis",+ "text-emphasis-color",+ "text-indent",+ "text-shadow",+ "text-size-adjust",+ "text-underline-offset",+ "top",+ "transform",+ "transform-origin",+ "translate",+ "vertical-align",+ "visibility",+ "widows",+ "width",+ "word-spacing",+ "z-index",+ "zoom",+] as const;diff --git a/packages/css-data/src/index.ts b/packages/css-data/src/index.tsindex d18d86066c..ab9bd84100 100644--- a/packages/css-data/src/index.ts+++ b/packages/css-data/src/index.ts@@ -9,6 +9,7 @@ export {properties as propertyDescriptions,declarations as declarationDescriptions,} from "./__generated__/property-value-descriptions";+export * from "./__generated__/animatable-properties";// longhand property parsersexport * from "./property-parsers/index";diff --git a/packages/css-data/src/property-parsers/__snapshots__/transition-property-extractor.test.ts.snap b/packages/css-data/src/property-parsers/__snapshots__/transition-property-extractor.test.ts.snapnew file mode 100644index 0000000000..f7a48e63c6--- /dev/null+++ b/packages/css-data/src/property-parsers/__snapshots__/transition-property-extractor.test.ts.snap@@ -0,0 +1,299 @@+// Jest Snapshot v1, https://goo.gl/fbAQLP++exports[`extractTransitionProperties parses and extracts values from custom-property ease:+ {+ "property": {+ "type": "keyword",+ "value": "custom-property"+ },+ "duration": null,+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": null,+ "property": {+ "type": "keyword",+ "value": "custom-property",+ },+ "timing": {+ "type": "keyword",+ "value": "ease",+ },+}+`;++exports[`extractTransitionProperties parses and extracts values from custom-property ease:+ {+ "property": null,+ "duration": null,+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": null,+ "property": null,+ "timing": {+ "type": "keyword",+ "value": "ease",+ },+}+`;++exports[`extractTransitionProperties parses and extracts values from opacity 200ms 10ms:+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": null,+ "delay": {+ "type": "unit",+ "value": 10,+ "unit": "ms"+ }+ }+ 1`] = `+{+ "delay": {+ "type": "unit",+ "unit": "ms",+ "value": 10,+ },+ "duration": {+ "type": "unit",+ "unit": "ms",+ "value": 200,+ },+ "property": {+ "type": "keyword",+ "value": "opacity",+ },+ "timing": null,+}+`;++exports[`extractTransitionProperties parses and extracts values from opacity 200ms ease 50ms:+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": {+ "type": "unit",+ "value": 50,+ "unit": "ms"+ }+ }+ 1`] = `+{+ "delay": {+ "type": "unit",+ "unit": "ms",+ "value": 50,+ },+ "duration": {+ "type": "unit",+ "unit": "ms",+ "value": 200,+ },+ "property": {+ "type": "keyword",+ "value": "opacity",+ },+ "timing": {+ "type": "keyword",+ "value": "ease",+ },+}+`;++exports[`extractTransitionProperties parses and extracts values from opacity 200ms ease:+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": {+ "type": "unit",+ "unit": "ms",+ "value": 200,+ },+ "property": {+ "type": "keyword",+ "value": "opacity",+ },+ "timing": {+ "type": "keyword",+ "value": "ease",+ },+}+`;++exports[`extractTransitionProperties parses and extracts values from opacity 200ms:+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": null,+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": {+ "type": "unit",+ "unit": "ms",+ "value": 200,+ },+ "property": {+ "type": "keyword",+ "value": "opacity",+ },+ "timing": null,+}+`;++exports[`extractTransitionProperties parses and extracts values from opacity ease 50ms:+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 50,+ "unit": "ms"+ },+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": {+ "type": "unit",+ "unit": "ms",+ "value": 50,+ },+ "property": {+ "type": "keyword",+ "value": "opacity",+ },+ "timing": {+ "type": "keyword",+ "value": "ease",+ },+}+`;++exports[`extractTransitionProperties parses and extracts values from opacity ease:+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": null,+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": null,+ "property": {+ "type": "keyword",+ "value": "opacity",+ },+ "timing": {+ "type": "keyword",+ "value": "ease",+ },+}+`;++exports[`extractTransitionProperties parses and extracts values from transform 0.6s cubic-bezier(0.36, 0, 0.66, -0.56):+ {+ "property": {+ "type": "keyword",+ "value": "transform"+ },+ "duration": {+ "type": "unit",+ "value": 0.6,+ "unit": "s"+ },+ "timing": {+ "type": "keyword",+ "value": "cubic-bezier(0.36, 0, 0.66, -0.56)"+ },+ "delay": null+ }+ 1`] = `+{+ "delay": null,+ "duration": {+ "type": "unit",+ "unit": "s",+ "value": 0.6,+ },+ "property": {+ "type": "keyword",+ "value": "transform",+ },+ "timing": {+ "type": "keyword",+ "value": "cubic-bezier(0.36, 0, 0.66, -0.56)",+ },+}+`;diff --git a/packages/css-data/src/property-parsers/__snapshots__/transition.test.ts.snap b/packages/css-data/src/property-parsers/__snapshots__/transition.test.ts.snapindex 222563aec7..295c7f49fd 100644--- a/packages/css-data/src/property-parsers/__snapshots__/transition.test.ts.snap+++ b/packages/css-data/src/property-parsers/__snapshots__/transition.test.ts.snap@@ -127,3 +127,25 @@ exports[`parseTransition parse value and returns valid layer:],}`;++exports[`parseTransition throws error if any custom transition proeprty is used:+ {+ "type": "invalid",+ "value": "all 200ms ease 0s, --foo 200ms ease 0s"+ } 1`] = `+{+ "type": "invalid",+ "value": "all 200ms ease 0s, --foo 200ms ease 0s",+}+`;++exports[`parseTransition throws error if any custom transition timing function is used:+ {+ "type": "invalid",+ "value": "all 200ms custom-function 0s"+ } 1`] = `+{+ "type": "invalid",+ "value": "all 200ms custom-function 0s",+}+`;diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.tsindex 22ff946341..8bd15b24e0 100644--- a/packages/css-data/src/property-parsers/index.ts+++ b/packages/css-data/src/property-parsers/index.ts@@ -1,3 +1,4 @@export * from "./background";export * from "./box-shadow";export * from "./transition";+export * from "./transition-property-extractor";diff --git a/packages/css-data/src/property-parsers/transition-property-extractor.test.ts b/packages/css-data/src/property-parsers/transition-property-extractor.test.tsnew file mode 100644index 0000000000..6958aed6a2--- /dev/null+++ b/packages/css-data/src/property-parsers/transition-property-extractor.test.ts@@ -0,0 +1,413 @@+import { describe, expect, test } from "@jest/globals";+import { extractTransitionProperties } from "./transition-property-extractor";++describe("extractTransitionProperties", () => {+ test("parses and extracts values from opacity 200ms ease 50ms", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "opacity",+ },+ {+ type: "unit",+ value: 200,+ unit: "ms",+ },+ {+ type: "keyword",+ value: "ease",+ },+ {+ type: "unit",+ value: 50,+ unit: "ms",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "opacity" },+ duration: { type: "unit", value: 200, unit: "ms" },+ timing: { type: "keyword", value: "ease" },+ delay: { type: "unit", value: 50, unit: "ms" },+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": {+ "type": "unit",+ "value": 50,+ "unit": "ms"+ }+ }+ `);+ });++ test("parses and extracts values from opacity 200ms ease", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "opacity",+ },+ {+ type: "unit",+ value: 200,+ unit: "ms",+ },+ {+ type: "keyword",+ value: "ease",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "opacity" },+ duration: { type: "unit", value: 200, unit: "ms" },+ timing: { type: "keyword", value: "ease" },+ delay: null,+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ `);+ });++ test("parses and extracts values from opacity 200ms 10ms", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "opacity",+ },+ {+ type: "unit",+ value: 200,+ unit: "ms",+ },+ {+ type: "unit",+ value: 10,+ unit: "ms",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "opacity" },+ duration: { type: "unit", value: 200, unit: "ms" },+ timing: null,+ delay: { type: "unit", value: 10, unit: "ms" },+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": null,+ "delay": {+ "type": "unit",+ "value": 10,+ "unit": "ms"+ }+ }+ `);+ });++ test("parses and extracts values from opacity 200ms", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "opacity",+ },+ {+ type: "unit",+ value: 200,+ unit: "ms",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "opacity" },+ duration: { type: "unit", value: 200, unit: "ms" },+ timing: null,+ delay: null,+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 200,+ "unit": "ms"+ },+ "timing": null,+ "delay": null+ }+ `);+ });++ test("parses and extracts values from ease 10ms", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "ease",+ },+ {+ type: "unit",+ value: 10,+ unit: "ms",+ },+ ],+ });+ expect(result).toEqual({+ property: null,+ duration: { type: "unit", value: 10, unit: "ms" },+ timing: { type: "keyword", value: "ease" },+ delay: null,+ });+ });++ test("parses and extracts values from opacity ease", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "opacity",+ },+ {+ type: "keyword",+ value: "ease",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "opacity" },+ duration: null,+ timing: { type: "keyword", value: "ease" },+ delay: null,+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": null,+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ `);+ });++ test("parses and extracts values from opacity ease 50ms", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "opacity",+ },+ {+ type: "keyword",+ value: "ease",+ },+ {+ type: "unit",+ value: 50,+ unit: "ms",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "opacity" },+ duration: { type: "unit", value: 50, unit: "ms" },+ timing: { type: "keyword", value: "ease" },+ delay: null,+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "opacity"+ },+ "duration": {+ "type": "unit",+ "value": 50,+ "unit": "ms"+ },+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ `);+ });++ test("parses and extracts values from transform 0.6s cubic-bezier(0.36, 0, 0.66, -0.56)", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "transform",+ },+ {+ type: "unit",+ value: 0.6,+ unit: "s",+ },+ {+ type: "keyword",+ value: "cubic-bezier(0.36, 0, 0.66, -0.56)",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "transform" },+ duration: { type: "unit", value: 0.6, unit: "s" },+ timing: { type: "keyword", value: "cubic-bezier(0.36, 0, 0.66, -0.56)" },+ delay: null,+ });+ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "transform"+ },+ "duration": {+ "type": "unit",+ "value": 0.6,+ "unit": "s"+ },+ "timing": {+ "type": "keyword",+ "value": "cubic-bezier(0.36, 0, 0.66, -0.56)"+ },+ "delay": null+ }+ `);+ });++ test("parses and extracts values from custom-property ease", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "custom-property",+ },+ {+ type: "keyword",+ value: "ease",+ },+ ],+ });++ expect(result).toEqual({+ property: { type: "keyword", value: "custom-property" },+ duration: null,+ timing: { type: "keyword", value: "ease" },+ delay: null,+ });++ expect(result).toMatchSnapshot(`+ {+ "property": {+ "type": "keyword",+ "value": "custom-property"+ },+ "duration": null,+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ `);+ });++ test("parses and extracts values from custom-property ease", () => {+ const result = extractTransitionProperties({+ type: "tuple",+ value: [+ {+ type: "keyword",+ value: "ease",+ },+ ],+ });++ expect(result).toEqual({+ property: null,+ duration: null,+ timing: { type: "keyword", value: "ease" },+ delay: null,+ });++ expect(result).toMatchSnapshot(`+ {+ "property": null,+ "duration": null,+ "timing": {+ "type": "keyword",+ "value": "ease"+ },+ "delay": null+ }+ `);+ });+});diff --git a/packages/css-data/src/property-parsers/transition-property-extractor.ts b/packages/css-data/src/property-parsers/transition-property-extractor.tsnew file mode 100644index 0000000000..7299f712e5--- /dev/null+++ b/packages/css-data/src/property-parsers/transition-property-extractor.ts@@ -0,0 +1,55 @@+import type {+ KeywordValue,+ TupleValue,+ UnitValue,+} from "@webstudio-is/css-engine";++export const isTimingFunction = (timing: string) => {+ const regex =+ /^(ease(-in-out|-in|-out)?|linear|cubic-bezier\((-?\d+(\.\d+)?, ?){3}-?\d+(\.\d+)?\)|steps\(\d+(-?(start|end))?\))$/gm;+ return regex.test(timing);+};++export const extractTransitionProperties = (+ transition: TupleValue+): {+ property: KeywordValue | null;+ timing: KeywordValue | null;+ duration: UnitValue | null;+ delay: UnitValue | null;+} => {+ let property: KeywordValue | null = null;+ let timing: KeywordValue | null = null;+ let duration: UnitValue | null = null;+ let delay: UnitValue | null = null;++ const keywordValues: KeywordValue[] = [];+ const unitValues: UnitValue[] = [];++ for (const property of transition.value) {+ if (property.type === "keyword") {+ keywordValues.push(property);+ }++ if (property.type === "unit") {+ unitValues.push(property);+ }+ }++ if (keywordValues.length && isTimingFunction(keywordValues[0].value)) {+ timing = keywordValues[0];+ } else {+ property = keywordValues[0];+ timing = keywordValues[1] ?? null;+ }++ duration = unitValues[0] ?? null;+ delay = unitValues[1] ?? null;++ return {+ property,+ duration,+ timing,+ delay,+ };+};diff --git a/packages/css-data/src/property-parsers/transition.test.ts b/packages/css-data/src/property-parsers/transition.test.tsindex bc2a813e46..c9b0cb924b 100644--- a/packages/css-data/src/property-parsers/transition.test.ts+++ b/packages/css-data/src/property-parsers/transition.test.ts@@ -80,4 +80,24 @@ describe("parseTransition", () => {]}`);});++ test("throws error if any custom transition proeprty is used", () => {+ const transition = parseTransition(+ "all 200ms ease 0s, --foo 200ms ease 0s"+ );+ expect(transition).toMatchSnapshot(`+ {+ "type": "invalid",+ "value": "all 200ms ease 0s, --foo 200ms ease 0s"+ }`);+ });++ test("throws error if any custom transition timing function is used", () => {+ const transition = parseTransition("all 200ms custom-function 0s");+ expect(transition).toMatchSnapshot(`+ {+ "type": "invalid",+ "value": "all 200ms custom-function 0s"+ }`);+ });});diff --git a/packages/css-data/src/property-parsers/transition.ts b/packages/css-data/src/property-parsers/transition.tsindex 71ff94a717..ee919e25d6 100644--- a/packages/css-data/src/property-parsers/transition.ts+++ b/packages/css-data/src/property-parsers/transition.ts@@ -6,6 +6,8 @@ import type {TupleValueItem,Unit,} from "@webstudio-is/css-engine";+import { animatableProperties } from "../";+import { isTimingFunction } from "./transition-property-extractor";const cssTreeTryParseValue = (input: string) => {try {@@ -16,6 +18,13 @@ const cssTreeTryParseValue = (input: string) => {}};+type AnimatableProperty = (typeof animatableProperties)[number];+export const isAnimatableProperty = (+ property: string+): property is AnimatableProperty => {+ return animatableProperties.some((item) => item === property);+};+export const parseTransition = (transition: string): LayersValue | InvalidValue => {@@ -49,52 +58,72 @@ export const parseTransition = (};}- const layers: TupleValue[] = [];+ try {+ const layers: TupleValue[] = [];+ csstree.walk(cssAst, (node) => {+ if (node.type === "Value") {+ const children = node.children;+ let layer: csstree.CssNode[] = [];- csstree.walk(cssAst, (node) => {- if (node.type === "Value") {- const children = node.children;- let layer: csstree.CssNode[] = [];+ for (const child of children) {+ layer.push(child);- for (const child of children) {- layer.push(child);+ if (child.type === "Operator" || children.last === child) {+ const transition: TupleValueItem[] = [];- if (child.type === "Operator" || children.last === child) {- const transition: TupleValueItem[] = [];+ for (let index = 0; index < layer.length; index++) {+ const item = layer[index];+ if (item.type === "Identifier") {+ const isAnimatable = isAnimatableProperty(item.name);+ const isTimingFunc = isTimingFunction(item.name);- for (let index = 0; index < layer.length; index++) {- const item = layer[index];- if (item.type === "Identifier") {- transition.push({- type: "keyword",- value: item.name,- });- }+ if (isTimingFunc === false && isAnimatable === false) {+ throw new Error(`Invalid transition property: ${item.name}`);+ }- if (item.type === "Dimension") {- transition.push({- type: "unit",- value: Number(item.value),- unit: item.unit as Unit,- });+ transition.push({+ type: "keyword",+ value: item.name,+ });+ }++ if (item.type === "Dimension") {+ transition.push({+ type: "unit",+ value: Number(item.value),+ unit: item.unit as Unit,+ });+ }++ if (item.type === "Function") {+ transition.push({+ type: "keyword",+ value: csstree.generate(item),+ });+ }}- }- layers.push({- type: "tuple",- value: transition,- });- layer = [];- continue;+ layers.push({+ type: "tuple",+ value: transition,+ });+ layer = [];+ continue;+ }}}- }- });+ });- return layers.length > 0- ? {- type: "layers",- value: layers,- }- : { type: "invalid", value: transition };+ return layers.length > 0+ ? {+ type: "layers",+ value: layers,+ }+ : { type: "invalid", value: transition };+ } catch (error) {+ return {+ type: "invalid",+ value: transition,+ };+ }};