Pagination 문서
Pagination은 여러 페이지로 나뉜 콘텐츠를 오가며 탐색할 수 있게 해 주는 컴포넌트예요. 페이지 번호, 이전·다음 버튼, 그리고 사이에 들어갈 여백(ellipsis) 처리까지 한 번에 다뤄 줘요.
출처: 문서
본문
아래 예시는 전체 100개 항목을 10개씩 나누어 보여 주는 Pagination이에요. #snippet children을 통해 페이지 번호와 표시 범위를 직접 그려 볼 수 있어요.
<script lang="ts">
import { Pagination } from "bits-ui";
import CaretLeft from "phosphor-svelte/lib/CaretLeft";
import CaretRight from "phosphor-svelte/lib/CaretRight";
</script>
<Pagination.Root count={100} perPage={10}>
{#snippet children({ pages, range })}
<div class="my-8 flex items-center">
<Pagination.PrevButton
class="hover:bg-dark-10 disabled:text-muted-foreground mr-[25px] inline-flex size-10 items-center justify-center rounded-[9px] bg-transparent active:scale-[0.98] disabled:cursor-not-allowed hover:disabled:bg-transparent"
>
<CaretLeft class="size-6" />
</Pagination.PrevButton>
<div class="flex items-center gap-2.5">
{#each pages as page (page.key)}
{#if page.type === "ellipsis"}
<div
class="text-foreground-alt select-none text-[15px] font-medium"
>
...
</div>
{:else}
<Pagination.Page
{page}
class="hover:bg-dark-10 data-selected:bg-foreground data-selected:text-background inline-flex size-10 select-none items-center justify-center rounded-[9px] bg-transparent text-[15px] font-medium active:scale-[0.98] disabled:cursor-not-allowed disabled:opacity-50 hover:disabled:bg-transparent"
>
{page.value}
</Pagination.Page>
{/if}
{/each}
</div>
<Pagination.NextButton
class="hover:bg-dark-10 disabled:text-muted-foreground ml-[29px] inline-flex size-10 items-center justify-center rounded-[9px] bg-transparent active:scale-[0.98] disabled:cursor-not-allowed hover:disabled:bg-transparent"
>
<CaretRight class="size-6" />
</Pagination.NextButton>
</div>
<p class="text-muted-foreground text-center text-[13px]">
Showing {range.start} - {range.end}
</p>
{/snippet}
</Pagination.Root>
구조 (Structure)
Pagination의 기본 구조는 Pagination.Root가 페이지 목록을 pages로 내려 주고, 그 안에서 PrevButton, Page, NextButton을 조합해 그리는 방식이에요.
<script lang="ts">
import { Pagination } from "bits-ui";
</script>
<Pagination.Root let:pages>
<Pagination.PrevButton />
{#each pages as page (page.key)}
<Pagination.Page {page} />
{/each}
<Pagination.NextButton />
</Pagination.Root>
페이지 상태 다루기 (Managing Page State)
이번 절에서는 컴포넌트의 page 상태를 어떻게 다루는지 설명할게요.
양방향 바인딩 (Two-Way Binding)
간단하고 자동으로 상태를 동기화하고 싶다면 bind:page를 사용해요.
<script lang="ts">
import { Pagination } from "bits-ui";
let myPage = $state(1);
</script>
<button onclick={() => (myPage = 2)}> Go to page 2 </button>
<Pagination.Root bind:page={myPage}>
<!-- ...-->
</Pagination.Root>
완전 제어 (Fully Controlled)
상태의 읽기와 쓰기를 모두 직접 제어하고 싶다면 Function Binding을 사용해요.
<script lang="ts">
import { Pagination } from "bits-ui";
let myPage = $state(1);
function getPage() {
return myPage;
}
function setPage(newPage: number) {
myPage = newPage;
}
</script>
<Pagination.Root bind:page={getPage, setPage}>
<!-- ... -->
</Pagination.Root>
여백 처리 (Ellipsis)
pages snippet prop은 'page'와 'ellipsis' 두 종류의 항목으로 이루어져 있어요. 'page' 타입은 실제 페이지 번호를 뜻하고, 'ellipsis' 타입은 페이지 사이에 ...(ellipsis)를 렌더링하기 위한 자리 표시자예요.
API Reference
Pagination.Root
The root pagination component which contains all other pagination components.
| Property | Type | Description | Details |
|---|---|---|---|
count required |
number |
The total number of items.Default: —— undefined |
|
page $bindable |
number |
The selected page. You can bind this to a variable to control the selected page from outside the component.Default: —— undefined |
|
onPageChange |
function - (page: number) => void |
A function called when the selected page changes.Default: —— undefined |
|
perPage |
number |
The number of items per page.Default: 1 |
|
siblingCount |
number |
The number of page triggers to show on either side of the current page.Default: 1 |
|
loop |
boolean |
Whether or not the pagination should loop through the items when reaching the end while navigating with the keyboard.Default: false |
|
orientation |
enum - 'horizontal' | 'vertical' |
The orientation of the pagination. This determines how keyboard navigation will work with the component.Default: 'horizontal' |
|
ref $bindable |
HTMLDivElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet - type Page = { type: "page" value: number } type Ellipsis = { type: "ellipsis" } type PageItem = (Page | Ellipsis) & { /** A unique key to be used as the key in a svelte `#each` block. */ key: string } type ChildrenSnippetProps = { /** The items to iterate over and render for the pagination component */ pages: PageItem[] /** The range of pages to render */ range: { start: number; end: number } /** The currently active page */ currentPage: number } |
The children content to render.Default: —— undefined |
|
child |
Snippet - type Page = { type: "page" value: number } type Ellipsis = { type: "ellipsis" } type PageItem = (Page | Ellipsis) & { /** A unique key to be used as the key in a svelte `#each` block. */ key: string } type ChildSnippetProps = { /** The items to iterate over and render for the pagination component */ pages: PageItem[] /** The range of pages to render */ range: { start: number; end: number } /** The currently active page */ currentPage: number props: Record<string, unknown> } |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
Pagination.Page
A button that triggers a page change.
| Property | Type | Description | Details |
|---|---|---|---|
page |
PageItem - export type Page = { type: "page"; /** The page number the 'PageItem' represents */ value: number; } export type Ellipsis = { type: "ellipsis"; } export type PageItem = (Page | Ellipsis) & { /** Unique key for the item, for svelte #each block */ key: string; } |
The page item this component represents.Default: —— undefined |
|
ref $bindable |
HTMLButtonElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-selected |
'' |
Present on the current page element. | |
data-pagination-page |
'' |
Present on the page trigger element. |
Pagination.PrevButton
The previous button of the pagination.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLButtonElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-pagination-prev-button |
'' |
Present on the previous button element. |
Pagination.NextButton
The next button of the pagination.
| Property | Type | Description | Details |
|---|---|---|---|
ref $bindable |
HTMLButtonElement |
The underlying DOM element being rendered. You can bind to this to get a reference to the element.Default: null |
|
children |
Snippet |
The children content to render.Default: —— undefined |
|
child |
Snippet - type SnippetProps = { props: Record<string, unknown>; }; |
Use render delegation to render your own element. See Child Snippet docs for more information.Default: —— undefined |
| Data Attribute | Value | Description | Details |
|---|---|---|---|
data-pagination-next-button |
'' |
Present on the next button element. |
더 알아보기 (Learn more)
Pagination은 bind:page나 함수 바인딩으로 상태를 제어할 수 있어서, 목록이 길 때 한 화면에 담기 어려운 콘텐츠를 여러 페이지로 나눠 보여 주는 데 유용해요. 페이지마다 새 콘텐츠를 로드해야 한다면 목록을 비동기로 채우는 Async List 같은 컬렉션 개념과 함께 쓰면 더 완성도 높은 UI를 만들 수 있어요.