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.

experimental: Always show css value input instead of an add button (#5029)

Oleg Isonen714c36fbc017
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-keyframes.tsx b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-keyframes.tsx
index fa3defd4bd..a0596dcd93 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-keyframes.tsx
+++ b/apps/builder/app/builder/features/settings-panel/props-section/animation/animation-keyframes.tsx
@@ -14,8 +14,6 @@ import {
SectionTitle,
SectionTitleButton,
SectionTitleLabel,
- Box,
- Button,
} from "@webstudio-is/design-system";
import { MinusIcon, PlusIcon } from "@webstudio-is/icons";
import type { AnimationKeyframe } from "@webstudio-is/sdk";
@@ -26,7 +24,7 @@ import {
} from "~/builder/features/style-panel/shared/css-value-input";
import { useIds } from "~/shared/form-utils";
import { calcOffsets, findInsertionIndex, moveItem } from "./keyframe-helpers";
-import { CssEditor, type CssEditorApi } from "~/builder/shared/css-editor";
+import { CssEditor } from "~/builder/shared/css-editor";
import type { ComputedStyleDecl } from "~/shared/style-object-model";
const unitOptions = [
@@ -134,8 +132,6 @@ const Keyframe = ({
) => void;
}) => {
const ids = useIds(["offset"]);
- const apiRef = useRef<CssEditorApi>();
-
const declarations: Array<ComputedStyleDecl> = useMemo(
() =>
(Object.keys(value.styles) as Array<CssProperty>).map((property) => {
@@ -179,10 +175,10 @@ const Keyframe = ({
<Grid>
<CssEditor
showSearch={false}
+ showAddStyleInput
propertiesPosition="top"
virtualize={false}
declarations={declarations}
- apiRef={apiRef}
onAddDeclarations={(addedStyleMap) => {
const styles = { ...value.styles };
for (const [property, value] of addedStyleMap) {
@@ -209,17 +205,6 @@ const Keyframe = ({
}}
/>
</Grid>
- <Box css={{ paddingInline: theme.panel.paddingInline }}>
- <Button
- onClick={() => {
- apiRef.current?.showAddStyleInput();
- }}
- prefix={<PlusIcon />}
- color="ghost"
- >
- Add
- </Button>
- </Box>
</>
);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx b/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx
index cd92dd4983..663455ee71 100644
--- a/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx
@@ -1,4 +1,4 @@
-import { useRef, useState, type ReactNode } from "react";
+import { useState, type ReactNode } from "react";
import { useStore } from "@nanostores/react";
import { PlusIcon } from "@webstudio-is/icons";
import {
@@ -19,7 +19,7 @@ import {
} from "../../shared/use-style-data";
import { useComputedStyles } from "../../shared/model";
import { getDots } from "../../shared/style-section";
-import { CssEditor, type CssEditorApi } from "../../../../shared/css-editor";
+import { CssEditor } from "../../../../shared/css-editor";
import { $advancedStyleDeclarations } from "./stores";
import { $selectedInstanceKey } from "~/shared/awareness";
@@ -64,7 +64,6 @@ const AdvancedStyleSection = (props: {
export const Section = () => {
const advancedStyleDeclarations = useStore($advancedStyleDeclarations);
- const apiRef = useRef<CssEditorApi>();
const properties = advancedStyleDeclarations.map(
(styleDecl) => styleDecl.property
);
@@ -74,6 +73,7 @@ export const Section = () => {
const [recentPropertiesMap, setRecentPropertiesMap] = useState<
Map<string, Array<CssProperty>>
>(new Map());
+ const [showAddStyleInput, setShowAddStyleInput] = useState<boolean>(false);
const recentProperties = selectedInstanceKey
? (recentPropertiesMap.get(selectedInstanceKey) ?? [])
@@ -131,7 +131,7 @@ export const Section = () => {
label="Advanced"
properties={properties}
onAdd={() => {
- apiRef.current?.showAddStyleInput();
+ setShowAddStyleInput(true);
}}
>
<CssEditor
@@ -140,8 +140,9 @@ export const Section = () => {
onSetProperty={setProperty}
onAddDeclarations={handleAddDeclarations}
onDeleteAllDeclarations={handleDeleteAllDeclarations}
- apiRef={apiRef}
recentProperties={recentProperties}
+ showAddStyleInput={showAddStyleInput}
+ onToggleAddStyleInput={setShowAddStyleInput}
/>
</AdvancedStyleSection>
);
diff --git a/apps/builder/app/builder/shared/css-editor/css-editor.tsx b/apps/builder/app/builder/shared/css-editor/css-editor.tsx
index 07d7d2c269..e6de16b37d 100644
--- a/apps/builder/app/builder/shared/css-editor/css-editor.tsx
+++ b/apps/builder/app/builder/shared/css-editor/css-editor.tsx
@@ -4,7 +4,6 @@ import { colord } from "colord";
import {
memo,
useEffect,
- useImperativeHandle,
useRef,
useState,
type ChangeEvent,
@@ -307,32 +306,31 @@ const AdvancedDeclarationLonghand = memo(
}
);
-export type CssEditorApi = { showAddStyleInput: () => void } | undefined;
-
export const CssEditor = ({
onDeleteProperty,
onSetProperty,
onAddDeclarations,
onDeleteAllDeclarations,
declarations,
- apiRef,
showSearch = true,
virtualize = true,
propertiesPosition = "bottom",
recentProperties = [],
+ showAddStyleInput,
+ onToggleAddStyleInput,
}: {
declarations: Array<ComputedStyleDecl>;
onDeleteProperty: DeleteProperty;
onSetProperty: SetProperty;
onAddDeclarations: (styleMap: CssStyleMap) => void;
onDeleteAllDeclarations: (styleMap: CssStyleMap) => void;
- apiRef?: RefObject<CssEditorApi>;
showSearch?: boolean;
propertiesPosition?: "top" | "bottom";
virtualize?: boolean;
recentProperties?: Array<CssProperty>;
+ showAddStyleInput?: boolean;
+ onToggleAddStyleInput?: (show: boolean) => void;
}) => {
- const [isAdding, setIsAdding] = useState(false);
const addPropertyInputRef = useRef<HTMLInputElement>(null);
const lastRecentValueInputRef = useRef<HTMLInputElement>(null);
const lastRegularValueInputRef = useRef<HTMLInputElement>(null);
@@ -340,11 +338,6 @@ export const CssEditor = ({
const [searchProperties, setSearchProperties] =
useState<Array<CssProperty>>();
const containerRef = useRef<HTMLDivElement>(null);
- useImperativeHandle(apiRef, () => ({
- showAddStyleInput() {
- handleShowAddStyleInput();
- },
- }));
const declarationsMap = new Map(
declarations.map((decl) => [decl.property, decl])
@@ -372,7 +365,7 @@ export const CssEditor = ({
const handleShowAddStyleInput = () => {
flushSync(() => {
- setIsAdding(true);
+ onToggleAddStyleInput?.(true);
});
// User can click twice on the add button, so we need to focus the input on the second click after autoFocus isn't working.
addPropertyInputRef.current?.focus();
@@ -420,7 +413,7 @@ export const CssEditor = ({
};
const afterChangingStyles = () => {
- setIsAdding(false);
+ onToggleAddStyleInput?.(false);
requestAnimationFrame(() => {
// We are either focusing the last value input from the recent list if available or the search input.
const element =
@@ -481,7 +474,7 @@ export const CssEditor = ({
})}
<Box
css={
- isAdding
+ showAddStyleInput
? { paddingTop: theme.spacing[3] }
: // We hide it visually so you can tab into it to get shown.
{ overflow: "hidden", height: 0 }
@@ -496,12 +489,12 @@ export const CssEditor = ({
}}
onClose={afterChangingStyles}
onFocus={() => {
- if (isAdding === false) {
+ if (showAddStyleInput === false) {
handleShowAddStyleInput();
}
}}
onBlur={() => {
- setIsAdding(false);
+ onToggleAddStyleInput?.(false);
}}
ref={addPropertyInputRef}
/>
@@ -537,7 +530,7 @@ export const CssEditor = ({
searchProperties ?? [...recentProperties, ...currentProperties]
}
>
- <Flex gap="2" direction="column">
+ <Flex gap="1" direction="column">
{propertiesPosition === "bottom" && (
<>
{recentPropertiesAndAddStyleInput}
diff --git a/apps/builder/app/builder/shared/css-editor/index.ts b/apps/builder/app/builder/shared/css-editor/index.ts
index e3abb50d2c..6b60dd4dee 100644
--- a/apps/builder/app/builder/shared/css-editor/index.ts
+++ b/apps/builder/app/builder/shared/css-editor/index.ts
@@ -1 +1 @@
-export { CssEditor, type CssEditorApi } from "./css-editor";
+export { CssEditor } from "./css-editor";