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: Transition timing function UI (#2572)

Jaya Krishna Namburu950ef303f370
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.tsx
index 95c6d93d09..86ffee153c 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
@@ -22,16 +22,17 @@ import {
parseTransition,
} from "@webstudio-is/css-data";
import { InformationIcon } from "@webstudio-is/icons";
-import type { CreateBatchUpdate } from "../../shared/use-style-data";
+import type { DeleteProperty } from "../../shared/use-style-data";
import { type IntermediateStyleValue } from "../../shared/css-value-input";
import { TransitionProperty } from "./transition-property";
+import { TransitionTiming } from "./transition-timing";
type TransitionContentProps = {
index: number;
layer: TupleValue;
transition: string;
onEditLayer: (index: number, layer: LayersValue) => void;
- createBatchUpdate: CreateBatchUpdate;
+ deleteProperty: DeleteProperty;
};
export const TransitionContent = ({
@@ -39,6 +40,7 @@ export const TransitionContent = ({
transition,
onEditLayer,
index,
+ deleteProperty,
}: TransitionContentProps) => {
const [intermediateValue, setIntermediateValue] = useState<
IntermediateStyleValue | InvalidValue | undefined
@@ -92,6 +94,24 @@ export const TransitionContent = ({
onEditLayer(index, { type: "layers", value: [newLayer] });
};
+ const handleOnTimingSelection = (newTiming: TupleValueItem) => {
+ const value: TupleValueItem[] = [
+ property,
+ duration,
+ newTiming,
+ 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">
<Grid
@@ -107,6 +127,12 @@ export const TransitionContent = ({
property={property ?? { type: "keyword", value: "all" }}
onPropertySelection={handleOnPropertySelection}
/>
+
+ <TransitionTiming
+ /* Browser defaults for transition-property - ease */
+ timing={timing ?? { type: "keyword", value: "ease" }}
+ onTimingSelection={handleOnTimingSelection}
+ />
</Grid>
<Separator css={{ gridColumn: "span 2" }} />
<Flex
@@ -144,11 +170,22 @@ export const TransitionContent = ({
state={intermediateValue?.type === "invalid" ? "invalid" : undefined}
value={intermediateValue?.value ?? ""}
onChange={handleChange}
+ onBlur={handleComplete}
onKeyDown={(event) => {
if (event.key === "Enter") {
handleComplete();
event.preventDefault();
}
+
+ if (event.key === "Escape") {
+ if (intermediateValue === undefined) {
+ return;
+ }
+
+ deleteProperty("transition", { isEphemeral: true });
+ setIntermediateValue(undefined);
+ event.preventDefault();
+ }
}}
/>
</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.tsx
index db5e438656..f51bfb756b 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
@@ -8,11 +8,30 @@ import { SubtractIcon } from "@webstudio-is/icons";
import { FloatingPanel } from "~/builder/shared/floating-panel";
import { TransitionContent } from "./transition-content";
import type { LayerProps } from "../../style-layers-list";
-import { toValue } from "@webstudio-is/css-engine";
+import { TupleValue, toValue } from "@webstudio-is/css-engine";
+import { findTimingFunctionFromValue } from "./transition-utils";
export const Layer = (props: LayerProps) => {
const { id, index, layer, isHighlighted, onDeleteLayer, disabled } = props;
- const transition = useMemo(() => toValue(layer), [layer]);
+ const { transition, label } = useMemo(() => {
+ const label: TupleValue = {
+ type: "tuple",
+ value: [],
+ };
+
+ for (const prop of layer.value) {
+ if (prop.type === "keyword") {
+ label.value.push({
+ type: "keyword",
+ value: findTimingFunctionFromValue(prop.value) ?? prop.value,
+ });
+ } else {
+ label.value.push(prop);
+ }
+ }
+
+ return { transition: toValue(layer), label: toValue(label) };
+ }, [layer]);
return (
<FloatingPanel
@@ -23,7 +42,7 @@ export const Layer = (props: LayerProps) => {
layer={layer}
transition={transition}
onEditLayer={props.onEditLayer}
- createBatchUpdate={props.createBatchUpdate}
+ deleteProperty={props.deleteProperty}
/>
}
>
@@ -31,7 +50,7 @@ export const Layer = (props: LayerProps) => {
id={id}
active={isHighlighted}
index={index}
- label={<Label truncate>{transition}</Label>}
+ label={<Label truncate>{label}</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.tsx
index 89c9bd8cb3..7ea87d8677 100644
--- 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.tsx
@@ -49,7 +49,7 @@ export const TransitionProperty = ({
property,
onPropertySelection,
}: TransitionPropertyProps) => {
- const [inputValue, setInputValue] = useState(property?.value ?? "all");
+ const [inputValue, setInputValue] = useState(property.value ?? "all");
useEffect(() => setInputValue(property.value), [property.value]);
const {
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
new file mode 100644
index 0000000000..14835c0832
--- /dev/null
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
@@ -0,0 +1,186 @@
+import { useEffect, useMemo, useState } from "react";
+import type { KeywordValue, TupleValueItem } from "@webstudio-is/css-engine";
+import {
+ Combobox,
+ ComboboxAnchor,
+ ComboboxContent,
+ ComboboxListbox,
+ InputField,
+ Label,
+ NestedInputButton,
+ useCombobox,
+ css,
+ ComboboxLabel,
+ theme,
+ ComboboxListboxItem,
+ ComboboxSeparator,
+} from "@webstudio-is/design-system";
+import {
+ defaultFunctions,
+ easeInFunctions,
+ easeOutFunctions,
+ easeInOutFunctions,
+ timingFunctions,
+ findTimingFunctionFromValue,
+ type TimingFunctions,
+ type EaseInFunction,
+ type EaseOutFunction,
+ type DefaultFunction,
+ type EaseInOutFunction,
+} from "./transition-utils";
+
+type NameAndLabel = { name: TimingFunctions; value: string };
+
+type TransitionTimingProps = {
+ timing: KeywordValue;
+ onTimingSelection: (timing: TupleValueItem) => void;
+};
+
+const filterTimingFunctuionsFromList = <T extends TimingFunctions>(
+ functions: NameAndLabel[],
+ timingFunctions: { [key in T]: string }
+) => {
+ return functions.filter(
+ (prop: NameAndLabel): prop is { name: T; value: string } => {
+ return Object.keys(timingFunctions).includes(prop.name);
+ }
+ );
+};
+
+const comboBoxStyles = css({ zIndex: theme.zIndices[1] });
+
+export const TransitionTiming = ({
+ timing,
+ onTimingSelection,
+}: TransitionTimingProps) => {
+ const [inputValue, setInputValue] = useState(
+ findTimingFunctionFromValue(timing.value) ?? timing.value ?? ""
+ );
+ useEffect(
+ () =>
+ setInputValue(
+ findTimingFunctionFromValue(timing.value) ?? timing.value ?? ""
+ ),
+ [timing.value]
+ );
+
+ const {
+ items,
+ isOpen,
+ getComboboxProps,
+ getToggleButtonProps,
+ getInputProps,
+ getMenuProps,
+ getItemProps,
+ } = useCombobox<NameAndLabel>({
+ items: (Object.keys(timingFunctions) as TimingFunctions[]).map((prop) => ({
+ name: prop,
+ value: timingFunctions[prop],
+ })),
+ value: {
+ name: inputValue as TimingFunctions,
+ value: timingFunctions[inputValue as TimingFunctions] ?? "",
+ },
+ selectedItem: undefined,
+ itemToString: (value) => value?.name ?? "",
+ onInputChange: (value) => setInputValue(value ?? ""),
+ onItemSelect: (prop) => {
+ setInputValue(prop.name);
+ onTimingSelection({ type: "keyword", value: prop.value });
+ },
+ });
+
+ const filteredTimingFunctions = useMemo(() => {
+ const defaults = filterTimingFunctuionsFromList<DefaultFunction>(
+ items,
+ defaultFunctions
+ );
+ const easeIns = filterTimingFunctuionsFromList<EaseInFunction>(
+ items,
+ easeInFunctions
+ );
+ const easeOuts = filterTimingFunctuionsFromList<EaseOutFunction>(
+ items,
+ easeOutFunctions
+ );
+ const easeInOuts = filterTimingFunctuionsFromList<EaseInOutFunction>(
+ items,
+ easeInOutFunctions
+ );
+
+ return {
+ defaults,
+ easeIns,
+ easeOuts,
+ easeInOuts,
+ };
+ }, [items]);
+
+ const { defaults, easeIns, easeOuts, easeInOuts } = filteredTimingFunctions;
+
+ const renderItem = (item: NameAndLabel, index: number) => {
+ return (
+ <ComboboxListboxItem
+ key={item.name}
+ selectable={false}
+ {...getItemProps({ item, index })}
+ css={{ paddingLeft: theme.spacing[11] }}
+ >
+ {item.name}
+ </ComboboxListboxItem>
+ );
+ };
+
+ return (
+ <>
+ <Label>Timing</Label>
+ <Combobox>
+ <div {...getComboboxProps()}>
+ <ComboboxAnchor>
+ <InputField
+ autoFocus
+ {...getInputProps({ onKeyDown: (e) => e.stopPropagation() })}
+ placeholder="ease"
+ suffix={<NestedInputButton {...getToggleButtonProps()} />}
+ />
+ </ComboboxAnchor>
+ <ComboboxContent
+ align="end"
+ sideOffset={5}
+ className={comboBoxStyles()}
+ >
+ <ComboboxListbox {...getMenuProps()}>
+ {isOpen && (
+ <>
+ <ComboboxLabel>Default</ComboboxLabel>
+ {defaults.map(renderItem)}
+ <ComboboxSeparator />
+
+ <ComboboxLabel>Ease In</ComboboxLabel>
+ {easeIns.map((prop, index) =>
+ renderItem(prop, defaults.length + index)
+ )}
+ <ComboboxSeparator />
+
+ <ComboboxLabel>Ease Out</ComboboxLabel>
+ {easeOuts.map((prop, index) =>
+ renderItem(prop, defaults.length + easeIns.length + index)
+ )}
+ <ComboboxSeparator />
+
+ <ComboboxLabel>Ease In Out</ComboboxLabel>
+ {easeInOuts.map((prop, index) =>
+ renderItem(
+ prop,
+ defaults.length + easeIns.length + easeOuts.length + index
+ )
+ )}
+ </>
+ )}
+ </ComboboxListbox>
+ </ComboboxContent>
+ </div>
+ </Combobox>
+ </>
+ );
+};
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.ts
new file mode 100644
index 0000000000..ce59179437
--- /dev/null
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts
@@ -0,0 +1,65 @@
+export const defaultFunctions = {
+ linear: "linear",
+ ease: "ease",
+ "ease in": "ease-in",
+ "ease out": "ease-out",
+ "ease-in-out": "ease-in-out",
+} as const;
+
+export const easeInFunctions = {
+ "ease-in-sine": "cubic-bezier(0.12, 0, 0.39, 0)",
+ "ease-in-quad": "cubic-bezier(0.11, 0, 0.5, 0)",
+ "ease-in-cubic": "cubic-bezier(0.32, 0, 0.67, 0)",
+ "ease-in-quart": "cubic-bezier(0.5, 0, 0.75, 0)",
+ "ease-in-quint": "cubic-bezier(0.64, 0, 0.78, 0)",
+ "ease-in-expo": "cubic-bezier(0.7, 0, 0.84, 0)",
+ "ease-in-circ": "cubic-bezier(0.55, 0, 1, 0.45)",
+ "ease-in-back": "cubic-bezier(0.36, 0, 0.66, -0.56)",
+} as const;
+
+export const easeOutFunctions = {
+ "ease-out-sine": "cubic-bezier(0.61, 1, 0.88, 1)",
+ "ease-out-quad": "cubic-bezier(0.5, 1, 0.89, 1)",
+ "ease-out-cubic": "cubic-bezier(0.33, 1, 0.68, 1)",
+ "ease-out-quart": "cubic-bezier(0.25, 1, 0.5, 1)",
+ "ease-out-quint": "cubic-bezier(0.22, 1, 0.36, 1)",
+ "ease-out-expo": "cubic-bezier(0.16, 1, 0.3, 1)",
+ "ease-out-circ": "cubic-bezier(0, 0.55, 0.45, 1)",
+ "ease-out-back": "cubic-bezier(0.34, 1.56, 0.64, 1)",
+} as const;
+
+export const easeInOutFunctions = {
+ "ease-in-out-sine": "cubic-bezier(0.37, 0, 0.63, 1)",
+ "ease-in-out-quad": "cubic-bezier(0.45, 0, 0.55, 1)",
+ "ease-in-out-cubic": "cubic-bezier(0.65, 0, 0.35, 1)",
+ "ease-in-out-quart": "cubic-bezier(0.76, 0, 0.24, 1)",
+ "ease-in-out-quint": "cubic-bezier(0.83, 0, 0.17, 1)",
+ "ease-in-out-expo": "cubic-bezier(0.87, 0, 0.13, 1)",
+ "ease-in-out-circ": "cubic-bezier(0.85, 0, 0.15, 1)",
+ "ease-in-out-back": "cubic-bezier(0.68, -0.6, 0.32, 1.6)",
+} as const;
+
+export const timingFunctions = {
+ ...defaultFunctions,
+ ...easeInFunctions,
+ ...easeOutFunctions,
+ ...easeInOutFunctions,
+} as const;
+
+export type DefaultFunction = keyof typeof defaultFunctions;
+export type EaseInFunction = keyof typeof easeInFunctions;
+export type EaseOutFunction = keyof typeof easeOutFunctions;
+export type EaseInOutFunction = keyof typeof easeInOutFunctions;
+export type TimingFunctions =
+ | DefaultFunction
+ | EaseInFunction
+ | EaseOutFunction
+ | EaseInOutFunction;
+
+export const findTimingFunctionFromValue = (
+ timingFunction: string
+): TimingFunctions | undefined => {
+ return (Object.keys(timingFunctions) as TimingFunctions[]).find(
+ (key: TimingFunctions) => timingFunctions[key] === timingFunction
+ );
+};
diff --git a/apps/builder/app/builder/features/style-panel/style-layers-list.tsx b/apps/builder/app/builder/features/style-panel/style-layers-list.tsx
index 5d0a96c9a4..4beab7131e 100644
--- a/apps/builder/app/builder/features/style-panel/style-layers-list.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-layers-list.tsx
@@ -17,7 +17,10 @@ import {
updateLayer,
} from "./style-layer-utils";
import { useMemo } from "react";
-import type { CreateBatchUpdate } from "./shared/use-style-data";
+import type {
+ CreateBatchUpdate,
+ DeleteProperty,
+} from "./shared/use-style-data";
export type LayerProps = {
id: string;
@@ -29,6 +32,7 @@ export type LayerProps = {
onDeleteLayer: (index: number) => void;
onEditLayer: (index: number, layers: LayersValue) => void;
createBatchUpdate: CreateBatchUpdate;
+ deleteProperty: DeleteProperty;
};
type LayerListProperties = RenderCategoryProps & {
@@ -45,6 +49,7 @@ export const LayersList = ({
currentStyle,
renderLayer,
createBatchUpdate,
+ deleteProperty,
}: LayerListProperties) => {
const layersCount = getLayerCount(property, currentStyle);
@@ -93,6 +98,7 @@ export const LayersList = ({
onLayerHide: handleHideLayer,
onDeleteLayer: handleDeleteLayer,
createBatchUpdate,
+ deleteProperty,
onEditLayer,
});
})}
diff --git a/apps/builder/app/builder/features/style-panel/style-sections.tsx b/apps/builder/app/builder/features/style-panel/style-sections.tsx
index 44ae66d9ac..3d718f063a 100644
--- a/apps/builder/app/builder/features/style-panel/style-sections.tsx
+++ b/apps/builder/app/builder/features/style-panel/style-sections.tsx
@@ -29,6 +29,7 @@ import {
} from "./sections";
export const categories = [
+ "transitions",
"layout",
"flexChild",
"gridChild",
@@ -42,7 +43,6 @@ export const categories = [
"boxShadows",
"outline",
"effects",
- "transitions",
] as const;
export type Category = (typeof categories)[number];