All files / components/menu-button MenuButton.vue

93.61% Statements 44/47
66.66% Branches 14/21
90% Functions 9/10
93.61% Lines 44/47

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    1x 7x       1x     1x 1x 1x 1x 1x 1x 1x 1x 1x                                                               5x                                     5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x             5x         5x                             1x 1x 1x 1x 1x   10x 10x 10x                 3x           1x     15x                     2x   2x                 1x 1x                                          
<template>
	<div
		class="cdx-menu-button"
		:class="rootClasses"
		:style="rootStyle"
	>
		<cdx-toggle-button
			:id="toggleId"
			ref="toggle"
			v-bind="otherAttrs"
			v-model="expanded"
			:disabled="computedDisabled"
			quiet
			aria-haspopup="menu"
			:aria-controls="menuId"
			:aria-expanded="expanded"
			@blur="expanded = false"
			@keydown="onKeydown"
		>
			<!-- @slot MenuButton content -->
			<slot />
		</cdx-toggle-button>
		<div class="cdx-menu-button__menu-wrapper">
			<cdx-menu
				:id="menuId"
				ref="menu"
				v-model:selected="modelWrapper"
				v-model:expanded="expanded"
				:menu-items="menuItems"
				v-bind="menuConfig"
				role="menu"
				:aria-labelledby="toggleId"
			/>
		</div>
	</div>
</template>
 
<script lang="ts">
import {
	PropType,
	Ref,
	ComponentPublicInstance,
	defineComponent,
	ref,
	toRef
} from 'vue';
 
import CdxToggleButton from '../toggle-button/ToggleButton.vue';
import CdxMenu from '../menu/Menu.vue';
import useFieldData from '../../composables/useFieldData';
import useFloatingMenu from '../../composables/useFloatingMenu';
import useGeneratedId from '../../composables/useGeneratedId';
import useModelWrapper from '../../composables/useModelWrapper';
import useSplitAttributes from '../../composables/useSplitAttributes';
import { MenuButtonItemData, MenuConfig } from '../../types';
 
export default defineComponent( {
	name: 'CdxMenuButton',
	components: {
		CdxToggleButton,
		CdxMenu
	},
	inheritAttrs: false,
	props: {
		/**
		 * Value of the current selection.
		 *
		 * Must be bound with `v-model:selected`.
		 */
		selected: {
			type: [ String, Number, null ] as PropType<string|number|null>,
			required: true
		},
 
		/**
		 * Menu items. See the MenuItemData type.
		 */
		menuItems: {
			type: Array as PropType<MenuButtonItemData[]>,
			required: true
		},
 
		/**
		 * Configuration for various menu features. All properties default to false.
		 *
		 * See the MenuConfig type.
		 */
		menuConfig: {
			type: Object as PropType<MenuConfig>,
			default: () => {
				return {} as MenuConfig;
			}
		},
 
		/**
		 * Whether the dropdown is disabled.
		 */
		disabled: {
			type: Boolean,
			default: false
		}
	},
 
	emits: [
		/**
		 * When the selected value changes.
		 *
		 * @property {string | number} selected The new selected value
		 */
		'update:selected'
	],
 
	setup( props, { emit, attrs } ) {
		const menu = ref<InstanceType<typeof CdxMenu>>();
		const toggle = ref<InstanceType<typeof CdxToggleButton>>();
		const selectedProp = toRef( props, 'selected' );
		const modelWrapper = useModelWrapper( selectedProp, emit, 'update:selected' );
		const expanded = ref( false );
		const toggleId = useGeneratedId( 'menuToggle' );
		const menuId = useGeneratedId( 'menu' );
		const { computedDisabled } = useFieldData( toRef( props, 'disabled' ) );
 
		// Get helpers from useSplitAttributes() composable.
		const { rootClasses, rootStyle, otherAttrs } = useSplitAttributes( attrs );
 
		function onKeydown( e: KeyboardEvent ) {
			if (
				!menu.value ||
				computedDisabled.value ||
				props.menuItems.length === 0 ||
				e.key === ' '
			) {
				return;
			}
			menu.value.delegateKeyNavigation( e );
		}
 
		useFloatingMenu( toggle as Ref<ComponentPublicInstance>, menu, {
			useAvailableWidth: true,
			placement: 'bottom-start',
			offset: 4
		} );
 
		return {
			computedDisabled,
			expanded,
			menu,
			menuId,
			modelWrapper,
			onKeydown,
			toggle,
			toggleId,
			rootClasses,
			rootStyle,
			otherAttrs
		};
	}
} );
</script>
 
<style lang="less">
@import ( reference ) '@wikimedia/codex-design-tokens/theme-wikimedia-ui.less';
 
.cdx-menu-button {
	&__menu-wrapper {
		position: relative;
	}
 
	.cdx-menu {
		min-width: @size-800;
		max-width: @size-1600;
	}
 
	// Overrides when used within a Dialog component
	.cdx-dialog & {
		// The menu is positioned relative to the dialog backdrop, not the triggering element.
		position: static;
	}
}
</style>