fix: restore focus on reload
This commit is contained in:
parent
23bff41601
commit
f1a64b260b
|
@ -36,9 +36,10 @@ $purifier = new HTMLPurifier($purifier_config);
|
||||||
<meta name="turbolinks-cache-control" content="no-preview">
|
<meta name="turbolinks-cache-control" content="no-preview">
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// https://stackoverflow.com/a/44353026
|
// https://stackoverflow.com/a/48234990/79461
|
||||||
var reloadWithTurbolinks = (function () {
|
var reloadWithTurbolinks = (function () {
|
||||||
var scrollPosition;
|
var scrollPosition;
|
||||||
|
var focusId;
|
||||||
|
|
||||||
function reload() {
|
function reload() {
|
||||||
Turbolinks.visit(window.location.toString(), {action: 'replace'})
|
Turbolinks.visit(window.location.toString(), {action: 'replace'})
|
||||||
|
@ -46,12 +47,17 @@ $purifier = new HTMLPurifier($purifier_config);
|
||||||
|
|
||||||
document.addEventListener('turbolinks:before-render', function () {
|
document.addEventListener('turbolinks:before-render', function () {
|
||||||
scrollPosition = [window.scrollX, window.scrollY];
|
scrollPosition = [window.scrollX, window.scrollY];
|
||||||
|
focusId = document.activeElement.id;
|
||||||
});
|
});
|
||||||
document.addEventListener('turbolinks:load', function () {
|
document.addEventListener('turbolinks:load', function () {
|
||||||
if (scrollPosition) {
|
if (scrollPosition) {
|
||||||
window.scrollTo.apply(window, scrollPosition);
|
window.scrollTo.apply(window, scrollPosition);
|
||||||
scrollPosition = null
|
scrollPosition = null
|
||||||
}
|
}
|
||||||
|
if (focusId) {
|
||||||
|
document.getElementById(focusId).focus();
|
||||||
|
focusId = null;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return reload;
|
return reload;
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user