Skip to content

Commit 865e2fc

Browse files
authored
Merge pull request #390 from appwrite/fix-tooltip
2 parents 8e32f56 + c15b4be commit 865e2fc

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

v2/pink-sb/src/lib/Popover.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
const activeInstance = activePopover.get();
1313
const inDialogGroup = hasContext('dialog-group');
14+
const inTooltipGroup = hasContext('tooltip-group');
1415
1516
let id = 'popover-' + Math.random().toString(36).substring(2, 9);
1617
let referenceElement: HTMLSpanElement;
@@ -82,7 +83,7 @@
8283
}
8384
8485
function portalPopover(node: HTMLElement) {
85-
if (!portal && !inDialogGroup) return;
86+
if (!portal && !inDialogGroup && !inTooltipGroup) return;
8687
8788
const target = !inDialogGroup
8889
? document.body

v2/pink-sb/src/lib/Tooltip.svelte

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script lang="ts">
2-
import { tick, hasContext } from 'svelte';
32
import type { Placement } from '@floating-ui/dom';
3+
import { tick, hasContext, setContext } from 'svelte';
44
import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom';
55
66
export let portal: boolean = false;
@@ -19,6 +19,8 @@
1919
let referenceElement: HTMLSpanElement;
2020
const id = 'tooltip-' + Math.random().toString(36).substring(2, 9);
2121
22+
setContext('tooltip-group', true);
23+
2224
const inDialogGroup = hasContext('dialog-group');
2325
2426
// hide tooltip if disabled becomes true
@@ -56,11 +58,18 @@
5658
async function update() {
5759
if (!referenceElement || !tooltipElement) return;
5860
59-
const firstChild = referenceElement.firstElementChild;
61+
let firstChild = referenceElement.firstElementChild;
6062
if (!(firstChild instanceof HTMLElement)) {
6163
return;
6264
}
6365
66+
// If element has no bounding box like
67+
// display: contents, then use its child!
68+
const rect = firstChild.getBoundingClientRect();
69+
if (rect.width === 0 && rect.height === 0 && firstChild.firstElementChild) {
70+
firstChild = firstChild.firstElementChild as HTMLElement;
71+
}
72+
6473
const { x, y } = await computePosition(firstChild, tooltipElement, {
6574
placement,
6675
middleware: [offset(offsetAmount), flip(), shift()]

v2/pink-sb/src/stories/Tooltip.stories.svelte

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</script>
2020

2121
<script>
22+
import Popover from '$lib/Popover.svelte';
2223
import { Button } from '$lib/button/index.js';
2324
import { Story, Template } from '@storybook/addon-svelte-csf';
2425
let toggle = true;
@@ -36,10 +37,28 @@
3637
<Story name="Default" />
3738
<Story name="Hover" {play} />
3839

40+
<Story name="With Popover">
41+
<div class="container-extended">
42+
<Tooltip>
43+
<Popover let:toggle>
44+
<Button on:click={toggle}>Click me</Button>
45+
<div slot="tooltip">Popover content here</div>
46+
</Popover>
47+
<p slot="tooltip">Open menu</p>
48+
</Tooltip>
49+
</div>
50+
</Story>
51+
3952
<style>
4053
.container {
4154
margin: 12rem auto;
4255
display: grid;
4356
place-content: center;
4457
}
58+
.container-extended {
59+
display: flex;
60+
height: 100vh;
61+
align-items: center;
62+
justify-content: center;
63+
}
4564
</style>

0 commit comments

Comments
 (0)