aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/components/SearchInput.vue
blob: 22b5eef497784fd6e1b6d76dd54d738c0f9a0376 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<template>
  <div class="search-bar">
    <label for="search" class="search-label"></label>
    <input
      type="text"
      ref="search"
      :value="value"
      @input="$emit('input', $event.target.value.toLowerCase())"
      @keyup.enter.exact="$emit('search:open')"
      @keyup.alt.enter="$emit('search:open', '_blank')"
    />
  </div>
</template>

<script>
export default {
  name: "SearchInput",
  props: ["value"],
  mounted() {
    this._keyListener = function (event) {
      if (event.key === "/") {
        event.preventDefault();
        this.$emit("search:focus");
        this.$nextTick(() => {
          this.$refs.search.focus();
        });
      }
      if (event.key === "Escape") {
        this.$refs.search.value = "";
        this.$refs.search.blur();
        this.$emit("search:cancel");
      }
    };
    document.addEventListener("keydown", this._keyListener.bind(this));
  },
  beforeDestroy() {
    document.removeEventListener("keydown", this._keyListener);
  },
};
</script>

<style lang="scss" scoped></style>