@primer/react-brand
Version:
Primer Brand is a GitHub's design system for creating React-based marketing websites and digital experiences.
150 lines (149 loc) • 4.39 kB
JavaScript
import e from "../node_modules/clsx/dist/clsx.js";
import { ExpandableArrow as t } from "../ExpandableArrow/ExpandableArrow.js";
import { Button as n } from "../Button/Button.js";
import { useWindowSize as r } from "../hooks/useWindowSize.js";
import { buildPaginationModel as i } from "./model.js";
import a from "./Pagination.module.js";
import o, { memo as s, useCallback as c } from "react";
import { jsx as l, jsxs as u } from "react/jsx-runtime";
//#region src/Pagination/Pagination.tsx
var d = {
prev: "Previous",
next: "Next",
prevAriaLabel: "Previous Page",
nextAriaLabel: "Next Page"
}, f = s(({ id: t, className: n, pageCount: s, currentPage: u, onPageChange: f, hrefBuilder: h = m, pageAttributesBuilder: g, marginPageCount: _ = 1, showPages: v = !0, surroundingPageCount: y = 2, labels: b, "aria-label": x, "data-testid": S, ...C }) => {
let { width: w } = r();
w && w < 768 && (_ = 1, y = 0);
let T = b?.prev ?? d.prev, E = b?.next ?? d.next, D = b?.prevAriaLabel ?? d.prevAriaLabel, O = b?.nextAriaLabel ?? d.nextAriaLabel, k = o.useRef(null), A = c((e) => (t) => {
f && f(t, e);
}, [f]), j = o.useMemo(() => i(s, u, v, _, y).map((e) => /* @__PURE__ */ l(p, {
page: e,
hrefBuilder: h,
pageAttributesBuilder: g,
prevLabel: T,
nextLabel: E,
prevAriaLabel: D,
nextAriaLabel: O,
onClick: A(e.num)
}, `${e.type}-${e.num}`)), [
s,
u,
v,
_,
y,
h,
g,
T,
E,
D,
O,
A
]);
return /* @__PURE__ */ l("nav", {
ref: k,
id: t,
className: e(a.Pagination, n),
"data-testid": S,
"aria-label": x || "Pagination",
...C,
children: /* @__PURE__ */ l("div", {
className: e(a.Pagination__inner),
children: j
})
});
}), p = ({ page: r, hrefBuilder: i, pageAttributesBuilder: s, prevLabel: c, nextLabel: d, prevAriaLabel: f, nextAriaLabel: p, onClick: m }) => {
let [h, g] = o.useState(!1), _ = {
role: "button",
size: "small",
variant: "subtle",
onClick: r.disabled ? void 0 : m
}, { className: v, ...y } = s?.(r.num, r) ?? {};
switch (r.type) {
case "PREV": return /* @__PURE__ */ l(n, {
..._,
as: "a",
rel: "prev",
href: r.disabled ? void 0 : i(r.num),
"aria-disabled": r.disabled || void 0,
"aria-label": f,
...y,
className: e(a.Pagination__controlItem, v),
onMouseEnter: () => g(!0),
onMouseLeave: () => g(!1),
onFocus: () => g(!0),
onBlur: () => g(!1),
children: /* @__PURE__ */ u("span", {
className: a.Pagination__controlContent,
children: [/* @__PURE__ */ l("span", {
className: e(a.Pagination__controlArrowWrapper, a["Pagination__controlArrowWrapper--previous"]),
children: /* @__PURE__ */ l(t, {
hidden: !0,
reverse: !0,
expanded: !r.disabled && h,
className: a.Pagination__controlArrow
})
}), /* @__PURE__ */ l("span", {
className: a.Pagination__controlText,
children: c
})]
})
});
case "NEXT": return /* @__PURE__ */ l(n, {
..._,
as: "a",
rel: "next",
href: r.disabled ? void 0 : i(r.num),
"aria-disabled": r.disabled || void 0,
"aria-label": p,
...y,
className: e(a.Pagination__controlItem, v),
onMouseEnter: () => g(!0),
onMouseLeave: () => g(!1),
onFocus: () => g(!0),
onBlur: () => g(!1),
children: /* @__PURE__ */ u("span", {
className: a.Pagination__controlContent,
children: [/* @__PURE__ */ l("span", {
className: a.Pagination__controlText,
children: d
}), /* @__PURE__ */ l("span", {
className: e(a.Pagination__controlArrowWrapper, a["Pagination__controlArrowWrapper--next"]),
children: /* @__PURE__ */ l(t, {
hidden: !0,
expanded: !r.disabled && h,
className: a.Pagination__controlArrow
})
})]
})
});
case "NUM": return /* @__PURE__ */ l(n, {
..._,
as: "a",
variant: r.selected ? "primary" : "subtle",
className: e(a.Pagination__pageItem, v),
href: i(r.num),
"aria-label": `Page ${r.num}${r.precedesBreak ? "..." : ""}`,
"aria-current": r.selected ? "page" : void 0,
...y,
children: r.num
});
case "BREAK": return /* @__PURE__ */ l(n, {
..._,
as: "a",
className: e(a.Pagination__pageItem, v),
variant: "subtle",
role: "presentation",
href: void 0,
onClick: void 0,
...y,
children: "…"
});
}
};
function m(e) {
return `#${e}`;
}
//#endregion
export { f as Pagination };
//# sourceMappingURL=Pagination.js.map