Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: position holder offset should include margin #857

Merged
merged 7 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions integration_tests/specs/css/css-position/sticky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,35 @@ describe('Position sticky', () => {

await snapshot();
});

it('should work with margin', async() => {
const container1 = createElement('div', {
style: {
position: 'relative',
width: '100px',
height: '100px',
backgroundColor: 'lightblue'
}
});

document.body.appendChild(container1);

const container2 = createElement('div', {
style: {
position: 'sticky',
top: '100px',
left: '100px',
width: '100px',
height: '100px',
backgroundColor: 'mediumpurple',
marginTop: '40px',
marginLeft: '140px'
}
});

document.body.appendChild(container2);

await snapshot();
});

});
4 changes: 2 additions & 2 deletions kraken/lib/src/css/positioned.dart
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ class CSSPositionedLayout {
} else {
placeholderOffset = _getPlaceholderToParentOffset(child.renderPositionPlaceholder!, parent);
// Use original offset in normal flow if no top and bottom is set.
top = placeholderOffset.dy + childMarginTop;
top = placeholderOffset.dy;
}

double left;
Expand All @@ -513,7 +513,7 @@ class CSSPositionedLayout {
} else {
placeholderOffset ??= _getPlaceholderToParentOffset(child.renderPositionPlaceholder!, parent);
// Use original offset in normal flow if no left and right is set.
left = placeholderOffset.dx + childMarginLeft;
left = placeholderOffset.dx;
}

x = left;
Expand Down
5 changes: 5 additions & 0 deletions kraken/lib/src/rendering/flex.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ class RenderFlexLayout extends RenderLayoutBox {
RenderBoxModel? childRenderBoxModel;
if (child is RenderBoxModel) {
childRenderBoxModel = child;
} else if (child is RenderPositionPlaceholder) {
childRenderBoxModel = child.positioned;
}
if (childRenderBoxModel == null) {
return 0;
Expand All @@ -348,7 +350,10 @@ class RenderFlexLayout extends RenderLayoutBox {
RenderBoxModel? childRenderBoxModel;
if (child is RenderBoxModel) {
childRenderBoxModel = child;
} else if (child is RenderPositionPlaceholder) {
childRenderBoxModel = child.positioned;
}

if (childRenderBoxModel == null) {
return 0;
}
Expand Down
12 changes: 10 additions & 2 deletions kraken/lib/src/rendering/flow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -903,9 +903,17 @@ class RenderFlowLayout extends RenderLayoutBox {

double? childMarginLeft = 0;
double? childMarginTop = 0;

RenderBoxModel? childRenderBoxModel;
if (child is RenderBoxModel) {
childMarginLeft = child.renderStyle.marginLeft.computedValue;
childMarginTop = _getChildMarginTop(child);
childRenderBoxModel = child;
} else if (child is RenderPositionPlaceholder) {
childRenderBoxModel = child.positioned;
}

if (childRenderBoxModel is RenderBoxModel) {
childMarginLeft = childRenderBoxModel.renderStyle.marginLeft.computedValue;
childMarginTop = _getChildMarginTop(childRenderBoxModel);
}

// No need to add padding and border for scrolling content box.
Expand Down