[SPIKE] Modifications to pagination component #3644
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A little bit of a self-indulgent spike refactoring how the pagination component works, in an effort to resolve #3324 and (potentially) make it a little simpler for teams to use.
Component page:
https://govuk-frontend-pr-3644.herokuapp.com/components/pagination
Temporary example page showing various pagination lengths in various states:
https://govuk-frontend-pr-3644.herokuapp.com/examples/pagination
Background
When investigating #3324 I came up with a couple of potential fixes, but ultimately it seemed (to me, at least) that the root of the problem is that the component lacks context about what information it’s handling. It currently only receives a limited set of explicitly categorised information and is told to format and spit it out again without interrogating it; so when it comes to processing the data more thoroughly (e.g. deciding which things should be hidden on mobile) it can only work off assumptions.
One solution is to allow service teams to provide that context by manually flagging what things are and in what contexts they should be visible, but this creates a burden on the implementor to get all the nuances of it right—if the service has multiple paginations, they'd need to get it right multiple times—and none of it is particularly easy to explain in documentation.
This is my first pass at a “do the hard work to make it simple” approach that brings more of the logic into the Nunjucks.
Instead of a backend (or other code) needing to decide ahead of time which pages to render in the pagination, what labels they use, where and when ellipses should appear, etc., this rewrite now only requires implementors to provide a list of (ordered) URLs and what the current page is: the component handles everything else.
Changes
...to how the component is used
items
array, rather than just the subset that they wish to show on the page. (This may reduce complexity in backend code, as the same pagination code can be used in multiple pages and contexts?)item.ellipsis
parameter has been removed. Ellipsis placement and visibility is now determined automatically.item.current
parameter has been replaced by acurrentPage
parameter.item.number
parameter is no longer required. Numbers are automatically generated where not defined.neighbouringPages
parameter has been added, allowing macro users to configure how many pages 'neighbouring' the current page to show....to how the component works
visiblePagesLower
andvisiblePagesUpper
) are determined using a combination ofcurrentPage
andneighbouringPages
parameters.items
array is now looped through and has various tests ran against it to determine whether it should be output or not.currentPage
are more than 1 index apart, ellipses are appended after the first item.visiblePagesLower
are 1 or more indexes apart, the ellipses will only be shown on mobile.currentPage
are more than 1 index apart, ellipses are prepended before the last item.visiblePagesUpper
are 1 or more indexes apart, the ellipses will only be shown on mobile.visiblePagesLower
andvisiblePagesUpper
, but is not thecurrentPage
, it is rendered but will be hidden on mobile.currentPage
, it is rendered with a 'current' class.1 | 2 | 3
instead of1 | … | 3
).Thoughts
This is a bit of a compromise between the current method (implementors provide an array of page, with links, optional numbering and other attribute info) and a more fully-automated approach (an implementor doesn't even provide an array and simply say there are 40 pages of results).
Whilst the latter would be even simpler to use, it has the trade-off of removing or restricting a number of features that the current pagination component is capable of, such as being able to customise the labelling of items or use non-incremental URL schemes for paging.
Todo
currentPage
selections andneighbouringPages
combinations hasn't been tested. There may still be edge cases.item.visuallyHiddenText
anditem.attributes
separately for each item. Defining a way to set these globally for all items would also seem like a nice enhancement.