Skip to content

Commit

Permalink
feat: scroll to top of list when clicking on next/prev page (#49)
Browse files Browse the repository at this point in the history
* feat: scroll to top of list when clicking on next/prev page

* fix: use vuejs ref instead of querySelector to do the scroll
  • Loading branch information
MaximeMRF authored Dec 31, 2023
1 parent 412f344 commit c082668
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions resources/pages/home/main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const filters = ref<PackagesFilters>({
search: params.search,
page: +(props.meta.currentPage || 1),
})
const scrollToTopRef = ref<HTMLElement | null>(null)
watchDeep(filters, () => {
router.visit('/', {
Expand All @@ -29,6 +30,19 @@ watchDeep(filters, () => {
preserveScroll: true,
})
})
function changePage(newPage: number) {
filters.value.page = newPage
scrollToTop()
}
function scrollToTop() {
const el = scrollToTopRef.value
if (el) {
el.scrollIntoView({ behavior: 'smooth' })
}
}
</script>

<template>
Expand All @@ -48,7 +62,11 @@ watchDeep(filters, () => {

<!-- Search and sort -->
<div class="w-full flex flex-col">
<div class="w-full flex flex-col justify-between gap-2" md="items-center flex-row">
<div
ref="scrollToTopRef"
class="w-full flex flex-col justify-between gap-2"
md="items-center flex-row"
>
<SearchBar v-model="filters.search" />
<SortBy v-model="filters.sort" />
</div>
Expand All @@ -61,7 +79,7 @@ watchDeep(filters, () => {
:pages="meta.pages"
:current-page="meta.currentPage"
:total="meta.total"
@update:current-page="filters.page = $event"
@update:current-page="changePage($event)"
/>
</div>
</div>
Expand Down

0 comments on commit c082668

Please sign in to comment.