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 1
1
<script lang =" ts" >
2
2
import type { Package } from ' $lib/server/content' ;
3
+ import { fix_position } from ' ../../../../../packages/site-kit/src/lib/actions/utils' ;
3
4
import PackageCard from ' ./PackageCard.svelte' ;
4
5
5
6
interface Props {
10
11
11
12
let { title, description, packages }: Props = $props ();
12
13
14
+ let header: HTMLElement ;
15
+
13
16
const INITIAL_ITEMS = 3 ;
14
17
let showAll = $state (false );
15
18
let visiblePackages = $derived (showAll ? packages : packages .slice (0 , INITIAL_ITEMS ));
16
19
</script >
17
20
18
21
<section class =" category" >
19
- <header >
22
+ <header bind:this ={ header } >
20
23
<h2 >
21
24
{title }
22
25
</h2 >
40
43
class =" raised"
41
44
aria-label =" Show more"
42
45
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
44
60
>
45
61
46
62
{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 ) {
2
4
let scroll_parent : HTMLElement | null = element ;
3
5
4
6
while ( ( scroll_parent = scroll_parent . parentElement ) ) {
@@ -9,6 +11,7 @@ export function fix_position(element: HTMLElement, fn: Function) {
9
11
10
12
const top = element . getBoundingClientRect ( ) . top ;
11
13
fn ( ) ;
14
+ await tick ( ) ;
12
15
const delta = element . getBoundingClientRect ( ) . top - top ;
13
16
14
17
if ( delta !== 0 ) {
You can’t perform that action at this time.
0 commit comments