Skip to content

Commit 5ba9dc3

Browse files
authored
experimental: Reusable css editor (#4945)
## Description For using in animations UI we need a reusable css editor. ## Steps for reproduction This PR should not have changed how advanced section works at all. This is pure refactoring. ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent a344b34 commit 5ba9dc3

File tree

11 files changed

+579
-475
lines changed

11 files changed

+579
-475
lines changed

apps/builder/app/builder/features/style-panel/sections/advanced/advanced.tsx

+34-456
Large diffs are not rendered by default.

apps/builder/app/builder/features/style-panel/sections/advanced/stores.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { computed } from "nanostores";
22
import {
33
hyphenateProperty,
44
type CssProperty,
5-
type StyleMap,
5+
type CssStyleMap,
66
} from "@webstudio-is/css-engine";
77
import { $matchingBreakpoints, getDefinedStyles } from "../../shared/model";
88
import { sections } from "../sections";
@@ -42,7 +42,7 @@ export const $advancedStylesLonghands = computed(
4242
styles,
4343
settings
4444
) => {
45-
const advancedStyles: StyleMap = new Map();
45+
const advancedStyles: CssStyleMap = new Map();
4646

4747
if (instancePath === undefined) {
4848
return advancedStyles;

apps/builder/app/builder/features/style-panel/sections/advanced/add-style-input.tsx apps/builder/app/builder/shared/css-editor/add-style-input.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import {
3030
toValue,
3131
type StyleProperty,
3232
} from "@webstudio-is/css-engine";
33-
import { deleteProperty, setProperty } from "../../shared/use-style-data";
33+
import {
34+
deleteProperty,
35+
setProperty,
36+
} from "../../features/style-panel/shared/use-style-data";
3437
import { composeEventHandlers } from "~/shared/event-utils";
3538
import { parseStyleInput } from "./parse-style-input";
3639

apps/builder/app/builder/features/style-panel/sections/advanced/copy-paste-menu.tsx apps/builder/app/builder/shared/css-editor/copy-paste-menu.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ import {
1111
hyphenateProperty,
1212
mergeStyles,
1313
toValue,
14-
type StyleMap,
14+
type CssProperty,
15+
type CssStyleMap,
1516
} from "@webstudio-is/css-engine";
16-
import { useStore } from "@nanostores/react";
17-
import { $advancedStylesLonghands } from "./stores";
1817

1918
export const copyAttribute = "data-declaration";
2019

2120
export const CopyPasteMenu = ({
2221
children,
2322
properties,
23+
styleMap,
2424
onPaste,
2525
}: {
2626
children: ReactNode;
2727
properties: Array<string>;
28+
styleMap: CssStyleMap;
2829
onPaste: (cssText: string) => void;
2930
}) => {
30-
const advancedStylesLonghands = useStore($advancedStylesLonghands);
3131
const lastClickedProperty = useRef<string>();
3232

3333
const handlePaste = () => {
@@ -37,8 +37,8 @@ export const CopyPasteMenu = ({
3737
const handleCopyAll = () => {
3838
// We want to only copy properties that are currently in front of the user.
3939
// That includes search or any future filters.
40-
const currentStyleMap: StyleMap = new Map();
41-
for (const [property, value] of advancedStylesLonghands) {
40+
const currentStyleMap: CssStyleMap = new Map();
41+
for (const [property, value] of styleMap) {
4242
const isEmpty = toValue(value) === "";
4343
if (properties.includes(property) && isEmpty === false) {
4444
currentStyleMap.set(hyphenateProperty(property), value);
@@ -55,7 +55,7 @@ export const CopyPasteMenu = ({
5555
if (property === undefined) {
5656
return;
5757
}
58-
const value = advancedStylesLonghands.get(property);
58+
const value = styleMap.get(property as CssProperty);
5959

6060
if (value === undefined) {
6161
return;

apps/builder/app/builder/features/style-panel/sections/advanced/advanced.stories.tsx apps/builder/app/builder/shared/css-editor/css-editor.stories.tsx

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { getStyleDeclKey, type StyleDecl } from "@webstudio-is/sdk";
22
import { registerContainers } from "~/shared/sync";
3-
import { Section } from "./advanced";
3+
import { CssEditor as CssEditorComponent } from "./css-editor";
44
import {
55
$breakpoints,
66
$instances,
77
$selectedBreakpointId,
88
$styles,
99
$styleSourceSelections,
1010
} from "~/shared/nano-states";
11-
import { setProperty } from "../../shared/use-style-data";
11+
import { setProperty } from "../../features/style-panel/shared/use-style-data";
1212
import { $awareness } from "~/shared/awareness";
13+
import type { CssStyleMap } from "@webstudio-is/css-engine";
1314

1415
const backgroundImage: StyleDecl = {
1516
breakpointId: "base",
@@ -42,11 +43,24 @@ setProperty("accentColor")({ type: "keyword", value: "red" });
4243
setProperty("alignContent")({ type: "keyword", value: "normal" });
4344
setProperty("opacity")({ type: "unit", unit: "number", value: 11.2 });
4445

45-
export const Advanced = () => {
46-
return <Section />;
46+
export const CssEditor = () => {
47+
const styleMap: CssStyleMap = new Map([
48+
["background-image", backgroundImage.value],
49+
["accent-color", { type: "keyword", value: "red" }],
50+
["align-content", { type: "keyword", value: "normal" }],
51+
["opacity", { type: "unit", unit: "number", value: 11.2 }],
52+
]);
53+
return (
54+
<CssEditorComponent
55+
styleMap={styleMap}
56+
onDeleteProperty={() => undefined}
57+
onSetProperty={() => () => undefined}
58+
onAddProperties={() => undefined}
59+
/>
60+
);
4761
};
4862

4963
export default {
50-
title: "Style Panel/Advanced",
51-
component: Advanced,
64+
title: "Style Panel",
65+
component: CssEditor,
5266
};

0 commit comments

Comments
 (0)