Skip to content

Commit

Permalink
[Tooltip]: adds arrow on tooltip default mode
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminknox committed Oct 30, 2023
1 parent cf1055f commit 1a1111a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/components/floating/floating.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
}
</script>

<div bind:this={floating} class="leo-floating">
<div on:mouseenter on:mouseleave bind:this={floating} class="leo-floating">
<slot />
</div>

Expand Down
102 changes: 81 additions & 21 deletions src/components/tooltip/tooltip.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@
let arrow: HTMLElement
let trigger: HTMLElement
let arrowPlacement: string = undefined
function positionArrow(
e: CustomEvent<{ middlewareData: MiddlewareData; placement: Placement }>
) {
const { x: arrowX, y: arrowY } = e.detail.middlewareData.arrow
arrowPlacement = e.detail.placement.split('-')[0]
const staticSide = {
top: 'bottom',
right: 'left',
bottom: 'top',
left: 'right'
}[e.detail.placement.split('-')[0]]
}[arrowPlacement]
if (!arrow) return
Expand All @@ -54,6 +58,37 @@
})
}
let tooltipHovered = false
let triggerHovered = false
const handleMouseleave = (() => {
let timeout
return () => {
clearTimeout(timeout)
timeout = setTimeout(() => {
if (!triggerHovered && !tooltipHovered) {
setVisible(false)
}
}, 150)
}
})()
const handleTriggerMouseenter = () => {
triggerHovered = true
setVisible(true)
}
const handleTriggerMouseleave = () => {
triggerHovered = false
handleMouseleave()
}
const handleTooltipMouseleave = () => {
tooltipHovered = false
handleMouseleave()
}
function setVisible(newVisible: boolean) {
if (newVisible === visible) return
Expand All @@ -62,13 +97,7 @@
}
</script>

<div
class="leo-tooltip"
on:mouseenter={() => setVisible(true)}
on:mouseleave={() => setVisible(false)}
on:focusin={() => setVisible(true)}
on:focusout={() => setVisible(false)}
>
<div class="leo-tooltip">
{#key visibleInternal}
<Floating
target={trigger}
Expand All @@ -77,6 +106,8 @@
{placement}
{shift}
autoUpdate
on:mouseleave={handleTooltipMouseleave}
on:mouseenter={() => (tooltipHovered = true)}
middleware={[arrowMiddleware({ padding: 0, element: arrow })]}
on:computedposition={positionArrow}
>
Expand All @@ -93,16 +124,19 @@
<slot name="content">
{text}
</slot>
<div
class="arrow"
hidden={mode === 'default' || mode === 'mini'}
bind:this={arrow}
/>
<div class={`arrow ${arrowPlacement}`} bind:this={arrow} />
</div>
</Floating>
{/key}

<div class="trigger" bind:this={trigger}>
<div
on:focusin={() => setVisible(true)}
on:focusout={() => setVisible(false)}
on:mouseenter={handleTriggerMouseenter}
on:mouseleave={handleTriggerMouseleave}
class="trigger"
bind:this={trigger}
>
<slot />
</div>
</div>
Expand Down Expand Up @@ -131,13 +165,39 @@
font: var(--leo-font-primary-default-regular);
}
.leo-tooltip .tooltip .arrow {
position: absolute;
background: var(--background);
width: 8px;
height: 8px;
transform: rotate(45deg);
z-index: -1;
.leo-tooltip .tooltip {
& .arrow {
position: absolute;
background: var(--background);
width: 8px;
height: 8px;
transform: rotate(45deg);
z-index: -1;
}
&.default .arrow {
border: var(--border-width) solid var(--border-color);
z-index: 10;
&.left,
&.bottom {
border-bottom: 0;
}
&.right,
&.bottom {
border-right: 0;
}
&.right,
&.top {
border-top: 0;
}
&.left,
&.top {
border-left: 0;
}
}
}
.leo-tooltip .tooltip.hero {
Expand Down

0 comments on commit 1a1111a

Please sign in to comment.