Skip to content

A self-hosted media server and file management system

5.0 KB
Raw Blame
<script setup lang="ts">
import type { HoverCardProps } from '@aminnausin/cedar-ui';

import { ref, useTemplateRef, watch, nextTick } from 'vue';
import { ProiconsCommentExclamation } from '@/components/cedar-ui/icons';
import { cn } from '@aminnausin/cedar-ui';

const props = withDefaults(defineProps<HoverCardProps>(), {
    hoverCardDelay: 600,
    hoverCardLeaveDelay: 500,
    margin: 0,
    paddingLeft: 0,
    scrollContainer: 'body',
    disabled: false,
    icon: ProiconsCommentExclamation,
});

const hoverCardHovered = ref<boolean>(false);
const hoverCardTimout = ref<number | null>(null);
const hoverCardLeaveTimeout = ref<number | null>(null);
const tooltipStyles = ref<Record<string, string>>({});

const tooltip = useTemplateRef('tooltip');

const init = ref(false);

const hoverCardEnter = (event: MouseEvent) => {
    if (props.content === '' || props.disabled) return;

    if (hoverCardLeaveTimeout.value) clearTimeout(hoverCardLeaveTimeout.value);

    if (hoverCardHovered.value) return;

    if (hoverCardTimout.value) clearTimeout(hoverCardTimout.value);

    if (!init.value) init.value = true; // Loads into Dom after hover for the first time

    hoverCardTimout.value = globalThis.setTimeout(() => {
        hoverCardHovered.value = true;
        updateTooltipPosition(event);
    }, props.hoverCardDelay);
};

const hoverCardLeave = () => {
    if (hoverCardTimout.value) clearTimeout(hoverCardTimout.value);

    if (!hoverCardHovered.value) return;
    if (hoverCardLeaveTimeout.value) clearTimeout(hoverCardLeaveTimeout.value);

    hoverCardLeaveTimeout.value = globalThis.setTimeout(() => {
        hoverCardHovered.value = false;
    }, props.hoverCardLeaveDelay);
};

const updateTooltipPosition = (event: MouseEvent) => {
    const elem = event.target as HTMLElement;
    const parentRect = elem.parentElement?.getBoundingClientRect();

    let rect = elem.getBoundingClientRect();

    if (parentRect && parentRect.height < rect.height) {
        rect = parentRect;
    }

    const scrollY = props.scrollContainer === 'body' ? document.body.scrollTop : window.scrollY;
    const scrollX = props.scrollContainer === 'body' ? document.body.scrollLeft : window.scrollX;

    const top = rect.bottom + props.margin + scrollY;
    const left = rect.left + scrollX + props.paddingLeft;

    tooltipStyles.value = { left: `${left}px`, top: `${top}px` };

    nextTick(() => {
        if (!tooltip.value) return;
        const tipRect = tooltip.value.getBoundingClientRect();
        const overflow = tipRect.right - window.innerWidth + 20;
        if (overflow > 0) tooltipStyles.value = { left: `${left - overflow}px`, top: `${top}px` };
    });
};

watch(
    () => props.content,
    () => {
        if (!props.content || props.content.trim() === '') hoverCardLeave();
    },
);
//relative
</script>

<template>
    <div @mouseover="hoverCardEnter" @mouseleave="hoverCardLeave">
        <slot name="trigger">
            <a href="#_" class="hover:underline">Trigger</a>
        </slot>
        <Teleport to="body" v-if="init">
            <Transition enter-from-class="opacity-0" enter-to-class="opacity-100" leave-from-class="opacity-100" leave-to-class="opacity-0">
                <div
                    @mouseover="hoverCardEnter"
                    @mouseleave="hoverCardLeave"
                    v-show="hoverCardHovered"
                    v-cloak
                    :class="
                        cn(
                            'group absolute z-301',
                            'transition-opacity duration-(--duration-input) ease-in-out',
                            'flex gap-2 p-3',
                            'border border-neutral-200/70 dark:border-none',
                            'bg-white dark:bg-neutral-800/70',
                            'rounded-md text-sm shadow-md backdrop-blur-lg',
                            'max-w-md md:max-w-xl xl:max-w-3xl',
                            positionClasses,
                        )
                    "
                    :style="tooltipStyles"
                    :ref="'tooltip'"
                >
                    <slot name="icon">
                        <template v-if="!iconHidden">
                            <component class="mb-auto size-5 shrink-0" :is="icon" />
                        </template>
                    </slot>
                    <div class="scrollbar-minimal h-full max-h-[50vh] w-full overflow-auto">
                        <slot name="content">
                            <div class="flex min-h-0 flex-col gap-2">
                                <h4 class="break-word" v-if="contentTitle">{{ contentTitle }}</h4>
                                <p class="dark:text-foreground-1 break-word w-full pe-0.5 text-wrap whitespace-pre-wrap" v-if="content">{{ content }}</p>
                            </div>
                        </slot>
                    </div>
                </div>
            </Transition>
        </Teleport>
    </div>
</template>
<style lang="css" scoped>
.break-word {
    word-break: break-word;
}
</style>