Skip to content

Commit f9e5db5

Browse files
Segmented control list (#2195)
* Updatging SegmentedControl to a role="list" * Remove SegmentedControl-button--selected class * Stylelint auto-fixes * Create two-gifts-yawn.md Co-authored-by: Actions Auto Build <[email protected]>
1 parent 6899cba commit f9e5db5

File tree

4 files changed

+41
-27
lines changed

4 files changed

+41
-27
lines changed

.changeset/two-gifts-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@primer/css": patch
3+
---
4+
5+
Updating the SegmentedControl component to role=list and use `ul`.

docs/src/stories/components/SegmentedControl/SegmentedControl.stories.jsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ IconsAndText.args = {
7474
}
7575

7676
export const IconsOnlyTemplate = ({fullWidth, ariaLabel, iconOnlyWhenNarrow}) => (
77-
<>
78-
<segmented-control role="toolbar" aria-label={ariaLabel} class={classNames(fullWidth, iconOnlyWhenNarrow)}>
77+
<segmented-control>
78+
<ul aria-label={ariaLabel} class={classNames(fullWidth, iconOnlyWhenNarrow)}>
7979
<SegmentedControlButtonTemplate text="Outline" leadingVisual iconOnly />
8080
<SegmentedControlButtonTemplate text="Write" leadingVisual iconOnly />
8181
<SegmentedControlButtonTemplate text="Preview" leadingVisual iconOnly />
8282
<SegmentedControlButtonTemplate text="Publish" leadingVisual iconOnly selected />
83-
</segmented-control>
84-
</>
83+
</ul>
84+
</segmented-control>
8585
)
8686

8787
export const IconsOnly = IconsOnlyTemplate.bind({})

docs/src/stories/components/SegmentedControl/SegmentedControlButton.stories.jsx

+18-16
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ export default {
3333
// build every component case here in the template (private api)
3434
export const SegmentedControlButtonTemplate = ({selected, text, leadingVisual, iconOnly }) => (
3535
<>
36-
<button className={clsx(
37-
'SegmentedControl-button',
38-
iconOnly && `SegmentedControl-button--iconOnly`,
39-
selected && `SegmentedControl-button--selected`,
40-
)}
41-
aria-current={selected}
42-
aria-label={iconOnly && text}
43-
>
44-
<div class="SegmentedControl-content">
45-
{leadingVisual && (
46-
<svg class="SegmentedControl-leadingVisual octicon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.679 7.932c.412-.621 1.242-1.75 2.366-2.717C5.175 4.242 6.527 3.5 8 3.5c1.473 0 2.824.742 3.955 1.715 1.124.967 1.954 2.096 2.366 2.717a.119.119 0 010 .136c-.412.621-1.242 1.75-2.366 2.717C10.825 11.758 9.473 12.5 8 12.5c-1.473 0-2.824-.742-3.955-1.715C2.92 9.818 2.09 8.69 1.679 8.068a.119.119 0 010-.136zM8 2c-1.981 0-3.67.992-4.933 2.078C1.797 5.169.88 6.423.43 7.1a1.619 1.619 0 000 1.798c.45.678 1.367 1.932 2.637 3.024C4.329 13.008 6.019 14 8 14c1.981 0 3.67-.992 4.933-2.078 1.27-1.091 2.187-2.345 2.637-3.023a1.619 1.619 0 000-1.798c-.45-.678-1.367-1.932-2.637-3.023C11.671 2.992 9.981 2 8 2zm0 8a2 2 0 100-4 2 2 0 000 4z"></path></svg>
36+
<li>
37+
<button className={clsx(
38+
'SegmentedControl-button',
39+
iconOnly && `SegmentedControl-button--iconOnly`,
40+
selected && `SegmentedControl-button--selected`,
4741
)}
48-
{!iconOnly && (
49-
<span class="SegmentedControl-text" data-content={text}>{text}</span>
50-
)}
51-
</div>
52-
</button>
42+
aria-current={selected}
43+
aria-label={iconOnly && text}
44+
>
45+
<div class="SegmentedControl-content">
46+
{leadingVisual && (
47+
<svg class="SegmentedControl-leadingVisual octicon" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M1.679 7.932c.412-.621 1.242-1.75 2.366-2.717C5.175 4.242 6.527 3.5 8 3.5c1.473 0 2.824.742 3.955 1.715 1.124.967 1.954 2.096 2.366 2.717a.119.119 0 010 .136c-.412.621-1.242 1.75-2.366 2.717C10.825 11.758 9.473 12.5 8 12.5c-1.473 0-2.824-.742-3.955-1.715C2.92 9.818 2.09 8.69 1.679 8.068a.119.119 0 010-.136zM8 2c-1.981 0-3.67.992-4.933 2.078C1.797 5.169.88 6.423.43 7.1a1.619 1.619 0 000 1.798c.45.678 1.367 1.932 2.637 3.024C4.329 13.008 6.019 14 8 14c1.981 0 3.67-.992 4.933-2.078 1.27-1.091 2.187-2.345 2.637-3.023a1.619 1.619 0 000-1.798c-.45-.678-1.367-1.932-2.637-3.023C11.671 2.992 9.981 2 8 2zm0 8a2 2 0 100-4 2 2 0 000 4z"></path></svg>
48+
)}
49+
50+
<span class="SegmentedControl-text" data-content={text}>{text}</span>
51+
)}
52+
</div>
53+
</button>
54+
</li>
5355
</>
5456
)
5557

src/segmented-control/segmented-control.scss

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
// SegmentedControl
2+
// stylelint-disable selector-max-type
23

34
.SegmentedControl {
4-
position: relative;
55
display: inline-flex;
6+
list-style: none;
67
background-color: var(--color-segmented-control-bg);
78
// stylelint-disable-next-line primer/borders
89
border-radius: var(--primer-borderRadius-medium, $border-radius);
910
// stylelint-disable-next-line primer/box-shadow
1011
box-shadow: var(--primer-borderInset-thin, inset 0 0 0 $border-width) var(--color-border-default);
12+
13+
li {
14+
position: relative;
15+
display: inline-flex;
16+
}
1117
}
1218

1319
// Button -----------------------------------------
@@ -27,19 +33,19 @@
2733
// stylelint-disable-next-line primer/borders
2834
border-radius: var(--primer-borderRadius-medium, $border-radius);
2935

30-
&:not(.SegmentedControl-button--selected):hover .SegmentedControl-content {
36+
&[aria-current='false']:hover .SegmentedControl-content {
3137
background-color: var(--color-segmented-control-button-hover-bg);
3238
transition-duration: var(--primer-duration-fast, 80ms);
3339
}
3440

35-
&:not(.SegmentedControl-button--selected):active .SegmentedControl-content {
41+
&[aria-current='false']:active .SegmentedControl-content {
3642
background-color: var(--color-segmented-control-button-active-bg);
3743
transition-duration: 0;
3844
}
3945

4046
// Selected
4147

42-
&.SegmentedControl-button--selected {
48+
&[aria-current='true'] {
4349
// stylelint-disable-next-line primer/typography
4450
font-weight: var(--base-text-weight-semibold, $font-weight-bold);
4551
background-color: var(--color-btn-bg);
@@ -61,8 +67,8 @@
6167
transition: border-color var(--primer-duration-medium, 160ms) cubic-bezier(0.3, 0.1, 0.5, 1);
6268
}
6369

64-
&.SegmentedControl-button--selected::before,
65-
&.SegmentedControl-button--selected + .SegmentedControl-button::before {
70+
&[aria-current='true']::before,
71+
&[aria-current='true'] + .SegmentedControl-button::before {
6672
border-color: transparent;
6773
}
6874
}
@@ -110,7 +116,8 @@
110116
.SegmentedControl--fullWidth {
111117
display: flex;
112118

113-
.SegmentedControl-button {
119+
.SegmentedControl-button,
120+
li {
114121
flex: 1;
115122
justify-content: center;
116123
}

0 commit comments

Comments
 (0)