{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pagination-nav",
  "type": "registry:ui",
  "title": "<ui-pagination-nav>",
  "description": "Carbon `PaginationNav` (`cds-pagination-nav`): numbered pages between Previous and Next, the pages that don't fit folded into \"…\" selects (core/pagination-nav). `page` is 0-based, as Carbon's; `items-shown` page items show (at least 4); `loop` wraps Previous / Next; `disable-overflow` turns the \"…\" selects off. Each page is a button (`aria-current=\"page\"` on the current one, `tabindex=\"0\"` so Safari focuses it on click and Tab reaches it) and the change is announced (\"Page 3 of 25\"). Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-pagination-nav",
    "@kanok-ui/icon-button",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-field",
    "@kanok-ui/shared-localize",
    "@kanok-ui/tooltip",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/pagination-nav/pagination-nav.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/pagination-nav/pagination-nav.ts",
      "content": "import { html, css, nothing, svg, type PropertyValues } from 'lit';\nimport { customElement, property, state } from 'lit/decorators.js';\nimport { classMap } from 'lit/directives/class-map.js';\nimport { calculateCuts, navItems, navItemsShown, navStep, nextCuts, type NavCuts, type NavItem, type NavSize } from '../../core/pagination-nav.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport { UiElement } from '../shared/element.ts';\nimport { srOnlyStyles } from '../shared/field.ts';\nimport type { TooltipPlacement } from '../tooltip/tooltip.ts';\nimport '../icon-button/icon-button.ts';\n\n// Carbon's CaretLeft / CaretRight and OverflowMenuHorizontal (32 grid at 16px)\nconst caretLeft = svg`<path d=\"M20 24 10 16 20 8z\" />`;\nconst caretRight = svg`<path d=\"M12 8 22 16 12 24z\" />`;\nconst dots = svg`<circle cx=\"8\" cy=\"16\" r=\"2\" /><circle cx=\"16\" cy=\"16\" r=\"2\" /><circle cx=\"24\" cy=\"16\" r=\"2\" />`;\n\n/** Below Carbon's sm breakpoint (320px) the row falls back to fewer items. */\nconst NARROW = '(max-width: 20rem)';\n\n/**\n * Carbon `PaginationNav` (`cds-pagination-nav`): numbered pages between Previous and Next, the pages that don't fit\n * folded into \"…\" selects (core/pagination-nav). `page` is 0-based, as Carbon's; `items-shown` page items show (at\n * least 4); `loop` wraps Previous / Next; `disable-overflow` turns the \"…\" selects off. Each page is a button\n * (`aria-current=\"page\"` on the current one, `tabindex=\"0\"` so Safari focuses it on click and Tab reaches it) and the\n * change is announced (\"Page 3 of 25\").\n *\n * @fires ui-change - detail: { page } (0-based) after the user chose a page\n * @csspart base - The `<nav>`\n * @csspart page - Each page button\n * @csspart overflow - Each \"…\" select\n * @cssprop --ui-button-radius - Corner radius of the page buttons (the button setting)\n * @cssprop --ui-focus-offset - `outline-offset` of its focus ring (default -2px: inside; Kanok: 2px outside)\n * @cssprop --ui-pagination-nav-mark-height - Height of the current page's underline (default 4px; Kanok: 2px)\n */\n@customElement('ui-pagination-nav')\nexport class UiPaginationNav extends UiElement {\n    static override readonly styles = [\n        srOnlyStyles,\n        css`\n            :host {\n                display: block;\n                --_h: var(--ui-field-height-lg, 48px);\n            }\n\n            :host([size='md']) {\n                --_h: var(--ui-field-height-md, 40px);\n            }\n\n            :host([size='sm']) {\n                --_h: var(--ui-field-height-sm, 32px);\n            }\n\n            .list {\n                display: flex;\n                align-items: center;\n                margin: 0;\n                padding: 0;\n                list-style: none;\n            }\n\n            /* Carbon .cds--pagination-nav__page: body-compact-01 at line-height 1, the field's height square at least */\n            .page {\n                position: relative;\n                display: block;\n                box-sizing: border-box;\n                min-inline-size: var(--_h);\n                margin: 0;\n                padding: calc((var(--_h) - 14px) / 2) var(--ui-space-xxs);\n                border: none;\n                border-radius: var(--ui-button-radius, 0); /* page buttons: the button setting */\n                outline: var(--ui-focus-width, 2px) solid transparent;\n                outline-offset: var(--ui-focus-offset, -2px); /* the page buttons are discrete controls: a design can ring them outside */\n                background: none;\n                color: var(--ui-ink);\n                font: var(--ui-font-body-sm);\n                line-height: 1;\n                letter-spacing: var(--ui-tracking-body);\n                text-align: center;\n                cursor: pointer;\n                user-select: none;\n                transition:\n                    background-color var(--ui-duration-moderate) var(--ui-easing),\n                    color var(--ui-duration-moderate) var(--ui-easing);\n            }\n\n            @media (any-hover: hover) {\n                .page:hover {\n                    background-color: var(--ui-canvas-hover);\n                }\n            }\n\n            .page:focus {\n                outline-color: var(--ui-focus);\n            }\n\n            /* a mouse or touch focus shows no ring in a design that sets --ui-pointer-focus (Kanok: keyboard only) */\n            .page:focus:not(:focus-visible) {\n                outline-color: var(--ui-pointer-focus, var(--ui-focus));\n            }\n\n            .page:disabled {\n                background: none;\n                color: color-mix(in srgb, var(--ui-ink-muted) 50%, transparent);\n                outline: none;\n                pointer-events: none;\n            }\n\n            .active {\n                font-weight: 600;\n            }\n\n            /* the current page's 16 × 4 underline (Carbon pseudo-underline); --ui-pagination-nav-mark-height: a design can\n               draw it as thin as its other \"you are here\" marks (Kanok: 2px, as the selected tab) */\n            .page::after,\n            .icon-wrapper::after {\n                content: '';\n                position: absolute;\n                inset-block-end: 0;\n                inset-inline-start: 50%;\n                display: block;\n                inline-size: 0;\n                block-size: var(--ui-pagination-nav-mark-height, var(--ui-space-xxs));\n                background-color: var(--ui-border-interactive);\n                opacity: 0;\n                transition: width var(--ui-duration-moderate) var(--ui-easing);\n            }\n\n            .active::after {\n                inline-size: var(--ui-space-md);\n                inset-inline-start: calc(50% - 8px);\n                opacity: 1;\n            }\n\n            .select-item {\n                position: relative;\n            }\n\n            /* the \"…\" select: its text hidden behind the dots icon */\n            select.page {\n                appearance: none;\n                max-block-size: var(--_h);\n                color: transparent;\n                text-indent: calc(50% - 4.5px);\n                font-size: var(--ui-field-font-size, 14px);\n            }\n\n            select.page option {\n                color: var(--ui-ink);\n                background: var(--ui-surface-1);\n            }\n\n            .icon-wrapper {\n                position: absolute;\n                inset-block-start: 0;\n                inline-size: 100%;\n                block-size: 100%;\n                pointer-events: none;\n            }\n\n            .dots {\n                position: absolute;\n                inset-block-start: calc(50% - 4px);\n                inset-inline-start: calc(50% - 8px);\n                inline-size: 16px;\n                block-size: 16px;\n                fill: var(--ui-ink);\n            }\n\n            select:disabled + .icon-wrapper .dots {\n                fill: color-mix(in srgb, var(--ui-ink-muted) 50%, transparent);\n            }\n\n            .caret {\n                inline-size: 16px;\n                block-size: 16px;\n                fill: currentColor;\n            }\n\n            :host(:dir(rtl)) .caret {\n                transform: rotate(0.5turn);\n            }\n\n            @media (prefers-reduced-motion: reduce) {\n                .page::after,\n                .icon-wrapper::after {\n                    transition: none;\n                }\n            }\n\n            @media (forced-colors: active) {\n                .active::after {\n                    background-color: Highlight;\n                }\n            }\n        `,\n    ];\n\n    /** The current page, 0-based (Carbon). */\n    @property({ type: Number }) page = 0;\n\n    @property({ type: Number, attribute: 'total-items' }) totalItems = 1;\n\n    /** Page items in the row (at least 4). */\n    @property({ type: Number, attribute: 'items-shown' }) itemsShown = 10;\n\n    /** Previous on the first page goes to the last, Next on the last to the first. */\n    @property({ type: Boolean }) loop = false;\n\n    /** The \"…\" selects can't be opened. */\n    @property({ type: Boolean, attribute: 'disable-overflow' }) disableOverflow = false;\n\n    @property({ reflect: true }) size: NavSize = 'lg';\n\n    /** Where Previous / Next show their tooltip. */\n    @property({ attribute: 'tooltip-placement' }) tooltipPlacement: TooltipPlacement = 'bottom';\n\n    @state() private cuts: NavCuts = { front: 0, back: 0 };\n\n    @state() private narrow = false;\n\n    private readonly localize = new LocalizeController(this);\n    private media?: MediaQueryList;\n    private previous = 0;\n\n    override connectedCallback() {\n        super.connectedCallback();\n        this.media = globalThis.matchMedia?.(NARROW);\n        this.narrow = !!this.media?.matches;\n        this.media?.addEventListener('change', this.onMedia);\n    }\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        this.media?.removeEventListener('change', this.onMedia);\n    }\n\n    private readonly onMedia = (event: MediaQueryListEvent) => {\n        this.narrow = event.matches;\n    };\n\n    private get shown() {\n        return navItemsShown(this.itemsShown, this.size, this.narrow);\n    }\n\n    protected override willUpdate(changed: PropertyValues<this>) {\n        // the page stays a page; the row is laid out again when its size changes, and follows a page that moved out of it\n        this.page = Math.min(Math.max(this.page, 0), Math.max(this.totalItems - 1, 0));\n        if (changed.has('totalItems') || changed.has('itemsShown') || changed.has('size') || (changed as Map<string, unknown>).has('narrow')) this.cuts = calculateCuts(this.page, this.totalItems, this.shown);\n        else if (changed.has('page')) this.cuts = nextCuts(this.cuts, this.page, this.previous, this.totalItems, this.shown);\n        this.previous = this.page;\n    }\n\n    private go(page: number) {\n        if (page === this.page || page < 0 || page >= this.totalItems) return;\n        this.page = page;\n        this.dispatchEvent(new CustomEvent('ui-change', { detail: { page }, bubbles: true, composed: true }));\n    }\n\n    private renderItem(item: NavItem) {\n        const t = this.localize.t.pagination;\n        if (item.kind === 'page') {\n            return html`<li>\n                <button class=${classMap({ page: true, active: item.active })} part=\"page\" type=\"button\" tabindex=\"0\" data-page=${item.page + 1} aria-current=${item.active ? 'page' : nothing} @click=${() => this.go(item.page)}>\n                    <span class=\"sr-only\">${item.active ? t.navActivePage : t.navPage}</span>\n                    ${item.page + 1}\n                </button>\n            </li>`;\n        }\n        const options = Array.from({ length: item.count }, (_, i) => html`<option value=${item.from + i}>${item.from + i + 1}</option>`);\n        return html`<li class=\"select-item\">\n            <select\n                class=\"page\"\n                part=\"overflow\"\n                tabindex=\"0\"\n                aria-label=${t.navSelect}\n                ?disabled=${this.disableOverflow}\n                @change=${(event: Event) => {\n                    const select = event.target as HTMLSelectElement;\n                    this.go(Number(select.value));\n                    select.value = '';\n                }}\n            >\n                <option value=\"\" hidden></option>\n                ${options}\n            </select>\n            <div class=\"icon-wrapper\"><svg class=\"dots\" viewBox=\"0 0 32 32\" aria-hidden=\"true\">${dots}</svg></div>\n        </li>`;\n    }\n\n    override render() {\n        const t = this.localize.t.pagination;\n        const total = Math.max(this.totalItems, 0);\n        const items = navItems(this.page, total, this.shown, this.cuts);\n        const step = (delta: -1 | 1) => () => this.go(navStep(this.page, total, delta, this.loop));\n        return html`<nav part=\"base\" aria-label=${t.navLabel}>\n            <ul class=\"list\">\n                <li>\n                    <ui-icon-button kind=\"ghost\" size=${this.size} label=${t.navPrevious} tooltip-placement=${this.tooltipPlacement} ?disabled=${!this.loop && this.page <= 0} @click=${step(-1)}>\n                        <svg class=\"caret\" viewBox=\"0 0 32 32\" aria-hidden=\"true\">${caretLeft}</svg>\n                    </ui-icon-button>\n                </li>\n                ${items.map((item) => this.renderItem(item))}\n                <li>\n                    <ui-icon-button kind=\"ghost\" size=${this.size} label=${t.navNext} tooltip-placement=${this.tooltipPlacement} ?disabled=${!this.loop && this.page >= total - 1} @click=${step(1)}>\n                        <svg class=\"caret\" viewBox=\"0 0 32 32\" aria-hidden=\"true\">${caretRight}</svg>\n                    </ui-icon-button>\n                </li>\n            </ul>\n            <div class=\"sr-only\" aria-live=\"polite\" aria-atomic=\"true\">${t.navStatus.replace('{page}', String(this.page + 1)).replace('{total}', String(total))}</div>\n        </nav>`;\n    }\n}\n\nexport interface UiPaginationNavEventMap extends HTMLElementEventMap {\n    'ui-change': CustomEvent<{ page: number }>;\n}\n\n/** Typed listeners for UiPaginationNav's events (declaration merging with the class). */\nexport interface UiPaginationNav {\n    addEventListener<K extends keyof UiPaginationNavEventMap>(type: K, listener: (this: UiPaginationNav, event: UiPaginationNavEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiPaginationNavEventMap>(type: K, listener: (this: UiPaginationNav, event: UiPaginationNavEventMap[K]) => unknown, options?: boolean | EventListenerOptions): void;\n    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-pagination-nav': UiPaginationNav;\n    }\n}\n"
    }
  ]
}
