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.

Implement basic style source ui (#962)

Bogdan Chadkin0200ea2c5746
diff --git a/apps/designer/app/designer/features/style-panel/shared/style-info.ts b/apps/designer/app/designer/features/style-panel/shared/style-info.ts
index 1c59db280a..6ac5fc75bf 100644
--- a/apps/designer/app/designer/features/style-panel/shared/style-info.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/style-info.ts
@@ -8,10 +8,15 @@ import type {
} from "@webstudio-is/css-data";
import { properties } from "@webstudio-is/css-data";
import { utils } from "@webstudio-is/project";
-import type { Instance, Styles } from "@webstudio-is/project-build";
+import type {
+ Instance,
+ Styles,
+ StyleSource as StyleSourceType,
+} from "@webstudio-is/project-build";
import {
selectedInstanceBrowserStyleStore,
selectedInstanceIdStore,
+ selectedStyleSourceStore,
stylesIndexStore,
useBreakpoints,
useRootInstance,
@@ -75,13 +80,13 @@ for (const [property, value] of Object.entries(properties)) {
}
}
-export const getLocalStyle = (
- stylesByInstanceId: Map<Instance["id"], Styles>,
+const getSelectedStyle = (
+ stylesByStyleSourceId: Map<StyleSourceType["id"], Styles>,
breakpointId: string,
- instanceId: string
+ styleSourceId: string
) => {
const style: Style = {};
- const instanceStyles = stylesByInstanceId.get(instanceId);
+ const instanceStyles = stylesByStyleSourceId.get(styleSourceId);
if (instanceStyles === undefined) {
return style;
}
@@ -192,23 +197,26 @@ export const useStyleInfo = () => {
const [selectedBreakpoint] = useSelectedBreakpoint();
const selectedBreakpointId = selectedBreakpoint?.id;
const selectedInstanceId = useStore(selectedInstanceIdStore);
+ const selectedStyleSource = useStore(selectedStyleSourceStore);
+ const selectedStyleSourceId = selectedStyleSource?.id;
const browserStyle = useStore(selectedInstanceBrowserStyleStore);
const [rootInstance] = useRootInstance();
- const { stylesByInstanceId } = useStore(stylesIndexStore);
+ const { stylesByInstanceId, stylesByStyleSourceId } =
+ useStore(stylesIndexStore);
- const localStyle = useMemo(() => {
+ const selectedStyle = useMemo(() => {
if (
selectedBreakpointId === undefined ||
- selectedInstanceId === undefined
+ selectedStyleSourceId === undefined
) {
return {};
}
- return getLocalStyle(
- stylesByInstanceId,
+ return getSelectedStyle(
+ stylesByStyleSourceId,
selectedBreakpointId,
- selectedInstanceId
+ selectedStyleSourceId
);
- }, [stylesByInstanceId, selectedBreakpointId, selectedInstanceId]);
+ }, [stylesByStyleSourceId, selectedBreakpointId, selectedStyleSourceId]);
const cascadedBreakpointIds = useMemo(
() => getCascadedBreakpointIds(breakpoints, selectedBreakpointId),
@@ -256,7 +264,7 @@ export const useStyleInfo = () => {
const computed = browserStyle?.[property];
const inherited = inheritedInfo[property];
const cascaded = cascadedInfo[property];
- const local = localStyle?.[property];
+ const local = selectedStyle?.[property];
const value = local ?? cascaded?.value ?? inherited?.value ?? computed;
if (value) {
styleInfoData[property] = {
@@ -268,7 +276,7 @@ export const useStyleInfo = () => {
}
}
return styleInfoData;
- }, [browserStyle, inheritedInfo, cascadedInfo, localStyle]);
+ }, [browserStyle, inheritedInfo, cascadedInfo, selectedStyle]);
return styleInfoData;
};
diff --git a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
index 0f9fa88cf0..de92147ede 100644
--- a/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
+++ b/apps/designer/app/designer/features/style-panel/shared/use-style-data.ts
@@ -102,7 +102,9 @@ export const useStyleData = ({ selectedInstance, publish }: UseStyleData) => {
styleSourceSelections,
{
instanceId,
- values: [...selections, styleSourceId],
+ values: selections.includes(styleSourceId)
+ ? selections
+ : [...selections, styleSourceId],
},
(styleSourceSelection) =>
styleSourceSelection.instanceId === instanceId
diff --git a/apps/designer/app/designer/features/style-panel/style-panel.tsx b/apps/designer/app/designer/features/style-panel/style-panel.tsx
index c4fd5c7121..696286c045 100644
--- a/apps/designer/app/designer/features/style-panel/style-panel.tsx
+++ b/apps/designer/app/designer/features/style-panel/style-panel.tsx
@@ -1,20 +1,20 @@
-import type { Publish } from "~/shared/pubsub";
-import { willRender } from "~/designer/shared/breakpoints";
+import { isFeatureEnabled } from "@webstudio-is/feature-flags";
import {
+ theme,
Box,
Card,
DeprecatedParagraph,
- SearchField,
} from "@webstudio-is/design-system";
import type { Instance } from "@webstudio-is/project-build";
+import type { Publish } from "~/shared/pubsub";
+import { willRender } from "~/designer/shared/breakpoints";
import { useStyleData } from "./shared/use-style-data";
import { StyleSettings } from "./style-settings";
-import { useState } from "react";
import {
useCanvasWidth,
useSelectedBreakpoint,
} from "~/designer/shared/nano-states";
-import { theme } from "@webstudio-is/design-system";
+import { StyleSourcesSection } from "./style-source-section";
type StylePanelProps = {
publish: Publish;
@@ -30,7 +30,6 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
const [breakpoint] = useSelectedBreakpoint();
const [canvasWidth] = useCanvasWidth();
- const [search, setSearch] = useState("");
if (
currentStyle === undefined ||
@@ -57,17 +56,17 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
return (
<>
- <Box css={{ px: theme.spacing[9], py: theme.spacing[3] }}>
- <SearchField
- placeholder="Search"
- onChange={(event) => {
- setSearch(event.target.value);
+ {isFeatureEnabled("styleSourceInput") && (
+ <Box
+ css={{
+ px: theme.spacing[9],
+ pb: theme.spacing[9],
+ boxShadow: `0px 1px 0 ${theme.colors.panelOutline}`,
}}
- onCancel={() => {
- setSearch("");
- }}
- />
- </Box>
+ >
+ <StyleSourcesSection />
+ </Box>
+ )}
<Box
css={{
@@ -75,7 +74,7 @@ export const StylePanel = ({ selectedInstance, publish }: StylePanelProps) => {
}}
>
<StyleSettings
- search={search}
+ search=""
currentStyle={currentStyle}
setProperty={setProperty}
deleteProperty={deleteProperty}
diff --git a/apps/designer/app/designer/features/style-panel/style-source-section.tsx b/apps/designer/app/designer/features/style-panel/style-source-section.tsx
new file mode 100644
index 0000000000..00d2e40e3b
--- /dev/null
+++ b/apps/designer/app/designer/features/style-panel/style-source-section.tsx
@@ -0,0 +1,199 @@
+import store from "immerhin";
+import { nanoid } from "nanoid";
+import { theme, DeprecatedText2 } from "@webstudio-is/design-system";
+import type {
+ StyleSource,
+ StyleSourceSelection,
+} from "@webstudio-is/project-build";
+import { ItemState, StyleSourceInput } from "./style-source";
+import { useStore } from "@nanostores/react";
+import {
+ availableStyleSourcesStore,
+ selectedInstanceIdStore,
+ selectedInstanceStyleSourcesStore,
+ selectedStyleSourceIdStore,
+ selectedStyleSourceStore,
+ styleSourceSelectionsStore,
+ styleSourcesStore,
+} from "~/shared/nano-states";
+import {
+ removeByMutable,
+ replaceByOrAppendMutable,
+} from "~/shared/array-utils";
+
+const createStyleSource = (name: string) => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ const selectedInstanceStyleSources = selectedInstanceStyleSourcesStore.get();
+ if (selectedInstanceId === undefined) {
+ return;
+ }
+ const newStyleSource: StyleSource = {
+ type: "token",
+ id: nanoid(),
+ name,
+ };
+ const newStyleSources = [...selectedInstanceStyleSources, newStyleSource];
+ const newStyleSourceSelection: StyleSourceSelection = {
+ instanceId: selectedInstanceId,
+ values: [
+ ...selectedInstanceStyleSources.map((styleSource) => styleSource.id),
+ newStyleSource.id,
+ ],
+ };
+ store.createTransaction(
+ [styleSourcesStore, styleSourceSelectionsStore],
+ (styleSources, styleSourceSelections) => {
+ // set new style source and local if not set before
+ for (const newStyleSource of newStyleSources) {
+ replaceByOrAppendMutable(
+ styleSources,
+ newStyleSource,
+ (styleSource) => styleSource.id === newStyleSource.id
+ );
+ }
+ replaceByOrAppendMutable(
+ styleSourceSelections,
+ newStyleSourceSelection,
+ (styleSourceSelection) =>
+ styleSourceSelection.instanceId === selectedInstanceId
+ );
+ }
+ );
+};
+
+const addStyleSourceToInstace = (styleSourceId: StyleSource["id"]) => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ const selectedInstanceStyleSources = selectedInstanceStyleSourcesStore.get();
+ if (selectedInstanceId === undefined) {
+ return;
+ }
+ const newStyleSourceSelection: StyleSourceSelection = {
+ instanceId: selectedInstanceId,
+ values: [
+ ...selectedInstanceStyleSources.map((styleSource) => styleSource.id),
+ styleSourceId,
+ ],
+ };
+ store.createTransaction(
+ [styleSourcesStore, styleSourceSelectionsStore],
+ (styleSources, styleSourceSelections) => {
+ // set local style source if not set before
+ for (const newStyleSource of selectedInstanceStyleSources) {
+ replaceByOrAppendMutable(
+ styleSources,
+ newStyleSource,
+ (styleSource) => styleSource.id === newStyleSource.id
+ );
+ }
+ replaceByOrAppendMutable(
+ styleSourceSelections,
+ newStyleSourceSelection,
+ (styleSourceSelection) =>
+ styleSourceSelection.instanceId === selectedInstanceId
+ );
+ }
+ );
+};
+
+const removeStyleSourceFromInstance = (styleSourceId: StyleSource["id"]) => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ store.createTransaction(
+ [styleSourceSelectionsStore],
+ (styleSourceSelections) => {
+ const styleSourceSelection = styleSourceSelections.find(
+ (styleSourceSelection) =>
+ styleSourceSelection.instanceId === selectedInstanceId
+ );
+ if (styleSourceSelection === undefined) {
+ return;
+ }
+ removeByMutable(
+ styleSourceSelection.values,
+ (item) => item === styleSourceId
+ );
+ }
+ );
+};
+
+const reorderStyleSources = (styleSourceIds: StyleSource["id"][]) => {
+ const selectedInstanceId = selectedInstanceIdStore.get();
+ store.createTransaction(
+ [styleSourceSelectionsStore],
+ (styleSourceSelections) => {
+ const styleSourceSelection = styleSourceSelections.find(
+ (styleSourceSelection) =>
+ styleSourceSelection.instanceId === selectedInstanceId
+ );
+ if (styleSourceSelection === undefined) {
+ return;
+ }
+ styleSourceSelection.values = styleSourceIds;
+ }
+ );
+};
+
+const convertToInputItem = (
+ styleSource: StyleSource,
+ selectedStyleSource?: StyleSource["id"]
+) => {
+ const state: ItemState =
+ selectedStyleSource === styleSource.id ? "selected" : "unselected";
+ if (styleSource.type === "local") {
+ return {
+ id: styleSource.id,
+ label: "Local",
+ isEditable: false,
+ state,
+ source: styleSource.type,
+ };
+ }
+ return {
+ id: styleSource.id,
+ label: styleSource.name,
+ isEditable: true,
+ state,
+ source: styleSource.type,
+ };
+};
+
+export const StyleSourcesSection = () => {
+ const availableStyleSources = useStore(availableStyleSourcesStore);
+ const selectedInstanceStyleSources = useStore(
+ selectedInstanceStyleSourcesStore
+ );
+ const selectedStyleSource = useStore(selectedStyleSourceStore);
+ const items = availableStyleSources.map((styleSource) =>
+ convertToInputItem(styleSource)
+ );
+ const value = selectedInstanceStyleSources.map((styleSource) =>
+ convertToInputItem(styleSource, selectedStyleSource?.id)
+ );
+
+ return (
+ <>
+ <DeprecatedText2 css={{ py: theme.spacing[9] }} variant="label">
+ Style Sources
+ </DeprecatedText2>
+
+ <StyleSourceInput
+ items={items}
+ value={value}
+ onCreateItem={({ label }) => {
+ createStyleSource(label);
+ }}
+ onSelectAutocompleteItem={({ id }) => {
+ addStyleSourceToInstace(id);
+ }}
+ onRemoveItem={({ id }) => {
+ removeStyleSourceFromInstance(id);
+ }}
+ onSort={(items) => {
+ reorderStyleSources(items.map((item) => item.id));
+ }}
+ onSelectItem={(selectedItem) => {
+ selectedStyleSourceIdStore.set(selectedItem?.id);
+ }}
+ />
+ </>
+ );
+};
diff --git a/apps/designer/app/shared/nano-states/nano-states.ts b/apps/designer/app/shared/nano-states/nano-states.ts
index f4201e68c2..d787392e13 100644
--- a/apps/designer/app/shared/nano-states/nano-states.ts
+++ b/apps/designer/app/shared/nano-states/nano-states.ts
@@ -117,6 +117,23 @@ export const styleSourcesIndexStore = computed(
};
}
);
+/**
+ * find all non-local style sources
+ * scoped to current tree or whole project
+ */
+export const availableStyleSourcesStore = shallowComputed(
+ [styleSourcesStore, treeIdStore],
+ (styleSources, treeId) => {
+ if (treeId === undefined) {
+ return [];
+ }
+ return styleSources.filter(
+ (styleSource) =>
+ styleSource.type !== "local" &&
+ (styleSource.treeId === treeId || styleSource.treeId === undefined)
+ );
+ }
+);
export const useSetStyleSources = (styleSources: StyleSources) => {
useSyncInitializeOnce(() => {
@@ -133,7 +150,7 @@ export const useSetStyleSourceSelections = (
});
};
-const selectedStyleSourceIdStore = atom<undefined | StyleSource["id"]>(
+export const selectedStyleSourceIdStore = atom<undefined | StyleSource["id"]>(
undefined
);
@@ -173,6 +190,7 @@ export const stylesIndexStore = computed(
}
return {
+ stylesByStyleSourceId,
stylesByInstanceId,
};
}
@@ -225,6 +243,7 @@ export const selectedInstanceStore = computed(
return instancesIndex.instancesById.get(selectedInstanceId);
}
);
+
export const selectedInstanceBrowserStyleStore = atom<undefined | Style>();
export const selectedInstanceStyleSourcesStore = computed(
@@ -242,22 +261,25 @@ export const selectedInstanceStyleSourcesStore = computed(
)?.values ?? [];
const { styleSourcesById } = styleSourcesIndex;
const selectedInstanceStyleSources: StyleSources = [];
- // generate style source when selection is empty or not exist
- // it is synchronized whenever instance style sources
- // or styles are updated
- if (styleSourceIds.length === 0 && treeId !== undefined) {
- selectedInstanceStyleSources.push({
- type: "local",
- treeId,
- id: nanoid(),
- });
- }
+ let hasLocal = false;
for (const styleSourceId of styleSourceIds) {
const styleSource = styleSourcesById.get(styleSourceId);
if (styleSource !== undefined) {
selectedInstanceStyleSources.push(styleSource);
+ if (styleSource.type === "local") {
+ hasLocal = true;
+ }
}
}
+ // generate style source when selection has not local style sources
+ // it is synchronized whenever instance style sources or styles are updated
+ if (hasLocal === false && treeId !== undefined) {
+ selectedInstanceStyleSources.push({
+ type: "local",
+ treeId,
+ id: nanoid(),
+ });
+ }
return selectedInstanceStyleSources;
}
);
diff --git a/packages/feature-flags/src/flags.ts b/packages/feature-flags/src/flags.ts
index a823008048..b3661d4ac1 100644
--- a/packages/feature-flags/src/flags.ts
+++ b/packages/feature-flags/src/flags.ts
@@ -3,3 +3,4 @@ export const example = false;
export const dark = false;
export const propertyReset = false;
export const share2 = false;
+export const styleSourceInput = false;