Pagination block

A simple pagination component for long listings such as search results.

github location npm version

Usage

When to use this component

Use pagination:

  • for long listings (such as search results and article archives) that consist of more than 100 items. If your list consists of fewer items you may not need to add pagination.
  • when most users will only look at the content on the first page or first few pages.

When not to use this component

Do not use pagination:

  • for linear content such as a long page or article.
  • when you are asking the user to complete a form. Instead, use vf-button to let the user move to the next page and a Back link to let them move to the previous page.

Also see the recommendations by Nielsen Norman and the pagination compoment in gov.uk.

Accessibility

This component targets WCAG 2.1 AA accessibility.

Variants

Nunjucks syntax

Depending on your environment you'll want to use render or include. As a rule of thumb: server-side use include, precompiled browser use render. If you're using vf-eleventy you should use include.

Using include

You'll need to pass a context object from your code or Yaml file (example), as well as the path to the Nunjucks template. Nunjucks' include is an abstraction of render and provides some additional portability.


{% set context fromYourYamlFile %}
- or -
{% set context = {
  "exampleMultiColumns": "false",
  "component-type": "block",
  "previous_text": "Previous",
  "previous_url": "JavaScript:Void(0);",
  "next_text": "Next",
  "next_url": "JavaScript:Void(0);",
  "jumpBack_text": -10,
  "jumpBack_url": "JavaScript:Void(0);",
  "jumpForward_text": 10,
  "jumpForward_url": "JavaScript:Void(0);",
  "pagination__list": [
    {
      "page_number": "Previous",
      "page_href": "JavaScript:Void(0);",
      "item_modifier": "vf-pagination__item--previous-page"
    },
    {
      "page_number": 1,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": 2,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": "..."
    },
    {
      "page_number": 17,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": 18,
      "item_modifier": "vf-pagination__item--is-active"
    },
    {
      "page_number": 19,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": "..."
    },
    {
      "page_number": 91,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": 92,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": "Next",
      "page_href": "JavaScript:Void(0);",
      "item_modifier": "vf-pagination__item--next-page"
    }
  ]
}
 %}
{% include "../path_to/vf-pagination/vf-pagination.njk" %}
                

Using render

This approach is best for bare-bones Nunjucks environments, such as precompiled templates with the Nunjucks slim runtime where include is not be available.

{% render '@vf-pagination', {
  "exampleMultiColumns": "false",
  "component-type": "block",
  "previous_text": "Previous",
  "previous_url": "JavaScript:Void(0);",
  "next_text": "Next",
  "next_url": "JavaScript:Void(0);",
  "jumpBack_text": -10,
  "jumpBack_url": "JavaScript:Void(0);",
  "jumpForward_text": 10,
  "jumpForward_url": "JavaScript:Void(0);",
  "pagination__list": [
    {
      "page_number": "Previous",
      "page_href": "JavaScript:Void(0);",
      "item_modifier": "vf-pagination__item--previous-page"
    },
    {
      "page_number": 1,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": 2,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": "..."
    },
    {
      "page_number": 17,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": 18,
      "item_modifier": "vf-pagination__item--is-active"
    },
    {
      "page_number": 19,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": "..."
    },
    {
      "page_number": 91,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": 92,
      "page_href": "JavaScript:Void(0);"
    },
    {
      "page_number": "Next",
      "page_href": "JavaScript:Void(0);",
      "item_modifier": "vf-pagination__item--next-page"
    }
  ]
} %}
                
HTML
<nav class="vf-pagination" aria-label="Pagination">
  <ul class="vf-pagination__list">
    <li class="vf-pagination__item vf-pagination__item--previous-page">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        Previous<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        1<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        2<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item">
      <span class="vf-pagination__label">...</span>
    </li>
    <li class="vf-pagination__item">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        17<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item vf-pagination__item--is-active">
      <span class="vf-pagination__label" aria-current="page">
        <span class="vf-u-sr-only">Page </span>18
      </span>
    </li>
    <li class="vf-pagination__item">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        19<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item">
      <span class="vf-pagination__label">...</span>
    </li>
    <li class="vf-pagination__item">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        91<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        92<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
    <li class="vf-pagination__item vf-pagination__item--next-page">
      <a href="JavaScript:Void(0);" class="vf-pagination__link">
        Next<span class="vf-u-sr-only"> page</span>
      </a>
    </li>
  </ul>
</nav>
              

Examples

Installation info

This repository is distributed with npm. After installing npm and yarn, you can install with this command.

$ yarn add --dev @visual-framework/vf-pagination

Sass/CSS

The style files included are written in Sass. If you're using a VF-core project, you can import it like this:

@import "@visual-framework/vf-pagination/index.scss";

Make sure you import Sass requirements along with the modules. You can use a project boilerplate or the vf-sass-starter

Changelog

Changelog

1.1.1

  • UI changes to fix accessibility issues
  • Changed : Modified the vf-pagination__link class to support vf-link styles
  • Changed : Modified the style for disabled links like 'Previous' (when on 1st page) so that it does not fail contrast check. Added example to showcase this behaviour.
  • Changed : Updated the current page number style to show bold text

1.1.0

  • reduces the amount of variants to 'start again'.
  • centres the default variant

1.0.1

  • Fixes CSS to match stylelint rules.
  • https://github.com/visual-framework/vf-core/pull/1405

1.0.1-rc.2

  • changes any set- style functions to cleaner version

1.0.0-rc.2

  • right align pagination options

1.0.0-beta.2

  • Version bump only for package @visual-framework/vf-pagination

1.0.0-alpha.7

  • Version bump only for package @visual-framework/vf-pagination

Assets



File system location: components/vf-pagination

Find an issue on this page? Propose a change or discuss it.