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
<toggle-switch src="/view-components/rails-app/toggle_switch" data-csrf="-SqJ6YQuRKrrGQ5TAFPm4He85D2JXjr_FAPhtlipRoMTW3Te8JOVGfpPXw74tZ9_ZmqaYVyBqxQkXVPKg3oLzA" data-view-component="true" class="ToggleSwitch"><span class="ToggleSwitch-statusIcon"> <svg hidden="hidden" data-target="toggle-switch.errorIcon" aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert-fill color-fg-danger"> <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575ZM8 5a.75.75 0 0 0-.75.75v2.5a.75.75 0 0 0 1.5 0v-2.5A.75.75 0 0 0 8 5Zm1 6a1 1 0 1 0-2 0 1 1 0 0 0 2 0Z"></path> </svg> <span hidden="hidden" data-target="toggle-switch.loadingSpinner" data-view-component="true"> <svg style="box-sizing: content-box; color: var(--color-icon-primary);" width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true" data-view-component="true" class="anim-rotate"> <circle cx="8" cy="8" r="7" stroke="currentColor" stroke-opacity="0.25" stroke-width="2" vector-effect="non-scaling-stroke" fill="none" /> <path d="M15 8a7.002 7.002 0 00-7-7" stroke="currentColor" stroke-width="2" stroke-linecap="round" vector-effect="non-scaling-stroke" /> </svg> <span class="sr-only">Loading</span> </span></span><span aria-hidden="true" data-view-component="true" class="ToggleSwitch-status"> <div data-view-component="true" class="ToggleSwitch-statusOn">On</div> <div data-view-component="true" class="ToggleSwitch-statusOff">Off</div></span><button data-target="toggle-switch.switch" data-action="click:toggle-switch#toggle" aria-pressed="false" data-view-component="true" class="ToggleSwitch-track"> <div aria-hidden="true" data-view-component="true" class="ToggleSwitch-icons"> <div data-view-component="true" class="ToggleSwitch-lineIcon"> <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-view-component="true"> <path fill-rule="evenodd" d="M8 2a.75.75 0 0 1 .75.75v11.5a.75.75 0 0 1-1.5 0V2.75A.75.75 0 0 1 8 2Z" /> </svg></div> <div data-view-component="true" class="ToggleSwitch-circleIcon"> <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-view-component="true"> <path fill-rule="evenodd" d="M8 12.5a4.5 4.5 0 1 0 0-9 4.5 4.5 0 0 0 0 9ZM8 14A6 6 0 1 0 8 2a6 6 0 0 0 0 12Z" /> </svg></div> </div> <div data-view-component="true" class="ToggleSwitch-knob"></div></button></toggle-switch>
1
render(Primer::Alpha::ToggleSwitch.new(src: UrlHelpers.toggle_switch_index_path))
No notes provided.
No params configured.
app/components/primer/alpha/toggle_switch.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* ToggleSwitch *//* Catalyst in dotcom applies a display: block to all web component elements. This** rule overrides it so the status label and toggle switch are laid out correctly. */.ToggleSwitch.ToggleSwitch { display: inline-flex;}.ToggleSwitch { align-items: center; display: inline-flex; gap: var(--controlStack-medium-gap-condensed);}.ToggleSwitch--checked .ToggleSwitch-statusOn { height: auto; visibility: visible; }.ToggleSwitch--checked .ToggleSwitch-statusOff { height: 0; visibility: hidden; }.ToggleSwitch-track { position: relative; display: block; width: var(--base-size-64); height: var(--control-medium-size); padding: 0; overflow: hidden; -webkit-text-decoration: none; text-decoration: none; cursor: pointer; -webkit-user-select: none; user-select: none; background-color: var(--controlTrack-bgColor-rest); border: var(--borderWidth-thin) solid var(--controlTrack-borderColor-rest); border-radius: var(--borderRadius-medium); transition-timing-function: cubic-bezier(0.5, 1, 0.89, 1); transition-duration: 80ms; transition-property: background-color, border-color; -webkit-appearance: none; appearance: none;}.ToggleSwitch-track:focus,.ToggleSwitch-track:focus-visible { outline-offset: 1px; }.ToggleSwitch-track:hover { background-color: var(--controlTrack-bgColor-hover); }.ToggleSwitch-track:active { background-color: var(--controlTrack-bgColor-active); }@media (pointer: coarse) { :is(.ToggleSwitch-track::before) { position: absolute; top: 50%; left: 50%; width: 100%; height: 100%; min-height: 44px; content: ""; transform: translateX(-50%) translateY(-50%); } }@media (prefers-reduced-motion) {.ToggleSwitch-track { transition: none;} .ToggleSwitch-track * { transition: none; } }.ToggleSwitch-track[aria-pressed='true'][disabled] { color: var(--control-checked-fgColor-disabled); background-color: var(--controlTrack-bgColor-disabled); border-color: transparent;}.ToggleSwitch-track[aria-pressed='true'] { background-color: var(--control-checked-bgColor-rest); border-color: var(--borderColor-transparent);}.ToggleSwitch-track[aria-pressed='true']:not([disabled]):hover { background-color: var(--control-checked-bgColor-hover); }.ToggleSwitch-track[aria-pressed='true']:not([disabled]):active { background-color: var(--control-checked-bgColor-active); }.ToggleSwitch-track[aria-pressed='true'] .ToggleSwitch-knob { background-color: var(--controlKnob-bgColor-checked); border-color: var(--controlKnob-borderColor-checked); transform: translateX(100%); }.ToggleSwitch-track[aria-pressed='true'] .ToggleSwitch-lineIcon { transform: translateX(0%); }.ToggleSwitch-track[aria-pressed='true'] .ToggleSwitch-circleIcon { transform: translateX(100%); }.ToggleSwitch-track[disabled] { cursor: not-allowed; background-color: var(--controlTrack-bgColor-disabled); border-color: transparent; transition-property: none;}.ToggleSwitch-track[disabled] .ToggleSwitch-knob { border-color: var(--borderColor-default); box-shadow: none; }.ToggleSwitch-track[disabled] .ToggleSwitch-lineIcon { color: var(--controlTrack-fgColor-disabled); }.ToggleSwitch-track[disabled] .ToggleSwitch-circleIcon { color: var(--controlTrack-fgColor-disabled); }.ToggleSwitch-icons { display: flex; align-items: center; width: 100%; height: 100%; overflow: hidden;}.ToggleSwitch-lineIcon { /* stylelint-disable-next-line primer/typography */ line-height: 0; color: var(--control-checked-fgColor-rest); transition-duration: 80ms; transition-property: transform; transform: translateX(-100%); flex: 1 0 50%;}.ToggleSwitch-circleIcon { /* stylelint-disable-next-line primer/typography */ line-height: 0; color: var(--controlTrack-fgColor-rest); transition-duration: 80ms; transition-property: transform; transform: translateX(0); flex: 1 0 50%;}.ToggleSwitch-knob { position: absolute; top: 0; bottom: 0; z-index: 1; width: 50%; background-color: var(--controlKnob-bgColor-rest); border: var(--borderWidth-thin) solid var(--controlKnob-borderColor-rest); border-radius: var(--borderRadius-medium); box-shadow: var(--shadow-resting-medium), var(--button-default-shadow-inset); transition-timing-function: cubic-bezier(0.5, 1, 0.89, 1); transition-duration: 80ms; transition-property: transform;}@media (prefers-reduced-motion) {.ToggleSwitch-knob { transition: none;} }.ToggleSwitch-status { position: relative; font-size: var(--text-body-size-medium); line-height: var(--text-title-lineHeight-large); color: var(--fgColor-default); text-align: right;}.ToggleSwitch-statusIcon { display: flex; width: var(--base-size-16); /* stylelint-disable-next-line primer/spacing */ margin-top: 0.063rem;}.ToggleSwitch--small .ToggleSwitch-status { font-size: var(--text-body-size-small); }.ToggleSwitch--small .ToggleSwitch-track { width: var(--base-size-48); height: var(--control-xsmall-size); }.ToggleSwitch--disabled .ToggleSwitch-status { color: var(--fgColor-muted); }.ToggleSwitch-statusOn { height: 0; visibility: hidden;}.ToggleSwitch-statusOff { height: auto; visibility: visible;}.ToggleSwitch--statusAtEnd { flex-direction: row-reverse;}.ToggleSwitch--statusAtEnd .ToggleSwitch-status { text-align: left; }
app/components/primer/alpha/toggle_switch.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import {controller, target, attr} from '@github/catalyst'@controllerclass ToggleSwitchElement extends HTMLElement { @target switch: HTMLElement @target loadingSpinner: HTMLElement @target errorIcon: HTMLElement @attr turbo = false private toggling = false get src(): string | null { const src = this.getAttribute('src') if (!src) return null const link = this.ownerDocument.createElement('a') link.href = src return link.href } get csrf(): string | null { const csrfElement = this.querySelector('[data-csrf]') return this.getAttribute('data-csrf') || (csrfElement instanceof HTMLInputElement && csrfElement.value) || null } get csrfField(): string { // the authenticity token is passed into the element and is not generated in js land return this.getAttribute('csrf-field') || 'authenticity_token' } isRemote(): boolean { return this.src != null } async toggle() { if (this.toggling) return this.toggling = true if (this.isDisabled()) { return } if (!this.isRemote()) { this.performToggle() this.toggling = false return } // toggle immediately to tell screen readers the switch was clicked this.performToggle() this.setLoadingState() try { await this.submitForm() } catch (error) { if (error instanceof Error) { // because we toggle immediately when the switch is clicked, toggle back to the // old state on failure this.setErrorState(error.message || 'An error occurred, please try again.') this.performToggle() } return } finally { this.toggling = false } this.setSuccessState() } turnOn(): void { if (this.isDisabled()) { return } this.switch.setAttribute('aria-pressed', 'true') this.classList.add('ToggleSwitch--checked') } turnOff(): void { if (this.isDisabled()) { return } this.switch.setAttribute('aria-pressed', 'false') this.classList.remove('ToggleSwitch--checked') } isOn(): boolean { return this.switch.getAttribute('aria-pressed') === 'true' } isOff(): boolean { return !this.isOn() } isDisabled(): boolean { return this.switch.getAttribute('disabled') != null } disable(): void { this.switch.setAttribute('disabled', 'disabled') } enable(): void { this.switch.removeAttribute('disabled') } private performToggle(): void { if (this.isOn()) { this.turnOff() } else { this.turnOn() } } private setLoadingState(): void { this.errorIcon.setAttribute('hidden', 'hidden') this.loadingSpinner.removeAttribute('hidden') const event = new CustomEvent('toggleSwitchLoading', {bubbles: true}) this.dispatchEvent(event) } private setSuccessState(): void { const event = new CustomEvent('toggleSwitchSuccess', {bubbles: true}) this.dispatchEvent(event) this.setFinishedState(false) } private setErrorState(message: string): void { const event = new CustomEvent('toggleSwitchError', {bubbles: true, detail: message}) this.dispatchEvent(event) this.setFinishedState(true) } private setFinishedState(error: boolean): void { if (error) { this.errorIcon.removeAttribute('hidden') } this.loadingSpinner.setAttribute('hidden', 'hidden') } private async submitForm() { const body = new FormData() if (this.csrf) { body.append(this.csrfField, this.csrf) } body.append('value', this.isOn() ? '1' : '0') if (!this.src) throw new Error('invalid src') let response const requestHeaders: {[key: string]: string} = { 'Requested-With': 'XMLHttpRequest', } if (this.turbo) { requestHeaders['Accept'] = 'text/vnd.turbo-stream.html' } try { response = await fetch(this.src, { credentials: 'same-origin', method: 'POST', headers: requestHeaders, body, }) } catch (error) { throw new Error('A network error occurred, please try again.') } if (!response.ok) { throw new Error(await response.text()) } const contentType = response.headers.get('Content-Type') if (window.Turbo && this.turbo && contentType?.startsWith('text/vnd.turbo-stream.html')) { window.Turbo.renderStreamMessage(await response.text()) } }}declare global { interface Window { ToggleSwitchElement: typeof ToggleSwitchElement Turbo: { renderStreamMessage: (message: string) => void } }}if (!window.customElements.get('toggle-switch')) { window.ToggleSwitchElement = ToggleSwitchElement window.customElements.define('toggle-switch', ToggleSwitchElement)}