Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/three-places-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Animate `SegmentedControl` when switching between items
34 changes: 29 additions & 5 deletions packages/react/src/SegmentedControl/SegmentedControl.module.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.SegmentedControl {
/* TODO: use primitive `control.medium.size` when it is available instead of '32px' */
--segmented-control-icon-width: 32px;
--segmented-control-outer-radius: var(--borderRadius-medium);

display: inline-flex;

Expand All @@ -12,6 +13,24 @@
background-color: var(--controlTrack-bgColor-rest);
border: var(--borderWidth-thin) solid var(--controlTrack-borderColor-rest, transparent);
border-radius: var(--borderRadius-medium);
position: relative;

@supports (anchor-name: --value) {
&::before {
position-anchor: --segmented-control-current-item;
content: '';
position: absolute;
inset: anchor(top) anchor(right) anchor(bottom) anchor(left);
background-color: var(--controlKnob-bgColor-rest);
border: var(--borderWidth-thin) solid var(--controlKnob-borderColor-rest);
/* stylelint-disable-next-line primer/borders */
border-radius: var(--segmented-control-outer-radius);

@media (prefers-reduced-motion: no-preference) {
transition: inset var(--base-duration-200) var(--base-easing-easeInOut);
}
}
}

/* Responsive full-width */
&[data-full-width='true'] {
Expand Down Expand Up @@ -209,7 +228,6 @@
/* TODO: use primitive `primer.control.medium.paddingInline.normal` when it is available */
--segmented-control-button-inner-padding: 12px;
--segmented-control-button-bg-inset: 4px;
--segmented-control-outer-radius: var(--borderRadius-medium);

width: 100%;
height: 100%;
Expand Down Expand Up @@ -294,6 +312,8 @@
border-color: transparent;
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.

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.


/*
innerRadius = outerRadius - distance/2
Expand All @@ -314,10 +334,14 @@
padding-right: var(--segmented-control-button-inner-padding);
/* stylelint-disable-next-line primer/spacing */
padding-left: var(--segmented-control-button-inner-padding);
background-color: var(--controlKnob-bgColor-rest);
border-color: var(--controlKnob-borderColor-rest);
/* stylelint-disable-next-line primer/borders */
border-radius: var(--segmented-control-outer-radius);
anchor-name: --segmented-control-current-item;

@supports not (anchor-name: --value) {
background-color: var(--controlKnob-bgColor-rest);
border-color: var(--controlKnob-borderColor-rest);
/* stylelint-disable-next-line primer/borders */
border-radius: var(--segmented-control-outer-radius);
}
}
}

Expand Down
Loading