<link rel="stylesheet" href="/assets/themes/default/css/pagination.css?cb=" />

<div class="pagination js-pagination">
    <a href="#top" class="pagination__previous js-previous"><span>Previous</span></a>
    <a href="#top" class="pagination__page js-page js-first selected" data-page="1">1</a>
    <span class="pagination__dots js-dots-left">&#8230;</span>
    <a href="#top" class="pagination__page js-page js-second-page" data-page="2">2</a>
    <a href="#top" class="pagination__page js-page js-third-page" data-page="3">3</a>
    <a href="#top" class="pagination__page js-page js-fourth-page" data-page="4">4</a>
    <span class="pagination__dots js-dots-right">&#8230;</span>
    <a href="#top" class="pagination__page js-page js-last-page" data-page="5">5</a>
    <a href="#top" class="pagination__next js-next"><span>Next</span></a>
</div>
<link rel="stylesheet" href="/assets/themes/{{theme}}/css/pagination.css?cb={{cacheBuster}}" />

<div class="pagination js-pagination">
    <a href="#top" class="pagination__previous js-previous"><span>Previous</span></a>
    <a href="#top" class="pagination__page js-page js-first selected" data-page="1">1</a>
    <span class="pagination__dots js-dots-left">&#8230;</span>
    <a href="#top" class="pagination__page js-page js-second-page" data-page="2">2</a>
    <a href="#top" class="pagination__page js-page js-third-page" data-page="3">3</a>
    <a href="#top" class="pagination__page js-page js-fourth-page" data-page="4">4</a>
    <span class="pagination__dots js-dots-right">&#8230;</span>
    <a href="#top" class="pagination__page js-page js-last-page" data-page="5">5</a>
    <a href="#top" class="pagination__next js-next"><span>Next</span></a>
</div>
{
  "theme": "default",
  "firstInstance": true
}
  • Content:
    export default class Pagination {
        constructor(module, onChange) {
            this.module = module;
            this.onChange = onChange;
            this.pages = this.module.querySelectorAll('.js-page');
            this.previousButton = this.module.querySelector('.js-previous');
            this.nextButton = this.module.querySelector('.js-next');
            this.currentPage = 1;
            this.numberOfPages = 1;
            this.init();
        }
    
        init() {
            this.pages.forEach((page) => {
                page.addEventListener('click', (e) => {
                    e.preventDefault();
    
                    const topAnchor = document.querySelector('#top');
                    if (topAnchor) {
                        topAnchor.scrollIntoView({ block: 'center' });
                    }
    
                    const link = e.target;
                    this.currentPage = parseInt(link.dataset.page, 10);
                    this.updateUI();
                    if (this.onChange) {
                        this.onChange();
                    }
                });
            });
    
            this.previousButton.addEventListener('click', (e) => {
                if (!e.target.classList.contains('disabled')) {
                    this.currentPage--;
                    this.updateUI();
                    if (this.onChange) {
                        this.onChange();
                    }
                }
            });
    
            this.nextButton.addEventListener('click', (e) => {
                if (!e.target.classList.contains('disabled')) {
                    this.currentPage++;
                    this.updateUI();
                    if (this.onChange) {
                        this.onChange();
                    }
                }
            });
    
            this.updateUI();
        }
    
        setNumberOfPages(numberOfPages) {
            this.numberOfPages = numberOfPages;
            this.updateUI();
        }
    
        setPage(currentPage) {
            this.currentPage = currentPage;
            this.updateUI();
        }
    
        resetButtons() {
            this.pages.forEach((btn) => {
                btn.classList.remove('selected');
            });
        }
    
        updateUI() {
            if (this.numberOfPages > 1) {
                this.module.style.display = 'flex';
            } else {
                this.module.style.display = 'none';
                return;
            }
    
            if (this.currentPage > 1) {
                this.previousButton.classList.remove('disabled');
            } else {
                this.previousButton.classList.add('disabled');
            }
            this.module.querySelector('.js-dots-left').style.display = this.currentPage > 3 ? '' : 'none';
            this.module.querySelector('.js-dots-right').style.display = this.currentPage < this.numberOfPages - 2 ? '' : 'none';
    
            if (this.currentPage < this.numberOfPages) {
                this.nextButton.classList.remove('disabled');
            } else {
                this.nextButton.classList.add('disabled');
            }
    
            if (this.currentPage < 4) {
                this.setPageNumbers(2);
            } else if (this.currentPage >= this.numberOfPages - 2) {
                this.setPageNumbers(this.numberOfPages - 3);
            } else {
                this.setPageNumbers(this.currentPage - 1);
            }
    
            this.resetButtons();
            this.module.querySelector(`.js-page[data-page='${this.currentPage}']`).classList.add('selected');
        }
    
        setPageNumbers(startNumber) {
            this.setPageButton(startNumber, '.js-second-page');
            this.setPageButton(startNumber + 1, '.js-third-page');
            this.setPageButton(startNumber + 2, '.js-fourth-page');
    
            const lastPageButton = this.module.querySelector('.js-last-page');
            if (this.numberOfPages > 4) {
                lastPageButton.innerText = this.numberOfPages;
                lastPageButton.dataset.page = this.numberOfPages;
                lastPageButton.style.display = '';
                lastPageButton.setAttribute('aria-label', `Page ${this.numberOfPages}`);
            } else {
                lastPageButton.style.display = 'none';
            }
        }
    
        setPageButton(pageNumber, pageSelector) {
            const pageButton = this.module.querySelector(pageSelector);
            if (pageNumber > this.numberOfPages) {
                pageButton.style.display = 'none';
            } else {
                pageButton.style.display = '';
                pageButton.innerText = pageNumber;
                pageButton.dataset.page = pageNumber;
                pageButton.setAttribute('aria-label', `Page ${pageNumber}`);
            }
        }
    }
    
  • URL: /components/raw/pagination/pagination.js
  • Filesystem Path: source/patterns/03-components/listings/pagination/pagination.js
  • Size: 4.4 KB
  • Content:
    @import 'source/scss/01-settings/_import';
    @import 'source/scss/02-tools/_import';
    
    .pagination {
        @extend %spacer-large;
        align-items: center;
        display: none;
        justify-content: center;
        width: 100%;
    
        &__previous,
        &__next {
            border-bottom: rem(2) solid $color-black;
            border-right: rem(2) solid $color-black;
            display: inline-block;
            flex-shrink: 0;
            height: rem(6);
            right: 0;
            width: rem(6);
    
            span {
                @extend %visually-hidden;
            }
    
            &.disabled {
                border-bottom-color: $color-steel-grey;
                border-right-color: $color-steel-grey;
            }
        }
    
        &__dots {
            margin-right: rem(14);
            text-align: center;
    
            @include breakpoint(medium) {
                width: rem(48);
            }
        }
    
        &__previous {
            margin-right: rem(16);
            transform: rotate(135deg);
    
            @include breakpoint(medium) {
                margin-right: rem(32);
            }
        }
    
        &__next {
            margin-left: rem(16);
            transform: rotate(-45deg);
    
            @include breakpoint(medium) {
                margin-right: rem(32);
            }
        }
    
        &__page {
            @extend %heading04;
            color: $color-black;
            margin-right: rem(14);
            padding-bottom: 0;
            position: relative;
            text-decoration: none;
    
            &.selected {
                pointer-events: none;
                text-decoration: underline;
            }
        }
    }
    
  • URL: /components/raw/pagination/pagination.scss
  • Filesystem Path: source/patterns/03-components/listings/pagination/pagination.scss
  • Size: 1.5 KB

No notes defined.