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
Css preview (#1740)
Oleg Isonenb19142881b5c
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsxindex 6462256372..532166f79d 100644--- a/apps/builder/app/builder/builder.tsx+++ b/apps/builder/app/builder/builder.tsx@@ -12,6 +12,8 @@ import { Inspector } from "./features/inspector";import { isCanvasPointerEventsEnabledStore } from "./shared/nano-states";import { Topbar } from "./features/topbar";import builderStyles from "./builder.css";+// eslint-disable-next-line import/no-internal-modules+import prismStyles from "prismjs/themes/prism-solarizedlight.min.css";import { Footer } from "./features/footer";import {CanvasIframe,@@ -50,7 +52,10 @@ registerContainers();// Can cause FOUC because of remix-island, be very accurate adding anything hereexport const links = () => {- return [{ rel: "stylesheet", href: builderStyles }];+ return [+ { rel: "stylesheet", href: builderStyles },+ { rel: "stylesheet", href: prismStyles },+ ];};const useSetProject = (project: Project) => {diff --git a/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsx b/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsxnew file mode 100644index 0000000000..d5b517321d--- /dev/null+++ b/apps/builder/app/builder/features/sidebar-left/navigator/css-preview.tsx@@ -0,0 +1,56 @@+import {+ ScrollArea,+ css,+ textVariants,+ theme,+} from "@webstudio-is/design-system";+import { useInstanceStyleData } from "../../style-panel/shared/style-info";+import { selectedInstanceSelectorStore } from "~/shared/nano-states";+import { useStore } from "@nanostores/react";+import { createCssEngine } from "@webstudio-is/css-engine";+import { CollapsibleSection } from "~/builder/shared/collapsible-section";+import { useMemo } from "react";+import * as Prism from "prismjs";++const preStyle = css(textVariants.mono, {+ margin: 0,+ height: theme.spacing[24],+ userSelect: "text",+ cursor: "text",+});++const useHighlightedCss = () => {+ const selectedInstanceSelector = useStore(selectedInstanceSelectorStore);+ const style = useInstanceStyleData(selectedInstanceSelector);+ return useMemo(() => {+ if (Object.keys(style).length === 0) {+ return;+ }+ const cssEngine = createCssEngine();+ const rule = cssEngine.addStyleRule("instance", { style });+ const cssText = rule.styleMap.toString();+ return Prism.highlight(cssText, Prism.languages.css, "css");+ }, [style]);+};++export const CssPreview = () => {+ const code = useHighlightedCss();++ if (code === undefined) {+ return null;+ }++ return (+ <CollapsibleSection label="CSS Preview" fullWidth>+ <ScrollArea verticalOnly={false} css={{ px: theme.spacing[9] }}>+ <pre tabIndex={0} className={preStyle()}>+ <code+ className="language-css"+ style={{ whiteSpace: "break-spaces" }}+ dangerouslySetInnerHTML={{ __html: code }}+ ></code>+ </pre>+ </ScrollArea>+ </CollapsibleSection>+ );+};diff --git a/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx b/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsxindex e7e20e9853..befbc99efe 100644--- a/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx+++ b/apps/builder/app/builder/features/sidebar-left/navigator/navigator.tsx@@ -1,8 +1,9 @@-import { Flex } from "@webstudio-is/design-system";+import { Flex, Separator } from "@webstudio-is/design-system";import { NavigatorTree } from "~/builder/shared/navigator-tree";import { Header, CloseButton } from "../header";import type { Publish } from "~/shared/pubsub";import { usePublishInstanceTreeShortcuts } from "~/builder/shared/shortcuts";+import { CssPreview } from "./css-preview";type NavigatorProps = {isClosable?: boolean;@@ -19,8 +20,10 @@ export const Navigator = ({ isClosable, onClose, publish }: NavigatorProps) => {title="Navigator"suffix={isClosable && <CloseButton onClick={() => onClose?.()} />}/>- <Flex css={{ flexGrow: 1, flexDirection: "column" }}>+ <Flex grow direction="column" justify="end"><NavigatorTree />+ <Separator />+ <CssPreview /></Flex></Flex>);diff --git a/apps/builder/package.json b/apps/builder/package.jsonindex 9518098c0e..1d6b40753c 100644--- a/apps/builder/package.json+++ b/apps/builder/package.json@@ -76,6 +76,7 @@"nanostores": "^0.7.1","no-case": "^3.0.4","pretty-bytes": "^6.0.0",+ "prismjs": "^1.28.0","react": "^18.2.0","react-color": "^2.19.3","react-dom": "^18.2.0",@@ -104,6 +105,7 @@"@storybook/react": "^6.5.16","@types/hyphenate-style-name": "^1.0.0","@types/lodash.debounce": "^4.0.6",+ "@types/prismjs": "^1.26.0","@types/react": "^18.0.35","@types/react-color": "^3.0.6","@types/react-dom": "^18.0.11",diff --git a/packages/css-engine/src/core/css-engine.test.ts b/packages/css-engine/src/core/css-engine.test.tsindex f7d54876ab..5db1f50beb 100644--- a/packages/css-engine/src/core/css-engine.test.ts+++ b/packages/css-engine/src/core/css-engine.test.ts@@ -37,7 +37,9 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c1 { color: red }+ .c1 {+ color: red+ }}"`);});@@ -50,7 +52,9 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (max-width: 1000px) {- .c1 { color: red }+ .c1 {+ color: red+ }}"`);});@@ -63,7 +67,9 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 360px) and (max-width: 1000px) {- .c1 { color: red }+ .c1 {+ color: red+ }}"`);});@@ -75,7 +81,9 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c { display: block }+ .c {+ display: block+ }}"`);engine.addStyleRule(".c1", {@@ -84,8 +92,12 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c { display: block }- .c1 { color: red }+ .c {+ display: block+ }+ .c1 {+ color: red+ }}"`);@@ -97,11 +109,17 @@ describe("CssEngine", () => {// Default media query should allways be the first to have the lowest source order specificityexpect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c { display: block }- .c1 { color: red }+ .c {+ display: block+ }+ .c1 {+ color: red+ }}@media all and (min-width: 0px) {- .c1 { color: blue }+ .c1 {+ color: blue+ }}"`);});@@ -127,13 +145,19 @@ describe("CssEngine", () => {// Default media query should allways be the first to have the lowest source order specificityexpect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c3 { display: block }+ .c3 {+ display: block+ }}@media all and (min-width: 0px) {- .c1 { display: block }+ .c1 {+ display: block+ }}@media all and (min-width: 300px) {- .c2 { display: flex }+ .c2 {+ display: flex+ }}"`);});@@ -149,8 +173,12 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c0 { display: block }- .c1 { display: flex }+ .c0 {+ display: block+ }+ .c1 {+ display: flex+ }}"`);@@ -166,8 +194,12 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c1 { display: flex }- .c0 { display: block }+ .c1 {+ display: flex+ }+ .c0 {+ display: block+ }}"`);});@@ -183,7 +215,10 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block; color: red }+ .c {+ display: block;+ color: red+ }}"`);});@@ -198,7 +233,9 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { background-color: red }+ .c {+ background-color: red+ }}"`);});@@ -214,12 +251,18 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block; color: red }+ .c {+ display: block;+ color: red+ }+ }"+ `);+ expect(rule1.cssText).toMatchInlineSnapshot(`+ ".c {+ display: block;+ color: red}"`);- expect(rule1.cssText).toMatchInlineSnapshot(- `".c { display: block; color: red }"`- );engine.addStyleRule(".c2", {style: {...style0,@@ -229,8 +272,14 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block; color: red }- .c2 { display: block; color: green }+ .c {+ display: block;+ color: red+ }+ .c2 {+ display: block;+ color: green+ }}"`);});@@ -246,22 +295,34 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block; color: red }+ .c {+ display: block;+ color: red+ }+ }"+ `);+ expect(rule.cssText).toMatchInlineSnapshot(`+ ".c {+ display: block;+ color: red}"`);- expect(rule.cssText).toMatchInlineSnapshot(- `".c { display: block; color: red }"`- );rule.styleMap.set("color", { type: "keyword", value: "green" });- expect(rule.cssText).toMatchInlineSnapshot(- `".c { display: block; color: green }"`- );+ expect(rule.cssText).toMatchInlineSnapshot(`+ ".c {+ display: block;+ color: green+ }"+ `);expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block; color: green }+ .c {+ display: block;+ color: green+ }}"`);});@@ -276,13 +337,17 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { color: red }+ .c {+ color: red+ }}"`);engine.addMediaRule(mediaId0, { minWidth: 10 });expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 10px) {- .c { color: red }+ .c {+ color: red+ }}"`);});@@ -295,13 +360,17 @@ describe("CssEngine", () => {});expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block }+ .c {+ display: block+ }}"`);engine.addMediaRule(mediaId0, mediaRuleOptions0);expect(engine.cssText).toMatchInlineSnapshot(`"@media all and (min-width: 0px) {- .c { display: block }+ .c {+ display: block+ }}"`);});@@ -366,7 +435,9 @@ describe("CssEngine", () => {rule.styleMap.delete("display");expect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c { color: black }+ .c {+ color: black+ }}"`);});@@ -408,7 +479,9 @@ describe("CssEngine", () => {rule.styleMap.delete("display");expect(engine.cssText).toMatchInlineSnapshot(`"@media all {- .c { background-image: url(foo.png) }+ .c {+ background-image: url(foo.png)+ }}"`);});diff --git a/packages/css-engine/src/core/rules.ts b/packages/css-engine/src/core/rules.tsindex 0aa96f26ad..d632ab8b37 100644--- a/packages/css-engine/src/core/rules.ts+++ b/packages/css-engine/src/core/rules.ts@@ -6,6 +6,7 @@ class StylePropertyMap {#styleMap: Map<StyleProperty, StyleValue | undefined> = new Map();#isDirty = false;#string = "";+ #indent = 0;#transformValue?: TransformValue;onChange?: () => void;constructor(transformValue?: TransformValue) {@@ -35,20 +36,25 @@ class StylePropertyMap {this.#isDirty = true;this.onChange?.();}- toString() {- if (this.#isDirty === false) {+ toString({ indent = 0 } = {}) {+ if (this.#isDirty === false && indent === this.#indent) {return this.#string;}+ this.#indent = indent;const block: Array<string> = [];+ const spaces = " ".repeat(indent);for (const [property, value] of this.#styleMap) {if (value === undefined) {continue;}block.push(- `${toProperty(property)}: ${toValue(value, this.#transformValue)}`+ `${spaces}${toProperty(property)}: ${toValue(+ value,+ this.#transformValue+ )}`);}- this.#string = block.join("; ");+ this.#string = block.join(";\n");this.#isDirty = false;return this.#string;}@@ -75,7 +81,13 @@ export class StyleRule {this.onChange?.();};get cssText() {- return `${this.selectorText} { ${this.styleMap} }`;+ return this.toString();+ }+ toString(options = { indent: 0 }) {+ const spaces = " ".repeat(options.indent);+ return `${spaces}${this.selectorText} {\n${this.styleMap.toString({+ indent: options.indent + 2,+ })}\n${spaces}}`;}}@@ -98,12 +110,15 @@ export class MediaRule {return rule;}get cssText() {+ return this.toString();+ }+ toString() {if (this.rules.length === 0) {return "";}const rules = [];for (const rule of this.rules) {- rules.push(` ${rule.cssText}`);+ rules.push(rule.toString({ indent: 2 }));}let conditionText = "";const { minWidth, maxWidth } = this.options;@@ -125,6 +140,9 @@ export class PlaintextRule {constructor(cssText: string) {this.cssText = cssText;}+ toString() {+ return this.cssText;+ }}export type FontFaceOptions = {@@ -141,6 +159,9 @@ export class FontFaceRule {this.options = options;}get cssText() {+ return this.toString();+ }+ toString() {const decls = [];const { fontFamily, fontStyle, fontWeight, fontDisplay, src } =this.options;diff --git a/packages/css-engine/src/core/to-value.test.ts b/packages/css-engine/src/core/to-value.test.tsindex 9908ad2c72..073500a972 100644--- a/packages/css-engine/src/core/to-value.test.ts+++ b/packages/css-engine/src/core/to-value.test.ts@@ -116,7 +116,7 @@ describe("Convert WS CSS Values to native CSS strings", () => {});- expect(value).toBe("auto,10px,calc(10px),url(foo.png)");+ expect(value).toBe("auto, 10px, calc(10px), url(foo.png)");});test("tuple", () => {diff --git a/packages/css-engine/src/core/to-value.ts b/packages/css-engine/src/core/to-value.tsindex 467c4ae4c0..7af0afa333 100644--- a/packages/css-engine/src/core/to-value.ts+++ b/packages/css-engine/src/core/to-value.ts@@ -91,7 +91,9 @@ export const toValue = (}if (value.type === "layers") {- return value.value.map((value) => toValue(value, transformValue)).join(",");+ return value.value+ .map((value) => toValue(value, transformValue))+ .join(", ");}if (value.type === "tuple") {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 7441a5eb25..464afee4a5 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -224,6 +224,9 @@ importers:pretty-bytes:specifier: ^6.0.0version: 6.0.0+ prismjs:+ specifier: ^1.28.0+ version: 1.28.0react:specifier: 18.2.0version: 18.2.0@@ -303,6 +306,9 @@ importers:'@types/lodash.debounce':specifier: ^4.0.6version: 4.0.7+ '@types/prismjs':+ specifier: ^1.26.0+ version: 1.26.0'@types/react':specifier: ^18.0.35version: 18.0.35@@ -8737,6 +8743,10 @@ packages:/@types/[email protected]:resolution: {integrity: sha512-VjID5MJb1eGKthz2qUerWT8+R4b9N+CHvGCzg9fn4kWZgaF9AhdYikQio3R7wV8YY1NsQKPaCwKz1Yff+aHNUQ==}+ /@types/[email protected]:+ resolution: {integrity: sha512-ZTaqn/qSqUuAq1YwvOFQfVW1AR/oQJlLSZVustdjwI+GZ8kr0MSHBj0tsXPW1EqHubx50gtBEjbPGsdZwQwCjQ==}+ dev: true+/@types/[email protected]:resolution: {integrity: sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w==}