Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion v2/pink-sb/src/lib/Popover.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

const activeInstance = activePopover.get();
const inDialogGroup = hasContext('dialog-group');
const inTooltipGroup = hasContext('tooltip-group');

let id = 'popover-' + Math.random().toString(36).substring(2, 9);
let referenceElement: HTMLSpanElement;
Expand Down Expand Up @@ -82,7 +83,7 @@
}

function portalPopover(node: HTMLElement) {
if (!portal && !inDialogGroup) return;
if (!portal && !inDialogGroup && !inTooltipGroup) return;

const target = !inDialogGroup
? document.body
Expand Down
13 changes: 11 additions & 2 deletions v2/pink-sb/src/lib/Tooltip.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import { tick, hasContext } from 'svelte';
import type { Placement } from '@floating-ui/dom';
import { tick, hasContext, setContext } from 'svelte';
import { autoUpdate, computePosition, flip, offset, shift } from '@floating-ui/dom';

export let portal: boolean = false;
Expand All @@ -19,6 +19,8 @@
let referenceElement: HTMLSpanElement;
const id = 'tooltip-' + Math.random().toString(36).substring(2, 9);

setContext('tooltip-group', true);

const inDialogGroup = hasContext('dialog-group');

// hide tooltip if disabled becomes true
Expand Down Expand Up @@ -56,11 +58,18 @@
async function update() {
if (!referenceElement || !tooltipElement) return;

const firstChild = referenceElement.firstElementChild;
let firstChild = referenceElement.firstElementChild;
if (!(firstChild instanceof HTMLElement)) {
return;
}

// If element has no bounding box like
// display: contents, then use its child!
const rect = firstChild.getBoundingClientRect();
if (rect.width === 0 && rect.height === 0 && firstChild.firstElementChild) {
firstChild = firstChild.firstElementChild as HTMLElement;
}

const { x, y } = await computePosition(firstChild, tooltipElement, {
placement,
middleware: [offset(offsetAmount), flip(), shift()]
Expand Down
19 changes: 19 additions & 0 deletions v2/pink-sb/src/stories/Tooltip.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</script>

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

<Story name="With Popover">
<div class="container-extended">
<Tooltip>
<Popover let:toggle>
<Button on:click={toggle}>Click me</Button>
<div slot="tooltip">Popover content here</div>
</Popover>
<p slot="tooltip">Open menu</p>
</Tooltip>
</div>
</Story>

<style>
.container {
margin: 12rem auto;
display: grid;
place-content: center;
}
.container-extended {
display: flex;
height: 100vh;
align-items: center;
justify-content: center;
}
</style>