Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(pagination): stop the pager wrapping on narrow screens (#2967)

Guillem Arias Fauste6245b9cbd008
diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb
new file mode 100644
index 0000000000..5dc7d64a98
--- /dev/null
+++ b/app/helpers/pagination_helper.rb
@@ -0,0 +1,66 @@
+module PaginationHelper
+ # Below this many slots, Pagy only emits a short series because the
+ # collection itself has that few pages — so every number is one or two
+ # digits and the whole row fits a phone. Nothing is hidden there, otherwise a
+ # 3-page pager would render as "1 3" with the middle page missing.
+ SHORT_SERIES_LENGTH = 5
+
+ # Per-slot rendering plan for `pagy.series` on the shared pagination bar, one
+ # entry per slot:
+ #
+ # :visible shown at every width
+ # :desktop_only hidden below `sm`
+ # :collapsed hidden below `sm`, and a mobile-only "…" stands in for the
+ # run of hidden slots starting here
+ #
+ # A full series (up to ~9 slots, and 3-digit page numbers on a large
+ # collection) needs ~198px of row. A 360px phone leaves ~158px once the
+ # chevrons and the per-page select are accounted for, so the row could not
+ # fit — it used to wrap the last page onto a second line.
+ #
+ # On small screens we keep the first page, the last page, the current page
+ # and Pagy's own gaps, and drop the rest. Dropping alone is not enough: it
+ # would render "1 3 … 760" for [1, 2, "3", 4, 5, :gap, 760], or "1 6" for a
+ # gapless 6-page series, implying those pages are adjacent when a page was
+ # silently removed. So each dropped run is represented by a "…" of its own —
+ # unless Pagy already put a gap right beside that run, which would otherwise
+ # read as "… …".
+ #
+ # `sm:` is a viewport breakpoint, so a narrow *column* on a wide screen (the
+ # account activity feed with both sidebars open) still gets the full series.
+ # The row is a nowrap flex that scrolls, so that case degrades to a short
+ # horizontal scroll rather than wrapping again.
+ def pagination_mobile_plan(series)
+ return Array.new(series.length, :visible) if series.length <= SHORT_SERIES_LENGTH
+
+ plan = series.each_with_index.map do |item, index|
+ keep = item == :gap || # Pagy's own elision
+ item.is_a?(String) || # the current page
+ index.zero? || # first page
+ index == series.length - 1 # last page
+ keep ? :visible : :desktop_only
+ end
+
+ # Promote the head of each dropped run to `:collapsed` so the view can put
+ # a "…" there, unless a real gap already borders the run. Runs are read off
+ # `dropped` rather than `plan`, which is being mutated as we go — reading
+ # the live array made every slot after the head look like a fresh run and
+ # emitted one "…" per hidden page.
+ dropped = plan.map { |state| state == :desktop_only }
+
+ plan.each_index do |index|
+ next unless dropped[index]
+ next if index.positive? && dropped[index - 1] # not the head of its run
+
+ run_end = index
+ run_end += 1 while dropped[run_end + 1]
+
+ before = index.positive? ? series[index - 1] : nil
+ after = series[run_end + 1]
+
+ plan[index] = :collapsed unless before == :gap || after == :gap
+ end
+
+ plan
+ end
+end
diff --git a/app/views/shared/_pagination.html.erb b/app/views/shared/_pagination.html.erb
index f622b7197d..993e04092e 100644
--- a/app/views/shared/_pagination.html.erb
+++ b/app/views/shared/_pagination.html.erb
@@ -1,7 +1,9 @@
<%# locals: (pagy:) %>
<nav class="flex w-full items-center justify-between">
- <div class="flex items-center gap-1 mr-3">
+ <%# min-w-0 so the series pill can actually shrink below its content width;
+ without it the flex item keeps its intrinsic size and overflows the nav. %>
+ <div class="flex min-w-0 items-center gap-1 mr-3">
<div>
<% if pagy.prev %>
<%= link_to pagy_url_for(pagy, pagy.prev),
@@ -15,22 +17,35 @@
</div>
<% end %>
</div>
- <div class="rounded-xl p-1 bg-container-inset">
- <% pagy.series.each do |series_item| %>
+ <%# A flex row, not a block: as a block the inline-flex page links flowed as
+ inline content and wrapped like words, dropping the last page onto a
+ second line. `overflow-x-auto` is the backstop for a container too
+ narrow even for the reduced set (see PaginationHelper). %>
+ <% series = pagy.series %>
+ <% mobile_plan = pagination_mobile_plan(series) %>
+ <div class="rounded-xl p-1 bg-container-inset flex items-center flex-nowrap overflow-x-auto max-w-full">
+ <% series.each_with_index do |series_item, index| %>
+ <% mobile_state = mobile_plan[index] %>
+ <%# Stands in for the run of pages hidden from here on a phone, so the
+ remaining numbers don't read as adjacent. Mirrors the real gap's
+ markup and disappears from `sm` up, where the run is shown. %>
+ <% if mobile_state == :collapsed %>
+ <span class="inline-flex sm:hidden shrink-0 items-center px-2 py-1 text-sm font-medium text-secondary" aria-hidden="true">...</span>
+ <% end %>
<% if series_item.is_a?(Integer) %>
<%= link_to pagy_url_for(pagy, series_item),
data: { turbo_frame: :_top },
- class: "rounded-md px-2 py-1 inline-flex items-center text-sm font-medium text-secondary hover:border-secondary hover:text-secondary" do %>
+ class: class_names("rounded-md px-2 py-1 shrink-0 items-center text-sm font-medium text-secondary hover:border-secondary hover:text-secondary", mobile_state == :visible ? "inline-flex" : "hidden sm:inline-flex") do %>
<%= series_item %>
<% end %>
<% elsif series_item.is_a?(String) %>
<%= link_to pagy_url_for(pagy, series_item),
data: { turbo_frame: :_top },
- class: "rounded-md px-2 py-1 bg-container border border-secondary shadow-xs inline-flex items-center text-sm font-medium text-primary" do %>
+ class: "rounded-md px-2 py-1 bg-container border border-secondary shadow-xs inline-flex shrink-0 items-center text-sm font-medium text-primary" do %>
<%= series_item %>
<% end %>
<% elsif series_item == :gap %>
- <span class="inline-flex items-center px-2 py-1 text-sm font-medium text-secondary">...</span>
+ <span class="inline-flex shrink-0 items-center px-2 py-1 text-sm font-medium text-secondary">...</span>
<% end %>
<% end %>
</div>
diff --git a/test/helpers/pagination_helper_test.rb b/test/helpers/pagination_helper_test.rb
new file mode 100644
index 0000000000..c741bfc5c1
--- /dev/null
+++ b/test/helpers/pagination_helper_test.rb
@@ -0,0 +1,84 @@
+require "test_helper"
+
+class PaginationHelperTest < ActionView::TestCase
+ include PaginationHelper
+
+ # Renders what a phone would show: kept slots, plus a "…" wherever a run of
+ # pages is collapsed. Pagy marks the current page by making it a String.
+ def mobile_render(series)
+ plan = pagination_mobile_plan(series)
+ series.each_with_index.filter_map do |item, i|
+ case plan[i]
+ when :visible then item == :gap ? "…" : item.to_s
+ when :collapsed then "…"
+ end
+ end.join(" ")
+ end
+
+ test "collapses the middle of a long series behind an ellipsis" do
+ assert_equal "1 … 5 … 760", mobile_render([ 1, :gap, 3, 4, "5", 6, 7, :gap, 760 ])
+ end
+
+ test "keeps the first and last page of a long series" do
+ plan = pagination_mobile_plan([ "1", 2, 3, 4, 5, :gap, 760 ])
+
+ assert_equal :visible, plan.first, "first page must stay"
+ assert_equal :visible, plan.last, "last page must stay"
+ end
+
+ # A short series only happens when the collection has that few pages, so the
+ # numbers are 1-2 digits and the row fits. Hiding here would render "1 3".
+ test "leaves a short series untouched" do
+ assert_equal "1 2 3", mobile_render([ "1", 2, 3 ])
+ end
+
+ # Regression: dropping pages without standing an ellipsis in their place made
+ # the survivors look adjacent — "1 3 … 760" hid page 2 with no sign of it.
+ test "a collapsed run away from any gap gets its own ellipsis" do
+ assert_equal "1 … 3 … 760", mobile_render([ 1, 2, "3", 4, 5, :gap, 760 ])
+ end
+
+ test "a gapless series still marks the pages it drops" do
+ assert_equal "1 … 6", mobile_render([ "1", 2, 3, 4, 5, 6 ])
+ end
+
+ # Pagy's own gap already stands for the skipped pages; adding another beside
+ # it would render "… …".
+ test "does not double up on an ellipsis Pagy already emitted" do
+ assert_equal "1 … 760", mobile_render([ "1", 2, 3, 4, 5, :gap, 760 ])
+ end
+
+ test "never hides a gap or the current page" do
+ plan = pagination_mobile_plan([ 1, :gap, 3, 4, "5", 6, 7, :gap, 760 ])
+
+ assert_equal :visible, plan[1], ":gap must stay"
+ assert_equal :visible, plan[4], "current page must stay"
+ assert_equal :visible, plan[7], ":gap must stay"
+ end
+
+ # Whatever is collapsed, a phone must never be shown two numbers that are not
+ # actually consecutive without a "…" between them.
+ test "no surviving pair of pages reads as adjacent unless it is" do
+ checked = 0
+
+ [
+ [ 1, 2, "3", 4, 5, :gap, 760 ],
+ [ "1", 2, 3, 4, 5, 6 ],
+ [ 1, :gap, 3, 4, "5", 6, 7, :gap, 760 ],
+ [ 1, 2, 3, 4, "5", 6, 7 ],
+ [ 1, :gap, 756, 757, 758, 759, "760" ],
+ [ 1, "2", 3, 4, 5, 6 ] # current next to the first page: 1 and 2 do survive side by side
+ ].each do |series|
+ tokens = mobile_render(series).split(" ")
+ tokens.each_cons(2) do |a, b|
+ next if a == "…" || b == "…"
+ checked += 1
+ assert_equal a.to_i + 1, b.to_i,
+ "#{series.inspect} rendered #{tokens.join(' ')}: #{a} and #{b} are not consecutive"
+ end
+ end
+
+ assert_operator checked, :>, 0,
+ "every pair was separated by an ellipsis, so this asserted nothing — add a series where two numbers survive side by side"
+ end
+end