Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

Fix behavior when tapping on a same-page anchor link #126

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Turbolinks/WebView.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
// Adapter interface

visitProposedToLocationWithAction: function(location, action) {
this.postMessage("visitProposed", { location: location.absoluteURL, action: action })
if (this.controller.locationIsSamePageAnchor(location)) {
this.controller.scrollToAnchor(location.anchor)
} else {
this.postMessage("visitProposed", { location: location.absoluteURL, action: action })
}
},

visitStarted: function(visit) {
Expand Down
5 changes: 5 additions & 0 deletions TurbolinksDemo/server/lib/turbolinks_demo/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<li><a href="/nonexistent">Trigger an HTTP 404</a> to see how Turbolinks handles error responses.</li>
<li><a href="/protected">Visit a protected area</a> to see how to handle an unauthorized session using a separate web view.</li>
<li><a href="/numbers">Load a native view controller</a> to see how to intercept the URL and handle it natively.</li>
<li><a href="#same-page-anchor">Scroll to an element on this page</a>.</li>
<li><a href="https://github.com/turbolinks/turbolinks" target="_blank">Follow an external link</a> to open it in Safari.</li>
<li><a href="#" data-behavior="post-message">Post a message from JavaScript</a> to see how it can be handled natively.</li>
</ul>

<div id="same-page-anchor" style="margin-top: 100vh; height: 100vh">
Same-page anchor. <a href="#">Back to top</a>.
</div>