Skip to content

Commit 698fc38

Browse files
committed
feat(tpl): keep focus between forward/backward
1 parent 6287b80 commit 698fc38

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/tpl/defaultTheme/frontend/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
} catch (err) {
7373
}
7474

75+
var lastFocused;
76+
7577
function enableFilter() {
7678
if (!document.querySelector) {
7779
var filter = document.getElementById && document.getElementById('panel-filter');
@@ -229,6 +231,19 @@
229231
}
230232
}
231233

234+
function keepFocusOnBackwardForward() {
235+
if (window.onpageshow === undefined || !document.querySelector) return;
236+
document.body.querySelector('.item-list').addEventListener('focusin', function (e) {
237+
lastFocused = e.target;
238+
});
239+
window.addEventListener('pageshow', function () {
240+
if (lastFocused && lastFocused !== document.activeElement) {
241+
lastFocused.focus();
242+
lastFocused.scrollIntoView({block: 'center'});
243+
}
244+
});
245+
}
246+
232247
function focusChildOnNavUp() {
233248
if (!document.querySelector) return;
234249

@@ -272,6 +287,7 @@
272287
if (text !== prevChildName) continue;
273288
var elLink = item.querySelector(selectorLink);
274289
if (elLink) {
290+
lastFocused = elLink;
275291
elLink.focus();
276292
elLink.scrollIntoView({block: 'center'});
277293
}
@@ -1316,6 +1332,7 @@
13161332
}
13171333

13181334
enableFilter();
1335+
keepFocusOnBackwardForward();
13191336
focusChildOnNavUp();
13201337
enableKeyboardNavigate();
13211338
enhanceUpload();

0 commit comments

Comments
 (0)