-
Notifications
You must be signed in to change notification settings - Fork 9
/
MenuItem.vue
557 lines (490 loc) · 14 KB
/
MenuItem.vue
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<template>
<li
:id="id"
role="option"
class="cdx-menu-item"
:class="rootClasses"
:aria-disabled="disabled"
:aria-selected="selected"
@mouseenter="onMouseEnter"
@mouseleave="onMouseLeave"
@mousedown.prevent="onMouseDown"
@click="onClick"
>
<!-- @slot Custom menu item content. -->
<slot>
<component
:is="contentTag"
:href="url ? url : undefined"
class="cdx-menu-item__content"
>
<!-- Thumbnail, thumbnail placeholder, or icon. -->
<template v-if="showThumbnail">
<span
v-if="!thumbnailLoaded"
class="cdx-menu-item__thumbnail-placeholder"
>
<cdx-icon
:icon="defaultThumbnailIcon"
class="cdx-menu-item__thumbnail-placeholder__icon"
/>
</span>
<Transition name="cdx-menu-item__thumbnail">
<span
v-if="thumbnailLoaded"
:style="thumbnailStyle"
class="cdx-menu-item__thumbnail"
/>
</Transition>
</template>
<cdx-icon
v-else-if="icon"
:icon="icon"
class="cdx-menu-item__icon"
/>
<!-- Item text. -->
<span class="cdx-menu-item__text">
<!-- Item label. -->
<cdx-search-result-title
v-if="highlightQuery"
:title="title"
:search-query="searchQuery"
:lang="language?.label"
/>
<span
v-else
class="cdx-menu-item__text__label"
:lang="language?.label"
>
<bdi>{{ title }}</bdi>
</span>
<!-- Item search query match (e.g. alias). -->
<template v-if="match">
<!-- Add a space before the match. Vue strips whitespace between everything
except plain text, so we can't rely on a newline to add a natural space
here. -->
<!-- eslint-disable-next-line vue/no-useless-mustaches -->
{{ ' ' }}
<cdx-search-result-title
v-if="highlightQuery"
:title="match"
:search-query="searchQuery"
:lang="language?.match"
/>
<span
v-else
class="cdx-menu-item__text__match"
:lang="language?.match"
>
<bdi>{{ match }}</bdi>
</span>
</template>
<!-- Item description. -->
<span
v-if="description"
class="cdx-menu-item__text__description"
:lang="language?.description"
>
<bdi>{{ description }}</bdi>
</span>
</span>
</component>
</slot>
</li>
</template>
<script lang="ts">
import { PropType, computed, defineComponent, ref, onMounted } from 'vue';
import { cdxIconImageLayoutFrameless, Icon } from '@wikimedia/codex-icons';
import CdxIcon from '../icon/Icon.vue';
import CdxSearchResultTitle from '../search-result-title/SearchResultTitle.vue';
import { MenuState, Thumbnail, MenuItemLanguageData } from '../../types';
/**
* Item within a Menu component.
*
* A value must be provided, and various optional elements can be displayed:
* - A human-readable label
* - A description
* - A thumbnail or icon
*
* Alternately, the entire content and layout of the menu item can be customized via the default
* slot.
*
* For search results, the search query can be visually differentiated within the result title.
*/
export default defineComponent( {
name: 'CdxMenuItem',
components: { CdxIcon, CdxSearchResultTitle },
props: {
/**
* ID for HTML `id` attribute.
*/
id: {
type: String,
required: true
},
/**
* The value provided to the parent menu component when this menu item is selected.
*/
value: {
type: [ String, Number ] as PropType<string|number>,
required: true
},
/**
* Whether the menu item is disabled.
*/
disabled: {
type: Boolean,
default: false
},
/**
* Whether this menu item is selected.
*/
selected: {
type: Boolean,
default: false
},
/**
* Whether this menu item is being pressed.
*/
active: {
type: Boolean,
default: false
},
/**
* Whether this menu item is hovered over or highlighted via keyboard navigation.
*/
highlighted: {
type: Boolean,
default: false
},
/**
* Label for the menu item. If this isn't provided, the value will be displayed instead.
*/
label: {
type: String,
default: ''
},
/**
* Text that matches current search query. Only used for search results and will be
* displayed after the label.
*/
match: {
type: String,
default: ''
},
/**
* URL for the menu item. If provided, the content of the menu item will be wrapped in an
* anchor tag.
*/
url: {
type: String,
default: ''
},
/**
* Icon for the menu item.
*/
icon: {
type: [ String, Object ] as PropType<Icon>,
default: ''
},
/**
* Whether a thumbnail (or a placeholder icon) should be displayed.
*/
showThumbnail: {
type: Boolean,
default: false
},
/**
* Thumbnail for the menu item.
*/
thumbnail: {
type: [ Object, null ] as PropType<Thumbnail|null>,
default: null
},
/**
* Description of the menu item.
*/
description: {
type: [ String, null ] as PropType<string|null>,
default: ''
},
/**
* The search query to be highlighted within the menu item's title.
*/
searchQuery: {
type: String,
default: ''
},
/**
* Whether to bold menu item labels.
*/
boldLabel: {
type: Boolean,
default: false
},
/**
* Whether to hide description text overflow via an ellipsis.
*/
hideDescriptionOverflow: {
type: Boolean,
default: false
},
/**
* Optional language codes for label, match, and description.
*
* If included, that language code will be added as a `lang` attribute to the element
* wrapping that text node.
*/
language: {
type: Object as PropType<MenuItemLanguageData>,
default: () => {
return {};
}
}
},
emits: [
/**
* Emitted when the menu item becomes selected, active or highlighted in response to
* user interaction. Handled in the Menu component.
*
* @property {MenuState} menuState State to change
* @property {boolean} setState Whether to set that state to this menu item
*/
'change'
],
setup: ( props, { emit } ) => {
const thumbnailLoaded = ref( false );
const thumbnailStyle = ref( {} );
const onMouseEnter = () => {
emit( 'change', 'highlighted', true );
};
const onMouseLeave = () => {
// Remove visual highlighted state. This will also remove the visual active state while
// maintaining the menu item's active status within the menu. See comments on the
// `--active` class below for more details.
emit( 'change', 'highlighted', false );
};
const onMouseDown = ( e: MouseEvent ) => {
// Only apply the active state on main mouse button click. This avoids applying active
// styles on right click, for example. See T304605.
if ( e.button === 0 ) {
emit( 'change', 'active', true );
}
};
const onClick = () => {
emit( 'change', 'selected', true );
};
const highlightQuery = computed( () => props.searchQuery.length > 0 );
const rootClasses = computed( () : Record<string, boolean> => {
return {
'cdx-menu-item--selected': props.selected,
// Only show the active visual state when the menu item is both active and
// highlighted. This means, on mousedown -> mouseleave, the menu item is still
// technically tracked by the menu as active, but will not appear active to the
// user. This also means in the case of mousedown -> mouseleave -> mouseenter, the
// menu item will appear active again, and on click (releasing the mouse button),
// the item will be selected.
'cdx-menu-item--active': props.active && props.highlighted,
'cdx-menu-item--highlighted': props.highlighted,
'cdx-menu-item--enabled': !props.disabled,
'cdx-menu-item--disabled': props.disabled,
'cdx-menu-item--highlight-query': highlightQuery.value,
'cdx-menu-item--bold-label': props.boldLabel,
'cdx-menu-item--has-description': !!props.description,
'cdx-menu-item--hide-description-overflow': props.hideDescriptionOverflow
};
} );
// If a URL is provided for this menu item, the content will be wrapped in an <a> tag.
// Otherwise, it'll just be wrapped in a <span>.
const contentTag = computed( () => props.url ? 'a' : 'span' );
// Get the title, which will be passed to the Title component. Must be a string.
const title = computed( () => props.label || String( props.value ) );
const preloadThumbnail = ( url: string ) => {
const escapedUrl = url.replace( /([\\"\n])/g, '\\$1' );
const image = new Image();
image.onload = () => {
thumbnailStyle.value = { backgroundImage: `url("${escapedUrl}")` };
thumbnailLoaded.value = true;
};
image.onerror = () => {
thumbnailLoaded.value = false;
};
image.src = escapedUrl;
};
onMounted( () => {
if ( props.thumbnail?.url && props.showThumbnail ) {
preloadThumbnail( props.thumbnail.url );
}
} );
return {
onMouseEnter,
onMouseLeave,
onMouseDown,
onClick,
highlightQuery,
rootClasses,
contentTag,
title,
defaultThumbnailIcon: cdxIconImageLayoutFrameless,
thumbnailStyle,
thumbnailLoaded
};
}
} );
</script>
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/dist/theme-wikimedia-ui.less';
@import '../../themes/mixins/common.less';
// TODO: Tokenize.
@font-size-browser: 16;
@font-size-base: 14 / @font-size-browser;
// Line heights are best-practice unit-less.
@line-height-menu-item: ( 22 / @font-size-browser );
@min-width-select: 280px;
@line-height-component: unit( ( 20 / @font-size-browser / @font-size-base ), em );
@size-icon-relative: unit( ( @size-icon / @font-size-browser / @font-size-base ), em );
@padding-vertical-menu-item: 8px;
// The size of the input icon and footer icon containers in the TypeaheadSearch component, and the
// thumbnail in this component. We want these to be the same size so that these figures vertically
// line up nicely when they are used together in the TypeaheadSearch component.
@size-search-figure: 40px;
@margin-end-menu-item-thumbnail: @padding-vertical-menu-item;
@border-color-menu-item-thumbnail: @color-gray200;
@background-color-menu-item-placeholder: @background-color-framed;
// Lighter than `@color-accessory`. See T286851.
@color-menu-item-thumbnail-placeholder-icon: @color-placeholder;
.cdx-menu-item {
list-style: none;
position: relative;
padding: @padding-vertical-menu-item @padding-horizontal-base;
line-height: @line-height-100;
transition-property: @transition-property-base;
transition-duration: @transition-duration-base;
&__content {
display: flex;
align-items: center;
line-height: @line-height-menu-item;
.hyphens();
&,
&:hover {
text-decoration: none;
}
}
&--has-description &__content {
align-items: flex-start;
}
&__text__description {
display: block;
}
&__thumbnail-placeholder,
&__thumbnail {
background-position: center;
background-repeat: no-repeat;
background-size: cover;
// Prevent thumbnail width from shrinking when descriptions are long.
flex-shrink: 0;
width: @size-search-figure;
height: @size-search-figure;
margin-right: @margin-end-menu-item-thumbnail;
// TODO: use token for border color. Currently, a token exists for this color called
// `border-color-base--disabled`, but this token name will likely change and is also not
// appropriate for this use case. For now, we'll directly use the color.
border: @border-width-base @border-style-base @color-gray300;
border-radius: @border-radius-base;
}
&__thumbnail {
display: inline-block;
// Fade in transition applied to the thumbnail on show
&-enter-active {
transition-property: @transition-property-fade;
transition-duration: @transition-duration-base;
}
&-enter-from {
opacity: @opacity-transparent;
}
}
&__thumbnail-placeholder {
background-color: @background-color-menu-item-placeholder;
display: inline-flex;
align-items: center;
justify-content: center;
&__icon {
color: @color-menu-item-thumbnail-placeholder-icon;
}
}
&__icon {
// Make sure the icon inherits the content element's color.
color: inherit;
margin-right: @margin-end-menu-item-thumbnail;
// Icons must scale with font size to maintain top alignment with the label text.
svg {
width: @size-icon-relative;
height: @size-icon-relative;
}
}
&--bold-label {
.cdx-menu-item__text__label {
font-weight: @font-weight-bold;
}
}
&--hide-description-overflow {
.cdx-menu-item__text {
// Prevent text section (label/title and description) from overflowing its container.
// This is needed once we apply the `white-space: nowrap` rule to the description text
// below via `.text-overflow()` mixin.
overflow: hidden;
}
.cdx-menu-item__text__description {
.text-overflow( @visible: false );
}
}
&--enabled {
// We need to set the color here, so it applies to all icons and text, regardless of whether
// the default slot is customized or not. Then, we need to specifically target the
// `&__content` element, which may be an anchor tag, since many sites set a color on `a`.
&,
.cdx-menu-item__content {
color: @color-base;
}
.cdx-menu-item__text__description {
// Different color for description.
color: @color-accessory;
}
&:hover,
&.cdx-menu-item--highlighted {
background-color: @background-color-base--hover;
cursor: @cursor-base--hover;
}
&.cdx-menu-item--active,
&.cdx-menu-item--active:hover {
background-color: @background-color-base--active;
color: @color-progressive;
.cdx-menu-item__content,
.cdx-menu-item__text__description {
color: @color-progressive;
}
}
&.cdx-menu-item--selected,
&.cdx-menu-item--selected:hover {
background-color: @background-color-base--active;
}
&.cdx-menu-item--selected:hover,
&.cdx-menu-item--selected.cdx-menu-item--highlighted {
color: @color-progressive;
.cdx-menu-item__content,
.cdx-menu-item__text__description {
color: @color-progressive;
}
}
}
/* stylelint-disable no-descending-specificity */
&--disabled {
color: @color-base--disabled;
cursor: @cursor-base--disabled;
.cdx-menu-item__text__description {
color: @color-base--disabled;
}
}
/* stylelint-enable no-descending-specificity */
}
</style>