Skip to content

Commit f622bee

Browse files
committed
fix scroll
1 parent fe0cf69 commit f622bee

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

apps/svelte.dev/src/routes/packages/Category.svelte

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import type { Package } from '$lib/server/content';
3+
import { fix_position } from '../../../../../packages/site-kit/src/lib/actions/utils';
34
import PackageCard from './PackageCard.svelte';
45
56
interface Props {
@@ -10,13 +11,15 @@
1011
1112
let { title, description, packages }: Props = $props();
1213
14+
let header: HTMLElement;
15+
1316
const INITIAL_ITEMS = 3;
1417
let showAll = $state(false);
1518
let visiblePackages = $derived(showAll ? packages : packages.slice(0, INITIAL_ITEMS));
1619
</script>
1720

1821
<section class="category">
19-
<header>
22+
<header bind:this={header}>
2023
<h2>
2124
{title}
2225
</h2>
@@ -40,7 +43,20 @@
4043
class="raised"
4144
aria-label="Show more"
4245
aria-pressed={showAll}
43-
onclick={() => (showAll = !showAll)}><span class="icon"></span></button
46+
onclick={(e) => {
47+
const { bottom } = header.getBoundingClientRect();
48+
49+
// if the current section is wholly visible, don't muck about with the scroll position
50+
if (bottom > 0) {
51+
showAll = !showAll;
52+
return;
53+
}
54+
55+
// otherwise, keep the button in the same position
56+
fix_position(e.currentTarget, () => {
57+
showAll = !showAll;
58+
});
59+
}}><span class="icon"></span></button
4460
>
4561

4662
{showAll ? 'show less' : `show all (${packages.length})`}

packages/site-kit/src/lib/actions/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export function fix_position(element: HTMLElement, fn: Function) {
1+
import { tick } from 'svelte';
2+
3+
export async function fix_position(element: HTMLElement, fn: Function) {
24
let scroll_parent: HTMLElement | null = element;
35

46
while ((scroll_parent = scroll_parent.parentElement)) {
@@ -9,6 +11,7 @@ export function fix_position(element: HTMLElement, fn: Function) {
911

1012
const top = element.getBoundingClientRect().top;
1113
fn();
14+
await tick();
1215
const delta = element.getBoundingClientRect().top - top;
1316

1417
if (delta !== 0) {

0 commit comments

Comments
 (0)