Skip to content

Commit ca4f02f

Browse files
Avo 4 - human-readable pagination pages (#4353)
1 parent 2757f27 commit ca4f02f

3 files changed

Lines changed: 53 additions & 23 deletions

File tree

app/assets/stylesheets/css/pagination.css

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
.pagination__controls, .pagination__info, .pagination__nav (elements) */
55

66
.pagination {
7-
@apply flex w-full flex-col items-stretch justify-between gap-2 sm:flex-row sm:items-end sm:gap-0;
7+
@apply flex w-full flex-col items-stretch justify-between gap-2 sm:flex-row sm:flex-wrap sm:items-end sm:gap-0;
88
}
99

1010
/* === Per-page selector (left) === */
1111

1212
.pagination__per-page {
1313
--pagination-per-page-width: 10rem; /* w-48 equivalent; use 165px for exact Figma match */
14-
@apply flex items-center;
14+
@apply flex shrink-0 items-center;
1515
}
1616

1717
.pagination__per-page-input {
@@ -49,13 +49,19 @@
4949

5050
/* === Controls pill (right) === */
5151

52+
.pagination__controls-wrap {
53+
@apply flex w-full min-w-0 sm:flex-[1_1_28rem] sm:min-w-[22rem] sm:justify-end;
54+
}
55+
5256
.pagination__controls {
53-
@apply flex w-fit shrink-0 items-center gap-3 rounded-lg border border-tertiary bg-primary overflow-hidden ps-3 pe-2 overflow-visible;
57+
@apply flex w-full min-w-0 max-w-full items-center gap-3 rounded-lg border border-tertiary bg-primary ps-3 pe-2 overflow-x-auto overflow-y-hidden overscroll-x-contain touch-pan-x sm:w-fit;
58+
-webkit-overflow-scrolling: touch;
59+
scrollbar-gutter: stable;
5460
}
5561

5662
/* Info text with right-border divider */
5763
.pagination__info {
58-
@apply flex items-center py-1 pe-3 border-e border-tertiary whitespace-nowrap overflow-hidden text-ellipsis text-sm text-content leading-5 font-normal;
64+
@apply shrink-0 flex items-center py-1 pe-3 border-e border-tertiary whitespace-nowrap text-sm text-content leading-5 font-normal;
5965

6066
&:last-child {
6167
@apply border-e-0 pe-1;
@@ -68,12 +74,12 @@
6874

6975
/* Nav buttons wrapper */
7076
.pagination__nav {
71-
@apply flex items-center;
77+
@apply shrink-0 flex items-center;
7278
}
7379

7480
/* === Pagy nav === */
7581
.pagy.series-nav {
76-
@apply flex items-center gap-0.5;
82+
@apply flex min-w-max items-center gap-0.5 whitespace-nowrap;
7783

7884
a[role="separator"] {
7985
@apply px-2 text-sm text-content-secondary;

app/components/avo/paginator_component.html.erb

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,29 @@
2929
</div>
3030

3131
<%# Right: info + navigation grouped pill %>
32-
<div class="pagination__controls">
33-
<% if @resource.pagination_type.default? %>
34-
<div class="pagination__info">
35-
<span class="pagination__info-number">
36-
<%= "#{@pagy.from}-#{@pagy.to}" %>
37-
</span>
38-
&nbsp; of &nbsp;
39-
<span>
40-
<%= "#{@pagy.count}" %>
41-
</span>
42-
</div>
43-
<% end %>
32+
<div class="pagination__controls-wrap">
33+
<div class="pagination__controls">
34+
<% if @resource.pagination_type.default? %>
35+
<div class="pagination__info">
36+
<span class="pagination__info-number">
37+
<%= "#{formatted_number(@pagy.from)}-#{formatted_number(@pagy.to)}" %>
38+
</span>
39+
&nbsp; of &nbsp;
40+
<% if @pagy.count >= 10_000 %>
41+
<span title="<%= formatted_count %>" data-tippy="tooltip">
42+
<%= number_to_social(@pagy.count, start_at: 10_000) %>
43+
</span>
44+
<% else %>
45+
<span><%= formatted_count %></span>
46+
<% end %>
47+
</div>
48+
<% end %>
4449

45-
<% if @pagy.pages > 1 %>
46-
<div class="pagination__nav">
47-
<%== @pagy.series_nav(anchor_string: "data-turbo-frame=\"#{@turbo_frame}\"") %>
48-
</div>
49-
<% end %>
50+
<% if @pagy.pages > 1 %>
51+
<div class="pagination__nav">
52+
<%== formatted_series_nav %>
53+
</div>
54+
<% end %>
55+
</div>
5056
</div>
5157
</div>

app/components/avo/paginator_component.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class Avo::PaginatorComponent < Avo::BaseComponent
4+
NUMBER_DELIMITER = "."
5+
46
prop :resource
57
prop :parent_record
68
prop :parent_resource
@@ -52,4 +54,20 @@ def per_page_option_label(option)
5254
num = helpers.content_tag(:span, option, class: "pagination__per-page-option-num")
5355
"#{num} #{t("avo.per_page").downcase}".html_safe
5456
end
57+
58+
def formatted_count
59+
formatted_number(@pagy.count)
60+
end
61+
62+
def formatted_series_nav
63+
@pagy.series_nav(anchor_string: %(data-turbo-frame="#{@turbo_frame}"))
64+
.gsub(/>(\d{4,})</) { |match| match.sub($1, formatted_number($1)) }
65+
.html_safe
66+
end
67+
68+
private
69+
70+
def formatted_number(number)
71+
helpers.number_with_delimiter(number, delimiter: NUMBER_DELIMITER)
72+
end
5573
end

0 commit comments

Comments
 (0)