Skip to content

Commit

Permalink
fix: Collapsed state was incorrectly calculated in some cases. (#4492)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
istarkov authored Dec 1, 2024
1 parent 71e0be8 commit 93fc476
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
41 changes: 33 additions & 8 deletions apps/builder/app/canvas/collapsed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,28 @@ const getInstanceSize = (instanceId: string, tagName: HtmlTags | undefined) => {

const MAX_SIZE_TO_USE_OPTIMIZATION = 50;

const findFirstNonContentsParent = (element: Element) => {
// Start with the element's parent
let parent = element.parentElement;

// Continue traversing up until we find a non-contents parent or reach the top
while (parent) {
// Get the computed style of the parent
const computedStyle = window.getComputedStyle(parent);

// Check if the display is not 'contents'
if (computedStyle.display !== "contents") {
return parent;
}

// Move up to the next parent
parent = parent.parentElement;
}

// Return null if no non-contents parent is found
return null;
};

const recalculate = () => {
const rootInstanceId = $selectedPage.get()?.rootInstanceId;

Expand Down Expand Up @@ -180,20 +202,23 @@ const recalculate = () => {
elementsToRecalculate.push(element);
}

const elementPosition = window.getComputedStyle(element).position;
const elementStyle = window.getComputedStyle(element);
// const elementPosition = window.getComputedStyle(element).position;

const parentElement = findFirstNonContentsParent(element);

if (element.parentElement) {
if (parentElement) {
if (
elementPosition === "absolute" ||
elementPosition === "fixed" ||
element.offsetParent == null
elementStyle.position === "absolute" ||
elementStyle.position === "fixed" ||
element.offsetParent == null // collapsed or none
) {
parentsWithAbsoluteChildren.set(
element.parentElement,
parentsWithAbsoluteChildren.get(element.parentElement) ?? 0
parentElement,
parentsWithAbsoluteChildren.get(parentElement) ?? 0
);
} else {
parentsWithAbsoluteChildren.set(element.parentElement, 1);
parentsWithAbsoluteChildren.set(parentElement, 1);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion apps/builder/app/canvas/features/build-mode/block-template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ export const BlockTemplate = React.forwardRef<
return;
}

return <div style={{ display: "contents" }} ref={ref} {...props} />;
const childrenCount = React.Children.count(props.children);

return (
<div
style={{ display: childrenCount === 0 ? "block" : "contents" }}
ref={ref}
{...props}
/>
);
}) as AnyComponent;

0 comments on commit 93fc476

Please sign in to comment.