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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
<script>
import { GlAvatar, GlButton, GlIcon, GlBadge, GlTooltipDirective } from '@gitlab/ui';
import { s__, sprintf } from '~/locale';
import {
CLICK_MENU_ITEM_ACTION,
CLICK_PINNED_MENU_ITEM_ACTION,
TRACKING_UNKNOWN_ID,
TRACKING_UNKNOWN_PANEL,
} from '~/super_sidebar/constants';
import eventHub from '../event_hub';
import NavItemLink from './nav_item_link.vue';
import NavItemRouterLink from './nav_item_router_link.vue';
export default {
i18n: {
pin: s__('Navigation|Pin %{title}'),
pinItem: s__('Navigation|Pin item'),
unpin: s__('Navigation|Unpin %{title}'),
unpinItem: s__('Navigation|Unpin item'),
},
name: 'NavItem',
components: {
GlAvatar,
GlButton,
GlIcon,
GlBadge,
NavItemLink,
NavItemRouterLink,
},
directives: {
GlTooltip: GlTooltipDirective,
},
inject: {
pinnedItemIds: { default: { ids: [] } },
panelSupportsPins: { default: false },
panelType: { default: '' },
},
props: {
isInPinnedSection: {
type: Boolean,
required: false,
default: false,
},
isStatic: {
type: Boolean,
required: false,
default: false,
},
item: {
type: Object,
required: true,
},
linkClasses: {
type: Object,
required: false,
default: () => ({}),
},
isSubitem: {
type: Boolean,
required: false,
default: false,
},
isFlyout: {
type: Boolean,
required: false,
default: false,
},
},
data() {
return {
isMouseIn: false,
canClickPinButton: false,
};
},
computed: {
pillData() {
return this.item.pill_count;
},
hasPill() {
return (
Number.isFinite(this.pillData) ||
(typeof this.pillData === 'string' && this.pillData !== '')
);
},
isPinnable() {
return this.panelSupportsPins && !this.isStatic;
},
isPinned() {
return this.pinnedItemIds.ids.includes(this.item.id);
},
trackingProps() {
// Set extra event data to debug missing IDs / Panel Types
const extraData =
!this.item.id || !this.panelType
? { 'data-track-extra': JSON.stringify({ title: this.item.title }) }
: {};
return {
'data-track-action': this.isInPinnedSection
? CLICK_PINNED_MENU_ITEM_ACTION
: CLICK_MENU_ITEM_ACTION,
'data-track-label': this.item.id ?? TRACKING_UNKNOWN_ID,
'data-track-property': this.panelType
? `nav_panel_${this.panelType}`
: TRACKING_UNKNOWN_PANEL,
...extraData,
};
},
/**
* Some QA specs rely on a stable "Project overview"/"Group overview" nav
* item data-qa-submenu-item attribute value.
*
* This computed ensures that those particular nav items use the `id` of
* the item rather than its title for that QA attribute.
*
* In future, probably all nav items should do this, for consistency.
* See https://gitlab.com/gitlab-org/gitlab/-/issues/422925.
*/
qaSubMenuItem() {
const { id } = this.item;
if (id === 'project_overview' || id === 'group_overview') return id.replace(/_/g, '-');
return this.item.title;
},
linkProps() {
return {
...this.$attrs,
...this.trackingProps,
item: this.item,
'data-qa-submenu-item': this.qaSubMenuItem,
'data-method': this.item.data_method ?? null,
};
},
computedLinkClasses() {
return {
'gl-px-2 gl-mx-2 gl-line-height-normal': this.isSubitem,
'gl-px-3': !this.isSubitem,
'gl-pl-5! gl-rounded-small': this.isFlyout,
'gl-rounded-base': !this.isFlyout,
[this.item.link_classes]: this.item.link_classes,
...this.linkClasses,
};
},
navItemLinkComponent() {
return this.item.to ? NavItemRouterLink : NavItemLink;
},
hasAvatar() {
return Boolean(this.item.entity_id);
},
hasEndSpace() {
return this.hasPill || this.isPinnable || this.isFlyout;
},
avatarShape() {
return this.item.avatar_shape || 'rect';
},
pinAriaLabel() {
return sprintf(this.$options.i18n.pin, {
title: this.item.title,
});
},
unpinAriaLabel() {
return sprintf(this.$options.i18n.unpin, {
title: this.item.title,
});
},
activeIndicatorStyle() {
const style = {
width: '3px',
borderRadius: '3px',
marginRight: '1px',
};
// The active indicator is too close to the avatar for items with one, so shift
// it left by 1px.
//
// The indicator is absolutely positioned using rem units. This tweak for this
// edge case is in pixel units, so that it does not scale with root font size.
if (this.hasAvatar) style.transform = 'translateX(-1px)';
return style;
},
},
mounted() {
if (this.item.is_active) {
this.$el.scrollIntoView(false);
}
eventHub.$on('updatePillValue', this.updatePillValue);
},
destroyed() {
eventHub.$off('updatePillValue', this.updatePillValue);
},
methods: {
togglePointerEvents() {
this.canClickPinButton = this.isMouseIn;
},
updatePillValue({ value, itemId }) {
if (this.item.id === itemId) {
// https://gitlab.com/gitlab-org/gitlab/-/issues/428246
// fixing this linting issue is causing the pills not to async update
//
// eslint-disable-next-line vue/no-mutating-props
this.item.pill_count = value;
}
},
},
};
</script>
<template>
<li
class="gl-relative show-on-focus-or-hover--context hide-on-focus-or-hover--context transition-opacity-on-hover--context"
data-testid="nav-item"
@mouseenter="isMouseIn = true"
@mouseleave="isMouseIn = false"
>
<component
:is="navItemLinkComponent"
#default="{ isActive }"
v-bind="linkProps"
class="gl-relative gl-display-flex gl-align-items-center gl-min-h-7 gl-gap-3 gl-mb-1 gl-py-2 gl-text-black-normal! gl-hover-bg-t-gray-a-08 gl-focus-bg-t-gray-a-08 gl-text-decoration-none! gl-focus--focus show-on-focus-or-hover--control hide-on-focus-or-hover--control"
:class="computedLinkClasses"
data-testid="nav-item-link"
>
<div
:class="[isActive ? 'gl-opacity-10' : 'gl-opacity-0']"
class="active-indicator gl-bg-blue-500 gl-absolute gl-left-2 gl-top-2 gl-bottom-2 gl-transition-slow"
aria-hidden="true"
:style="activeIndicatorStyle"
data-testid="active-indicator"
></div>
<div v-if="!isFlyout" class="gl-flex-shrink-0 gl-w-6 gl-display-flex">
<slot name="icon">
<gl-icon v-if="item.icon" :name="item.icon" class="gl-m-auto item-icon" />
<gl-icon
v-else-if="isInPinnedSection"
name="grip"
class="gl-m-auto gl-text-gray-400 js-draggable-icon gl-cursor-grab show-on-focus-or-hover--target"
/>
<gl-avatar
v-else-if="hasAvatar"
:size="24"
:shape="avatarShape"
:entity-name="item.title"
:entity-id="item.entity_id"
:src="item.avatar"
/>
</slot>
</div>
<div class="gl-flex-grow-1 gl-text-gray-900 gl-truncate-end">
{{ item.title }}
<div v-if="item.subtitle" class="gl-font-sm gl-text-gray-500 gl-truncate-end">
{{ item.subtitle }}
</div>
</div>
<slot name="actions"></slot>
<span v-if="hasEndSpace" class="gl-text-right gl-relative gl-min-w-6">
<gl-badge
v-if="hasPill"
size="sm"
variant="neutral"
class="gl-bg-t-gray-a-08!"
:class="{
'hide-on-focus-or-hover--target transition-opacity-on-hover--target': isPinnable,
}"
>
{{ pillData }}
</gl-badge>
</span>
</component>
<template v-if="isPinnable">
<gl-button
v-if="isPinned"
v-gl-tooltip.noninteractive.right.viewport="$options.i18n.unpinItem"
:aria-label="unpinAriaLabel"
category="tertiary"
class="show-on-focus-or-hover--target transition-opacity-on-hover--target always-animate gl-absolute gl-right-3 gl-top-2"
:class="{ 'gl-pointer-events-none': !canClickPinButton }"
data-testid="nav-item-unpin"
icon="thumbtack-solid"
size="small"
@click="$emit('pin-remove', item.id)"
@transitionend="togglePointerEvents"
/>
<gl-button
v-else
v-gl-tooltip.noninteractive.right.viewport="$options.i18n.pinItem"
:aria-label="pinAriaLabel"
category="tertiary"
class="show-on-focus-or-hover--target transition-opacity-on-hover--target always-animate gl-absolute gl-right-3 gl-top-2"
:class="{ 'gl-pointer-events-none': !canClickPinButton }"
data-testid="nav-item-pin"
icon="thumbtack"
size="small"
@click="$emit('pin-add', item.id)"
@transitionend="togglePointerEvents"
/>
</template>
</li>
</template>
|