Skip to content

Commit c3ce975

Browse files
committed
fix: make TS the default without JS
It's the default when JS is enabled, by checking the checkboxes on startup. This can cause noticeable flickering on low-powered devices because the JS view is visible first. This makes the TS view visible by default without JS, causing less flickering for the majority of people (because they'll prefer TS)
1 parent 205d791 commit c3ce975

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

apps/svelte.dev/src/app.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
}
6060
}
6161

62-
if (localStorage.getItem('svelte:prefers-ts') !== 'false') {
63-
for (const node of document.querySelectorAll('.ts-toggle')) {
62+
if (localStorage.getItem('svelte:prefers-ts') === 'false') {
63+
for (const node of document.querySelectorAll('.js-toggle')) {
6464
node.checked = true;
6565
}
6666
}

packages/site-kit/src/lib/components/Text.svelte

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
afterNavigate(update);
1212
1313
function update() {
14-
const inputs = container.querySelectorAll('.ts-toggle') as NodeListOf<HTMLInputElement>;
14+
const inputs = container.querySelectorAll('.js-toggle') as NodeListOf<HTMLInputElement>;
1515
1616
for (const input of inputs) {
17-
input.checked = $prefers_ts;
17+
input.checked = !$prefers_ts;
1818
}
1919
}
2020
2121
function toggle(e: Event) {
22-
if ((e.target as HTMLElement).classList.contains('ts-toggle')) {
22+
if ((e.target as HTMLElement).classList.contains('js-toggle')) {
2323
const input = e.target as HTMLInputElement;
24-
$prefers_ts = input.checked;
24+
$prefers_ts = !input.checked;
2525
fix_position(input, update);
2626
}
2727
}
@@ -32,7 +32,7 @@
3232
.composedPath()
3333
.find((node) => (node as HTMLElement).classList.contains('code-block')) as HTMLElement;
3434
35-
const ts = !!parent.querySelector('.ts-toggle:checked');
35+
const ts = !parent.querySelector('.js-toggle:checked');
3636
const code = parent.querySelector(`pre:${ts ? 'last' : 'first'}-of-type code`) as HTMLElement;
3737
3838
let result = '';
@@ -132,13 +132,13 @@
132132
}
133133
}
134134
135-
&:has(.ts-toggle:checked) {
136-
.filename[data-ext='.js']::after {
137-
content: '.ts';
135+
&:has(.js-toggle:checked) {
136+
.filename[data-ext='.ts']::after {
137+
content: '.js';
138138
}
139139
}
140140
141-
.ts-toggle {
141+
.js-toggle {
142142
appearance: none;
143143
display: flex;
144144
align-items: center;
@@ -160,21 +160,21 @@
160160
161161
&::before {
162162
content: 'JS';
163+
opacity: 0.3;
163164
}
164165
165166
&::after {
166167
content: 'TS';
167168
border-left: none;
168-
opacity: 0.3;
169169
}
170170
171171
&:checked {
172172
&::before {
173-
opacity: 0.3;
173+
opacity: 1;
174174
}
175175
176176
&::after {
177-
opacity: 1;
177+
opacity: 0.3;
178178
}
179179
}
180180
}
@@ -236,11 +236,11 @@
236236
}
237237
}
238238
239-
&:has(.ts-toggle:checked) pre:first-of-type {
239+
&:has(.js-toggle:not(:checked)) pre:first-of-type {
240240
display: none;
241241
}
242242
243-
&:has(.ts-toggle:not(:checked)) pre:last-of-type {
243+
&:has(.js-toggle:checked) pre:last-of-type {
244244
display: none;
245245
}
246246

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export async function render_content_markdown(
171171
}
172172

173173
if (converted) {
174-
html += `<input class="ts-toggle" title="Toggle language" type="checkbox" aria-label="Toggle JS/TS">`;
174+
html += `<input class="js-toggle" title="Toggle language" type="checkbox" aria-label="Toggle JS/TS">`;
175175
}
176176

177177
if (options.copy) {

0 commit comments

Comments
 (0)