Skip to content

A self-hosted media server and file management system

fix: player tooltip has code duplication

aminnausinbeb096830dbb
diff --git a/resources/js/components/video/VideoTimeline.vue b/resources/js/components/video/VideoTimeline.vue
index 9a386d5a..c57efa84 100644
--- a/resources/js/components/video/VideoTimeline.vue
+++ b/resources/js/components/video/VideoTimeline.vue
@@ -28,7 +28,8 @@ const progressBar = useTemplateRef('progress-bar');
const containerWidth = ref(0);
const timeElapsed = defineModel<number>({ required: true });
-const timeSeeking = ref('');
+const timeSeekingDisplay = ref('');
+const timeSeekingSeconds = ref(0);
const thumbX = computed(() => {
if (!progressContainer.value) return 0;
@@ -49,13 +50,13 @@ const handleKeydown = (event: KeyboardEvent) => {
function showTooltip(event: MouseEvent | TouchEvent) {
if (!progressTooltip.value) return;
- progressTooltip.value.tooltipToggle(true);
+ progressTooltip.value.show();
if ('touches' in event) handleProgressTooltip(event);
}
function hideTooltip() {
if (!progressTooltip.value) return;
- progressTooltip.value.tooltipToggle(false);
+ progressTooltip.value.hide();
}
const handleProgressTooltip = throttle((event: MouseEvent | TouchEvent) => {
@@ -82,7 +83,8 @@ const getProgressTooltip = (clientX: number) => {
const steppedDuration = Math.round((percent * props.timeDuration) / sliderStep) * sliderStep;
const clampedDuration = Math.min(Math.max(0, steppedDuration), props.timeDuration);
- timeSeeking.value = toFormattedDuration(clampedDuration, true, 'digital') ?? '00:00';
+ timeSeekingSeconds.value = clampedDuration;
+ timeSeekingDisplay.value = toFormattedDuration(clampedDuration, true, 'digital') ?? '00:00';
};
let resizeObserver: ResizeObserver | null = null;
@@ -120,13 +122,12 @@ defineExpose({ progressTooltip });
<section class="relative flex h-8 min-h-8 w-full flex-1 flex-col-reverse rounded-full px-2">
<VideoTooltipTimeline
ref="progress-tooltip"
- tooltip-position="top"
- class="-top-4 left-0"
- :tooltip-text="timeSeeking"
+ :tooltip-text="timeSeekingDisplay"
+ :tooltip-time="timeSeekingSeconds"
:target-element="progressContainer ?? undefined"
- :offset="videoButtonOffset"
- :tooltip-arrow="false"
+ :offset="2"
:tooltip-delay="0"
+ class="bottom-4"
/>
<div class="group peer pointer-events-auto relative flex h-2 min-h-2 items-center select-none" ref="progress-container" role="group" aria-label="Video progress slider">
<!-- Padding -->
diff --git a/resources/js/components/video/VideoTooltipTimeline.vue b/resources/js/components/video/VideoTooltipTimeline.vue
index fb60ec8d..a6fd4f5f 100644
--- a/resources/js/components/video/VideoTooltipTimeline.vue
+++ b/resources/js/components/video/VideoTooltipTimeline.vue
@@ -1,13 +1,14 @@
<script setup lang="ts">
-import { reactive, ref, useTemplateRef } from 'vue';
+import { useTooltipVisibility } from './tooltip/useTooltipVisibility';
+import { useTemplateRef } from 'vue';
import { getClientX } from '@/service/util';
import { cn } from '@aminnausin/cedar-ui';
const props = withDefaults(
defineProps<{
tooltipText?: string;
+ tooltipTime: number;
tooltipArrow?: boolean;
- tooltipPosition?: 'top' | 'bottom';
offset?: number;
style?: string;
targetElement?: HTMLElement;
@@ -18,46 +19,16 @@ const props = withDefaults(
tooltipDelay: 200,
tooltipLeaveDelay: 100,
tooltipText: 'Tooltip Text',
- tooltipArrow: true,
- tooltipPosition: 'top',
- offset: 0,
- style: '',
+ tooltipTime: 0,
+ tooltipArrow: false,
+ offset: 2,
},
);
-const data = reactive<{
- tooltipTimout: NodeJS.Timeout | null;
- tooltipLeaveTimeout: NodeJS.Timeout | null;
-}>({
- tooltipTimout: null,
- tooltipLeaveTimeout: null,
-});
+const { tooltipVisible, show, hide } = useTooltipVisibility(props.tooltipDelay, props.tooltipLeaveDelay);
-const tooltipVisible = ref(false);
const tooltip = useTemplateRef('tooltip');
-const tooltipEnter = () => {
- if (data.tooltipLeaveTimeout) clearTimeout(data.tooltipLeaveTimeout);
-
- if (tooltipVisible.value) return;
-
- if (data.tooltipTimout) clearTimeout(data.tooltipTimout);
-
- data.tooltipTimout = globalThis.setTimeout(() => {
- tooltipVisible.value = true;
- if (!tooltip.value) return;
- }, props.tooltipDelay);
-};
-
-const tooltipLeave = () => {
- if (data.tooltipTimout) clearTimeout(data.tooltipTimout);
- if (!tooltipVisible.value) return;
- if (data.tooltipLeaveTimeout) clearTimeout(data.tooltipLeaveTimeout);
- data.tooltipLeaveTimeout = globalThis.setTimeout(() => {
- tooltipVisible.value = false;
- }, props.tooltipLeaveDelay);
-};
-
function calculateTooltipPosition(event: MouseEvent | TouchEvent) {
if (!tooltip.value) return;
@@ -74,40 +45,38 @@ function calculateTooltipPosition(event: MouseEvent | TouchEvent) {
tooltip.value.style.transform = `translateX(${left}px)`;
}
-const tooltipToggle = (state: boolean = true) => {
- if (!state) {
- tooltipLeave();
- return;
- }
-
- tooltipEnter();
-};
-
-defineExpose({ calculateTooltipPosition, tooltipToggle, tooltipVisible });
+defineExpose({
+ calculateTooltipPosition,
+ show,
+ hide,
+});
</script>
<template>
<div
ref="tooltip"
+ :class="
+ cn(
+ 'pointer-events-none absolute flex flex-col items-center justify-center gap-4 opacity-0 transition-opacity duration-75 ease-in',
+ { 'duration-input scale-100 opacity-100 ease-out': tooltipVisible },
+ style,
+ )
+ "
style="z-index: 9"
- v-cloak
- :class="cn('absolute flex items-center justify-center opacity-0 transition-opacity duration-75 ease-in', { 'duration-input opacity-100 ease-out': tooltipVisible }, style)"
>
- <slot name="content">
- <div :class="cn('pointer-events-none h-full w-full scale-0 transition-[scale] duration-75 ease-in', { 'duration-input scale-100 ease-out': tooltipVisible })">
- <p
- class="bg-opacity-90 flex min-h-4 shrink-0 items-center justify-center rounded-md bg-neutral-800 px-2 py-1 font-mono text-xs whitespace-nowrap shadow-xs backdrop-blur-xs"
- >
- {{ tooltipText }}
- </p>
- <div
- ref="tooltipArrow"
- v-show="tooltipArrow"
- class="absolute bottom-0 left-1/2 inline-flex w-2.5 -translate-x-1/2 translate-y-full items-center justify-center overflow-hidden"
- >
- <div class="bg-opacity-90 h-1.5 w-1.5 origin-top-left -rotate-45 transform bg-neutral-800"></div>
- </div>
+ <div :class="cn('mx-auto scale-0 transition-[scale] duration-75 ease-in', { 'duration-input scale-100 ease-out': tooltipVisible })">
+ <p
+ class="bg-opacity-90 flex min-h-4 shrink-0 items-center justify-center rounded-md bg-neutral-800 px-2 py-1 font-mono text-xs whitespace-nowrap shadow-xs backdrop-blur-xs"
+ >
+ {{ tooltipText }}
+ </p>
+ <div
+ ref="tooltipArrow"
+ v-show="tooltipArrow"
+ class="absolute bottom-0 left-1/2 inline-flex w-2.5 -translate-x-1/2 translate-y-full items-center justify-center overflow-hidden"
+ >
+ <div class="bg-opacity-90 h-1.5 w-1.5 origin-top-left -rotate-45 transform bg-neutral-800"></div>
</div>
- </slot>
+ </div>
</div>
</template>
diff --git a/resources/js/components/video/tooltip/VideoTooltipBase.vue b/resources/js/components/video/tooltip/VideoTooltipBase.vue
index 30002c9f..98492466 100644
--- a/resources/js/components/video/tooltip/VideoTooltipBase.vue
+++ b/resources/js/components/video/tooltip/VideoTooltipBase.vue
@@ -1,5 +1,7 @@
<script setup lang="ts">
-import { nextTick, onMounted, onUnmounted, reactive, ref, useTemplateRef, watch } from 'vue';
+import { nextTick, onMounted, onUnmounted, ref, useTemplateRef, watch } from 'vue';
+import { useTooltipVisibility } from './useTooltipVisibility';
+import { cn } from '@aminnausin/cedar-ui';
const props = withDefaults(
defineProps<{
@@ -10,69 +12,43 @@ const props = withDefaults(
verticalOffset?: string;
style?: string;
targetElement?: HTMLElement;
+ tooltipDelay?: number;
+ tooltipLeaveDelay?: number;
}>(),
{
+ tooltipDelay: 200,
+ tooltipLeaveDelay: 100,
tooltipText: 'Tooltip Text',
tooltipArrow: false,
tooltipPosition: 'top',
- offset: 8,
verticalOffset: '-3rem',
- style: '',
+ offset: 8,
},
);
-const data = reactive<{
- tooltipDelay: number;
- tooltipLeaveDelay: number;
- tooltipTimout: NodeJS.Timeout | null;
- tooltipLeaveTimeout: NodeJS.Timeout | null;
-}>({
- tooltipDelay: 200,
- tooltipLeaveDelay: 100,
- tooltipTimout: null,
- tooltipLeaveTimeout: null,
-});
-
const cachedBoundingElement = ref<HTMLElement>();
-const tooltipVisible = ref(false);
const tooltipWidth = ref(48);
const tooltip = useTemplateRef('tooltip');
-function tooltipEnter(event?: MouseEvent) {
- if (data.tooltipLeaveTimeout) clearTimeout(data.tooltipLeaveTimeout);
-
- if (tooltipVisible.value && event) return;
+const { tooltipVisible, show, hide } = useTooltipVisibility(props.tooltipDelay, props.tooltipLeaveDelay);
- if (data.tooltipTimout) clearTimeout(data.tooltipTimout);
-
- tooltipWidth.value = tooltip.value?.offsetWidth ?? 48;
-
- data.tooltipTimout = globalThis.setTimeout(
+function tooltipEnter(event?: MouseEvent) {
+ show(
+ event ? props.tooltipDelay : 0,
async () => {
- tooltipVisible.value = true;
-
if (!tooltip.value) return;
await nextTick();
tooltipWidth.value = tooltip.value.offsetWidth;
calculateTooltipPosition(event);
- tooltipLeave(4000);
+ hide(4000);
},
- event ? data.tooltipDelay : 0,
+ () => (tooltipWidth.value = tooltip.value?.offsetWidth ?? 48),
);
}
-function tooltipLeave(timeout: number = data.tooltipLeaveDelay) {
- if (data.tooltipTimout) clearTimeout(data.tooltipTimout);
- if (!tooltipVisible.value) return;
- if (data.tooltipLeaveTimeout) clearTimeout(data.tooltipLeaveTimeout);
- data.tooltipLeaveTimeout = globalThis.setTimeout(() => {
- tooltipVisible.value = false;
- }, timeout);
-}
-
-function calculateTooltipPosition(event?: MouseEvent) {
+function calculateTooltipPosition(event?: MouseEvent | TouchEvent) {
if (!tooltip.value || !props.targetElement) return;
const sourceElement = (event?.target as HTMLElement) ?? cachedBoundingElement.value;
@@ -96,9 +72,9 @@ function calculateTooltipPosition(event?: MouseEvent) {
tooltip.value.style.left = `${left}px`;
}
-function tooltipToggle(event: MouseEvent, state: boolean = true) {
+function tooltipToggle(event?: MouseEvent, state: boolean = true) {
if (!state) {
- tooltipLeave();
+ hide();
return;
}
@@ -106,7 +82,7 @@ function tooltipToggle(event: MouseEvent, state: boolean = true) {
}
const handleTooltipLeave = (e: Event) => {
- tooltipLeave(50);
+ hide(50);
};
onMounted(() => {
@@ -117,7 +93,7 @@ onUnmounted(() => {
window.removeEventListener('resize', handleTooltipLeave);
});
-defineExpose({ tooltipToggle });
+defineExpose({ calculateTooltipPosition, tooltipToggle, tooltipEnter, tooltipLeave: hide, tooltipVisible });
watch(
() => props.tooltipText,
@@ -129,15 +105,14 @@ watch(
</script>
<template>
- <Transition
- enter-active-class="ease-out duration-150"
- enter-from-class="scale-[0.8] opacity-60"
- enter-to-class="scale-100 opacity-100"
- leave-active-class="ease-in-out duration-100"
- leave-from-class="scale-100 opacity-100"
- leave-to-class="scale-[0.1] opacity-50"
- >
- <div ref="tooltip" v-show="tooltipVisible" :class="`absolute text-white!`" style="z-index: 9" :style="{ top: verticalOffset }">
+ <Transition enter-from-class="scale-[0.8]" leave-to-class="scale-[0.1]">
+ <div
+ v-show="tooltipVisible"
+ ref="tooltip"
+ :class="cn('absolute text-white opacity-60 transition-[opacity,scale] duration-75 ease-in', { 'duration-input scale-100 opacity-100 ease-out': tooltipVisible }, style)"
+ style="z-index: 9"
+ :style="{ top: verticalOffset }"
+ >
<slot name="content">
<p
class="bg-opacity-90 flex min-h-4 shrink-0 items-center justify-center rounded-md bg-neutral-800/90 px-2 py-1 font-mono text-xs whitespace-nowrap shadow-xs backdrop-blur-xs"
diff --git a/resources/js/components/video/tooltip/useTooltipVisibility.ts b/resources/js/components/video/tooltip/useTooltipVisibility.ts
new file mode 100644
index 00000000..e2a436ff
--- /dev/null
+++ b/resources/js/components/video/tooltip/useTooltipVisibility.ts
@@ -0,0 +1,34 @@
+import { reactive, ref } from 'vue';
+
+export function useTooltipVisibility(delay: number = 200, leaveDelay: number = 100) {
+ const tooltipVisible = ref(false);
+ const timeouts = reactive<{ enter: NodeJS.Timeout | null; leave: NodeJS.Timeout | null }>({
+ enter: null,
+ leave: null,
+ });
+
+ function show(overrideDelay?: number, cb?: () => void, prepare?: () => void) {
+ if (timeouts.leave) clearTimeout(timeouts.leave);
+ if (tooltipVisible.value) return;
+ // Not sure why event was checked for here if (tooltipVisible.value && event) return;
+ if (timeouts.enter) clearTimeout(timeouts.enter);
+
+ timeouts.enter = globalThis.setTimeout(() => {
+ tooltipVisible.value = true;
+ if (cb) cb();
+ }, overrideDelay ?? delay);
+ }
+
+ function hide(overrideDelay?: number, cb?: () => void) {
+ if (timeouts.enter) clearTimeout(timeouts.enter);
+ if (!tooltipVisible.value) return;
+ if (timeouts.leave) clearTimeout(timeouts.leave);
+
+ timeouts.leave = globalThis.setTimeout(() => {
+ tooltipVisible.value = false;
+ if (cb) cb();
+ }, overrideDelay ?? leaveDelay);
+ }
+
+ return { tooltipVisible, show, hide };
+}