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
Inject props store to sdk (#871)
Bogdan Chadkinff049a000c47
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsxindex 9e1acd68b7..8c512da926 100644--- a/apps/designer/app/canvas/canvas.tsx+++ b/apps/designer/app/canvas/canvas.tsx@@ -1,15 +1,16 @@import { useCallback, useMemo, useState } from "react";+import { computed } from "nanostores";import { useStore } from "@nanostores/react";import store from "immerhin";import type { CanvasData } from "@webstudio-is/project";import {createElementsTree,- useAllUserProps,registerComponents,registerComponentsMeta,customComponentsMeta,setParams,type OnChangeChildren,+ setPropsByInstanceIdStore,} from "@webstudio-is/react-sdk";import { publish, useSubscribe } from "~/shared/pubsub";import { registerContainers, useCanvasStore } from "~/shared/sync";@@ -24,6 +25,7 @@ import { useManageDesignModeStyles, GlobalStyles } from "./shared/styles";import { useTrackSelectedElement } from "./shared/use-track-selected-element";import { WrapperComponentDev } from "./features/wrapper-component";import {+ propsIndexStore,rootInstanceContainer,selectedInstanceStore,useBreakpoints,@@ -147,6 +149,11 @@ type CanvasProps = {data: CanvasData;};+const propsByInstanceIdStore = computed(+ propsIndexStore,+ (propsIndex) => propsIndex.propsByInstanceId+);+export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {if (data.tree === null) {throw new Error("Tree is null");@@ -155,8 +162,9 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {const assets = useAssets(data.assets);useSetBreakpoints(data.breakpoints);useSetDesignTokens(data.designTokens);- useAllUserProps(data.props);useSetProps(data.tree.props);+ // inject props store to sdk+ setPropsByInstanceIdStore(propsByInstanceIdStore);useSetStyles(data.tree.styles);useSetRootInstance(data.tree.root);setParams(data.params ?? null);diff --git a/packages/react-sdk/src/app/custom-components/image.tsx b/packages/react-sdk/src/app/custom-components/image.tsxindex b885adc34d..55254b13a1 100644--- a/packages/react-sdk/src/app/custom-components/image.tsx+++ b/packages/react-sdk/src/app/custom-components/image.tsx@@ -6,7 +6,7 @@ import {} from "react";import { Image as WebstudioImage, loaders } from "@webstudio-is/image";import { Image as SdkImage } from "../../components/image";-import { useUserPropsAsset } from "../../user-props/use-user-props-asset";+import { usePropAsset } from "../../props";import { idAttribute } from "../../tree/wrapper-component";import { getParams } from "../params";@@ -18,11 +18,11 @@ export const Image = forwardRef<ElementRef<typeof defaultTag>, Props>((props, ref) => {const componentId = props[idAttribute] as string;- const asset = useUserPropsAsset(componentId, "src");+ const asset = usePropAsset(componentId, "src");const params = getParams();const loader = useMemo(() => {- if (asset == null) {+ if (asset === undefined) {return null;}if (asset.location === "REMOTE") {diff --git a/packages/react-sdk/src/index.ts b/packages/react-sdk/src/index.tsindex e07591e769..8ad95fd070 100644--- a/packages/react-sdk/src/index.ts+++ b/packages/react-sdk/src/index.ts@@ -1,7 +1,7 @@export * from "./css";export * from "./tree";export * from "./components";-export * from "./user-props";+export * from "./user-props/all-user-props";export * from "./pubsub";export * from "./db";export * from "./app";@@ -10,3 +10,4 @@ export {customComponentsMeta,} from "./app/custom-components";export type { MetaProps, WsComponentMeta } from "./components/component-type";+export { setPropsByInstanceIdStore } from "./props";diff --git a/packages/react-sdk/src/props.ts b/packages/react-sdk/src/props.tsnew file mode 100644index 0000000000..cc03a85445--- /dev/null+++ b/packages/react-sdk/src/props.ts@@ -0,0 +1,69 @@+import { useMemo } from "react";+import { atom, computed, type ReadableAtom } from "nanostores";+import { useStore } from "@nanostores/react";+import type { Instance, Props } from "@webstudio-is/project-build";+import type { Asset } from "@webstudio-is/asset-uploader";++type PropsByInstanceId = Map<Instance["id"], Props>;++type PropsByInstanceIdStore = ReadableAtom<PropsByInstanceId>;++let propsByInstanceIdStore: PropsByInstanceIdStore = atom(new Map());++export const setPropsByInstanceIdStore = (store: PropsByInstanceIdStore) => {+ propsByInstanceIdStore = store;+};++export const getPropsByInstanceIdStore = () => {+ return propsByInstanceIdStore;+};++export const getPropsByInstanceId = (props: Props) => {+ const propsByInstanceId: PropsByInstanceId = new Map();+ for (const prop of props) {+ let instanceProps = propsByInstanceId.get(prop.instanceId);+ if (instanceProps === undefined) {+ instanceProps = [];+ propsByInstanceId.set(prop.instanceId, instanceProps);+ }+ instanceProps.push(prop);+ }+ return propsByInstanceId;+};++// this utility is be used only for preview with static props+// so there is no need to use computed to optimize rerenders+export const useInstanceProps = (instanceId: Instance["id"]) => {+ const propsByInstanceId = useStore(propsByInstanceIdStore);+ const instanceProps = propsByInstanceId.get(instanceId);+ const instancePropsObject: Record<string, number | string | boolean> = {};+ if (instanceProps) {+ for (const prop of instanceProps) {+ if (prop.type !== "asset") {+ instancePropsObject[prop.name] = prop.value;+ }+ }+ }+ return instancePropsObject;+};++// this utility is be used for image component in both designer and preview+// so need to optimize rerenders with computed+export const usePropAsset = (instanceId: Instance["id"], name: string) => {+ const assetStore = useMemo(() => {+ return computed(propsByInstanceIdStore, (propsByInstanceId) => {+ const instanceProps = propsByInstanceId.get(instanceId);+ let asset: undefined | Asset = undefined;+ if (instanceProps) {+ for (const prop of instanceProps) {+ if (prop.type === "asset" && prop.name === name) {+ asset = prop.value;+ }+ }+ }+ return asset;+ });+ }, [instanceId, name]);+ const asset = useStore(assetStore);+ return asset;+};diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.tsindex 434106fa95..7a6ba22866 100644--- a/packages/react-sdk/src/tree/root.ts+++ b/packages/react-sdk/src/tree/root.ts@@ -1,15 +1,16 @@import type { ComponentProps } from "react";+import { atom } from "nanostores";import type { Breakpoint } from "@webstudio-is/css-data";import type { DesignToken } from "@webstudio-is/design-tokens";import type { Tree } from "@webstudio-is/project-build";import type { Asset } from "@webstudio-is/asset-uploader";-import { useAllUserProps } from "../user-props/";import type { InstanceProps } from "../db";import { createElementsTree } from "./create-elements-tree";import { WrapperComponent } from "./wrapper-component";import { registerComponents } from "../components";import { customComponents as defaultCustomComponents } from "../app/custom-components";import { setParams, type Params } from "../app/params";+import { getPropsByInstanceId, setPropsByInstanceIdStore } from "../props";export type Data = {tree: Tree | null;@@ -34,7 +35,9 @@ export const InstanceRoot = ({if (data.tree === null) {throw new Error("Tree is null");}- useAllUserProps(data.props);++ setPropsByInstanceIdStore(atom(getPropsByInstanceId(data.tree.props)));+setParams(data.params ?? null);registerComponents(customComponents);diff --git a/packages/react-sdk/src/tree/wrapper-component.tsx b/packages/react-sdk/src/tree/wrapper-component.tsxindex af6a22c41c..504e7b44cd 100644--- a/packages/react-sdk/src/tree/wrapper-component.tsx+++ b/packages/react-sdk/src/tree/wrapper-component.tsx@@ -1,7 +1,7 @@import { Fragment } from "react";import type { Instance } from "@webstudio-is/project-build";import { getComponent } from "../components";-import { useUserProps } from "../user-props/use-user-props";+import { useInstanceProps } from "../props";import type { OnChangeChildren } from "./create-elements-tree";const renderText = (text: string): Array<JSX.Element> => {@@ -39,9 +39,9 @@ export const WrapperComponent = ({children,...rest}: WrapperComponentProps) => {- const userProps = useUserProps(instance.id);+ const instanceProps = useInstanceProps(instance.id);const props = {- ...userProps,+ ...instanceProps,...rest,[idAttribute]: instance.id,[componentAttribute]: instance.component,diff --git a/packages/react-sdk/src/user-props/all-user-props.ts b/packages/react-sdk/src/user-props/all-user-props.tsindex c7d4cef36e..a16682353c 100644--- a/packages/react-sdk/src/user-props/all-user-props.ts+++ b/packages/react-sdk/src/user-props/all-user-props.ts@@ -1,26 +1,8 @@-import { useRef } from "react";import { atom } from "nanostores";-import { useStore } from "@nanostores/react";import type { Instance, PropsItem } from "@webstudio-is/project-build";-import type { InstanceProps } from "../db";export type AllUserProps = {[id: Instance["id"]]: PropsItem[];};export const allUserPropsContainer = atom<AllUserProps>({});--export const useAllUserProps = (initialUserProps?: Array<InstanceProps>) => {- // @todo ssr workaround for https://github.com/webstudio-is/webstudio-designer/issues/213- const ref = useRef(false);- if (ref.current === false && initialUserProps !== undefined) {- const propsMap: AllUserProps = {};- for (const item of initialUserProps) {- propsMap[item.instanceId] = item.props;- }- //We don't need to trigger rerender when setting the initial value- allUserPropsContainer.set(propsMap);- ref.current = true;- }- return useStore(allUserPropsContainer);-};diff --git a/packages/react-sdk/src/user-props/index.ts b/packages/react-sdk/src/user-props/index.tsdeleted file mode 100644index e8e6330a95..0000000000--- a/packages/react-sdk/src/user-props/index.ts+++ /dev/null@@ -1,2 +0,0 @@-export * from "./all-user-props";-export * from "./use-user-props";diff --git a/packages/react-sdk/src/user-props/use-user-props-asset.ts b/packages/react-sdk/src/user-props/use-user-props-asset.tsdeleted file mode 100644index 140726ed08..0000000000--- a/packages/react-sdk/src/user-props/use-user-props-asset.ts+++ /dev/null@@ -1,34 +0,0 @@-import { useMemo } from "react";-import type { Asset } from "@webstudio-is/asset-uploader";-import type { Instance, PropsItem } from "@webstudio-is/project-build";-import { useAllUserProps } from "./all-user-props";--/**- * Get asset for prop, like src on the Image component- */-export const useUserPropsAsset = (- instanceId: Instance["id"],- propName: PropsItem["name"]-): Asset | undefined => {- const allUserProps = useAllUserProps();-- const propsData = allUserProps[instanceId];- const asset = useMemo(() => {- if (propsData == null) {- return undefined;- }- const prop = propsData.find((prop) => prop.name === propName);-- if (prop == null) {- return undefined;- }-- if (prop.type === "asset") {- return prop.value;- }-- return undefined;- }, [propName, propsData]);-- return asset;-};diff --git a/packages/react-sdk/src/user-props/use-user-props.ts b/packages/react-sdk/src/user-props/use-user-props.tsdeleted file mode 100644index 172e4ffd23..0000000000--- a/packages/react-sdk/src/user-props/use-user-props.ts+++ /dev/null@@ -1,31 +0,0 @@-import { useMemo } from "react";-import type { Instance, PropsItem } from "@webstudio-is/project-build";-import { useAllUserProps } from "./all-user-props";--type UserProps = { [name: PropsItem["name"]]: string | number | boolean };--/**- * User props mapped in prop:value format,- * up to date with props panel.- */-export const useUserProps = (instanceId: Instance["id"]) => {- const allUserProps = useAllUserProps();-- const propsData = allUserProps[instanceId];- const props = useMemo(() => {- if (propsData == null) {- return {};- }- const result: UserProps = {};-- for (const userProp of propsData) {- if (userProp.type !== "asset") {- result[userProp.name] = userProp.value;- }- }-- return result;- }, [propsData]);-- return props;-};diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 177f272afe..f3029b7c5c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -5085,7 +5085,7 @@ packages:react-refresh: 0.11.0schema-utils: 3.1.1source-map: 0.7.4- webpack: 5.75.0+ webpack: [email protected]dev: true/@prisma/client/[email protected]:@@ -7364,7 +7364,7 @@ packages:typescript: 4.7.4unfetch: 4.2.0util-deprecate: 1.0.2- webpack: 5.75.0+ webpack: [email protected]dev: true/@storybook/core-client/6.5.14_hrl2l4xchpnd6hlkqocppvpxx4:@@ -8113,7 +8113,7 @@ packages:react: 17.0.2react-dom: [email protected]typescript: 4.7.4- webpack: 5.75.0+ webpack: [email protected]transitivePeerDependencies:- '@storybook/mdx2-csf'- bluebird@@ -8715,7 +8715,7 @@ packages:react-docgen-typescript: [email protected]tslib: 2.4.0typescript: 4.7.4- webpack: 5.75.0+ webpack: [email protected]transitivePeerDependencies:- supports-colordev: true