budibase/packages/bbui/src/ActionMenu/ActionMenu.svelte

44 lines
901 B
Svelte
Raw Normal View History

2021-04-12 17:10:55 +02:00
<script>
import { setContext } from "svelte"
2021-04-23 09:41:49 +02:00
import Popover from "../Popover/Popover.svelte"
import Menu from "../Menu/Menu.svelte"
export let disabled = false
export let align = "left"
export let portalTarget
2021-04-23 09:41:49 +02:00
let anchor
let dropdown
2021-04-12 17:10:55 +02:00
2021-04-23 09:41:49 +02:00
// This is needed because display: contents is considered "invisible".
// It should only ever be an action button, so should be fine.
function getAnchor(node) {
anchor = node.firstChild
}
export const hide = () => {
dropdown.hide()
}
export const show = () => {
dropdown.show()
}
2021-08-18 00:04:54 +02:00
const openMenu = event => {
2021-08-17 23:57:11 +02:00
if (!disabled) {
event.stopPropagation()
show()
}
}
setContext("actionMenu", { show, hide })
2021-04-12 17:10:55 +02:00
</script>
2021-04-28 14:42:17 +02:00
<div use:getAnchor on:click={openMenu}>
<slot name="control" />
2021-04-12 17:10:55 +02:00
</div>
<Popover bind:this={dropdown} {anchor} {align} {portalTarget}>
2021-04-23 09:41:49 +02:00
<Menu>
<slot />
2021-04-23 09:41:49 +02:00
</Menu>
</Popover>