File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed
apps/svelte.dev/src/routes/packages
packages/site-kit/src/lib/actions Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change 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 {
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 >
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 }) ` }
Original file line number Diff line number Diff line change 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 ) {
You can’t perform that action at this time.
0 commit comments