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

Set positions via CSS custom properties instead of directly on target elements. #51

Merged
merged 3 commits into from
Nov 21, 2022

Conversation

jgerigmeyer
Copy link
Member

@jgerigmeyer jgerigmeyer commented Nov 16, 2022

Description

Before, we were calculating positions and then setting them directly via targetElement.style.setProperty(...). This isn't ideal for two reasons:

  1. It ignores the cascade. What if the anchor() call from the stylesheet is overridden by a different set of styles?
  2. It doesn't allow any math outside of the anchor function, e.g. top: calc(anchor(--my-anchor 100%) - 50px);.

With the changes in this PR, we are now replacing uses of anchor() with a unique generated CSS custom property directly in the stylesheet, and then after we calculate the position value we simply assign that pixel value to the relevant CSS custom property. This way we can allow CSS itself to handle the cascade, and any relevant math.

Steps to test/reproduce

Please explain how to best reproduce the issue and/or test the changes locally (including the pages/URLs/views/states to review).

View the demo and confirm that the examples still work (including the new "used in math function" example).

Screen Shot 2022-11-16 at 4 30 08 PM

@netlify
Copy link

netlify bot commented Nov 16, 2022

Deploy Preview for anchor-polyfill ready!

Name Link
🔨 Latest commit b967a98
🔍 Latest deploy log https://app.netlify.com/sites/anchor-polyfill/deploys/637bc7d40568000009af97c2
😎 Deploy Preview https://deploy-preview-51--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

@jgerigmeyer
Copy link
Member Author

@jgerigmeyer jgerigmeyer requested review from jerivas and removed request for oluoluoxenfree November 18, 2022 18:39
@jgerigmeyer
Copy link
Member Author

@mirisuzanne @jerivas Since Sana and Olu are out, would the two of you be willing to review this PR sometime? Not urgent, of course.

Copy link
Member

@jerivas jerivas left a comment

Choose a reason for hiding this comment

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

This looks really hard but works really well 👍 Glad the polyfill now leverages the cascade

@netlify
Copy link

netlify bot commented Nov 21, 2022

Deploy Preview for anchor-position-wpt failed.

Name Link
🔨 Latest commit b967a98
🔍 Latest deploy log https://app.netlify.com/sites/anchor-position-wpt/deploys/637bc7d4ba8b800008ebd74e

Copy link
Member

@mirisuzanne mirisuzanne left a comment

Choose a reason for hiding this comment

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

Love this approach, and the functionality looks great. Nice work! I remember some discussion about shorter UUIDs in the custom props? That's not urgent, but it would look nicer.

@jgerigmeyer
Copy link
Member Author

I remember some discussion about shorter UUIDs in the custom props? That's not urgent, but it would look nicer.

Yep! That's coming in the next PR... #52

* main:
  do not warn on TS version
  chore(deps): Automated dependency upgrades
@jgerigmeyer jgerigmeyer merged commit 175414e into main Nov 21, 2022
@jgerigmeyer jgerigmeyer deleted the update-via-css branch November 21, 2022 19:04
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.

4 participants