Skip to content

A self-hosted media server and file management system

8.2 KB
Raw Blame
<script setup lang="ts">
import type { SwipeDirection, ToastProps, ToastType } from '@aminnausin/cedar-ui';

import { useToastTimer, useSwipeHandler, SWIPE_THRESHOLD, TOAST_LIFE, VISIBLE_TOASTS_AMOUNT, cn } from '@aminnausin/cedar-ui';
import { CedarDanger, CedarInfo, CedarSuccess, CedarWarning, SvgSpinners90RingWithBg } from '@/components/cedar-ui/icons';
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import { ButtonCorner } from '../button';

const emit = defineEmits<(e: 'close', id: string) => void>(); // removeToast

const props = withDefaults(defineProps<ToastProps>(), {
    type: 'default',
    position: 'bottom-center',
    life: TOAST_LIFE,
    maxVisibleToasts: VISIBLE_TOASTS_AMOUNT,
    title: 'Title',
    html: '',
    style: '', // can be used since toast is instantiated with a function
    swipeDirections: () => [],
});

const leaveDirection = ref('translate-y-0');
const swipeDirections = computed(() => {
    if (props.swipeDirections.length > 0) return props.swipeDirections;

    const [y, x] = props.position.split('-');
    const directions: Array<SwipeDirection> = [];

    if (isSwipeDirection(y)) {
        directions.push(y);
    }

    if (isSwipeDirection(x)) {
        directions.push(x);
    }

    if (x === 'center') {
        directions.push('left', 'right');
    }

    return directions;
});

const isBottom = computed(() => {
    return props.position.includes('bottom');
});

const closeTimeout = ref<number | null>(null);
const stackTimeout = ref<number | null>(null);

const toastHovered = ref(false);
const isMounted = ref(false);

const { offset, isSwiping, onPointerDown, onPointerMove, onPointerUp, onPointerCancel } = useSwipeHandler({
    directions: swipeDirections,
    swipeThreshold: { px: SWIPE_THRESHOLD },
    onSwipeOut: onClose,
});

const { cancel: cancelToastTimer } = useToastTimer({
    duration: () => props.life || TOAST_LIFE,
    isPaused: () => props.expanded || !props.life || props.life === Infinity || toastHovered.value,
    onTimeout: onClose,
});

function getLeaveDirection() {
    const yDir = isBottom.value ? 'translate-y-full' : '-translate-y-full';

    if (isSwiping.value) {
        const xDir = offset.value.x > 0 ? 'translate-x-full' : '-translate-x-full';
        return Math.abs(offset.value.y) > 0 ? yDir : xDir;
    }

    if (props.toastCount === 1) {
        return yDir;
    }

    return 'translate-y-0';
}

function onClose() {
    if (closeTimeout.value) return;

    leaveDirection.value = getLeaveDirection();
    isMounted.value = false;
    cancelToastTimer();
    closeTimeout.value = globalThis.setTimeout(() => {
        emit('close', props.id);
    }, 350);
}

function clearCloseTimeout() {
    if (stackTimeout.value) {
        clearTimeout(stackTimeout.value);
        stackTimeout.value = null;
    }
    if (closeTimeout.value) {
        clearTimeout(closeTimeout.value);
        closeTimeout.value = null;
    }
}

function isSwipeDirection(val: string): val is SwipeDirection {
    return ['top', 'right', 'bottom', 'left'].includes(val);
}

function mapColour(type: ToastType) {
    switch (type) {
        case 'success':
            return 'text-success';
        case 'info':
            return 'text-primary dark:text-primary-muted';
        case 'warning':
            return 'text-warning';
        case 'danger':
            return 'text-danger-1';
        case 'default':
        case 'promise':
        default:
            return 'text-foreground-0';
    }
}

onMounted(() => {
    isMounted.value = true;

    if (stackTimeout.value) clearTimeout(stackTimeout.value);

    stackTimeout.value = globalThis.setTimeout(() => {
        props.stack();
    });
});

onBeforeUnmount(() => {
    clearCloseTimeout();
});
</script>

<template>
    <li
        ref="toastEl"
        :id="props.id"
        :class="[`toast absolute w-full touch-none transition-all duration-300 ease-out`, { 'pointer-events-none opacity-0': index >= maxVisibleToasts }, style]"
        :style="{
            '--offset-x': `${offset.x}px`,
            '--offset-y': `${offsetY}px`,
            '--scale': scale,
            '--z-index': zIndex,
            '--position-top': `${expanded ? (isBottom ? 'auto' : positionY) : 'inherit'}`,
            '--position-bottom': `${isBottom && expanded ? positionY : 'inherit'}`,
        }"
        :data-is-swiping="isSwiping"
        @mouseover="toastHovered = true"
        @mouseout="toastHovered = false"
        @dragend="onPointerUp"
        @pointerdown="onPointerDown"
        @pointermove="onPointerMove"
        @pointerup="onPointerUp"
        @pointercancel="onPointerCancel"
    >
        <Transition
            :enter-from-class="`opacity-0 ${isBottom ? 'translate-y-full' : '-translate-y-full'}`"
            :enter-to-class="`opacity-100 translate-y-0`"
            :leave-from-class="`opacity-100 translate-y-0`"
            :leave-to-class="`opacity-0 ${leaveDirection}`"
        >
            <div
                v-if="isMounted"
                role="alert"
                :class="[
                    { 'p-3 py-4': !html, 'p-0': html },
                    'flex items-start gap-1 rounded-md backdrop-blur-lg',
                    'group relative sm:select-text',
                    'transition-[opacity,translate,scale] duration-300 ease-out',
                    'bg-overlay-t text-foreground-0 shadow-[0_5px_15px_-3px_rgb(0_0_0/0.08)]',
                    'ring-r-inverse ring-1 ring-inset',
                ]"
            >
                <div v-if="!html" :class="['flex h-[19.5px] items-center justify-center', mapColour(type)]">
                    <CedarSuccess v-if="type === 'success'" class="toast-icon" />
                    <CedarInfo v-if="type === 'info'" class="toast-icon" />
                    <CedarWarning v-if="type === 'warning'" class="toast-icon" />
                    <CedarDanger v-if="type === 'danger'" class="toast-icon" />
                    <SvgSpinners90RingWithBg v-if="type === 'promise'" class="toast-icon" />
                </div>
                <div class="space-y-1.5">
                    <p :class="['line-clamp-2 pe-6 text-[13px] font-medium', mapColour(type)]" :title="title">
                        {{ title }}
                    </p>
                    <p
                        v-if="description"
                        :class="['scrollbar-minimal max-h-32 w-full touch-pan-y overflow-y-auto pe-2 text-xs leading-4 break-all whitespace-pre-wrap opacity-70']"
                    >
                        {{ description }}
                    </p>
                </div>

                <template v-if="!html">
                    <ButtonCorner
                        label="Close Toast"
                        :class="
                            cn(
                                'text-foreground-2 hover:text-foreground-1 dark:text-danger-3 hover:bg-surface-1 dark:bg-surface-1/50 dark:hover:bg-surface-1 dark:hover:text-danger-1 absolute right-0 mr-2.5 size-6 p-1.5',
                                'cursor-pointer rounded-full opacity-0',
                                {
                                    'top-1/2 -translate-y-1/2': !description && !html,
                                    'top-0 mt-2.5': description || html,
                                    'opacity-100': toastHovered,
                                    'opacity-0': !toastHovered,
                                },
                            )
                        "
                        :use-default-style="false"
                        @click="onClose"
                    />
                </template>
            </div>
        </Transition>
    </li>
</template>
<style lang="css" scoped>
.toast-icon {
    flex-shrink: 0;
    /* -mt-0.5 */
    margin-top: calc(var(--spacing) * -0.5) /* -0.125rem = -2px */;
    /* size-4 */
    width: calc(var(--spacing) * 4) /* 1rem = 16px */;
    height: calc(var(--spacing) * 4) /* 1rem = 16px */;
}

.toast {
    transform: translateY(var(--offset-y, 0px)) translateX(var(--offset-x, 0px)) scale(var(--scale, 1));
    z-index: var(--z-index, 200);
    top: var(--position-top, inherit);
    bottom: var(--position-bottom, inherit);
}

@media (prefers-reduced-motion: reduce) {
    .toast {
        transition: none !important;
    }
}

[data-is-swiping='true'] {
    transition-property: none !important;
}
</style>