Fix HoverPopover positioning

The popover would not stay fixed relative to its parent when the page
was being scrolled. We wrap it in a container that is positioned
relative similar to how we do it in the Popover component. This allows
us to position it more precisely while making it retain its position
relative to its parent when scrolled.
This commit is contained in:
Rūdolfs Ošiņš
2024-02-21 17:39:59 +00:00
parent d86ff1952a
commit 693f8991a0
2 changed files with 33 additions and 22 deletions
+29 -21
View File
@@ -3,7 +3,9 @@
export let onShow: () => void = () => {};
export let stylePopoverPositionLeft: string | undefined = undefined;
export let stylePopoverPositionRight: string | undefined = undefined;
export let stylePopoverPositionTop: string | undefined = undefined;
export let stylePopoverPositionBottom: string | undefined = undefined;
export let stylePopoverPadding: string | undefined = "1rem";
let visible: boolean = false;
@@ -17,38 +19,44 @@
</script>
<style>
.container {
position: relative;
}
.popover {
background: var(--color-background-float);
border-radius: var(--border-radius-regular);
border: 1px solid var(--color-border-hint);
box-shadow: var(--elevation-low);
position: relative;
right: 1rem;
position: absolute;
z-index: 10;
}
</style>
<div
role="button"
tabindex="0"
on:mouseenter={() => setVisible(true)}
on:mouseleave={() => setVisible(false)}>
<slot name="toggle" />
<div class="container">
<div
role="button"
tabindex="0"
on:mouseenter={() => setVisible(true)}
on:mouseleave={() => setVisible(false)}>
<slot name="toggle" />
{#if visible}
<!-- If this component is used inside a button (see `NodeId`, for example)
{#if visible}
<!-- If this component is used inside a button (see `NodeId`, for example)
we don’t want clicks in the popover to trigger button actions. So we
stop propagation of click events. -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div style:position="absolute" on:click|stopPropagation>
<div
class="popover"
style:padding={stylePopoverPadding}
style:left={stylePopoverPositionLeft}
style:top={stylePopoverPositionTop}>
<slot name="popover" />
<!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div style:position="absolute" on:click|stopPropagation>
<div
class="popover"
style:padding={stylePopoverPadding}
style:left={stylePopoverPositionLeft}
style:right={stylePopoverPositionRight}
style:bottom={stylePopoverPositionBottom}
style:top={stylePopoverPositionTop}>
<slot name="popover" />
</div>
</div>
</div>
{/if}
{/if}
</div>
</div>
+4 -1
View File
@@ -37,6 +37,9 @@
</script>
<style>
.container {
position: relative;
}
.popover {
background: var(--color-background-float);
border-radius: var(--border-radius-regular);
@@ -50,7 +53,7 @@
<svelte:window on:click={clickOutside} on:touchstart={clickOutside} />
<div bind:this={thisComponent} style:position="relative">
<div bind:this={thisComponent} class="container">
<slot name="toggle" {expanded} {toggle} />
{#if expanded}