Dropdown Menu
KDropdownMenu is a button (or any slotted content) that is clicked to trigger a menu popover beneath it.
Props
label
The label for the menu trigger.
items
An array of objects containing a required label
property and other optional properties which will render a menu of KDropdownItems
.
<KDropdownMenu
label="Documentation"
:items="[
{ label: 'Props', to: { path: '/components/dropdown-menu.html', hash: '#props' } },
{ label: 'Slots', to: { path: '/components/dropdown-menu.html', hash: '#slots' } },
{ label: 'Top', to: { path: '/components/dropdown-menu.html' } }
]"
/>
2
3
4
5
6
7
8
appearance
Use this prop to specify the display style for the dropdown menu. Can be either menu
(default) or selectionMenu
. The menu
style is the standard you have seen in the example above. Uses a standard primary
KButton
with hover state over items and no notion of "selection".
<KDropdownMenu label="Documentation" :items="items" />
The selectionMenu
style is used when a visual indication of the currently selected menu item is needed. selected
state is handled automatically when clicking a KDropdownItem
if used in conjunction with the items
prop. Each item should have a label
and a value
.
If using the items
slot, you will have access to the handleSelection()
method which should be called on each item's click event and takes the item
data as a parameter. This will enable you to attach to the @change
event (which returns the selected item) to track your selection.
<KDropdownMenu
:label="selectedItem.label"
appearance="selectionMenu"
@change="handleChange"
>
<template #items="{ handleSelection }">
<KDropdownItem
v-for="item in menuItems"
:selected="selectedItem.value === item.value"
@click="handleSelection(item)"
>
{{ item.label }}
</KDropdownItem>
</template>
</KDropdownMenu>
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
data() {
return {
selectedItem: {
label: 'Select an item',
value: ''
},
menuItems: [{
label: 'US (United States)',
value: 'us'
},
{
label: 'FR (France)',
value: 'fr'
}]
}
},
methods: {
handleChange (item) {
this.selectedItem = item
this.$toaster.open(`${item.label} clicked!`)
}
}
})
</script>
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
buttonAppearance
Use this prop to customize the trigger KButton appearance.
<KDropdownMenu
label="Documentation"
button-appearance="creation"
:items="items"
/>
2
3
4
5
showCaret
Use this prop if you would like the trigger button to display the caret.
<KDropdownMenu
label="Documentation"
:items="items"
show-caret
/>
2
3
4
5
caretColor
NOTE
Use this prop in conjunction with the showCaret
prop
Use this prop to customize the color of the caret
<KDropdownMenu
caret-color="lightgreen"
label="Select Type"
:items="items"
show-caret
/>
2
3
4
5
6
icon
A string for the KIcon
to be displayed on the dropdown button with or in place of the button label.
<KDropdownMenu
icon="cogwheel"
:items="items"
show-caret
/>
2
3
4
5
width
The width of the dropdown body (defaults to auto
). Currently we support numbers (will be converted to px
), auto
, and percentages for width.
<KDropdownMenu
label="Documentation"
:items="items"
width="500"
/>
2
3
4
5
kpopAttributes
Use the kpopAttributes
prop to configure the KPop props dropdown.
<KCard
title="KPopAttributes FTW"
body="Click the three dots in the upper right corner to see the example in action"
>
<template #actions>
<KDropdownMenu
:kpop-attributes="{
popoverClasses: 'some-class',
maxWidth: '100'
}"
:items="deepClone(defaultItemsUnselected)"
>
<template #default>
<KButton
appearance="btn-link"
size="small"
>
<template #icon>
<KIcon
icon="more"
color="blue"
size="16"
/>
</template>
</KButton>
</template>
</KDropdownMenu>
</template>
</KCard>
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
disabled
Use this prop to disable the dropdown, can be used in conjunction with disabledTooltip
prop.
disabledTooltip
Text to display on hover if dropdown is disabled.
<KDropdownMenu
label="Documentation"
:disabled="true"
disabled-tooltip="You can't click me"
:items="deepClone(defaultItemsUnselected)"
/>
2
3
4
5
6
Slots
There are 2 supported slots:
default
The trigger element for opening/closing the menu.
<KDropdownMenu :items="items">
<template #default>
<KButton
show-caret
appearance="creation"
>
Menu
</KButton>
</template>
</KDropdownMenu>
2
3
4
5
6
7
8
9
10
items
You can customize the appearance of dropdown items using this slot.
Slot props:
closeDropdown
- type:
Function
- Function that triggers dropdown close.
- type:
For an example of using the items slot see the KDropdownItem
section.
KDropdownItem
KDropdownMenu generates a KDropdownItem for each object in the items
prop array. At the most basic level, KDropdownItem is a wrapper around each item to display it correctly inside KDropdownMenu
. You can use the items
slot of the KDropdownMenu
to manually create your own menu items.
Properties
item
- the properties the router-link is built from, it expects alabel
and optionally ato
(for a router-link item) orvalue
(for aselectionMenu
item).disabled
- a boolean (defaults tofalse
), whether or not to disable the item.selected
- a boolean (defaults tofalse
), whether or not the item is selected if usingselectionMenu
appearance.hasDivider
- a boolean (defaults tofalse
), whether or not the item should have a divider bar displayed above itisDangerous
- a boolean (defaults tofalse
), whether or not to apply danger styles (text color is red)
<KDropdownItem :item="{ label: 'Leave the page', to: { path: '/' } }" />
There are 3 primary item types:
link
- the generic type generated using the
items
prop onKDropdownMenu
- the generic type generated using the
item
prop onKDropdownItem
- the generic type generated using the
button
- this item is generated if a handler is specified for the@click
event on aKDropdownItem
custom
- no special handling, you completely control the content
<KDropdownMenu label="Variety" width="250">
<template #items="{ closeDropdown }">
<KDropdownItem :item="youAreHere" />
<KDropdownItem
:item="{ label: 'A button with a a long long long long long long looooooooooooooooooooooooooooooooooooooooooooooong name' }"
has-divider
@click="clickHandler('Button clicked!')"
/>
<KDropdownItem
disabled
@click="clickHandler"
>
Disabled button
</KDropdownItem>
<KDropdownItem
:item="{ label: 'You are here 2', to: { path: '/components/dropdown-menu.html' } }"
disabled
@click="clickHandler"
>
Disabled to link
</KDropdownItem>
<KDropdownItem
@click="clickHandler"
>
Button w/ action
<KButton
appearance="btn-link"
@click.stop="() => { actionClickHandler(); closeDropdown(); }"
>
<KIcon icon="trash" />
</KButton>
</KDropdownItem>
<KDropdownItem
has-divider
is-dangerous
>
<a
href="http://www.google.com"
rel="noopener"
target="_blank"
>
External link
<KIcon
icon="externalLink"
size="12"
color="red"
/>
</a>
</KDropdownItem>
</template>
</KDropdownMenu>
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
Events
Event | Description |
---|---|
@click | Fires when a button type menu item is clicked |
@change | Fires when items within a selectionMenu are clicked; returns the selected menu item object or null |
@toggleDropdown | Fires when the button to toggle the menu is clicked; returns true if the menu is open, or false |