summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/super_sidebar/components/nav_item.vue
blob: 100f1d18793b4d736fe0d37b82adfe17a9373e6f (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<script>
import { GlCollapse, GlIcon } from '@gitlab/ui';

export default {
  name: 'NavItem',
  components: {
    GlCollapse,
    GlIcon,
  },
  props: {
    item: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      expanded: this.item.is_active,
    };
  },
  computed: {
    collapseIcon() {
      return this.expanded ? 'chevron-up' : 'chevron-down';
    },
    isSection() {
      return Boolean(this.item?.items?.length);
    },
    isActive() {
      if (this.isSection) {
        return !this.expanded && this.item.is_active;
      }
      return this.item.is_active;
    },
    linkProps() {
      if (this.isSection) {
        return {
          href: '#',
          'aria-hidden': true,
        };
      }
      return {
        href: this.item.link,
      };
    },
    linkClasses() {
      return {
        'gl-bg-t-gray-a-08': this.isActive,
      };
    },
  },
  methods: {
    click(event) {
      if (this.isSection) {
        event.preventDefault();
        this.expanded = !this.expanded;
      }
    },
  },
};
</script>

<template>
  <li>
    <a
      v-bind="linkProps"
      class="gl-rounded-base gl-relative gl-display-flex gl-py-3 gl-px-0 gl-line-height-normal gl-text-black-normal! gl-hover-bg-t-gray-a-08 gl-text-decoration-none!"
      :class="linkClasses"
      @click="click"
    >
      <div
        :class="[isActive ? 'gl-bg-blue-500' : 'gl-bg-transparent']"
        class="gl-absolute gl-left-2 gl-top-2 gl-bottom-2 gl-transition-slow"
        aria-hidden="true"
        style="width: 3px; border-radius: 3px; margin-right: 1px"
      ></div>
      <div class="gl-flex-shrink-0 gl-w-6 gl-mx-3">
        <slot name="icon">
          <gl-icon v-if="item.icon" :name="item.icon" class="gl-ml-2" />
        </slot>
      </div>
      <div class="gl-pr-3">
        {{ item.title }}
        <div v-if="item.subtitle" class="gl-font-sm gl-text-gray-500 gl-mt-1">
          {{ item.subtitle }}
        </div>
      </div>
      <span v-if="isSection" class="gl-flex-grow-1 gl-text-right gl-mr-3">
        <gl-icon :name="collapseIcon" />
      </span>
    </a>
    <gl-collapse v-if="isSection" :id="item.title" v-model="expanded">
      <ul class="gl-p-0">
        <nav-item
          v-for="subItem of item.items"
          :key="`${item.title}-${subItem.title}`"
          :item="subItem"
        />
      </ul>
    </gl-collapse>
  </li>
</template>