Skip to content

Commit 9aa343a

Browse files
jhroemerknowlerdanielroejellydeck
authored
feat: add keyboard shortcut highlight on "?" press (#440)
Co-authored-by: Nathan Knowler <nathan@knowler.dev> Co-authored-by: Daniel Roe <daniel@roe.dev> Co-authored-by: jellydeck <91427591+jellydeck@users.noreply.github.com>
1 parent 4af3271 commit 9aa343a

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

app/app.vue

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const { locale, locales } = useI18n()
1010
initPreferencesOnPrehydrate()
1111
1212
const isHomepage = computed(() => route.name === 'index')
13+
const showKbdHints = shallowRef(false)
1314
1415
const localeMap = locales.value.reduce(
1516
(acc, l) => {
@@ -21,8 +22,9 @@ const localeMap = locales.value.reduce(
2122
2223
useHead({
2324
htmlAttrs: {
24-
lang: () => locale.value,
25-
dir: () => localeMap[locale.value] ?? 'ltr',
25+
'lang': () => locale.value,
26+
'dir': () => localeMap[locale.value] ?? 'ltr',
27+
'data-kbd-hints': () => showKbdHints.value,
2628
},
2729
titleTemplate: titleChunk => {
2830
return titleChunk ? titleChunk : 'npmx - Better npm Package Browser'
@@ -33,16 +35,16 @@ if (import.meta.server) {
3335
setJsonLd(createWebSiteSchema())
3436
}
3537
36-
// Global keyboard shortcut: "/" focuses search or navigates to search page
38+
// Global keyboard shortcut:
39+
// "/" focuses search or navigates to search page
40+
// "?" highlights all keyboard shortcut elements
3741
function handleGlobalKeydown(e: KeyboardEvent) {
3842
const target = e.target as HTMLElement
3943
4044
const isEditableTarget =
4145
target.tagName === 'INPUT' || target.tagName === 'TEXTAREA' || target.isContentEditable
4246
43-
if (isEditableTarget) {
44-
return
45-
}
47+
if (isEditableTarget) return
4648
4749
if (e.key === '/') {
4850
e.preventDefault()
@@ -59,10 +61,20 @@ function handleGlobalKeydown(e: KeyboardEvent) {
5961
6062
router.push('/search')
6163
}
64+
65+
if (e.key === '?') {
66+
e.preventDefault()
67+
showKbdHints.value = true
68+
}
69+
}
70+
71+
function handleGlobalKeyup() {
72+
showKbdHints.value = false
6273
}
6374
6475
if (import.meta.client) {
6576
useEventListener(document, 'keydown', handleGlobalKeydown)
77+
useEventListener(document, 'keyup', handleGlobalKeyup)
6678
}
6779
</script>
6880

@@ -82,3 +94,25 @@ if (import.meta.client) {
8294
<ScrollToTop />
8395
</div>
8496
</template>
97+
98+
<style>
99+
/* Keyboard shortcut highlight on "?" key press */
100+
kbd {
101+
position: relative;
102+
}
103+
104+
kbd::before {
105+
content: '';
106+
position: absolute;
107+
inset: 0;
108+
border-radius: inherit;
109+
box-shadow: 0 0 4px 2px var(--accent);
110+
opacity: 0;
111+
transition: opacity 200ms ease-out;
112+
pointer-events: none;
113+
}
114+
115+
html[data-kbd-hints='true'] kbd::before {
116+
opacity: 1;
117+
}
118+
</style>

0 commit comments

Comments
 (0)