add back hotkey for searchbox

This commit is contained in:
Miao Wang 2024-04-09 00:45:04 +08:00
parent c10467b093
commit 95c919773e

View File

@ -1,9 +1,27 @@
<script setup>
import { defineModel } from "vue";
import { defineModel, ref, onMounted, onBeforeUnmount } from "vue";
const model = defineModel({type: String, default: ""});
const inputRef = ref(null);
const onGlobalKeyPress = (e) => {
if (e.key === "/" && document.activeElement !== inputRef.value) {
e.preventDefault();
inputRef.value.focus();
}
};
onMounted(() => {
window.addEventListener("keypress", onGlobalKeyPress);
});
onBeforeUnmount(() => {
window.removeEventListener("keypress", onGlobalKeyPress);
});
</script>
<template>
<input type="search" v-model="model" placeholder="按 / 搜索" autocomplete="off" class="ms-auto d-inline-flex align-self-center">
<input type="search" v-model="model" ref="inputRef" placeholder="按 / 搜索" autocomplete="off" class="ms-auto d-inline-flex align-self-center">
</template>
<style scoped>