Skip to content

Commit

Permalink
🎨 Improve Carousel performance with 0 debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
Frontendland committed Sep 20, 2024
1 parent 60eeddd commit a3cc89e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
12 changes: 7 additions & 5 deletions src/components/Carousel/Carousel.astro
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ const style = visibleItems > 1
const activeElement = carouselElement.children[page - 1] as HTMLLIElement

activeElement.dataset.active = 'true'
} else {
target.removeAttribute('data-paginated')
}
}

Expand Down Expand Up @@ -180,12 +178,16 @@ const style = visibleItems > 1
}

if (event.trusted) {
const carouselContaienr = target.closest('section').querySelector('[data-id="w-carousel"]')

liElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' })
liElement.dataset.active = 'true'

target.closest('section')
.querySelector('[data-id="w-carousel"]')
.dataset.paginated = 'true'
carouselContaienr.dataset.paginated = 'true'

setTimeout(() => {
carouselContaienr.removeAttribute('data-paginated')
}, 300)
}
})
</script>
7 changes: 4 additions & 3 deletions src/components/Carousel/Carousel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@
}
const scroll = debounceScroll((event: Event) => {
if (paginated) {
paginated = false
} else {
if (!paginated) {
const target = event.target as HTMLDivElement
const scrollLeft = target.scrollLeft
const itemWidth = target.children[0].clientWidth
Expand All @@ -110,6 +108,9 @@
paginated = true
updateValues()
setTimeout(() => {
paginated = false
}, 300)
}
onMount(() => {
Expand Down
7 changes: 4 additions & 3 deletions src/components/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ const Carousel = ({
}

const scroll = debounceScroll((event: Event) => {
if (paginated.current) {
paginated.current = false
} else {
if (!paginated.current) {
const target = event.target as HTMLDivElement
const scrollLeft = target.scrollLeft
const itemWidth = target.children[0].clientWidth
Expand All @@ -115,6 +113,9 @@ const Carousel = ({
paginated.current = true

updateValues(event.page)
setTimeout(() => {
paginated.current = false
}, 300)
}

useEffect(() => {
Expand Down

0 comments on commit a3cc89e

Please sign in to comment.