2021-09-09 07:34:51 +00:00
|
|
|
<!--
|
|
|
|
// Copyright © 2020 Anticrm Platform Contributors.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
-->
|
2021-11-25 11:09:37 +00:00
|
|
|
|
2021-09-09 07:34:51 +00:00
|
|
|
<script lang="ts">
|
2021-09-27 08:06:05 +00:00
|
|
|
import { tooltipstore as tooltip, closeTooltip } from '..'
|
2021-09-09 07:34:51 +00:00
|
|
|
import type { TooltipAligment } from '..'
|
|
|
|
import Label from './Label.svelte'
|
|
|
|
|
|
|
|
let tooltipHTML: HTMLElement
|
|
|
|
let dir: TooltipAligment
|
2021-09-27 08:06:05 +00:00
|
|
|
let rect: DOMRect
|
2021-11-25 11:09:37 +00:00
|
|
|
let rectAnchor: DOMRect
|
2021-11-04 11:17:36 +00:00
|
|
|
let tooltipSW: boolean // tooltipSW = true - Label; false - Component
|
2021-09-09 07:34:51 +00:00
|
|
|
|
2021-11-04 11:17:36 +00:00
|
|
|
$: tooltipSW = $tooltip.component ? false : true
|
2021-09-09 07:34:51 +00:00
|
|
|
$: {
|
2021-11-25 11:09:37 +00:00
|
|
|
if (($tooltip.label || $tooltip.component) && tooltipHTML) {
|
2021-09-09 07:34:51 +00:00
|
|
|
if ($tooltip.element) {
|
|
|
|
const doc = document.body.getBoundingClientRect()
|
2021-11-25 11:09:37 +00:00
|
|
|
rect = $tooltip.element.getBoundingClientRect()
|
|
|
|
rectAnchor = ($tooltip.anchor) ? $tooltip.anchor.getBoundingClientRect()
|
|
|
|
: $tooltip.element.getBoundingClientRect()
|
2021-09-09 07:34:51 +00:00
|
|
|
|
2021-09-27 08:06:05 +00:00
|
|
|
if ($tooltip.component) {
|
|
|
|
|
2021-11-25 11:09:37 +00:00
|
|
|
if (rectAnchor.bottom + tooltipHTML.clientHeight + 28 < doc.height) {
|
|
|
|
tooltipHTML.style.top = `calc(${rectAnchor.bottom}px + .75rem)`
|
2021-09-27 08:06:05 +00:00
|
|
|
dir = 'bottom'
|
2021-11-25 11:09:37 +00:00
|
|
|
} else if (rectAnchor.top > doc.height - rectAnchor.bottom) {
|
|
|
|
tooltipHTML.style.bottom = `calc(${doc.height - rectAnchor.y}px + .75rem)`
|
|
|
|
if (tooltipHTML.clientHeight > rectAnchor.top - 28) {
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.top = '1rem'
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.height = rectAnchor.top - 28 + 'px'
|
2021-09-27 08:06:05 +00:00
|
|
|
}
|
|
|
|
dir = 'top'
|
|
|
|
} else {
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.top = `calc(${rectAnchor.bottom}px + .75rem)`
|
|
|
|
if (tooltipHTML.clientHeight > doc.height - rectAnchor.bottom - 28) {
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.bottom = '1rem'
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.height = doc.height - rectAnchor.bottom - 28 + 'px'
|
2021-09-27 08:06:05 +00:00
|
|
|
}
|
|
|
|
dir = 'bottom'
|
|
|
|
}
|
2021-11-25 11:09:37 +00:00
|
|
|
if (rectAnchor.left + tooltipHTML.clientWidth + 16 > doc.width) {
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.left = ''
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.right = doc.width - rectAnchor.right + 'px'
|
2021-09-27 08:06:05 +00:00
|
|
|
} else {
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.left = rectAnchor.left + 'px'
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.right = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (!$tooltip.direction) {
|
2021-11-25 11:09:37 +00:00
|
|
|
if (rectAnchor.right < doc.width / 5) dir = 'right'
|
|
|
|
else if (rectAnchor.left > doc.width - doc.width / 5) dir = 'left'
|
|
|
|
else if (rectAnchor.top < tooltipHTML.clientHeight) dir = 'bottom'
|
2021-09-27 08:06:05 +00:00
|
|
|
else dir = 'top'
|
|
|
|
} else dir = $tooltip.direction
|
|
|
|
|
|
|
|
if (dir === 'right') {
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.top = rectAnchor.y + rectAnchor.height / 2 + 'px'
|
|
|
|
tooltipHTML.style.left = `calc(${rectAnchor.right}px + .75rem)`
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.transform = 'translateY(-50%)'
|
|
|
|
} else if (dir === 'left') {
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.top = rectAnchor.y + rectAnchor.height / 2 + 'px'
|
|
|
|
tooltipHTML.style.right = `calc(${doc.width - rectAnchor.x}px + .75rem)`
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.transform = 'translateY(-50%)'
|
|
|
|
} else if (dir === 'bottom') {
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.top = `calc(${rectAnchor.bottom}px + .5rem)`
|
|
|
|
tooltipHTML.style.left = rectAnchor.x + rectAnchor.width / 2 + 'px'
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.transform = 'translateX(-50%)'
|
|
|
|
} else if (dir === 'top') {
|
2021-11-25 11:09:37 +00:00
|
|
|
tooltipHTML.style.bottom = `calc(${doc.height - rectAnchor.y}px + .75rem)`
|
|
|
|
tooltipHTML.style.left = rectAnchor.x + rectAnchor.width / 2 + 'px'
|
2021-09-27 08:06:05 +00:00
|
|
|
tooltipHTML.style.transform = 'translateX(-50%)'
|
|
|
|
}
|
|
|
|
tooltipHTML.classList.remove('no-arrow')
|
2021-09-09 07:34:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tooltipHTML.style.top = '50%'
|
|
|
|
tooltipHTML.style.left = '50%'
|
|
|
|
tooltipHTML.style.width = 'min-content'
|
|
|
|
tooltipHTML.style.height = 'min-content'
|
|
|
|
tooltipHTML.style.transform = 'translate(-50%, -50%)'
|
|
|
|
tooltipHTML.classList.add('no-arrow')
|
|
|
|
}
|
|
|
|
tooltipHTML.style.visibility = 'visible'
|
|
|
|
} else if (tooltipHTML) tooltipHTML.style.visibility = 'hidden'
|
|
|
|
}
|
2021-09-27 08:06:05 +00:00
|
|
|
|
|
|
|
const hideTooltip = (): void => {
|
|
|
|
tooltipHTML.style.visibility = 'hidden'
|
|
|
|
closeTooltip()
|
|
|
|
}
|
|
|
|
|
|
|
|
const whileShow = (ev: MouseEvent): void => {
|
2021-11-25 11:09:37 +00:00
|
|
|
if ($tooltip.element && tooltipHTML) {
|
2021-09-27 08:06:05 +00:00
|
|
|
const rectP = tooltipHTML.getBoundingClientRect()
|
2021-11-25 11:09:37 +00:00
|
|
|
const dT: number = dir === 'bottom' ? 12 : 0
|
|
|
|
const dB: number = dir === 'top' ? 12 : 0
|
|
|
|
const inTrigger: boolean = ev.x >= rect.left && ev.x <= rect.right && ev.y >= rect.top && ev.y <= rect.bottom
|
|
|
|
const inPopup: boolean =
|
|
|
|
ev.x >= rectP.left && ev.x <= rectP.right && ev.y >= rectP.top - dT && ev.y <= rectP.bottom + dB
|
|
|
|
if (tooltipSW) {
|
|
|
|
if (!inTrigger) hideTooltip()
|
|
|
|
} else {
|
|
|
|
if (!(inTrigger || inPopup)) hideTooltip()
|
2021-11-04 11:17:36 +00:00
|
|
|
}
|
2021-09-27 08:06:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-09 07:34:51 +00:00
|
|
|
</script>
|
|
|
|
|
2021-09-27 08:06:05 +00:00
|
|
|
<svelte:window on:mousemove={(ev) => { whileShow(ev) }} />
|
|
|
|
{#if $tooltip.component}
|
|
|
|
<div class="popup" bind:this={tooltipHTML}>
|
|
|
|
{#if $tooltip.label}<div class="header"><Label label={$tooltip.label} /></div>{/if}
|
|
|
|
<svelte:component this={$tooltip.component} {...$tooltip.props} />
|
|
|
|
</div>
|
|
|
|
{:else if $tooltip.label}
|
2021-09-09 07:34:51 +00:00
|
|
|
<div class="tooltip {dir}" bind:this={tooltipHTML}>
|
|
|
|
<Label label={$tooltip.label} />
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style lang="scss">
|
2021-09-27 08:06:05 +00:00
|
|
|
.header {
|
2021-12-01 18:14:38 +00:00
|
|
|
margin-bottom: 1.5rem;
|
2021-09-27 08:06:05 +00:00
|
|
|
font-weight: 500;
|
|
|
|
font-size: 1rem;
|
|
|
|
color: var(--theme-caption-color);
|
|
|
|
}
|
|
|
|
|
|
|
|
.popup {
|
|
|
|
position: fixed;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
2021-12-01 18:14:38 +00:00
|
|
|
padding: 1.5rem;
|
2021-09-27 08:06:05 +00:00
|
|
|
color: var(--theme-caption-color);
|
2021-12-01 18:14:38 +00:00
|
|
|
background-color: var(--theme-button-bg-focused);
|
2021-09-27 08:06:05 +00:00
|
|
|
border: 1px solid var(--theme-button-border-enabled);
|
|
|
|
border-radius: .75rem;
|
|
|
|
user-select: none;
|
|
|
|
filter: drop-shadow(0 1.5rem 4rem rgba(0, 0, 0, .35));
|
2021-11-19 06:09:42 +00:00
|
|
|
z-index: 10000;
|
2021-09-27 08:06:05 +00:00
|
|
|
}
|
|
|
|
|
2021-09-09 07:34:51 +00:00
|
|
|
.tooltip {
|
|
|
|
position: fixed;
|
|
|
|
padding: .5rem;
|
|
|
|
color: var(--theme-caption-color);
|
|
|
|
background-color: var(--theme-tooltip-color);
|
|
|
|
border: 1px solid var(--theme-bg-accent-color);
|
|
|
|
border-radius: .5rem;
|
|
|
|
user-select: none;
|
|
|
|
text-align: center;
|
2021-11-19 06:09:42 +00:00
|
|
|
z-index: 10000;
|
2021-09-09 07:34:51 +00:00
|
|
|
|
|
|
|
&::after {
|
|
|
|
content: '';
|
|
|
|
position: absolute;
|
|
|
|
width: .875rem;
|
|
|
|
height: .875rem;
|
|
|
|
background-color: var(--theme-tooltip-color);
|
|
|
|
border: 1px solid var(--theme-bg-accent-color);
|
|
|
|
border-radius: 0 0 3px;
|
|
|
|
clip-path: polygon(100% 25%, 100% 100%, 25% 100%);
|
|
|
|
}
|
|
|
|
|
|
|
|
&.top::after,
|
|
|
|
&.bottom::after {
|
|
|
|
left: 50%;
|
|
|
|
margin-left: -.5rem;
|
|
|
|
}
|
|
|
|
&.top {
|
|
|
|
bottom: 100%;
|
|
|
|
box-shadow: 0px 8px 20px rgba(0, 0, 0, .35);
|
|
|
|
&::after {
|
|
|
|
bottom: -.3125rem;
|
|
|
|
transform: rotate(45deg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&.bottom {
|
|
|
|
top: 100%;
|
|
|
|
box-shadow: 0px -8px 20px rgba(0, 0, 0, .35);
|
|
|
|
&::after {
|
|
|
|
top: -.3125rem;
|
|
|
|
transform: rotate(-135deg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
&.right::after,
|
|
|
|
&.left::after {
|
|
|
|
top: 50%;
|
|
|
|
margin-top: -.5rem;
|
|
|
|
}
|
|
|
|
&.right {
|
|
|
|
left: 100%;
|
|
|
|
box-shadow: -8px 0px 20px rgba(0, 0, 0, .35);
|
|
|
|
&::after {
|
|
|
|
left: -.3125rem;
|
|
|
|
transform: rotate(135deg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&.left {
|
|
|
|
right: 100%;
|
|
|
|
box-shadow: 8px 0px 20px rgba(0, 0, 0, .35);
|
|
|
|
&::after {
|
|
|
|
right: -.3125rem;
|
|
|
|
transform: rotate(-45deg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.no-arrow {
|
|
|
|
box-shadow: 0px 0px 20px rgba(0, 0, 0, .75);
|
|
|
|
&::after {
|
|
|
|
content: none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|