diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9e0a72f81..6b97da279 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -10,8 +10,13 @@ module ApplicationHelper tag.span class: class_names("icon icon--#{name}", options.delete(:class)), "aria-hidden": true, **options end - def back_link_to(label, url, action, **options) - link_to url, class: "btn btn--back btn--circle-mobile", data: { controller: "hotkey", action: action }, **options do + def back_link_to(label, url, action, prefer_referrer: [], **options) + data = { controller: "hotkey", action: action } + if prefer_referrer.any? + data[:turbo_navigation_target] = "referrerBackLink" + data[:turbo_navigation_allowed_referrer_paths] = prefer_referrer.join(",") + end + link_to url, class: "btn btn--back btn--circle-mobile", data: data, **options do icon_tag("arrow-left") + tag.strong("Back to #{label}", class: "overflow-ellipsis") + tag.kbd("ESC", class: "txt-x-small hide-on-touch").html_safe end end diff --git a/app/helpers/boards_helper.rb b/app/helpers/boards_helper.rb index 5f12b9259..814b0b0e2 100644 --- a/app/helpers/boards_helper.rb +++ b/app/helpers/boards_helper.rb @@ -1,6 +1,6 @@ module BoardsHelper - def link_back_to_board(board) - back_link_to board.name, board, "keydown.left@document->hotkey#click keydown.esc@document->hotkey#click click->turbo-navigation#backIfSamePath" + def link_back_to_board(board, prefer_referrer: []) + back_link_to board.name, board, "keydown.left@document->hotkey#click keydown.esc@document->hotkey#click click->turbo-navigation#backIfSamePath", prefer_referrer: end def link_to_edit_board(board) diff --git a/app/javascript/controllers/turbo_navigation_controller.js b/app/javascript/controllers/turbo_navigation_controller.js index d755342e9..8f47dac9b 100644 --- a/app/javascript/controllers/turbo_navigation_controller.js +++ b/app/javascript/controllers/turbo_navigation_controller.js @@ -1,8 +1,12 @@ import { Controller } from "@hotwired/stimulus" export default class extends Controller { + static values = { label: String } + static targets = [ "referrerBackLink" ] + rememberLocation() { sessionStorage.setItem("referrerUrl", window.location.href) + sessionStorage.setItem("referrerLabel", this.labelValue) } backIfSamePath(event) { @@ -16,6 +20,19 @@ export default class extends Controller { Turbo.visit(this.#referrerUrl) } } + + referrerBackLinkTargetConnected(link) { + if (!this.#referrerUrl || !this.#referrerLabel) { return } + + const allowedPaths = (link.dataset.turboNavigationAllowedReferrerPaths || "").split(",") + const referrerPath = new URL(this.#referrerUrl).pathname + if (!allowedPaths.includes(referrerPath)) { return } + + link.href = this.#referrerUrl + const strong = link.querySelector("strong") + if (strong) { strong.textContent = `Back to ${this.#referrerLabel}` } + } + get #referrerPath() { if (!this.#referrerUrl) return null return new URL(this.#referrerUrl).pathname @@ -24,4 +41,8 @@ export default class extends Controller { get #referrerUrl() { return sessionStorage.getItem("referrerUrl") } + + get #referrerLabel() { + return sessionStorage.getItem("referrerLabel") + } } diff --git a/app/views/cards/show.html.erb b/app/views/cards/show.html.erb index fabd2ad02..fd9101d58 100644 --- a/app/views/cards/show.html.erb +++ b/app/views/cards/show.html.erb @@ -7,7 +7,7 @@ <% content_for :header do %>