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.

delete instance css rules associated with the deleted breakpoint

Oleg Isonena27e1f8530ea
diff --git a/app/canvas/shared/breakpoints.ts b/app/canvas/shared/breakpoints.ts
index f2cccf6422..0c05f3917f 100644
--- a/app/canvas/shared/breakpoints.ts
+++ b/app/canvas/shared/breakpoints.ts
@@ -5,8 +5,13 @@ import {
useSubscribe,
setBreakpoints,
} from "@webstudio-is/sdk";
-import { useBreakpoints, breakpointsContainer } from "./nano-values";
+import {
+ useBreakpoints,
+ breakpointsContainer,
+ rootInstanceContainer,
+} from "./nano-values";
import { publish } from "./pubsub";
+import { deleteCssRulesByBreakpoint } from "~/shared/css-utils";
store.register("breakpoints", breakpointsContainer);
@@ -52,12 +57,19 @@ const useBreakpointDelete = () => {
useSubscribe<"breakpointDelete", Breakpoint>(
"breakpointDelete",
(breakpoint) => {
- store.createTransaction([breakpointsContainer], (breakpoints) => {
- const index = breakpoints.findIndex(({ id }) => id == breakpoint.id);
- if (index !== -1) {
- breakpoints.splice(index, 1);
+ store.createTransaction(
+ [breakpointsContainer, rootInstanceContainer],
+ (breakpoints, rootInstance) => {
+ if (rootInstance === undefined) return;
+
+ const index = breakpoints.findIndex(({ id }) => id == breakpoint.id);
+ if (index !== -1) {
+ breakpoints.splice(index, 1);
+ }
+
+ deleteCssRulesByBreakpoint(rootInstance, breakpoint.id);
}
- });
+ );
}
);
};
diff --git a/app/shared/css-utils/__snapshots__/delete-breakpoint.test.ts.snap b/app/shared/css-utils/__snapshots__/delete-breakpoint.test.ts.snap
new file mode 100644
index 0000000000..59a74541fe
--- /dev/null
+++ b/app/shared/css-utils/__snapshots__/delete-breakpoint.test.ts.snap
@@ -0,0 +1,29 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Delete breakpoint basic 1`] = `
+Object {
+ "children": Array [
+ Object {
+ "children": Array [
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "3",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [
+ Object {
+ "breakpoint": "do not delete",
+ "style": Object {},
+ },
+ ],
+ "id": "2",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "1",
+}
+`;
diff --git a/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap b/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap
new file mode 100644
index 0000000000..59a74541fe
--- /dev/null
+++ b/app/shared/css-utils/__snapshots__/delete-css-rules-by-breakpoint.test.ts.snap
@@ -0,0 +1,29 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Delete breakpoint basic 1`] = `
+Object {
+ "children": Array [
+ Object {
+ "children": Array [
+ Object {
+ "children": Array [],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "3",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [
+ Object {
+ "breakpoint": "do not delete",
+ "style": Object {},
+ },
+ ],
+ "id": "2",
+ },
+ ],
+ "component": "Box",
+ "cssRules": Array [],
+ "id": "1",
+}
+`;
diff --git a/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts b/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts
new file mode 100644
index 0000000000..0fd4962d04
--- /dev/null
+++ b/app/shared/css-utils/delete-css-rules-by-breakpoint.test.ts
@@ -0,0 +1,30 @@
+import { type Instance } from "@webstudio-is/sdk";
+import { deleteCssRulesByBreakpoint } from "./delete-css-rules-by-breakpoint";
+
+describe("Delete breakpoint", () => {
+ test("basic", () => {
+ const instance: Instance = {
+ component: "Box",
+ id: "1",
+ cssRules: [{ breakpoint: "delete this", style: {} }],
+ children: [
+ {
+ component: "Box",
+ id: "2",
+ cssRules: [{ breakpoint: "do not delete", style: {} }],
+ children: [
+ {
+ component: "Box",
+ id: "3",
+ cssRules: [{ breakpoint: "delete this", style: {} }],
+ children: [],
+ },
+ ],
+ },
+ ],
+ };
+
+ deleteCssRulesByBreakpoint(instance, "delete this");
+ expect(instance).toMatchSnapshot();
+ });
+});
diff --git a/app/shared/css-utils/delete-css-rules-by-breakpoint.ts b/app/shared/css-utils/delete-css-rules-by-breakpoint.ts
new file mode 100644
index 0000000000..cca6aac8d4
--- /dev/null
+++ b/app/shared/css-utils/delete-css-rules-by-breakpoint.ts
@@ -0,0 +1,18 @@
+import { type Instance, type Breakpoint } from "@webstudio-is/sdk";
+
+export const deleteCssRulesByBreakpoint = (
+ instance: Instance,
+ breakpointId: Breakpoint["id"]
+) => {
+ const cssRules = [...instance.cssRules];
+ cssRules.forEach((cssRule, index) => {
+ if (cssRule.breakpoint === breakpointId) {
+ instance.cssRules.splice(index, 1);
+ }
+ });
+
+ for (const child of instance.children) {
+ if (typeof child === "string") continue;
+ deleteCssRulesByBreakpoint(child, breakpointId);
+ }
+};
diff --git a/app/shared/css-utils/index.ts b/app/shared/css-utils/index.ts
new file mode 100644
index 0000000000..5cab20bf12
--- /dev/null
+++ b/app/shared/css-utils/index.ts
@@ -0,0 +1 @@
+export * from "./delete-css-rules-by-breakpoint";