Skip to content

Commit 93fc476

Browse files
authored
fix: Collapsed state was incorrectly calculated in some cases. (#4492)
## Description ref #3994 2 bugs: 1. Wrong calculation of ``` Box Box display=contents Box ``` Example before this PR https://p-882ed593-40cc-4382-b119-15c588d0ce6f-dot-main.development.webstudio.is/ <img width="1311" alt="image" src="https://github.com/user-attachments/assets/0a036ed2-e054-41e6-9771-f8a655d1be7f"> Example After this PR https://p-882ed593-40cc-4382-b119-15c588d0ce6f-dot-fix-collapse.development.webstudio.is/ <img width="1310" alt="image" src="https://github.com/user-attachments/assets/368e12a3-0b9c-480a-ba43-b37b9a160469"> 2. It was possible that Content Block was like ``` Box contents Box contents ``` what caused inability to find it position, outline and collapse attributes ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 0000) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent 71e0be8 commit 93fc476

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

apps/builder/app/canvas/collapsed.ts

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,28 @@ const getInstanceSize = (instanceId: string, tagName: HtmlTags | undefined) => {
120120

121121
const MAX_SIZE_TO_USE_OPTIMIZATION = 50;
122122

123+
const findFirstNonContentsParent = (element: Element) => {
124+
// Start with the element's parent
125+
let parent = element.parentElement;
126+
127+
// Continue traversing up until we find a non-contents parent or reach the top
128+
while (parent) {
129+
// Get the computed style of the parent
130+
const computedStyle = window.getComputedStyle(parent);
131+
132+
// Check if the display is not 'contents'
133+
if (computedStyle.display !== "contents") {
134+
return parent;
135+
}
136+
137+
// Move up to the next parent
138+
parent = parent.parentElement;
139+
}
140+
141+
// Return null if no non-contents parent is found
142+
return null;
143+
};
144+
123145
const recalculate = () => {
124146
const rootInstanceId = $selectedPage.get()?.rootInstanceId;
125147

@@ -180,20 +202,23 @@ const recalculate = () => {
180202
elementsToRecalculate.push(element);
181203
}
182204

183-
const elementPosition = window.getComputedStyle(element).position;
205+
const elementStyle = window.getComputedStyle(element);
206+
// const elementPosition = window.getComputedStyle(element).position;
207+
208+
const parentElement = findFirstNonContentsParent(element);
184209

185-
if (element.parentElement) {
210+
if (parentElement) {
186211
if (
187-
elementPosition === "absolute" ||
188-
elementPosition === "fixed" ||
189-
element.offsetParent == null
212+
elementStyle.position === "absolute" ||
213+
elementStyle.position === "fixed" ||
214+
element.offsetParent == null // collapsed or none
190215
) {
191216
parentsWithAbsoluteChildren.set(
192-
element.parentElement,
193-
parentsWithAbsoluteChildren.get(element.parentElement) ?? 0
217+
parentElement,
218+
parentsWithAbsoluteChildren.get(parentElement) ?? 0
194219
);
195220
} else {
196-
parentsWithAbsoluteChildren.set(element.parentElement, 1);
221+
parentsWithAbsoluteChildren.set(parentElement, 1);
197222
}
198223
}
199224
}

apps/builder/app/canvas/features/build-mode/block-template.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@ export const BlockTemplate = React.forwardRef<
3434
return;
3535
}
3636

37-
return <div style={{ display: "contents" }} ref={ref} {...props} />;
37+
const childrenCount = React.Children.count(props.children);
38+
39+
return (
40+
<div
41+
style={{ display: childrenCount === 0 ? "block" : "contents" }}
42+
ref={ref}
43+
{...props}
44+
/>
45+
);
3846
}) as AnyComponent;

0 commit comments

Comments
 (0)