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

Add support for anchor-size() fn. #70

Merged
merged 9 commits into from
Jan 20, 2023
5 changes: 3 additions & 2 deletions src/polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ export const getPixelValue = async ({
anchorRect[axis] + anchorRect[dir] * ((percentage as number) / 100);
switch (targetProperty) {
case 'bottom':
value = (offsetParent as HTMLElement).clientHeight - value;
value = (offsetParent as HTMLElement).offsetHeight - value;
break;
case 'right':
value = (offsetParent as HTMLElement).clientWidth - value;
value = (offsetParent as HTMLElement).offsetWidth - value;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mirisuzanne I think these changes from clientWidth to offsetWidth might actually be incorrect -- and I'm also not sure if I need to worry about scaling here. This change seemed to fix one test (first test in https://github.com/web-platform-tests/wpt/blob/6cd6741bc20cd6b3ad9f95d19123b7850c696d79/css/css-anchor-position/anchor-position-inline-004.html), but it also caused other tests with borders to fail (first 2 in https://github.com/web-platform-tests/wpt/blob/7a0b0c285b91a0b1fa08da58c964b82e5cf40733/css/css-anchor-position/anchor-position-borders-001.html).

Do you know what the correct way to convert from a left to a right px value is? That's the goal here. E.g. given the distance x that would the left position to the right edge of a rectangle, how to consistently convert that to a right positioning value?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I play with this, it looks like right: 0px does not include borders for the containing block -- so I think this should remain clientWidth and clientHeight... 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, to get the right offset based on the x offset, you need: the width of the reference box, minus the x offset, minus the width of the target.

For the parent context width, the relevant box is the 'content box' (clientWidth?), but for the the target element you would need the full 'border box' including borders (offsetWidth?).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I added a hack in 2b48e06, but I'm not confident in it.

break;
}
return `${value}px`;
Expand All @@ -187,6 +187,7 @@ function position(rules: AnchorPositions) {
const root = document.documentElement;

Object.entries(rules).forEach(([targetSel, position]) => {
// @@@ This needs to be done for _every_ target element separately
const target: HTMLElement | null = document.querySelector(targetSel);

if (
Expand Down