Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Improve Pagination UI for Large Number of Articles #22

Open
LostMyCode opened this issue Jul 25, 2024 · 3 comments
Open

[Request] Improve Pagination UI for Large Number of Articles #22

LostMyCode opened this issue Jul 25, 2024 · 3 comments

Comments

@LostMyCode
Copy link

Currently, the pagination component displays a button for each page, which can result in a cluttered UI when there are a large number of articles. This PR aims to improve the pagination UI by implementing ellipsis truncation (...) to handle cases with many pages.
Instead of showing all page numbers, the pagination should show the first few pages, an ellipsis (...), and the last few pages. This will provide a cleaner and more user-friendly experience for navigating through a large number of articles.

image

@LostMyCode
Copy link
Author

nvm. resolved myself

diff --git a/src/layouts/components/Pagination.astro b/src/layouts/components/Pagination.astro
index fc0fb04..7e2c5aa 100644
--- a/src/layouts/components/Pagination.astro
+++ b/src/layouts/components/Pagination.astro
@@ -5,9 +5,32 @@ const indexPageLink = currentPage === 2;
 const hasPrevPage = currentPage > 1;
 const hasNextPage = totalPages > currentPage;
 
-let pageList = [];
-for (let i = 1; i <= totalPages; i++) {
-  pageList.push(i);
+const pageList: any[] = [];
+
+// Maximum number of page buttons to display
+const maxPageNumbers = 5;
+
+if (totalPages > maxPageNumbers) {
+  for (let i = 0; i < maxPageNumbers; i++) {
+    const page = currentPage + i - 2;
+
+    if (page <= 1 || pageList.includes(page) || page >= totalPages) {
+      continue;
+    }
+    pageList.push(page);
+  }
+}
+
+if (pageList[0] === 2) {
+  pageList.unshift(1);
+} else {
+  pageList.unshift(1, "...");
+}
+
+if (pageList[pageList.length - 1] === totalPages - 1) {
+  pageList.push(totalPages);
+} else {
+  pageList.push("...", totalPages);
 }
 ---
 
@@ -60,6 +83,12 @@ for (let i = 1; i <= totalPages; i++) {
 
       {/* page index */}
       {pageList.map((pagination, i) =>
+        pagination === "..." ? (
+          // Render ellipsis element
+          <span class="border border-gray-400 opacity-40 rounded-md h-10 w-10 leading-[36px] font-semibold text-dark flex items-center justify-center pointer-events-none">
+            ...
+          </span>
+        ) : // Render page number link
         pagination === currentPage ? (
           <span
             aria-current="page"
@@ -70,7 +99,7 @@ for (let i = 1; i <= totalPages; i++) {
         ) : (
           <a
             href={
-              i === 0
+              pagination === 1
                 ? `${section ? "/" + section : "/"}`
                 : `${section ? "/" + section : ""}/page/${pagination}`
             }

@tfsomrat
Copy link
Contributor

tfsomrat commented Aug 5, 2024

Hey @LostMyCode, Thanks for sharing. Please create a PR. we will merge it.

@LostMyCode
Copy link
Author

Gotcha! 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants