|
1 | 1 | <script lang="ts">
|
2 | 2 | import type { Package } from '$lib/server/content';
|
3 |
| - import { prefersReducedMotion } from 'svelte/motion'; |
4 | 3 | import PackageCard from './PackageCard.svelte';
|
5 | 4 |
|
6 | 5 | interface Props {
|
|
11 | 10 |
|
12 | 11 | let { title, description, packages }: Props = $props();
|
13 | 12 |
|
14 |
| - let content: HTMLElement; |
15 |
| - let scroller: HTMLElement; |
16 |
| -
|
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); |
| 13 | + const INITIAL_ITEMS = 3; |
| 14 | + let showAll = $state(false); |
| 15 | + let visiblePackages = $derived(showAll ? packages : packages.slice(0, INITIAL_ITEMS)); |
35 | 16 | </script>
|
36 | 17 |
|
37 |
| -<svelte:window onresize={update} /> |
38 |
| - |
39 | 18 | <section class="category">
|
40 | 19 | <header>
|
41 | 20 | <h2>
|
42 | 21 | {title}
|
43 | 22 | </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 | 23 | </header>
|
55 | 24 | {#if description}
|
56 | 25 | <h3>{@html description}</h3>
|
57 | 26 | {/if}
|
58 | 27 |
|
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} |
| 28 | + <div class="content"> |
| 29 | + {#each visiblePackages as pkg} |
| 30 | + <div class="item"> |
| 31 | + <PackageCard {pkg} /> |
69 | 32 | </div>
|
70 |
| - </div> |
| 33 | + {/each} |
| 34 | + </div> |
71 | 35 |
|
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> |
| 36 | + {#if packages.length > INITIAL_ITEMS} |
| 37 | + <div class="show-more-container"> |
| 38 | + <button class="show-more-btn" onclick={() => (showAll = !showAll)}> |
| 39 | + {showAll ? 'Show Less' : `Show More (${packages.length - INITIAL_ITEMS})`} |
| 40 | + </button> |
89 | 41 | </div>
|
90 |
| - </div> |
| 42 | + {/if} |
91 | 43 | </section>
|
92 | 44 |
|
93 | 45 | <style>
|
94 | 46 | .category {
|
95 |
| - --bleed: var(--sk-page-padding-side); |
96 |
| - margin-bottom: 4rem; |
| 47 | + margin-bottom: 3rem; |
97 | 48 | }
|
98 | 49 |
|
99 | 50 | header {
|
100 |
| - display: flex; |
101 | 51 | margin-bottom: 1rem;
|
102 |
| - align-items: center; |
103 |
| - gap: 2rem; |
104 | 52 |
|
105 | 53 | 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 |
| - } |
| 54 | + margin: 0; |
136 | 55 | }
|
137 | 56 | }
|
138 | 57 |
|
|
141 | 60 | font-size: 1.5rem;
|
142 | 61 | }
|
143 | 62 |
|
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 | 63 | .content {
|
184 | 64 | display: grid;
|
185 |
| - grid-auto-columns: 34rem; |
186 |
| - grid-auto-flow: column; |
| 65 | + grid-template-columns: repeat(3, 1fr); |
187 | 66 | gap: 2rem;
|
188 |
| - width: fit-content; |
| 67 | + margin-top: 1rem; |
189 | 68 | }
|
190 | 69 |
|
191 | 70 | .item {
|
192 | 71 | height: 16rem;
|
193 |
| - scroll-snap-align: start; |
| 72 | + min-width: 0; /* Prevents grid items from overflowing */ |
| 73 | + } |
| 74 | +
|
| 75 | + .show-more-container { |
| 76 | + display: flex; |
| 77 | + justify-content: flex-end; |
| 78 | + margin-top: 1rem; |
| 79 | + } |
| 80 | +
|
| 81 | + .show-more-btn { |
| 82 | + background: var(--sk-bg-3); |
| 83 | + border: 1px solid var(--sk-border); |
| 84 | + border-radius: var(--sk-border-radius); |
| 85 | + padding: 0.75rem 1.5rem; |
| 86 | + font: var(--sk-font-ui-medium); |
| 87 | + font-size: 1.2rem; |
| 88 | + color: var(--sk-text-1); |
| 89 | + cursor: pointer; |
| 90 | + transition: all 0.2s ease; |
| 91 | +
|
| 92 | + &:hover { |
| 93 | + background: var(--sk-bg-4); |
| 94 | + border-color: var(--sk-text-3); |
| 95 | + } |
| 96 | +
|
| 97 | + &:active { |
| 98 | + transform: translateY(1px); |
| 99 | + } |
| 100 | + } |
| 101 | +
|
| 102 | + @media (max-width: 1024px) { |
| 103 | + .content { |
| 104 | + grid-template-columns: 1fr; |
| 105 | + } |
194 | 106 | }
|
195 | 107 | </style>
|
0 commit comments