Components
Pagination
Pagination is a list of buttons used to navigate between different pages of content, such as search results or tables.
<ds-pagination
class="ds-pagination"
aria-label="Pagination menu"
>
<ul>
<li>
<button
class="ds-button"
type="button"
aria-label="Previous page"
>Previous</button>
</li>
<li>
<button class="ds-button" type="button" aria-label="Page 1">1</button>
</li>
<li>
<button
class="ds-button"
type="button"
aria-label="Page 2"
aria-current="true"
>2</button>
</li>
<li></li>
<li>
<button class="ds-button" type="button" aria-label="Page 9">9</button>
</li>
<li>
<button class="ds-button" type="button" aria-label="Page 10">10</button>
</li>
<li>
<button class="ds-button" type="button" aria-label="Next page">Next</button>
</li>
</ul>
</ds-pagination>const PreviewEn = () => { return ( <Pagination aria-label='Pagination menu'> <Pagination.List> <Pagination.Item> <Pagination.Button aria-label='Previous page'> Previous </Pagination.Button> </Pagination.Item> <Pagination.Item> <Pagination.Button aria-label='Page 1'>1</Pagination.Button> </Pagination.Item> <Pagination.Item> <Pagination.Button aria-label='Page 2' aria-current='true'> 2 </Pagination.Button> </Pagination.Item> <Pagination.Item /> <Pagination.Item> <Pagination.Button aria-label='Page 9'>9</Pagination.Button> </Pagination.Item> <Pagination.Item> <Pagination.Button aria-label='Page 10'>10</Pagination.Button> </Pagination.Item> <Pagination.Item> <Pagination.Button aria-label='Next page'>Next</Pagination.Button> </Pagination.Item> </Pagination.List> </Pagination> ); }; render(<PreviewEn />)
Usage
Add the class name ds-pagination to the <ds-pagination> element.
The web component automatically fills the buttons or anchors based on the data attributes data-current and data-total.
If you wish to use your own buttons, you can add <a> or <button> elements inside <ds-pagination>. If you fill these with text and do not pass any attributes, the web component will not change the buttons.
With links
You can use pagination with links (anchor tags).
<ds-pagination
class="ds-pagination"
aria-label="Pagination menu"
>
<ul>
<li>
<a
href="#previous-page"
class="ds-button"
aria-label="Previous page"
aria-hidden="false"
>Previous</a>
</li>
<li>
<a href="#page-1" class="ds-button" aria-label="Page 1">1</a>
</li>
<li>
<a
href="#page-2"
class="ds-button"
aria-label="Page 2"
aria-current="true"
>2</a>
</li>
<li>
<a href="#page-3" class="ds-button" aria-label="Page 3">3</a>
</li>
<li>
<a href="#page-4" class="ds-button" aria-label="Page 4">4</a>
</li>
<li>
<a href="#page-5" class="ds-button" aria-label="Page 5">5</a>
</li>
<li></li>
<li>
<a href="#page-10" class="ds-button" aria-label="Page 10">10</a>
</li>
<li>
<a
href="#next-page"
class="ds-button"
aria-label="Next page"
aria-hidden="false"
>Next</a>
</li>
</ul>
</ds-pagination>const WithAnchorEn = () => { const [currentPage, setCurrentPage] = useState(2); const { pages, nextButtonProps, prevButtonProps } = usePagination({ currentPage, setCurrentPage, totalPages: 10, showPages: 7, }); return ( <Pagination aria-label='Pagination menu'> <Pagination.List> <Pagination.Item> <Pagination.Button asChild aria-label='Previous page' {...prevButtonProps} > <a href='#previous-page'>Previous</a> </Pagination.Button> </Pagination.Item> {pages.map(({ page, itemKey, buttonProps }) => ( <Pagination.Item key={itemKey}> {typeof page === 'number' && ( <Pagination.Button asChild aria-label={`Page ${page}`} {...buttonProps} > <a href={`#page-${page}`}>{page}</a> </Pagination.Button> )} </Pagination.Item> ))} <Pagination.Item> <Pagination.Button asChild aria-label='Next page' {...nextButtonProps} > <a href='#next-page'>Next</a> </Pagination.Button> </Pagination.Item> </Pagination.List> </Pagination> ); }; render(<WithAnchorEn />)
CSS variables and data attributes
Sizes are controlled with data-size and colors with data-color. The component will inherit from the closest parent where these are set.
| Name | Value |
|---|---|
| --dsc-button-background | transparent |
| --dsc-button-background--active | var(--ds-color-surface-active) |
| --dsc-button-background--hover | var(--ds-color-surface-hover) |
| --dsc-button-color | var(--ds-color-text-subtle) |
| --dsc-button-color--active | var(--dsc-button-color) |
| --dsc-button-color--hover | var(--dsc-button-color) |
| --dsc-pagination-ellipsis | "\2026" |
| --dsc-pagination-gap | var(--ds-size-2) |
| --dsc-pagination-icon-size | var(--ds-icon-size) |
| --dsc-pagination-icon-url | url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='20' height='20' viewBox='0 0 24 24'%3E%3Cpath d='M9.47 5.97a.75.75 0 0 1 1.06 0l5.5 5.5a.75.75 0 0 1 0 1.06l-5.5 5.5a.75.75 0 1 1-1.06-1.06L14.44 12 9.47 7.03a.75.75 0 0 1 0-1.06'/%3E%3C/svg%3E") |
| --dsc-pagination-label | "Bla i sider" |
| --dsc-pagination-label-next | "Neste side" |
| --dsc-pagination-label-prev | "Forrige side" |
| Name | Value |
|---|---|
| data-variant | tertiary |
React
Pagination is a stateless component, and you must control which pages are displayed.
Use usePagination for pagination logic together with the single-part components.
Read more about usePagination.
import { Pagination } from "@digdir/designsystemet-react";
<Pagination>
<Pagination.List>
<Pagination.Item>
<Pagination.Button aria-label="Previous page">Previous</Pagination.Button>
</Pagination.Item>
<Pagination.Item>
<Pagination.Button aria-label="Page 1">1</Pagination.Button>
</Pagination.Item>
<Pagination.Item />
<Pagination.Item>
<Pagination.Button aria-label="Page 10">10</Pagination.Button>
</Pagination.Item>
<Pagination.Item>
<Pagination.Button aria-label="Next page">Next</Pagination.Button>
</Pagination.Item>
</Pagination.List>
</Pagination>;
Pagination
- aria-label
- Description
Sets the screen reader label for the Pagination area
- Type
string- Default
'Bla i sider'
- data-current
- Description
Current page number
- Type
string
- data-total
- Description
Total number of pages
- Type
string
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior. @deprecated This is not supported anymore, as the element needs to be `ds-pagination`
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| aria-label | string | 'Bla i sider' | Sets the screen reader label for the Pagination area |
| data-current | string | - | Current page number |
| data-total | string | - | Total number of pages |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. @deprecated This is not supported anymore, as the element needs to be `ds-pagination` |
PaginationButton
- aria-current
- Description
Toggle button as active Indicates the element that represents the current item within a container or set of related elements.
- Type
boolean | "false" | "true" | "page" | "step" | "location" | "date" | "time"- Default
false
- type
- Description
Specify the type of button. Unset when `asChild` is true
- Type
"submit" | "reset" | "button"- Default
'button'
- variant
- Description
Specify which variant to use
- Type
"primary" | "secondary" | "tertiary"- Default
'primary'
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| aria-current | boolean | "false" | "true" | "page" | "step" | "location" | "date" | "time" | false | Toggle button as active Indicates the element that represents the current item within a container or set of related elements. |
| type | "submit" | "reset" | "button" | 'button' | Specify the type of button. Unset when `asChild` is true |
| variant | "primary" | "secondary" | "tertiary" | 'primary' | Specify which variant to use |
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
PaginationItem
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
PaginationList
- asChild
- Description
Change the default rendered element for the one passed as a child, merging their props and behavior.
- Type
boolean- Default
false
| Name | Type | Default | Description |
|---|---|---|---|
| asChild | boolean | false | Change the default rendered element for the one passed as a child, merging their props and behavior. |
Additional logic for creating pagination with links
Below you see usePagination used with pagination to create pagination with links.
You use asChild to change Pagination.Button to <a>.
<ds-pagination
class="ds-pagination"
aria-label="Pagination menu"
>
<ul>
<li>
<a
href="#previous-page"
class="ds-button"
aria-label="Previous page"
aria-hidden="false"
>Previous</a>
</li>
<li>
<a href="#page-1" class="ds-button" aria-label="Page 1">1</a>
</li>
<li>
<a
href="#page-2"
class="ds-button"
aria-label="Page 2"
aria-current="true"
>2</a>
</li>
<li>
<a href="#page-3" class="ds-button" aria-label="Page 3">3</a>
</li>
<li>
<a href="#page-4" class="ds-button" aria-label="Page 4">4</a>
</li>
<li>
<a href="#page-5" class="ds-button" aria-label="Page 5">5</a>
</li>
<li></li>
<li>
<a href="#page-10" class="ds-button" aria-label="Page 10">10</a>
</li>
<li>
<a
href="#next-page"
class="ds-button"
aria-label="Next page"
aria-hidden="false"
>Next</a>
</li>
</ul>
</ds-pagination>const WithAnchorEn = () => { const [currentPage, setCurrentPage] = useState(2); const { pages, nextButtonProps, prevButtonProps } = usePagination({ currentPage, setCurrentPage, totalPages: 10, showPages: 7, }); return ( <Pagination aria-label='Pagination menu'> <Pagination.List> <Pagination.Item> <Pagination.Button asChild aria-label='Previous page' {...prevButtonProps} > <a href='#previous-page'>Previous</a> </Pagination.Button> </Pagination.Item> {pages.map(({ page, itemKey, buttonProps }) => ( <Pagination.Item key={itemKey}> {typeof page === 'number' && ( <Pagination.Button asChild aria-label={`Page ${page}`} {...buttonProps} > <a href={`#page-${page}`}>{page}</a> </Pagination.Button> )} </Pagination.Item> ))} <Pagination.Item> <Pagination.Button asChild aria-label='Next page' {...nextButtonProps} > <a href='#next-page'>Next</a> </Pagination.Button> </Pagination.Item> </Pagination.List> </Pagination> ); }; render(<WithAnchorEn />)