@yamada-ui/pagination
Version:
Yamada UI pagination component
407 lines (401 loc) • 16 kB
JavaScript
"use client"
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/pagination.tsx
var pagination_exports = {};
__export(pagination_exports, {
Pagination: () => Pagination
});
module.exports = __toCommonJS(pagination_exports);
var import_core2 = require("@yamada-ui/core");
var import_use_value2 = require("@yamada-ui/use-value");
var import_utils3 = require("@yamada-ui/utils");
var import_react2 = require("react");
// src/pagination-item.tsx
var import_core = require("@yamada-ui/core");
var import_ripple = require("@yamada-ui/ripple");
var import_utils2 = require("@yamada-ui/utils");
// src/pagination-icon.tsx
var import_icon = require("@yamada-ui/icon");
var import_jsx_runtime = require("react/jsx-runtime");
var PaginationEllipsisIcon = (props) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.Icon, { viewBox: "0 0 16 16", ...props, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"path",
{
d: "M2 8c0-.733.6-1.333 1.333-1.333.734 0 1.334.6 1.334 1.333s-.6 1.333-1.334 1.333C2.6 9.333 2 8.733 2 8zm9.333 0c0-.733.6-1.333 1.334-1.333C13.4 6.667 14 7.267 14 8s-.6 1.333-1.333 1.333c-.734 0-1.334-.6-1.334-1.333zM6.667 8c0-.733.6-1.333 1.333-1.333s1.333.6 1.333 1.333S8.733 9.333 8 9.333 6.667 8.733 6.667 8z",
fill: "currentColor"
}
) });
};
PaginationEllipsisIcon.displayName = "PaginationEllipsisIcon";
PaginationEllipsisIcon.__ui__ = "PaginationEllipsisIcon";
var PaginationFirstIcon = (props) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.Icon, { viewBox: "0 0 16 16", ...props, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"path",
{
d: "M6.85355 3.85355C7.04882 3.65829 7.04882 3.34171 6.85355 3.14645C6.65829 2.95118 6.34171 2.95118 6.14645 3.14645L2.14645 7.14645C1.95118 7.34171 1.95118 7.65829 2.14645 7.85355L6.14645 11.8536C6.34171 12.0488 6.65829 12.0488 6.85355 11.8536C7.04882 11.6583 7.04882 11.3417 6.85355 11.1464L3.20711 7.5L6.85355 3.85355ZM12.8536 3.85355C13.0488 3.65829 13.0488 3.34171 12.8536 3.14645C12.6583 2.95118 12.3417 2.95118 12.1464 3.14645L8.14645 7.14645C7.95118 7.34171 7.95118 7.65829 8.14645 7.85355L12.1464 11.8536C12.3417 12.0488 12.6583 12.0488 12.8536 11.8536C13.0488 11.6583 13.0488 11.3417 12.8536 11.1464L9.20711 7.5L12.8536 3.85355Z",
fill: "currentColor"
}
) });
};
PaginationFirstIcon.displayName = "PaginationFirstIcon";
PaginationFirstIcon.__ui__ = "PaginationFirstIcon";
var PaginationLastIcon = (props) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.Icon, { viewBox: "0 0 16 16", ...props, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"path",
{
d: "M2.14645 11.1464C1.95118 11.3417 1.95118 11.6583 2.14645 11.8536C2.34171 12.0488 2.65829 12.0488 2.85355 11.8536L6.85355 7.85355C7.04882 7.65829 7.04882 7.34171 6.85355 7.14645L2.85355 3.14645C2.65829 2.95118 2.34171 2.95118 2.14645 3.14645C1.95118 3.34171 1.95118 3.65829 2.14645 3.85355L5.79289 7.5L2.14645 11.1464ZM8.14645 11.1464C7.95118 11.3417 7.95118 11.6583 8.14645 11.8536C8.34171 12.0488 8.65829 12.0488 8.85355 11.8536L12.8536 7.85355C13.0488 7.65829 13.0488 7.34171 12.8536 7.14645L8.85355 3.14645C8.65829 2.95118 8.34171 2.95118 8.14645 3.14645C7.95118 3.34171 7.95118 3.65829 8.14645 3.85355L11.7929 7.5L8.14645 11.1464Z",
fill: "currentColor"
}
) });
};
PaginationLastIcon.displayName = "PaginationLastIcon";
PaginationLastIcon.__ui__ = "PaginationLastIcon";
var PaginationPrevIcon = (props) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.Icon, { viewBox: "0 0 16 16", ...props, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"path",
{
d: "M7.219 8l3.3 3.3-.943.943L5.333 8l4.243-4.243.943.943-3.3 3.3z",
fill: "currentColor"
}
) });
};
PaginationPrevIcon.displayName = "PaginationPrevIcon";
PaginationPrevIcon.__ui__ = "PaginationPrevIcon";
var PaginationNextIcon = (props) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_icon.Icon, { viewBox: "0 0 16 16", ...props, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"path",
{
d: "M8.781 8l-3.3-3.3.943-.943L10.667 8l-4.243 4.243-.943-.943 3.3-3.3z",
fill: "currentColor"
}
) });
};
PaginationNextIcon.displayName = "PaginationNextIcon";
PaginationNextIcon.__ui__ = "PaginationNextIcon";
// src/use-pagination.ts
var import_use_controllable_state = require("@yamada-ui/use-controllable-state");
var import_use_value = require("@yamada-ui/use-value");
var import_utils = require("@yamada-ui/utils");
var import_react = require("react");
var [PaginationProvider, usePaginationContext] = (0, import_utils.createContext)({
name: "PaginationContext",
errorMessage: `usePaginationContext returned is 'undefined'. Seems you forgot to wrap the components in "<Pagination />"`
});
var computedRange = (start, end) => Array.from({ length: end - start + 1 }, (_, index) => index + start);
var usePagination = ({
boundaries: _boundaries = 1,
defaultPage = 1,
isDisabled = false,
disabled = isDisabled,
page,
siblings: _siblings = 1,
total,
onChange: onChangeProp
}) => {
const siblings = (0, import_use_value.useValue)(_siblings);
const boundaries = (0, import_use_value.useValue)(_boundaries);
const [currentPage, setCurrentPage] = (0, import_use_controllable_state.useControllableState)({
defaultValue: defaultPage,
value: page,
onChange: onChangeProp
});
const range = (0, import_react.useMemo)(() => {
const minimumTotal = siblings * 2 + 3 + boundaries * 2;
if (minimumTotal >= total) return computedRange(1, total);
const prevSiblings = Math.max(currentPage - siblings, boundaries);
const nextSiblings = Math.min(currentPage + siblings, total - boundaries);
const prevDots = prevSiblings > boundaries + 2;
const nextDots = nextSiblings < total - (boundaries + 1);
if (!prevDots && nextDots) {
const prevPages = siblings * 2 + boundaries + 2;
return [
...computedRange(1, prevPages),
"ellipsis",
...computedRange(total - (boundaries - 1), total)
];
}
if (prevDots && !nextDots) {
const nextPages = boundaries + 1 + 2 * siblings;
return [
...computedRange(1, boundaries),
"ellipsis",
...computedRange(total - nextPages, total)
];
}
return [
...computedRange(1, boundaries),
"ellipsis",
...computedRange(prevSiblings, nextSiblings),
"ellipsis",
...computedRange(total - boundaries + 1, total)
];
}, [boundaries, siblings, currentPage, total]);
const onFirst = (0, import_react.useCallback)(() => setCurrentPage(1), [setCurrentPage]);
const onLast = (0, import_react.useCallback)(
() => setCurrentPage(total),
[setCurrentPage, total]
);
const onPrev = (0, import_react.useCallback)(
() => setCurrentPage((prev) => prev === 1 ? prev : prev - 1),
[setCurrentPage]
);
const onNext = (0, import_react.useCallback)(
() => setCurrentPage((prev) => prev === total ? prev : prev + 1),
[setCurrentPage, total]
);
const onChange = (0, import_react.useCallback)(
(page2) => setCurrentPage(page2),
[setCurrentPage]
);
return {
currentPage,
disabled,
range,
total,
onChange,
onFirst,
onLast,
onNext,
onPrev
};
};
// src/pagination-item.tsx
var import_jsx_runtime2 = require("react/jsx-runtime");
var iconMap = {
ellipsis: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PaginationEllipsisIcon, {}),
first: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PaginationFirstIcon, {}),
last: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PaginationLastIcon, {}),
next: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PaginationNextIcon, {}),
prev: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(PaginationPrevIcon, {})
};
var PaginationItem = (0, import_core.forwardRef)(
({
className,
isActive,
active = isActive,
page,
children = ((_a) => (_a = iconMap[page]) != null ? _a : page)(),
isDisabled,
disabled = isDisabled,
disableRipple,
...rest
}, ref) => {
const styles = usePaginationContext();
const ellipsis = page === "ellipsis";
const { onPointerDown, ...rippleProps } = (0, import_ripple.useRipple)({
...rest,
disabled: disableRipple || disabled || ellipsis
});
const css = {
alignItems: "center",
display: "flex",
justifyContent: "center",
overflow: "hidden",
position: "relative",
userSelect: "none",
...styles.item,
...styles[page]
};
const Component = import_core.ui[ellipsis ? "span" : "button"];
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
Component,
{
ref,
...!ellipsis ? {
type: "button",
"data-disabled": (0, import_utils2.dataAttr)(disabled),
"data-selected": (0, import_utils2.dataAttr)(active),
disabled
} : {},
className: (0, import_utils2.cx)(
"ui-pagination__item",
ellipsis ? "ui-pagination__item--ellipsis" : void 0,
className
),
tabIndex: !ellipsis ? 0 : -1,
__css: css,
...rest,
onPointerDown,
children: [
children,
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_ripple.Ripple, { ...rippleProps })
]
}
);
}
);
PaginationItem.displayName = "PaginationItem";
PaginationItem.__ui__ = "PaginationItem";
// src/pagination.tsx
var import_jsx_runtime3 = require("react/jsx-runtime");
var Pagination = (0, import_core2.forwardRef)((props, ref) => {
const [styles, mergedProps] = (0, import_core2.useComponentMultiStyle)("Pagination", props);
const {
className,
boundaries,
component: Component = PaginationItem,
defaultPage,
isDisabled,
disabled = isDisabled,
page,
siblings,
total,
withControls: _withControls = true,
withEdges: _withEdges = false,
containerProps,
controlNextProps,
controlPrevProps,
controlProps,
edgeFirstProps,
edgeLastProps,
edgeProps,
itemProps,
onChange: onChangeProp,
...rest
} = (0, import_core2.omitThemeProps)(mergedProps);
const withControls = (0, import_use_value2.useValue)(_withControls);
const withEdges = (0, import_use_value2.useValue)(_withEdges);
const { currentPage, range, onChange, onFirst, onLast, onNext, onPrev } = usePagination({
boundaries,
defaultPage,
disabled,
page,
siblings,
total,
onChange: onChangeProp
});
const children = (0, import_react2.useMemo)(
() => range.map((page2, key) => /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core2.ui.li, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
Component,
{
"aria-label": page2 !== "ellipsis" ? `Go to page ${page2}` : void 0,
active: currentPage === page2,
disabled,
...itemProps,
page: page2,
onClick: (0, import_utils3.handlerAll)(
itemProps == null ? void 0 : itemProps.onClick,
page2 !== "ellipsis" ? () => onChange(page2) : void 0
)
}
) }, key)),
[Component, currentPage, disabled, onChange, range, itemProps]
);
const css = {
...styles.container
};
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(PaginationProvider, { value: styles, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
import_core2.ui.nav,
{
ref,
className: (0, import_utils3.cx)("ui-pagination", className),
"data-disabled": (0, import_utils3.dataAttr)(disabled),
__css: css,
...containerProps,
children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
import_core2.ui.ul,
{
className: "ui-pagination-inner",
"data-disabled": (0, import_utils3.dataAttr)(disabled),
__css: {
alignItems: "center",
display: "flex",
...styles.inner
},
...rest,
children: [
withEdges ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core2.ui.li, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
Component,
{
className: "ui-pagination__item--first",
"aria-label": "Go to first page",
disabled: disabled || currentPage === 1,
page: "first",
...edgeProps,
...edgeFirstProps,
onClick: (0, import_utils3.handlerAll)(
edgeProps == null ? void 0 : edgeProps.onClick,
edgeFirstProps == null ? void 0 : edgeFirstProps.onClick,
onFirst
)
}
) }) : null,
withControls ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core2.ui.li, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
Component,
{
className: "ui-pagination__item--prev",
"aria-label": "Go to previous page",
disabled: disabled || currentPage === 1,
page: "prev",
...controlProps,
...controlPrevProps,
onClick: (0, import_utils3.handlerAll)(
controlProps == null ? void 0 : controlProps.onClick,
controlPrevProps == null ? void 0 : controlPrevProps.onClick,
onPrev
)
}
) }) : null,
children,
withControls ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core2.ui.li, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
Component,
{
className: "ui-pagination__item--next",
"aria-label": "Go to next page",
disabled: disabled || currentPage === total,
page: "next",
...controlProps,
...controlNextProps,
onClick: (0, import_utils3.handlerAll)(
controlProps == null ? void 0 : controlProps.onClick,
controlNextProps == null ? void 0 : controlNextProps.onClick,
onNext
)
}
) }) : null,
withEdges ? /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_core2.ui.li, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
Component,
{
className: "ui-pagination__item--last",
"aria-label": "Go to last page",
disabled: disabled || currentPage === total,
page: "last",
...edgeProps,
...edgeLastProps,
onClick: (0, import_utils3.handlerAll)(
edgeProps == null ? void 0 : edgeProps.onClick,
edgeLastProps == null ? void 0 : edgeLastProps.onClick,
onLast
)
}
) }) : null
]
}
)
}
) });
});
Pagination.displayName = "Pagination";
Pagination.__ui__ = "Pagination";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Pagination
});
//# sourceMappingURL=pagination.js.map