x
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<segmented-control> <ul aria-label="File view" role="list" data-view-component="true" class="SegmentedControl--medium SegmentedControl"> <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 Button--invisible-noVisuals"> <span class="Button-content"> <span class="Button-label">Preview</span> </span> </button> </li> <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">Raw</span> </span> </button> </li> <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">Blame</span> </span> </button> </li> </ul></segmented-control>
1
2
3
4
5
render(Primer::Alpha::SegmentedControl.new("aria-label": "File view")) do |component| component.with_item(label: "Preview", selected: true) component.with_item(label: "Raw") component.with_item(label: "Blame")end
No notes provided.
No params configured.
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* SegmentedControl */.SegmentedControl { --segmentedControl-item-padding: var(--control-small-paddingBlock); --overlay-offset: 0.5rem; display: inline-flex; list-style: none; background-color: var(--controlTrack-bgColor-rest, var(--color-segmented-control-bg)); border-color: var(--controlTrack-borderColor-rest, transparent); border-radius: var(--borderRadius-medium);}.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);}.SegmentedControl--small .SegmentedControl-item { height: var(--control-small-size); }:is(.SegmentedControl--small .SegmentedControl-item) .Button { /* stylelint-disable-next-line primer/spacing */ padding-inline: calc(var(--control-xsmall-paddingInline-normal) - var(--segmentedControl-item-padding)); }.SegmentedControl--small.SegmentedControl--iconOnly .SegmentedControl-item { width: var(--control-small-size); }.SegmentedControl--medium .SegmentedControl-item { height: var(--control-medium-size); }.SegmentedControl--medium.SegmentedControl--iconOnly .SegmentedControl-item { width: var(--control-medium-size); }/* item */.SegmentedControl-item { position: relative; display: inline-flex; height: var(--control-medium-size); /* stylelint-disable-next-line primer/spacing */ padding: var(--segmentedControl-item-padding); border: var(--borderWidth-thin) solid transparent; border-radius: var(--borderRadius-medium); justify-content: center; /* button color overrides */}:is(.SegmentedControl-item .Button--invisible):hover:not(:disabled) { background-color: var(--controlTrack-bgColor-hover, var(--color-action-list-item-default-hover-bg)); }:is(.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); }:is(.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); visibility: hidden; content: attr(data-content); }/* Separator lines */.SegmentedControl-item:not(:first-child)::before { position: absolute; inset: 0 0 0 -1px; /* stylelint-disable-next-line primer/spacing */ margin-top: var(--control-medium-paddingBlock); /* stylelint-disable-next-line primer/spacing */ margin-bottom: var(--control-medium-paddingBlock); content: ''; border-left: var(--borderWidth-thin) solid var(--borderColor-default); }/* Button ----------------------------------------- */.SegmentedControl-item .Button { width: 100%; min-width: -moz-fit-content; min-width: fit-content; height: 100%; font-weight: var(--base-text-weight-normal); border: 0; /* stylelint-disable-next-line primer/borders */ border-radius: calc(var(--borderRadius-medium) - var(--segmentedControl-item-padding) / 2); /* stylelint-disable-next-line primer/spacing */ padding-inline: calc(var(--control-medium-paddingInline-normal) - var(--segmentedControl-item-padding)); }:is(.SegmentedControl-item .Button):focus-visible { /* stylelint-disable-next-line primer/borders */ border-radius: calc(var(--borderRadius-medium) - var(--segmentedControl-item-padding) / 1); outline-offset: calc(var(--segmentedControl-item-padding) - var(--borderWidth-thin)); }.SegmentedControl-item .Button--invisible.Button--invisible-noVisuals .Button-label { color: var(--button-default-fgColor-rest); }.SegmentedControl-item .Button-content { flex: 1 1 auto; align-self: stretch; }/* use ellipsis with the assumption that icon only variant will be used when not enough space is available */.SegmentedControl-item .Button-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }/* 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'@controllerclass 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)}