Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions src/lib/components/Carousel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
}
const { size = 'default', gap = 32, header, children }: Props = $props();
let scroll = 0;
function calculateScrollAmount(prev = false) {
if (!carousel) return 0;
const direction = prev ? -1 : 1;
const carouselSize = carousel?.clientWidth;
const childSize = (carousel.childNodes[0] as HTMLUListElement)?.clientWidth + gap;
const carouselSize = carousel.clientWidth;
scroll = scroll || carouselSize;
const firstChild = carousel.querySelector('li') as HTMLElement;
if (!firstChild) return 0;
const childSize = firstChild.offsetWidth + gap;
const numberOfItems = Math.floor(carouselSize / childSize);
const overflow = scroll % childSize;
const amount = numberOfItems * childSize - overflow * direction;
scroll += amount * direction;
return amount * direction;
return numberOfItems * childSize * direction;
}
Comment on lines 15 to 28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

Fix zero-step bug when item is wider than viewport; prefer direct-child query and clamp page size.

If firstChild.offsetWidth + gap > carousel.clientWidth, Math.floor(...) becomes 0 so scrollBy({ left: 0 }) does nothing and buttons appear broken on narrow viewports. Also, querySelector('li') may match nested list items; use :scope > li.

-        const firstChild = carousel.querySelector('li') as HTMLElement;
+        const firstChild = carousel.querySelector(':scope > li') as HTMLElement | null;
         if (!firstChild) return 0;

-        const childSize = firstChild.offsetWidth + gap;
-        const numberOfItems = Math.floor(carouselSize / childSize);
-
-        return numberOfItems * childSize * direction;
+        const childSize = firstChild.offsetWidth + gap;
+        const itemsPerPage = Math.max(1, Math.floor(carouselSize / childSize));
+        return itemsPerPage * childSize * direction;

Optional typings outside this hunk (helps TS and DOM correctness):

// line 4
let carousel: HTMLUListElement;
// line 15
function calculateScrollAmount(prev = false): number {
🤖 Prompt for AI Agents
In src/lib/components/Carousel.svelte around lines 15 to 28, the calculation can
return 0 when an item (offsetWidth + gap) is larger than the carousel viewport
and querySelector('li') may match nested items; update the function to select
direct children via carousel.querySelector(':scope > li') and compute
numberOfItems as Math.max(1, Math.floor(carouselSize / childSize)) (so we always
scroll at least one item), ensure the function returns a number type, and
optionally add typing for carousel as HTMLUListElement and the function
signature as calculateScrollAmount(prev = false): number.

function next() {
Expand All @@ -44,9 +44,19 @@
let isStart = $state(true);
function handleScroll() {
if (!carousel) return;
isStart = carousel.scrollLeft <= 0;
isEnd = Math.ceil(carousel.scrollLeft + carousel.offsetWidth) >= carousel.scrollWidth;
isEnd = Math.ceil(carousel.scrollLeft + carousel.offsetWidth) >= carousel.scrollWidth - 1;
}
$effect(() => {
if (carousel) {
setTimeout(() => {
handleScroll();
}, 0);
}
});
</script>

<div>
Expand All @@ -66,7 +76,7 @@
<button
class="web-icon-button cursor-pointer"
aria-label="Move carousel forward"
disabled
disabled={isEnd}
onclick={next}
>
<span class="web-icon-arrow-right" aria-hidden="true"></span>
Expand All @@ -82,6 +92,12 @@
style:gap="{gap}px"
bind:this={carousel}
onscroll={handleScroll}
onwheel={(e) => {
if (e.deltaY !== 0) {
e.preventDefault();
carousel.scrollLeft += e.deltaY;
}
}}
>
{@render children()}
</ul>
Expand Down