Skip to content

Animate SegmentedControl when switching between items - #8304

Open
iansan5653 wants to merge 2 commits into
mainfrom
animate-segmented-control
Open

Animate SegmentedControl when switching between items#8304
iansan5653 wants to merge 2 commits into
mainfrom
animate-segmented-control

Conversation

@iansan5653

@iansan5653 iansan5653 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

SegmentedControl works nicely, but it would feel even nicer if there was a smooth transition when switching between items, as if it was a switch being toggled.

Until recently this would have been very challenging to implement. When the background and border are literally implemented as a background and border, there's no easy way to transition a background from one element to another one. We could use an absolutely positioned element and transition the position, but how would we know where to put it without expensive and complex JavaScript code?

Anchor positioning solves this cleanly! All we need to do is make the current element our anchor, then position the pseudo-element relative to that anchor. We can use inset with anchor() function values to get transitionable values and match the size of the underlying element perfectly. The result is simple code and a polished feel:

Screen.Recording.2026-08-13.at.5.30.53.PM.mov

Consumers don't need to make any changes to take advantage of this - they'll get the improved UX automatically.

Using @supports, there is no change in unsupported browsers - they keep the current functionality. And I've gated the motion transition behind an @media (prefers-reduced-motion: no-preference) rule to avoid disrupting/distracting motion for users who prefer to turn it off.

Changelog

New

Changed

  • Add anchor-positioning-powered CSS transitions for switching items in SegmentedControl

Removed

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

@iansan5653
iansan5653 requested a review from a team as a code owner August 13, 2026 21:33
@iansan5653
iansan5653 requested review from TylerJDev and a lite review from Copilot and removed request for Copilot August 13, 2026 21:33
@changeset-bot

changeset-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 57ffdad

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 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

Add a changeset for animating SegmentedControl transitions.
Copilot AI lite review requested due to automatic review settings August 13, 2026 21:34
@github-actions github-actions Bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Aug 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ 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.

border-style: solid;
border-width: var(--borderWidth-thin);
background-color: transparent;
transition: background-color var(--base-duration-200) var(--base-easing-easeInOut);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This ease on the background color helps avoid a slightly glitchy looking appearance when the hover background color appears over the sliding background if you move the mouse fast enough.

@github-actions
github-actions Bot requested a deployment to storybook-preview-8304 August 13, 2026 21:38 Abandoned

Copilot AI left a comment

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.

Pull request overview

This PR improves the SegmentedControl UX by adding a smooth “selection knob” transition when switching between items, using CSS Anchor Positioning (with @supports fallback) so consumers get the behavior automatically where supported.

Changes:

  • Add an anchor-positioned ::before “knob” on the SegmentedControl root that transitions its inset when selection changes (gated by prefers-reduced-motion).
  • Move selected-state visuals to the anchor-positioned knob in supporting browsers, while preserving the prior per-button styling via an @supports not fallback.
  • Add a changeset for a patch release describing the UX improvement.
Show a summary per file
File Description
packages/react/src/SegmentedControl/SegmentedControl.module.css Implements anchor-positioning-based selection knob positioning/transition with fallbacks.
.changeset/three-places-invent.md Patch changeset noting SegmentedControl animation when switching items.

Review details

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

border-style: solid;
border-width: var(--borderWidth-thin);
background-color: transparent;
transition: background-color var(--base-duration-200) var(--base-easing-easeInOut);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Meh, I don't think it's an issue to transition a background color. That's not "motion" like the actual motion is.

@primer-integration

Copy link
Copy Markdown

Integration test results from github/github-ui PR:

Passed  CI   Passed
Passed  VRT   Passed
Passed  Projects   Passed

All checks passed!

@iansan5653
iansan5653 enabled auto-merge August 14, 2026 13:59

@TylerJDev TylerJDev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very cool ✨

@iansan5653
iansan5653 disabled auto-merge August 14, 2026 18:39
@iansan5653
iansan5653 enabled auto-merge August 14, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants