Skip to content

Commit e6d47d7

Browse files
committed
fix position
1 parent fc0dae5 commit e6d47d7

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

packages/site-kit/src/lib/actions/legacy-details.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { page } from '$app/stores';
22
import { persisted } from 'svelte-persisted-store';
33
import { get } from 'svelte/store';
4+
import { fix_position } from './utils';
45

56
const show_legacy = persisted('svelte:show-legacy', true);
67

@@ -30,20 +31,13 @@ export function legacy_details(node: HTMLElement) {
3031

3132
show_legacy.set(detail.open);
3233

33-
const top = detail.getBoundingClientRect().top;
34-
35-
for (const other of details) {
36-
if (other !== detail) {
37-
other.open = detail.open;
34+
fix_position(detail, () => {
35+
for (const other of details) {
36+
if (other !== detail) {
37+
other.open = detail.open;
38+
}
3839
}
39-
}
40-
41-
const delta = detail.getBoundingClientRect().top - top;
42-
43-
if (delta !== 0) {
44-
// whichever element the user interacted with should stay in the same position
45-
(scroll_parent ?? document.body).scrollBy(0, delta);
46-
}
40+
});
4741

4842
setTimeout(() => {
4943
secondary = false;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function fix_position(element: HTMLElement, fn: Function) {
2+
let scroll_parent: HTMLElement | null = element;
3+
4+
while ((scroll_parent = scroll_parent.parentElement)) {
5+
if (/^(scroll|auto)$/.test(getComputedStyle(scroll_parent).overflowY)) {
6+
break;
7+
}
8+
}
9+
10+
const top = element.getBoundingClientRect().top;
11+
fn();
12+
const delta = element.getBoundingClientRect().top - top;
13+
14+
if (delta !== 0) {
15+
// whichever element the user interacted with should stay in the same position
16+
(scroll_parent ?? window).scrollBy(0, delta);
17+
}
18+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { afterNavigate } from '$app/navigation';
33
import type { Snippet } from 'svelte';
44
import { prefers_ts } from '../stores/prefers_ts';
5+
import { fix_position } from '../actions/utils';
56
67
let { children }: { children: Snippet } = $props();
78
@@ -19,8 +20,9 @@
1920
2021
function toggle(e: Event) {
2122
if ((e.target as HTMLElement).classList.contains('ts-toggle')) {
22-
$prefers_ts = (e.target as HTMLInputElement).checked;
23-
update();
23+
const input = e.target as HTMLInputElement;
24+
$prefers_ts = input.checked;
25+
fix_position(input, update);
2426
}
2527
}
2628

0 commit comments

Comments
 (0)