|
1 | 1 | <script lang="ts">
|
2 | 2 | import type { Package } from '$lib/server/content';
|
3 |
| - import { prefersReducedMotion } from 'svelte/motion'; |
| 3 | + import { fix_position } from '../../../../../packages/site-kit/src/lib/actions/utils'; |
4 | 4 | import PackageCard from './PackageCard.svelte';
|
5 | 5 |
|
6 | 6 | interface Props {
|
|
11 | 11 |
|
12 | 12 | let { title, description, packages }: Props = $props();
|
13 | 13 |
|
14 |
| - let content: HTMLElement; |
15 |
| - let scroller: HTMLElement; |
| 14 | + let header: HTMLElement; |
16 | 15 |
|
17 |
| - let behavior = $derived<ScrollBehavior>(prefersReducedMotion.current ? 'instant' : 'smooth'); |
18 |
| -
|
19 |
| - let at_start = $state(true); |
20 |
| - let at_end = $state(true); |
21 |
| -
|
22 |
| - function update() { |
23 |
| - at_start = scroller.scrollLeft === 0; |
24 |
| - at_end = scroller.scrollLeft + scroller.offsetWidth >= scroller.scrollWidth; |
25 |
| - } |
26 |
| -
|
27 |
| - function go(d: number) { |
28 |
| - const [a, b] = scroller.querySelectorAll('.item') as NodeListOf<HTMLElement>; |
29 |
| - const left = scroller.scrollLeft + d * (b.offsetLeft - a.offsetLeft); |
30 |
| -
|
31 |
| - scroller.scrollTo({ left, behavior }); |
32 |
| - } |
33 |
| -
|
34 |
| - $effect(update); |
| 16 | + const INITIAL_ITEMS = 3; |
| 17 | + let showAll = $state(false); |
| 18 | + let visiblePackages = $derived(showAll ? packages : packages.slice(0, INITIAL_ITEMS)); |
35 | 19 | </script>
|
36 | 20 |
|
37 |
| -<svelte:window onresize={update} /> |
38 |
| - |
39 | 21 | <section class="category">
|
40 |
| - <header> |
41 |
| - <h2> |
42 |
| - {title} |
43 |
| - </h2> |
44 |
| - |
45 |
| - {#if !at_start || !at_end} |
46 |
| - <div class="controls"> |
47 |
| - <button disabled={at_start} aria-label="Previous" class="raised icon" onclick={() => go(-1)} |
48 |
| - ></button> |
49 |
| - |
50 |
| - <button disabled={at_end} aria-label="Next" class="raised icon" onclick={() => go(1)} |
51 |
| - ></button> |
52 |
| - </div> |
53 |
| - {/if} |
54 |
| - </header> |
| 22 | + <h2 bind:this={header}> |
| 23 | + {title} |
| 24 | + </h2> |
| 25 | + |
55 | 26 | {#if description}
|
56 |
| - <h3>{@html description}</h3> |
| 27 | + <p>{@html description}</p> |
57 | 28 | {/if}
|
58 | 29 |
|
59 |
| - <div class="wrapper"> |
60 |
| - <!-- we duplicate the DOM for the sake of the gradient effect - |
61 |
| - without this, the scrollbar extends beyond the content area --> |
62 |
| - <div inert class="viewport"> |
63 |
| - <div bind:this={content} class="content"> |
64 |
| - {#each packages as pkg} |
65 |
| - <div class="item"> |
66 |
| - <PackageCard {pkg} /> |
67 |
| - </div> |
68 |
| - {/each} |
| 30 | + <div class="content"> |
| 31 | + {#each visiblePackages as pkg} |
| 32 | + <div class="item"> |
| 33 | + <PackageCard {pkg} /> |
69 | 34 | </div>
|
70 |
| - </div> |
| 35 | + {/each} |
| 36 | + </div> |
71 | 37 |
|
72 |
| - <div |
73 |
| - bind:this={scroller} |
74 |
| - class="viewport" |
75 |
| - onscroll={(e) => { |
76 |
| - const left = e.currentTarget.scrollLeft; |
77 |
| - content.style.translate = `-${left}px`; |
78 |
| - |
79 |
| - update(); |
80 |
| - }} |
81 |
| - > |
82 |
| - <div class="content"> |
83 |
| - {#each packages as pkg} |
84 |
| - <div class="item"> |
85 |
| - <PackageCard {pkg} /> |
86 |
| - </div> |
87 |
| - {/each} |
88 |
| - </div> |
| 38 | + {#if packages.length > INITIAL_ITEMS} |
| 39 | + <div class="show-more-container"> |
| 40 | + <label> |
| 41 | + <button |
| 42 | + class="raised" |
| 43 | + aria-label="Show more" |
| 44 | + aria-pressed={showAll} |
| 45 | + onclick={(e) => { |
| 46 | + const { bottom } = header.getBoundingClientRect(); |
| 47 | + |
| 48 | + // if the current section is wholly visible, don't muck about with the scroll position |
| 49 | + if (!showAll || bottom > 0) { |
| 50 | + showAll = !showAll; |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // otherwise, keep the button in the same position |
| 55 | + fix_position(e.currentTarget, () => { |
| 56 | + showAll = !showAll; |
| 57 | + }); |
| 58 | + }}><span class="icon"></span></button |
| 59 | + > |
| 60 | + |
| 61 | + {showAll ? 'show less' : `show all (${packages.length})`} |
| 62 | + </label> |
89 | 63 | </div>
|
90 |
| - </div> |
| 64 | + {/if} |
91 | 65 | </section>
|
92 | 66 |
|
93 | 67 | <style>
|
94 | 68 | .category {
|
95 |
| - --bleed: var(--sk-page-padding-side); |
96 |
| - margin-bottom: 4rem; |
| 69 | + margin-bottom: 3rem; |
97 | 70 | }
|
98 | 71 |
|
99 |
| - header { |
100 |
| - display: flex; |
101 |
| - margin-bottom: 1rem; |
102 |
| - align-items: center; |
103 |
| - gap: 2rem; |
104 |
| -
|
105 |
| - h2 { |
106 |
| - flex: 1; |
107 |
| - } |
108 |
| -
|
109 |
| - .controls { |
110 |
| - display: flex; |
111 |
| - gap: 0.5rem; |
112 |
| - } |
113 |
| -
|
114 |
| - button { |
115 |
| - background: var(--sk-bg-3); |
116 |
| -
|
117 |
| - &::after { |
118 |
| - content: ''; |
119 |
| - position: absolute; |
120 |
| - width: 100%; |
121 |
| - height: 100%; |
122 |
| - top: 0; |
123 |
| - left: 0; |
124 |
| - background: currentColor; |
125 |
| - mask: url(icons/chevron) 50% 50% no-repeat; |
126 |
| - mask-size: 2rem 2rem; |
127 |
| - } |
128 |
| -
|
129 |
| - &[aria-label='Next']::after { |
130 |
| - rotate: 180deg; |
131 |
| - } |
132 |
| -
|
133 |
| - &:disabled { |
134 |
| - background: none; |
135 |
| - } |
136 |
| - } |
| 72 | + h2 { |
| 73 | + margin: 0 0 1rem 0; |
137 | 74 | }
|
138 | 75 |
|
139 | 76 | h3 {
|
140 | 77 | font: var(--sk-font-ui-medium);
|
141 | 78 | font-size: 1.5rem;
|
142 | 79 | }
|
143 | 80 |
|
144 |
| - .wrapper { |
145 |
| - position: relative; |
146 |
| - } |
147 |
| -
|
148 |
| - .viewport { |
149 |
| - overscroll-behavior-x: contain; |
150 |
| - overscroll-behavior-y: auto; |
151 |
| - scroll-snap-type: x mandatory; |
152 |
| -
|
153 |
| - &[inert] { |
154 |
| - position: relative; |
155 |
| - margin: 0 calc(-1 * var(--bleed)); |
156 |
| - padding: 1rem var(--bleed); |
157 |
| - scroll-padding: 0 var(--bleed); |
158 |
| - overflow: hidden; |
159 |
| - filter: blur(0.5px); |
160 |
| - mask-image: linear-gradient( |
161 |
| - to right, |
162 |
| - rgb(0 0 0 / 0) 0%, |
163 |
| - rgb(0 0 0 / 0.5) var(--bleed), |
164 |
| - rgb(0 0 0 / 0) var(--bleed), |
165 |
| - rgb(0 0 0 / 0) calc(100% - var(--bleed)), |
166 |
| - rgb(0 0 0 / 0.5) calc(100% - var(--bleed)), |
167 |
| - rgb(0 0 0 / 0) 100% |
168 |
| - ); |
169 |
| - } |
170 |
| -
|
171 |
| - &:not([inert]) { |
172 |
| - position: absolute; |
173 |
| - top: 0; |
174 |
| - left: 0; |
175 |
| - width: 100%; |
176 |
| - height: 100%; |
177 |
| - overflow-x: auto; |
178 |
| - overflow-y: visible; |
179 |
| - margin: 1rem 0; |
180 |
| - } |
181 |
| - } |
182 |
| -
|
183 | 81 | .content {
|
184 | 82 | display: grid;
|
185 |
| - grid-auto-columns: 34rem; |
186 |
| - grid-auto-flow: column; |
| 83 | + grid-template-columns: 1fr; |
187 | 84 | gap: 2rem;
|
188 |
| - width: fit-content; |
| 85 | + margin-top: 1rem; |
| 86 | +
|
| 87 | + @media (min-width: 1024px) { |
| 88 | + grid-template-columns: repeat(3, 1fr); |
| 89 | + } |
189 | 90 | }
|
190 | 91 |
|
191 | 92 | .item {
|
192 | 93 | height: 16rem;
|
193 |
| - scroll-snap-align: start; |
| 94 | + min-width: 0; /* Prevents grid items from overflowing */ |
| 95 | + } |
| 96 | +
|
| 97 | + .show-more-container { |
| 98 | + display: flex; |
| 99 | + justify-content: flex-end; |
| 100 | + margin-top: 2rem; |
| 101 | +
|
| 102 | + label { |
| 103 | + font: var(--sk-font-ui-small); |
| 104 | + display: flex; |
| 105 | + align-items: center; |
| 106 | + gap: 1rem; |
| 107 | +
|
| 108 | + .icon { |
| 109 | + mask-size: 2rem; |
| 110 | + mask-image: url(icons/minus); |
| 111 | + } |
| 112 | +
|
| 113 | + button[aria-pressed='false'] .icon { |
| 114 | + mask-image: url(icons/plus); |
| 115 | + } |
| 116 | + } |
| 117 | +
|
| 118 | + button { |
| 119 | + order: 1; |
| 120 | + } |
| 121 | +
|
| 122 | + @media (min-width: 1024px) { |
| 123 | + justify-content: flex-start; |
| 124 | +
|
| 125 | + button { |
| 126 | + order: 0; |
| 127 | + } |
| 128 | + } |
194 | 129 | }
|
195 | 130 | </style>
|
0 commit comments