All files / components/menu-item MenuItem.vue

100% Statements 67/67
89.04% Branches 65/73
100% Functions 18/18
100% Lines 63/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    7x 21x       7x     7x 7x 7x 7x       7x                                                                                                                                                                                                                                                                                     619x                           621x                   621x     12x 12x     621x       4x   621x     10x 9x     621x 11x   621x 621x   621x 787x                                       621x 605x     621x 605x   621x                       7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   1477x 1477x 1477x 1477x                 12x 12x 12x   12x         4x 4x 4x   4x         10x 10x 10x   10x         11x 11x 11x   11x       1419x             1419x                                                                                                                                                                         7x 7x                                                                                                                                                                          
<template>
	<li
		:id="id"
		role="option"
		class="cdx-menu-item"
		:class="rootClasses"
		:aria-disabled="disabled"
		:aria-selected="selected"
		@mousemove="onMouseMove"
		@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. -->
				<cdx-thumbnail
					v-if="showThumbnail"
					:thumbnail="thumbnail"
					class="cdx-menu-item__thumbnail"
				/>
 
				<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 label supporting text. -->
					<template v-if="supportingText">
						<!-- eslint-disable-next-line vue/no-useless-mustaches -->
						{{ ' ' }}
						<span
							class="cdx-menu-item__text__supporting-text"
							:lang="language?.supportingText"
						>
							<bdi>{{ supportingText }}</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 } from 'vue';
import { Icon } from '@wikimedia/codex-icons';
import CdxIcon from '../icon/Icon.vue';
import CdxThumbnail from '../thumbnail/Thumbnail.vue';
import CdxSearchResultTitle from '../search-result-title/SearchResultTitle.vue';
import { MenuState, Thumbnail, MenuItemLanguageData } from '../../types';
 
/**
 * A selectable option included within a Menu.
 */
export default defineComponent( {
	name: 'CdxMenuItem',
 
	components: { CdxIcon, CdxThumbnail, 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 visually highlighted due to hover or 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: ''
		},
 
		/**
		 * Text that supports the label. Supporting text will appear next to the label in a more
		 * subtle color.
		 */E
		supportingText: {
			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, supporting text, 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 } ) => {
		/**
		 * Handle mousemove event.
		 *
		 * We use mousemove to achieve these behaviors:
		 *  - MenuItem will not be highlighted if the cursor happens to be on top of it when the
		 *    Menu expands, only when the user intentionally moves the mouse.
		 *  - In the case that the cursor is over the MenuItem but another MenuItem is highlighted,
		 *    on mousemove this item will become highlighted immediately.
		 */
		const onMouseMove = () => {
			// Only emit an event if this item is not already highlighted. This is necessary because
			// mousemove will fire on every move of the mouse over this element.
			if ( !props.highlighted ) {
				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 ) );
 
		return {
			onMouseMove,
			onMouseLeave,
			onMouseDown,
			onClick,
			highlightQuery,
			rootClasses,
			contentTag,
			title
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
@import ( reference ) '../../themes/mixins/common.less';
 
.cdx-menu-item {
	list-style: none;
	position: relative;
	padding: @spacing-50 @spacing-75;
	line-height: @line-height-medium;
	transition-property: @transition-property-base;
	transition-duration: @transition-duration-base;
 
	&__content {
		display: flex;
		align-items: center;
		// TODO: change from this deprecated value to @line-height-small.
		// This will require some associated changes in thumbnail size, see T322384
		line-height: @line-height-x-small;
		.hyphens();
 
		&,
		&:hover {
			text-decoration: @text-decoration-none;
		}
	}
 
	&--has-description &__content {
		align-items: flex-start;
	}
 
	&__text {
		// Make sure long words won't overflow the container.
		max-width: @size-full;
 
		&__description {
			display: block;
		}
	}
 
	&__thumbnail.cdx-thumbnail {
		margin-right: @spacing-50;
	}
 
	&__icon.cdx-icon {
		// Make sure the icon inherits the content element's color.
		color: inherit;
		margin-right: @spacing-50;
	}
 
	&--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( @param-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__supporting-text,
		.cdx-menu-item__text__description {
			color: @color-subtle;
		}
 
		&.cdx-menu-item--highlighted {
			background-color: @background-color-interactive;
			cursor: @cursor-base--hover;
		}
 
		&.cdx-menu-item--active {
			background-color: @background-color-progressive-subtle;
			color: @color-progressive;
 
			.cdx-menu-item__content,
			.cdx-menu-item__text__description {
				color: @color-progressive;
			}
		}
 
		&.cdx-menu-item--selected {
			background-color: @background-color-progressive-subtle;
		}
 
		&.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-disabled;
		cursor: @cursor-base--disabled;
 
		.cdx-menu-item__text__description {
			color: @color-disabled;
		}
	}
	/* stylelint-enable no-descending-specificity */
}
</style>