Skip to content

Conversation

rezrah
Copy link
Collaborator

@rezrah rezrah commented Jul 30, 2025

Summary

Resolves https://github.com/github/primer/issues/5353

Fixes a bug where AnchorNav didn't compensate for lost height when it becomes sticky. This caused noticeably jumpy layout shift underneath the sticky element.

List of notable changes:

  • Adds an equivalent height spacer element underneath the sticky nav element, which compensates for lost height. Only rendered in when nav is sticky.
  • Fixes two broken stories where AnchorNav was too close to top of page to work correctly. Added some top offset.
  • Added a unit test, and relevant helpers for testing related features in future
    • Reorganized the test file, as it had nested describe blocks and similarly named test cases

What should reviewers focus on?

Steps to test:

  1. Compare preview environement to prod

Supporting resources (related issues, external links, etc):

Contributor checklist:

  • All new and existing CI checks pass
  • Tests prove that the feature works and covers both happy and unhappy paths
  • Any drop in coverage, breaking changes or regressions have been documented above
  • UI Changes contain new visual snapshots (generated by adding update snapshots label to the PR)
  • All developer debugging and non-functional logging has been removed
  • Related issues have been referenced in the PR description

Reviewer checklist:

  • Check that pull request and proposed changes adhere to our contribution guidelines and code of conduct
  • Check that tests prove the feature works and covers both happy and unhappy paths
  • Check that there aren't other open Pull Requests for the same update/change

Screenshots:

Fixed in Storybook:

Before After
Screen.Recording.2025-07-30.at.13.43.13.mov
Screen.Recording.2025-07-30.at.13.42.46.mov

Fixed in Dotcom:

Before After*
Screen.Recording.2025-07-31.at.16.09.04.mov
Screen.Recording.2025-07-31.at.16.08.33.mov

Note

In dotcom there is an additional x-axis shift when anchor nav becomes sticky. This is because the AnchorNav is wrapped in a Section component, which includes a max-width that is no longer applied when the nav becomes stuck. AnchorNav isn't intended to be used inside a grid or width-constrained container. Once that's removed, it will behave as-seen in the video above. This PR won't address this. It instead fixes the bug causing vertical layout shift. cc/ @seangolob

Copy link

changeset-bot bot commented Jul 30, 2025

🦋 Changeset detected

Latest commit: 4efb5a1

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

This PR includes changesets to release 8 packages
Name Type
@primer/react-brand Patch
@primer/brand-docs Patch
@primer/brand-css Patch
@primer/brand-primitives Patch
@primer/brand-e2e Patch
@primer/brand-fonts Patch
@primer/brand-config Patch
@primer/brand-storybook Patch

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

Copy link
Contributor

github-actions bot commented Jul 30, 2025

🟢 No design token changes found

Copy link
Contributor

github-actions bot commented Jul 30, 2025

🟢 No visual differences found

Our visual comparison tests did not find any differences in the UI.

@seangolob
Copy link
Collaborator

@rezrah I have created a ticket to update our implementation in Dotcom. Thanks for flagging this!

@rezrah rezrah marked this pull request as ready for review July 31, 2025 19:27
@Copilot Copilot AI review requested due to automatic review settings July 31, 2025 19:27
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a layout shift issue in the AnchorNav component when it becomes sticky by adding a spacer element to compensate for the lost height.

  • Adds a height-compensating spacer element that appears only when the nav is sticky
  • Updates tests to cover the new spacer behavior with proper mocking of IntersectionObserver and scroll events
  • Fixes two broken stories by adding top offset to prevent the AnchorNav from being too close to the page top

Reviewed Changes

Copilot reviewed 4 out of 16 changed files in this pull request and generated 3 comments.

File Description
AnchorNav.tsx Adds navHeight state tracking and conditional spacer element to prevent layout shift
AnchorNav.test.tsx Comprehensive test setup for intersection observer and scroll behavior, plus test for spacer functionality
AnchorNav.features.stories.tsx Adds top padding to stories to fix positioning issues
tame-boxes-shout.md Changeset documenting the layout shift fix

@rezrah rezrah merged commit 826cd81 into main Aug 1, 2025
18 checks passed
@rezrah rezrah deleted the rezrah/fix-anchor-nav-sticky-bug branch August 1, 2025 08:02
@primer-css primer-css mentioned this pull request Jul 31, 2025
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