Skip to content

Replace polyfill with useAnchoredPosition#7725

Merged
TylerJDev merged 26 commits intomainfrom
tylerjdev/replace-css-anchor-positioning-polyfill
Apr 14, 2026
Merged

Replace polyfill with useAnchoredPosition#7725
TylerJDev merged 26 commits intomainfrom
tylerjdev/replace-css-anchor-positioning-polyfill

Conversation

@TylerJDev
Copy link
Copy Markdown
Member

@TylerJDev TylerJDev commented Apr 2, 2026

Part of https://github.com/github/primer/issues/6321

Closes https://github.com/github/primer/issues/6511, https://github.com/github/primer/issues/6538

Replaces the polyfill with our own JS-based anchoring as a fallback on browsers that do not support CSS anchor positioning.

Changelog

New

  • Adds additional CSS anchor positioning fallbacks for side='outside-right' and side='outside-left'.
  • Adds function to calculate offset if there's no remaining space for CSS anchor positioning to reposition to.

Changed

  • Replaces CSS anchor positioning polyfill with useAnchoredPosition hook.

Rollout strategy

  • Patch release
  • Minor release
  • Major release; if selected, include a written rollout or migration plan
  • None; if selected, include a brief description as to why

Testing & Reviewing

Merge checklist

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 2, 2026

🦋 Changeset detected

Latest commit: b92d59f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added staff Author is a staff member integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm labels Apr 2, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 2, 2026

⚠️ Action required

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Check the integration testing docs for step-by-step instructions. Or, apply the integration-tests: skipped manually label to skip these checks.

To publish a canary release for integration testing, apply the Canary Release label to this PR.

@primer
Copy link
Copy Markdown
Contributor

primer bot commented Apr 6, 2026

🤖 Lint issues have been automatically fixed and committed to this PR.

@TylerJDev TylerJDev force-pushed the tylerjdev/replace-css-anchor-positioning-polyfill branch from 1aab292 to 848c19c Compare April 6, 2026 21:35
@primer
Copy link
Copy Markdown
Contributor

primer bot commented Apr 6, 2026

🤖 Lint issues have been automatically fixed and committed to this PR.

Comment on lines +274 to +281
if (anchorElement) {
const overlayWidth = width ? parseInt(widthMap[width]) : null
const result = getDefaultPosition(anchorElement, overlayWidth)

if (result) {
currentOverlay.setAttribute('data-align', result.horizontal)
currentOverlay.style.setProperty('--anchored-overlay-anchor-offset-left', `${result.offset}px`)
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was added as a solution for https://github.com/github/primer/issues/6511. We calculate the required offset for the overlay to be where it can have the most space within the viewport.

This does mean we rely on JS + CSS, but this is a pretty lightweight way to do it. We'll only use JS when there isn't enough space for the overlay to exist, otherwise we rely fully on CSS anchor positioning

@github-actions github-actions bot temporarily deployed to storybook-preview-7725 April 13, 2026 14:34 Inactive
@github-actions github-actions bot temporarily deployed to storybook-preview-7725 April 13, 2026 15:20 Inactive
@TylerJDev TylerJDev added update snapshots 🤖 Command that updates VRT snapshots on the pull request labels Apr 13, 2026
@github-actions github-actions bot removed the update snapshots 🤖 Command that updates VRT snapshots on the pull request label Apr 13, 2026
@github-actions github-actions bot temporarily deployed to storybook-preview-7725 April 13, 2026 15:58 Inactive
@primer-integration
Copy link
Copy Markdown

👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/18469

@primer-integration
Copy link
Copy Markdown

Integration test results from github/github-ui:

Passed  CI   Passed
Running  VRT   Running
Passed  Projects   Passed

@TylerJDev TylerJDev requested a review from liuliu-dev April 13, 2026 20:05
Copy link
Copy Markdown
Contributor

@liuliu-dev liuliu-dev left a comment

Choose a reason for hiding this comment

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

I like the idea here :sparkle (the emoji selector issue here)

the offset approach to handle overflow is a smart fix!

Comment on lines +385 to +386
const leftOffset = Math.max(0, overlayWidth - rect.right + viewportMargin)
const rightOffset = Math.max(0, rect.left + overlayWidth - vw + viewportMargin)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I took a second to understand these 😅

Is leftOffset from ensuring overlayLeft >= viewportMargin, so viewportMargin - (rect.right - overlayWidth)>=0

and rightOffset from overlayRight <= vw - viewportMargin?

Could we add a short comment here explaining the formulas?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're right, it can be a bit confusing 😅 I added a comment to clarify! Thanks for the suggestion too!

@primer
Copy link
Copy Markdown
Contributor

primer bot commented Apr 14, 2026

🤖 Formatting issues have been automatically fixed and committed to this PR.

TylerJDev and others added 2 commits April 13, 2026 21:41
@github-actions github-actions bot temporarily deployed to storybook-preview-7725 April 14, 2026 01:42 Inactive
@TylerJDev
Copy link
Copy Markdown
Member Author

Can we make sure every new change is feature flagged as well?

Yup! We should be good to go!

@TylerJDev TylerJDev added the integration-tests: skipped manually Changes in this PR do not require an integration test label Apr 14, 2026
@TylerJDev TylerJDev added this pull request to the merge queue Apr 14, 2026
Merged via the queue into main with commit d7b9313 Apr 14, 2026
60 checks passed
@TylerJDev TylerJDev deleted the tylerjdev/replace-css-anchor-positioning-polyfill branch April 14, 2026 01:59
@primer primer bot mentioned this pull request Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Canary Release Apply this label when you want CI to create a canary release of the current PR integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm integration-tests: skipped manually Changes in this PR do not require an integration test staff Author is a staff member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants