mastoradio-fork/src/components/PopperMenu.svelte

63 lines
1.5 KiB
Svelte
Raw Normal View History

2020-03-07 19:13:21 +01:00
<span bind:this={btn} on:click={() => open()} class="popperMenu">
2020-02-22 01:59:26 +01:00
<slot name="btn">
<button>button</button>
</slot>
</span>
2020-02-22 03:50:37 +01:00
<div class="contextMenu__overlay" class:active={isActive} on:click={() => close()}></div>
2020-02-22 01:59:26 +01:00
<div class="contextMenu" bind:this={content} class:active={isActive}>
<slot name="content">
No content
</slot>
</div>
<script>
2020-02-22 03:50:37 +01:00
import { onMount, setContext } from 'svelte'
2020-02-22 01:59:26 +01:00
import { createPopper } from '@popperjs/core';
import detectOverflow from '@popperjs/core/lib/utils/detectOverflow.js';
let btn
let content
let isActive = false
export let needOffset
2020-02-22 01:59:26 +01:00
2020-02-22 03:50:37 +01:00
export function open () {
2020-02-22 01:59:26 +01:00
isActive = true
}
2020-02-22 03:50:37 +01:00
export function close () {
2020-02-22 01:59:26 +01:00
isActive = false
}
2020-02-22 03:50:37 +01:00
setContext('openMenu', open)
setContext('closeMenu', close)
2020-02-22 01:59:26 +01:00
onMount(() => {
createPopper(btn, content, {
placement: 'left-start',
modifiers: [
{
name: 'offset',
options: {
offset: ({ reference, popper }) => {
if (needOffset) {
return [-10, -reference.width - 5];
} else {
return [5, -reference.width - 5];
}
2020-02-22 01:59:26 +01:00
}
}
},
{
name: 'myModifier',
enabled: true,
phase: 'main',
requiresIfExists: ['offset'],
fn({ state }) {
const overflow = detectOverflow(state);
}
}
]
});
});
</script>