The personal finance app for everyone (by everyone)
Ruby
Fix reports page touch jitter and iOS Safari scroll freeze (#3132)
GFRd3249e6879d0
diff --git a/app/javascript/controllers/reports_sortable_controller.js b/app/javascript/controllers/reports_sortable_controller.jsindex a7453f1c3b..1bb0a2a46b 100644--- a/app/javascript/controllers/reports_sortable_controller.js+++ b/app/javascript/controllers/reports_sortable_controller.js@@ -3,14 +3,15 @@ import { Controller } from "@hotwired/stimulus";export default class extends Controller {static targets = ["section", "handle"];- // Short delay to prevent accidental touches on the grip handle+ // Hold delay to require deliberate press-and-hold before activating drag modestatic values = {- holdDelay: { type: Number, default: 150 },+ holdDelay: { type: Number, default: 800 },};connect() {this.draggedElement = null;this.placeholder = null;+ this.touchStartX = 0;this.touchStartY = 0;this.currentTouchY = 0;this.isTouching = false;@@ -21,6 +22,14 @@ export default class extends Controller {// ===== Mouse Drag Events =====dragStart(event) {+ // If a touch interaction is in progress, cancel native drag —+ // use touch events with hold delay instead.+ // This avoids blocking mouse/trackpad drag on touch-capable laptops.+ if (this.isTouching || this.pendingSection) {+ event.preventDefault();+ return;+ }+this.draggedElement = event.currentTarget;this.draggedElement.classList.add("opacity-50");this.draggedElement.setAttribute("aria-grabbed", "true");@@ -67,8 +76,8 @@ export default class extends Controller {}// ===== Touch Events =====- // Touch events are bound to the drag handle only, with a short hold delay- // to prevent accidental touches.+ // A press-and-hold gesture activates drag mode; a plain scroll gesture+ // (finger moves before the hold delay elapses) cancels it.touchStart(event) {// Find the parent section element from the handle@@ -78,10 +87,15 @@ export default class extends Controller {if (!section) return;this.pendingSection = section;+ this.touchStartX = event.touches[0].clientX;this.touchStartY = event.touches[0].clientY;this.currentTouchY = this.touchStartY;this.holdActivated = false;+ // Prevent text selection while waiting for hold to activate+ section.style.userSelect = "none";+ section.style.webkitUserSelect = "none";+// Start hold timerthis.holdTimer = setTimeout(() => {this.activateDrag();@@ -104,10 +118,24 @@ export default class extends Controller {}touchMove(event) {- if (!this.holdActivated || !this.isTouching || !this.draggedElement) return;+ const touchX = event.touches[0].clientX;+ const touchY = event.touches[0].clientY;++ // If hold hasn't activated yet, cancel if user moves too far (scrolling or swiping)+ // Uses Euclidean distance to catch diagonal gestures too+ if (!this.holdActivated) {+ const dx = touchX - this.touchStartX;+ const dy = touchY - this.touchStartY;+ if (dx * dx + dy * dy > 100) { // 10px radius+ this.cancelHold();+ }+ return;+ }++ if (!this.isTouching || !this.draggedElement) return;event.preventDefault();- this.currentTouchY = event.touches[0].clientY;+ this.currentTouchY = touchY;const afterElement = this.getDragAfterElement(this.currentTouchY);this.clearPlaceholders();@@ -144,6 +172,27 @@ export default class extends Controller {this.resetTouchState();}+ touchCancel() {+ this.cancelHold();++ if (this.draggedElement) {+ this.draggedElement.classList.remove("opacity-50", "scale-[1.02]");+ this.draggedElement.setAttribute("aria-grabbed", "false");+ }++ this.clearPlaceholders();+ this.resetTouchState();+ }++ // Belt-and-suspenders alongside the `[-webkit-touch-callout:none]` class:+ // if a long-press context menu/link-preview still fires (e.g. a browser+ // that ignores the CSS property), don't let it interrupt the hold gesture.+ suppressContextMenu(event) {+ if (this.pendingSection || this.isTouching) {+ event.preventDefault();+ }+ }+cancelHold() {if (this.holdTimer) {clearTimeout(this.holdTimer);@@ -152,6 +201,16 @@ export default class extends Controller {}resetTouchState() {+ // Restore text selection+ if (this.pendingSection) {+ this.pendingSection.style.userSelect = "";+ this.pendingSection.style.webkitUserSelect = "";+ }+ if (this.draggedElement) {+ this.draggedElement.style.userSelect = "";+ this.draggedElement.style.webkitUserSelect = "";+ }+this.isTouching = false;this.draggedElement = null;this.pendingSection = null;diff --git a/app/views/reports/_category_row.html.erb b/app/views/reports/_category_row.html.erbindex 2b932d1068..221fa4498e 100644--- a/app/views/reports/_category_row.html.erb+++ b/app/views/reports/_category_row.html.erb@@ -11,63 +11,70 @@%><tr data-category="category-<%= item[:category_id] %>"- class="text-secondary text-sm <%= show_border ? "table-divider" : "" %> <%= clickable ? "group/category-row" : "" %>">+ class="text-secondary text-sm group/category-row <%= show_border ? "table-divider" : "" %>"><td colspan="2" class="py-3 px-4 lg:px-6 <%= is_sub ? "pl-7 lg:pl-9" : "" %>">- <div class="flex items-center gap-2 <%= clickable ? "relative" : "" %>">- <% if is_sub %>- <div class="text-subdued">- <%= icon "corner-down-right" %>- </div>- <% end %>- <% if item[:category_icon] %>- <div class="h-7 w-7 flex-shrink-0 rounded-full flex justify-center items-center <%= clickable ? "group-hover/category-row:scale-105 transition-all duration-300" : "" %>"- style="- background-color: color-mix(in oklab, <%= item[:category_color] %> 10%, transparent);- border-color: color-mix(in oklab, <%= item[:category_color] %> 10%, transparent);- color: <%= item[:category_color] %>;- ">- <%= icon(item[:category_icon], color: "current", size: "sm") %>- </div>- <% else %>- <%= render DS::FilledIcon.new(- variant: :text,- hex_color: item[:category_color],- text: item[:category_name],- size: "md",- rounded: true- ) %>- <% end %>- <% if clickable %>- <%# Stretched link covers the category cell. The overlay is anchored to the- flex wrapper above, NOT to the <tr>: `position: relative` on a table row- does not establish a containing block in WebKit (it is left undefined by- CSS 2.1 9.3.1, and Safari does not implement it). Anchoring to the row- therefore let the overlay escape and blanket the whole page in Safari,- swallowing every click on /reports into this drill-down. %>- <%= link_to item[:category_name],- transactions_href,- class: "font-medium text-primary hover:underline before:absolute before:inset-0 before:content-['']",- data: { turbo_frame: "_top", turbo_prefetch: false } %>- <% else %>- <span class="font-medium text-primary">- <%= item[:category_name] %>- </span>- <% end %>- <span class="text-xs text-subdued whitespace-nowrap">- (<%= t("reports.transactions_breakdown.table.entries", count: item[:count]) %>)- </span>- </div>+ <%+ row_content = capture do+ if is_sub+ concat tag.div(icon("corner-down-right"), class: "text-subdued")+ end+ if item[:category_icon]+ concat tag.div(+ icon(item[:category_icon], color: "current", size: "sm"),+ class: "h-7 w-7 flex-shrink-0 rounded-full flex justify-center items-center #{"group-hover/category-row:scale-105 transition-all duration-300" if clickable}",+ style: "background-color: color-mix(in oklab, #{item[:category_color]} 10%, transparent); border-color: color-mix(in oklab, #{item[:category_color]} 10%, transparent); color: #{item[:category_color]};"+ )+ else+ concat render(DS::FilledIcon.new(+ variant: :text,+ hex_color: item[:category_color],+ text: item[:category_name],+ size: "md",+ rounded: true+ ))+ end+ concat tag.span(item[:category_name], class: "font-medium text-primary #{"hover:underline" if clickable}")+ concat tag.span("(#{t("reports.transactions_breakdown.table.entries", count: item[:count])})", class: "text-xs text-subdued whitespace-nowrap")+ end+ %>+ <%# The clickable variant wraps the whole cluster (icon, name, count) in a+ single <a> using normal flex flow — deliberately NOT the `position:+ relative`/`absolute` stretched-link trick, which inside a <table> row+ is a known source of WebKit table-layout scrollHeight miscalculation+ (see #3093 investigation). %>+ <% if clickable %>+ <%= link_to row_content, transactions_href,+ class: "flex items-center gap-2",+ data: { turbo_frame: "_top", turbo_prefetch: false } %>+ <% else %>+ <%= tag.div row_content, class: "flex items-center gap-2" %>+ <% end %></td><td class="py-3 px-4 lg:px-6 text-right">- <span class="text-sm <%= color_class %> privacy-sensitive">- <%= Money.new(item[:total], Current.family.currency).format %>- </span>+ <%+ amount_content = tag.span(+ Money.new(item[:total], Current.family.currency).format,+ class: "text-sm #{color_class} privacy-sensitive"+ )+ %>+ <% if clickable %>+ <%= link_to amount_content, transactions_href,+ class: "block", data: { turbo_frame: "_top", turbo_prefetch: false } %>+ <% else %>+ <%= amount_content %>+ <% end %></td><td class="py-3 px-4 lg:px-6 text-right">- <span class="text-sm text-secondary">- <%= percentage %>%- </span>+ <%+ percentage_content = tag.span("#{percentage}%", class: "text-sm text-secondary")+ %>+ <% if clickable %>+ <%= link_to percentage_content, transactions_href,+ class: "block", data: { turbo_frame: "_top", turbo_prefetch: false } %>+ <% else %>+ <%= percentage_content %>+ <% end %></td></tr>diff --git a/app/views/reports/index.html.erb b/app/views/reports/index.html.erbindex eb43da286e..81151f7077 100644--- a/app/views/reports/index.html.erb+++ b/app/views/reports/index.html.erb@@ -141,8 +141,13 @@<div data-controller="reports-sortable" data-action="dragover->reports-sortable#dragOver drop->reports-sortable#drop" role="list" aria-label="Reports sections"><% @reports_sections.each do |section| %><% next unless section[:visible] %>+ <%# [-webkit-touch-callout:none] suppresses iOS Safari's native long-press+ link preview on the amount/percentage anchors inside this section —+ without it, the callout preempts the touch, fires touchcancel, and+ the press-and-hold drag gesture never gets a chance to activate+ (see #3132 review). %><section- class="bg-container rounded-xl shadow-border-xs transition-all mb-6 group focus:outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2"+ class="bg-container rounded-xl shadow-border-xs transition-all mb-6 group focus:outline-none focus-visible:ring-2 focus-visible:ring-alpha-black-300 focus-visible:ring-offset-2 [-webkit-touch-callout:none]"data-reports-sortable-target="section"data-section-key="<%= section[:key] %>"data-controller="reports-section"@@ -159,6 +164,8 @@touchstart->reports-sortable#touchStarttouchmove->reports-sortable#touchMovetouchend->reports-sortable#touchEnd+ touchcancel->reports-sortable#touchCancel+ contextmenu->reports-sortable#suppressContextMenukeydown->reports-sortable#handleKeyDown"><div class="px-4 py-2 flex items-center justify-between"><div class="flex items-center gap-2">@@ -177,7 +184,7 @@</div><buttontype="button"- class="cursor-grab active:cursor-grabbing text-secondary hover:text-primary transition-colors p-0.5 opacity-0 group-hover:opacity-100"+ class="cursor-grab active:cursor-grabbing hidden lg:flex items-center justify-center w-6 h-6 text-secondary hover:text-primary transition-colors opacity-0 group-hover:opacity-100"aria-label="<%= t("reports.index.drag_to_reorder") %>"><%= icon("grip-vertical", size: "sm") %></button>diff --git a/test/controllers/reports_controller_test.rb b/test/controllers/reports_controller_test.rbindex 875aefb50a..d3dcb32d01 100644--- a/test/controllers/reports_controller_test.rb+++ b/test/controllers/reports_controller_test.rb@@ -541,15 +541,22 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestincome_href = transactions_path(q: { categories: [ income_category.name ], start_date: start_date, end_date: end_date })uncategorized_href = transactions_path(q: { categories: [ Category.uncategorized.filter_value ], start_date: start_date, end_date: end_date })- assert_select "tr[data-category='category-#{expense_category.id}'] a[href=?]", expense_href, text: expense_category.name- assert_select "tr[data-category='category-#{income_category.id}'] a[href=?]", income_href, text: income_category.name- assert_select "tr[data-category='category-uncategorized'] a[href=?]", uncategorized_href, text: Category.uncategorized.name+ assert_select "tr[data-category='category-#{expense_category.id}'] a[href=?]", expense_href, text: /#{Regexp.escape(expense_category.name)}/+ assert_select "tr[data-category='category-#{income_category.id}'] a[href=?]", income_href, text: /#{Regexp.escape(income_category.name)}/+ assert_select "tr[data-category='category-uncategorized'] a[href=?]", uncategorized_href, text: /#{Regexp.escape(Category.uncategorized.name)}/- # Hit target via stretched ::before, anchored to the category cell's flex- # wrapper. Deliberately NOT the <tr>: CSS 2.1 9.3.1 leaves position:relative- # undefined on table rows and WebKit does not implement it, so anchoring- # there let the overlay escape and swallow every click on the page in Safari.- assert_select "tr.group\\/category-row[data-category='category-#{expense_category.id}'] td div.relative a[class*='before:absolute'][class*='before:inset-0']"+ # Full-row hit target via a normal flex <a> wrapping icon+name+count —+ # deliberately not a `position: relative`/`absolute` stretched-link inside+ # the <table>, which caused a WebKit scrollHeight bug (see #3093).+ assert_select "tr[data-category='category-#{expense_category.id}'] a.flex.items-center"+ assert_select "tr[data-category='category-#{expense_category.id}'].relative", 0+ assert_select "tr[data-category='category-#{expense_category.id}'] a[class*='before:absolute']", 0++ # Amount and percentage cells are each wrapped in their own normal-flow+ # <a> to the same href, so the full row remains clickable without a+ # stretched-link overlay spanning the <tr>.+ assert_select "tr[data-category='category-#{expense_category.id}'] td:nth-child(2) a[href=?]", expense_href+ assert_select "tr[data-category='category-#{expense_category.id}'] td:nth-child(3) a[href=?]", expense_hrefendtest "index uncategorized category link stays on the stable filter value while the displayed text is localized" do@@ -565,7 +572,7 @@ class ReportsControllerTest < ActionDispatch::IntegrationTestassert_response :okhref = transactions_path(q: { categories: [ Category::UNCATEGORIZED_FILTER_VALUE ], start_date: start_date, end_date: end_date })- assert_select "tr[data-category='category-uncategorized'] a[href=?]", href, text: localized_name+ assert_select "tr[data-category='category-uncategorized'] a[href=?]", href, text: /#{Regexp.escape(localized_name)}/endtest "index excludes tax-advantaged account transactions from activity breakdown" do