Repository: woylie/flop_phoenix
Pull request #415 (2025-02-01 12:17:59)

remove wrapper_attrs from pagination options
contributes to #411 


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61bdcf8..8181ae8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,33 @@
 
 ## Unreleased
 
+### Changed
+
+- Accept global attributes for pagination component.
+- Remove default `class` from pagination component.
+- Remove `role` attribute from the pagination component. The `<nav>` element
+  already has the implicit ARIA role `navigation`.
+
+### How to upgrade
+
+Remove the `wrapper_opts` from your pagination options and pass them directly
+as attributes instead.
+
+```diff
+  <Flop.Phoenix.pagination
+    meta={@meta}
+    path={@path}
+-   opts={[
+-     wrapper_attrs: [
+-       class: "pagination",
+-       aria: [label: "Quppernerit"]
+-     ]
+-   ]}
++   class="pagination"
++   aria-label="Quppernerit"
+/>
+```
+
 ## [0.24.0] - 2025-02-01
 
 ### Added
diff --git a/lib/flop_phoenix.ex b/lib/flop_phoenix.ex
index 21adb6d..574db58 100644
--- a/lib/flop_phoenix.ex
+++ b/lib/flop_phoenix.ex
@@ -179,9 +179,6 @@ defmodule Flop.Phoenix do
     Default: `#{inspect(Pagination.default_opts()[:previous_link_attrs])}`.
   - `:previous_link_content` - The content for the link to the previous page.
     Default: `#{inspect(Pagination.default_opts()[:previous_link_content])}`.
-  - `:wrapper_attrs` - The attributes for the `<nav>` element that wraps the
-    pagination links.
-    Default: `#{inspect(Pagination.default_opts()[:wrapper_attrs])}`.
   """
   @type pagination_option ::
           {:current_link_attrs, keyword}
@@ -196,7 +193,6 @@ defmodule Flop.Phoenix do
           | {:pagination_list_item_attrs, keyword}
           | {:previous_link_attrs, keyword}
           | {:previous_link_content, Phoenix.HTML.safe() | binary}
-          | {:wrapper_attrs, keyword}
 
   @typedoc """
   Defines how many page links to render.
@@ -415,6 +411,19 @@ defmodule Flop.Phoenix do
     described in the `Customization` section of the module documentation.
     """
 
+  attr :rest, :global,
+    default: %{"aria-label": "Pagination"},
+    doc: """
+    The attributes are added to the outer `<nav>` element.
+
+
+    The `aria-label` defaults to `"Pagination"`. If your application is
+    localized, the label should be translated to the user locale. In languages
+    with latin characters, the first letter should be capitalized. If multiple
+    pagination components are rendered on the same page, each one should have
+    a distinct aria label.
+    """
+
   def pagination(%{path: nil, on_paginate: nil}) do
     raise Flop.Phoenix.PathOrJSError, component: :pagination
   end
@@ -430,7 +439,7 @@ defmodule Flop.Phoenix do
       path={@path}
       reverse={@reverse}
     >
-      <nav {@opts[:wrapper_attrs]}>
+      <nav {@rest}>
         <.pagination_link
           :if={p.pagination_type in [:page, :offset]}
           disabled={is_nil(p.previous_page)}
diff --git a/lib/flop_phoenix/pagination.ex b/lib/flop_phoenix/pagination.ex
index df5f9a2..4783676 100644
--- a/lib/flop_phoenix/pagination.ex
+++ b/lib/flop_phoenix/pagination.ex
@@ -288,12 +288,7 @@ defmodule Flop.Phoenix.Pagination do
         aria: [label: "Go to previous page"],
         class: "pagination-previous"
       ],
-      previous_link_content: "Previous",
-      wrapper_attrs: [
-        class: "pagination",
-        role: "navigation",
-        aria: [label: "pagination"]
-      ]
+      previous_link_content: "Previous"
     ]
   end
 


Test patch:
diff --git a/test/flop_phoenix_test.exs b/test/flop_phoenix_test.exs
index 999df2b..86b255f 100644
--- a/test/flop_phoenix_test.exs
+++ b/test/flop_phoenix_test.exs
@@ -72,9 +72,7 @@ defmodule Flop.PhoenixTest do
 
       nav = find_one(html, "nav:root")
 
-      assert attribute(nav, "aria-label") == "pagination"
-      assert attribute(nav, "class") == "pagination"
-      assert attribute(nav, "role") == "navigation"
+      assert attribute(nav, "aria-label") == "Pagination"
     end
 
     test "does not render anything if there is only one page" do
@@ -93,7 +91,7 @@ defmodule Flop.PhoenixTest do
              """) == []
     end
 
-    test "allows to overwrite wrapper class" do
+    test "allows to set global attributes" do
       assigns = %{meta: build(:meta_on_first_page)}
 
       html =
@@ -101,35 +99,15 @@ defmodule Flop.PhoenixTest do
         <Flop.Phoenix.pagination
           meta={@meta}
           on_paginate={JS.push("paginate")}
-          opts={[wrapper_attrs: [class: "boo"]]}
+          class="boo"
+          aria-label="Blaðsíðuskipting"
         />
         """)
 
       nav = find_one(html, "nav:root")
 
-      assert attribute(nav, "aria-label") == "pagination"
+      assert attribute(nav, "aria-label") == "Blaðsíðuskipting"
       assert attribute(nav, "class") == "boo"
-      assert attribute(nav, "role") == "navigation"
-    end
-
-    test "allows to add attributes to wrapper" do
-      assigns = %{meta: build(:meta_on_first_page)}
-
-      html =
-        parse_heex(~H"""
-        <Flop.Phoenix.pagination
-          meta={@meta}
-          on_paginate={JS.push("paginate")}
-          opts={[wrapper_attrs: [title: "paginate"]]}
-        />
-        """)
-
-      nav = find_one(html, "nav:root")
-
-      assert attribute(nav, "aria-label") == "pagination"
-      assert attribute(nav, "class") == "pagination"
-      assert attribute(nav, "role") == "navigation"
-      assert attribute(nav, "title") == "paginate"
     end
 
     test "renders previous link" do
@@ -1116,55 +1094,6 @@ defmodule Flop.PhoenixTest do
   end
 
   describe "pagination/1 with cursor pagination" do
-    test "renders pagination wrapper" do
-      assigns = %{meta: build(:meta_with_cursors)}
-
-      html =
-        parse_heex(~H"""
-        <Flop.Phoenix.pagination meta={@meta} path="/pets" />
-        """)
-
-      assert nav = find_one(html, "nav")
-      assert attribute(nav, "aria-label") == "pagination"
-      assert attribute(nav, "class") == "pagination"
-      assert attribute(nav, "role") == "navigation"
-    end
-
-    test "allows to overwrite wrapper class" do
-      assigns = %{
-        meta: build(:meta_with_cursors),
-        opts: [wrapper_attrs: [class: "boo"]]
-      }
-
-      html =
-        parse_heex(~H"""
-        <Flop.Phoenix.pagination meta={@meta} path="/pets" opts={@opts} />
-        """)
-
-      assert wrapper = find_one(html, "nav")
-      assert attribute(wrapper, "aria-label") == "pagination"
-      assert attribute(wrapper, "class") == "boo"
-      assert attribute(wrapper, "role") == "navigation"
-    end
-
-    test "allows to add attributes to wrapper" do
-      assigns = %{
-        meta: build(:meta_with_cursors),
-        opts: [wrapper_attrs: [title: "paginate"]]
-      }
-
-      html =
-        parse_heex(~H"""
-        <Flop.Phoenix.pagination meta={@meta} path="/pets" opts={@opts} />
-        """)
-
-      assert wrapper = find_one(html, "nav")
-      assert attribute(wrapper, "aria-label") == "pagination"
-      assert attribute(wrapper, "class") == "pagination"
-      assert attribute(wrapper, "role") == "navigation"
-      assert attribute(wrapper, "title") == "paginate"
-    end
-
     test "renders previous link" do
       assigns = %{meta: build(:meta_with_cursors)}