Skip to content

Commit 6e8ec5e

Browse files
committed
feat: add navlinks keyboard shortcuts
1 parent c6b200a commit 6e8ec5e

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

layouts/_default/baseof.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
</main>
4444

4545
{{ partial "docs/inject/body" . }}
46+
<script src="{{ .Site.BaseURL }}js/navlinks.js"></script>
4647
</body>
4748
</html>
4849

layouts/partials/docs/navigation-links.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
<li>
2323
{{ if ne $currentPageIndex $prevLinkIndex }}
2424
{{$prevPage := index $sitePages $prevLinkIndex}}
25-
<a href="{{ $prevPage.Link }}">{{ i18n "prev" }}</a>
25+
<a href="{{ $prevPage.Link }}" id="prev-page">{{ i18n "prev" }}</a>
2626
{{ end }}
2727
</li>
2828
<li>
2929
{{ if ne $currentPageIndex $nextLinkIndex }}
3030
{{$nextPage := index $sitePages $nextLinkIndex}}
31-
<a href="{{ $nextPage.Link }}">{{ i18n "next"}}</a>
31+
<a href="{{ $nextPage.Link }}" id="next-page">{{ i18n "next"}}</a>
3232
{{ end }}
3333
</li>
3434
</ul>

static/js/navlinks.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Script to handle the navigation links events.
3+
* When the user presses arrow left or arrow right, the page will be redirected to the previous/next page if corresponding link exists.
4+
*/
5+
6+
// Get the previous (id="prev-page") and next (id="next-page") links
7+
const prevPageLink = document.getElementById("prev-page");
8+
const nextPageLink = document.getElementById("next-page");
9+
10+
// Add event listener to the document
11+
document.addEventListener("keydown", (event) => {
12+
if (!event.ctrlKey && !event.altKey && !event.shiftKey && !event.metaKey) {
13+
return;
14+
} else {
15+
if (event.key === "ArrowLeft" && prevPageLink) {
16+
prevPageLink.click();
17+
}
18+
if (event.key === "ArrowRight" && nextPageLink) {
19+
nextPageLink.click();
20+
}
21+
}
22+
});

0 commit comments

Comments
 (0)