Previews

No matching results.

x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!-- Size small -->
<segmented-control>
<ul aria-label="Billing duration" role="list" data-view-component="true" class="SegmentedControl--small SegmentedControl">
<li class="SegmentedControl-item" role="listitem" data-targets="segmented-control.items">
<button data-action="click:segmented-control#select" aria-current="false" type="button" data-view-component="true" class="Button--invisible Button--small Button Button--invisible-noVisuals"> <span class="Button-content">
<span class="Button-label">Monthly</span>
</span>
</button>
</li>
<li class="SegmentedControl-item SegmentedControl-item--selected" role="listitem" data-targets="segmented-control.items">
<button data-action="click:segmented-control#select" aria-current="true" type="button" data-view-component="true" class="Button--invisible Button--small Button"> <span class="Button-content">
<span class="Button-label">Yearly</span>
<span class="Button-visual Button-trailingVisual">
<span data-view-component="true" class="Label Label--accent">-8%</span>
</span>
</span>
</button>
</li>
</ul>
</segmented-control>
<!-- Size medium -->
<segmented-control>
<ul aria-label="Billing duration" role="list" data-view-component="true" class="SegmentedControl--medium SegmentedControl">
<li class="SegmentedControl-item" role="listitem" data-targets="segmented-control.items">
<button data-action="click:segmented-control#select" aria-current="false" type="button" data-view-component="true" class="Button--invisible Button--medium Button Button--invisible-noVisuals"> <span class="Button-content">
<span class="Button-label">Monthly</span>
</span>
</button>
</li>
<li class="SegmentedControl-item SegmentedControl-item--selected" role="listitem" data-targets="segmented-control.items">
<button data-action="click:segmented-control#select" aria-current="true" type="button" data-view-component="true" class="Button--invisible Button--medium Button"> <span class="Button-content">
<span class="Button-label">Yearly</span>
<span class="Button-visual Button-trailingVisual">
<span data-view-component="true" class="Label Label--accent">-8%</span>
</span>
</span>
</button>
</li>
</ul>
</segmented-control>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Size small
render(Primer::Alpha::SegmentedControl.new("aria-label": "Billing duration", size: :small)) do |component|
component.with_item(label: "Monthly")
component.with_item(label: "Yearly", selected: true) do |button|
button.with_trailing_visual_label(scheme: :accent) { "-8%" }
end
end
# Size medium
render(Primer::Alpha::SegmentedControl.new("aria-label": "Billing duration", size: :medium)) do |component|
component.with_item(label: "Monthly")
component.with_item(label: "Yearly", selected: true) do |button|
button.with_trailing_visual_label(scheme: :accent) { "-8%" }
end
end

app/components/primer/alpha/segmented_control.css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* SegmentedControl */
.SegmentedControl {
--segmentedControl-item-padding: var(--control-small-paddingBlock,0.25rem);
display: inline-flex;
list-style: none;
background-color: var(--controlTrack-bgColor-rest, var(--color-segmented-control-bg));
border-radius: var(--borderRadius-medium,0.375rem);
}
.SegmentedControl--iconOnly .Button--iconOnly.Button--small,
.SegmentedControl--iconOnly .Button--iconOnly.Button--medium {
width: 100%;
padding-inline: 0 !important;
}
/* sizes */
.SegmentedControl--small {
--segmentedControl-item-padding: var(--control-xsmall-paddingBlock,0.125rem);
}
.SegmentedControl--small .SegmentedControl-item {
height: var(--control-small-size,1.75rem);
}
.SegmentedControl--small .SegmentedControl-item .Button {
padding-inline: calc(var(--control-xsmall-paddingInline-normal,0.5rem) - var(--segmentedControl-item-padding));
}
.SegmentedControl--small.SegmentedControl--iconOnly .SegmentedControl-item {
width: var(--control-small-size,1.75rem);
}
.SegmentedControl--medium .SegmentedControl-item {
height: var(--control-medium-size,2rem);
}
.SegmentedControl--medium.SegmentedControl--iconOnly .SegmentedControl-item {
width: var(--control-medium-size,2rem);
}
/* item */
.SegmentedControl-item {
position: relative;
display: inline-flex;
justify-content: center;
border: var(--borderWidth-thin,max(1px, 0.0625rem)) solid transparent;
border-radius: var(--borderRadius-medium,0.375rem);
height: var(--control-medium-size,2rem);
padding: var(--segmentedControl-item-padding);
/* button color overrides */
}
.SegmentedControl-item .Button--invisible:hover:not(:disabled) {
background-color: var(--controlTrack-bgColor-hover, var(--color-action-list-item-default-hover-bg));
}
.SegmentedControl-item .Button--invisible:active:not(:disabled) {
background-color: var(--controlTrack-bgColor-active, var(--color-action-list-item-default-active-bg));
}
/* Selected ---------------------------------------- */
.SegmentedControl-item.SegmentedControl-item--selected {
background-color: var(--controlKnob-bgColor-rest, var(--color-segmented-control-button-bg));
border-color: var(--controlKnob-borderColor-rest, var(--color-segmented-control-button-selected-border));
}
.SegmentedControl-item.SegmentedControl-item--selected .Button {
font-weight: var(--base-text-weight-semibold,600);
}
.SegmentedControl-item.SegmentedControl-item--selected .Button:hover {
background-color: transparent;
}
.SegmentedControl-item.SegmentedControl-item--selected::before {
border-color: transparent !important;
}
.SegmentedControl-item.SegmentedControl-item--selected + .SegmentedControl-item::before {
border-color: transparent;
}
/* renders a visibly hidden "copy" of the text in bold, reserving box space for when text becomes bold on selected */
.SegmentedControl-item .Button-label[data-content]::before {
display: block;
height: 0;
font-weight: var(--base-text-weight-semibold,600);
visibility: hidden;
content: attr(data-content);
}
/* Separator lines */
.SegmentedControl-item:not(:first-child)::before {
position: absolute;
inset: 0 0 0 -1px;
margin-top: var(--control-medium-paddingBlock,0.375rem);
margin-bottom: var(--control-medium-paddingBlock,0.375rem);
content: '';
border-left: var(--borderWidth-thin,max(1px, 0.0625rem)) solid var(--borderColor-default,var(--color-border-default));
}
/* Button ----------------------------------------- */
.SegmentedControl-item .Button {
height: 100%;
width: 100%;
border: 0;
font-weight: var(--base-text-weight-normal,400);
border-radius: calc(var(--borderRadius-medium,0.375rem) - var(--segmentedControl-item-padding) / 2);
padding-inline: calc(var(--control-medium-paddingInline-normal,0.75rem) - var(--segmentedControl-item-padding));
}
.SegmentedControl-item .Button:focus-visible {
outline-offset: calc(var(--segmentedControl-item-padding) - var(--borderWidth-thin,max(1px, 0.0625rem)));
border-radius: calc(var(--borderRadius-medium,0.375rem) - var(--segmentedControl-item-padding) / 1);
}
.SegmentedControl-item .Button--invisible.Button--invisible-noVisuals .Button-label {
color: var(--button-default-fgColor-rest,var(--color-btn-text));
}
/* fullWidth */
.SegmentedControl--fullWidth {
display: flex;
}
.SegmentedControl--fullWidth .SegmentedControl-item {
flex: 1;
justify-content: center;
}

app/components/primer/alpha/segmented_control.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import {controller, targets} from '@github/catalyst'
@controller
class SegmentedControlElement extends HTMLElement {
@targets items: HTMLElement[]
connectedCallback() {
this.#updateButtonLabels()
}
select(event: Event) {
const button = event.currentTarget as HTMLButtonElement
for (const item of this.items) {
item.classList.remove('SegmentedControl-item--selected')
item.querySelector('[aria-current]')?.setAttribute('aria-current', 'false')
}
button.closest('li.SegmentedControl-item')?.classList.add('SegmentedControl-item--selected')
button.setAttribute('aria-current', 'true')
}
// Updates the button labels to have a data-content attribute with the text
// This is for selection styling to avoid the text jumping. It only needs to be
// setup when the component is first loaded.
#updateButtonLabels() {
for (const label of this.querySelectorAll('.Button-label')) {
label.setAttribute('data-content', label.textContent || '')
}
}
}
declare global {
interface Window {
SegmentedControlElement: typeof SegmentedControlElement
}
}
if (!window.customElements.get('segmented-control')) {
window.SegmentedControlElement = SegmentedControlElement
window.customElements.define('segmented-control', SegmentedControlElement)
}