Page control

A row of dots for a flat sequence of pages.

navigation

Apple: Page controls

· Base UI: element

Pages

Cupertino

"use client"

import { useState } from "react"

import { PageControl } from "@applecn/ui/components/page-control"
import { Text } from "@applecn/ui/components/text"

const pages = ["Cupertino", "London", "Tokyo", "Sydney"]

export default function PageControlBasic() {
  const [index, setIndex] = useState(0)
  return (
    <div className="flex flex-col items-center gap-4 rounded-4xl bg-[linear-gradient(135deg,var(--system-blue),var(--system-purple))] p-6 text-white">
      <Text variant="title-1" emphasized color="inherit">
        {pages[index]}
      </Text>
      <PageControl
        aria-label="Cities"
        count={pages.length}
        index={index}
        onIndexChange={setIndex}
        background="prominent"
      />
    </div>
  )
}

Install

From the registry this site publishes, with dependencies resolved by the CLI.

npx shadcn@latest add https://applecn.vercel.app/r/page-control.json

Source

packages/ui/src/components/page-control.tsx

"use client"

import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import type { ComponentProps, KeyboardEvent } from "react"

/**
 * Page controls (HIG › Page controls): a row of 7 pt dots 9 pt apart, the current page filled.
 * Pressing a dot or using the arrow keys moves between pages. `prominent` shows the
 * thin-material capsule behind the dots when the control is primary navigation.
 */
const pageControlVariants = cva(
  "inline-flex items-center gap-(--page-control-gap)",
  {
    variants: {
      background: {
        automatic: "rounded-full px-2 py-1",
        prominent: "rounded-full material-thin px-2 py-1",
        minimal: "",
      },
    },
    defaultVariants: {
      background: "automatic",
    },
  }
)

type PageControlProps = Omit<ComponentProps<"div">, "onChange"> &
  VariantProps<typeof pageControlVariants> & {
    count: number
    index: number
    onIndexChange?: (index: number) => void
  }

function PageControl({
  className,
  background = "automatic",
  count,
  index,
  onIndexChange,
  onKeyDown,
  ...props
}: PageControlProps) {
  const move = (next: number) => {
    const clamped = Math.max(0, Math.min(count - 1, next))
    if (clamped !== index) onIndexChange?.(clamped)
  }
  const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
    if (event.key === "ArrowRight") {
      event.preventDefault()
      move(index + 1)
    } else if (event.key === "ArrowLeft") {
      event.preventDefault()
      move(index - 1)
    } else if (event.key === "Home") {
      event.preventDefault()
      move(0)
    } else if (event.key === "End") {
      event.preventDefault()
      move(count - 1)
    }
    onKeyDown?.(event)
  }

  return (
    // oxlint-disable-next-line jsx-a11y/interactive-supports-focus -- the dots are the focusable tabs; keys bubble up
    <div
      role="tablist"
      data-slot="page-control"
      data-background={background}
      className={cn(pageControlVariants({ background }), className)}
      onKeyDown={handleKeyDown}
      {...props}
    >
      {Array.from({ length: count }, (_, i) => (
        <button
          key={i}
          type="button"
          role="tab"
          aria-selected={i === index}
          aria-label={`Page ${i + 1}`}
          tabIndex={i === index ? 0 : -1}
          data-slot="page-control-dot"
          className="size-(--page-control-dot) rounded-full bg-label-4 transition-colors duration-(--duration-press) outline-none focus-visible:ring-4 focus-visible:ring-ring/60 aria-selected:bg-label"
          onClick={() => move(i)}
        />
      ))}
    </div>
  )
}

export { PageControl, pageControlVariants }
export type { PageControlProps }