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
Merged

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

merged 9 commits into from
Jan 20, 2023

Conversation

jgerigmeyer
Copy link
Member

@netlify
Copy link

netlify bot commented Jan 11, 2023

Deploy Preview for anchor-position-wpt canceled.

Name Link
🔨 Latest commit 12d5933
🔍 Latest deploy log https://app.netlify.com/sites/anchor-position-wpt/deploys/63cacc157f74f600081127a2

@netlify
Copy link

netlify bot commented Jan 11, 2023

Deploy Preview for anchor-polyfill ready!

Name Link
🔨 Latest commit 12d5933
🔍 Latest deploy log https://app.netlify.com/sites/anchor-polyfill/deploys/63cacc151fa90a000866c331
😎 Deploy Preview https://deploy-preview-70--anchor-polyfill.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@jgerigmeyer
Copy link
Member Author

anchor-size() function

@github-actions
Copy link
Contributor

WPT report for this branch: https://anchor-size-wpt-results--anchor-position-wpt.netlify.app

src/polyfill.ts Outdated
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.

@jgerigmeyer jgerigmeyer marked this pull request as ready for review January 13, 2023 21:54
* main:
  chore(deps): Automated dependency upgrades
src/polyfill.ts Outdated
Comment on lines 127 to 129
vertical = getCSSPropertyValue(targetEl, 'writing-mode').startsWith(
'vertical-',
);
Copy link
Member

Choose a reason for hiding this comment

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

I think ideally we also handle sideways- - it's experimental, but people use it. I'm not familiar with getCSSPropertyValue(), but I assume it's getting the computed value of the property? I think that should work.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, getCSSPropertyValue() is our shorthand fn for getComputedStyle(el).getPropertyValue(prop).trim().

Comment on lines +41 to +46
case 'block':
case 'self-block':
resolved = vertical ? 'width' : 'height';
break;
case 'inline':
case 'self-inline':
Copy link
Member

Choose a reason for hiding this comment

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

For the self- options, do we need to check the both the writing mode of the element and the parent? I'm not clear how that one works, but my guess would be we want to use whichever anchor-element axis is the target-element's block or inline?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think according to the spec it's reversed -- inline and block should use the writing mode of the element's containing block. I added a story: https://trello.com/c/KnqCnHx3/55-i-want-better-support-for-writing-modes

@jgerigmeyer
Copy link
Member Author

@sanajaved7 Ping on this review

anchorRect[axis] + anchorRect[dir] * ((percentage as number) / 100);
switch (targetProperty) {
case 'bottom': {
let offsetHeight = (offsetParent as HTMLElement).clientHeight;
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it would be good to surface this workaround for inline elements in the README (or some other docs)? I'm not an expert on clientWidth or clientHeight at all so I don't know if this approach has any drawbacks but maybe it could be useful context for users just in case.

And either way, we might want to pull out any notes or things we want to call out about our implementation throughout the polyfill into a single place for users so this could be done later too as a part of that.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sanajaved7 What do you think about the new limitations section of the readme?

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks great! 💯

Copy link
Contributor

@sanajaved7 sanajaved7 left a comment

Choose a reason for hiding this comment

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

Just had one question about docs and notes but looks good! 🎉

@jgerigmeyer jgerigmeyer merged commit 239332e into main Jan 20, 2023
@jgerigmeyer jgerigmeyer deleted the anchor-size branch January 20, 2023 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants