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
simplify usePreviewCss
Oleg Isonenbd341cd74ca4
diff --git a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx b/apps/designer/app/canvas/features/wrapper-component/use-css.tsxindex db547b0882..ef84f5f1ed 100644--- a/apps/designer/app/canvas/features/wrapper-component/use-css.tsx+++ b/apps/designer/app/canvas/features/wrapper-component/use-css.tsx@@ -14,34 +14,17 @@ type UseCssProps = {css: CSS;};-type UpdatesReset = Array<{- property: string;- value: undefined;-}>;--const usePreviewCss = ({ instance, css }: UseCssProps) => {- const [previewCss, setPreviewCss] = useState<- StyleUpdates["updates"] | UpdatesReset- >([]);-+const usePreviewCss = (instance: Instance) => {useSubscribe(`previewStyle:${instance.id}`, ({ updates }) => {- setPreviewCss(updates);+ for (const update of updates) {+ const property = `--${toVarNamespace(instance, update.property)}`;+ if (update.value === undefined) {+ document.body.style.removeProperty(property);+ continue;+ }+ document.body.style.setProperty(property, toValue(update.value));+ }});-- // We are building a map for unsetting the ephemeral values we previously set for the preview- useEffect(() => {- const reset = previewCss.map(({ property }) => ({- property,- value: undefined,- }));- setPreviewCss(reset);- // previewCss in deps leads to an infinite loop, css is like a cache key in this case,- // as soon as `css` changes we can reset the preview- // @todo need a more correct approach than this- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [css]);-- return previewCss;};const voidElements =@@ -71,17 +54,6 @@ const defaultStyle = {};export const useCss = ({ instance, css }: UseCssProps): string => {- const previewCss = usePreviewCss({ instance, css });-- return useMemo(() => {- for (const update of previewCss) {- if (update.value === undefined) {- continue;- }- const property = `--${toVarNamespace(instance, update.property)}`;- document.body.style.setProperty(property, toValue(update.value));- }-- return createCss(css)(defaultStyle);- }, [css, previewCss]);+ usePreviewCss(instance);+ return useMemo(() => createCss(css)(defaultStyle), [css]);};diff --git a/packages/react-sdk/src/stitches/to-css.ts b/packages/react-sdk/src/stitches/to-css.tsindex e2b0433c30..ce29759db2 100644--- a/packages/react-sdk/src/stitches/to-css.ts+++ b/packages/react-sdk/src/stitches/to-css.ts@@ -37,7 +37,7 @@ export const toVarNamespace = (instance: Instance, property: string) => {return `${property}-${instance.id}`;};-export const toVarValue = (+const toVarValue = (namespace: string,value?: StyleValue,options: Options = defaultOptions