index.vue 3.44 KB
<template>
  <FloatingTooltip ref="tooltip"
                   v-bind="tooltipProps"
                   v-on="mergedListeners"
                   @click.native.stop>
    <slot />
    <template #popper>
      <slot name="content">
        <span v-if="hasTooltipContent" class="tool-tips-content-label">{{ tooltipContent }}</span>
      </slot>
    </template>
  </FloatingTooltip>
</template>

<script>
import { Tooltip as FloatingTooltip } from "floating-vue";

export default {
	name: "BaseTooltip",
	components: {
		FloatingTooltip,
	},
	inheritAttrs: false,
	props: {
		content: {
			type: [String, Number],
			default: null,
		},
		options: {
			type: [Object, String, Number],
			default: null,
		},
		hideAfter: {
			type: [Number, String],
			default: 0,
		},
	},
	data() {
		return {
			hideTimer: null,
		};
	},
	computed: {
		tooltipProps() {
			const attrs = {
				...this.$attrs,
			};
			delete attrs.content;
			if (typeof this.options === "string" || typeof this.options === "number") {
				return {
					...attrs,
					theme: attrs.theme || "white-tooltip",
				};
			}
			return {
				...(this.options || {}),
				...attrs,
				theme: (this.options || {}).theme || attrs.theme || "white-tooltip",
			};
		},
		tooltipContent() {
			if (this.content !== null && this.content !== undefined)
				return this.content;
			if (typeof this.options === "string" || typeof this.options === "number")
				return this.options;
			if (this.options && Object.prototype.hasOwnProperty.call(this.options, "content"))
				return this.options.content;
			return null;
		},
		hasTooltipContent() {
			return this.tooltipContent !== null && this.tooltipContent !== undefined && this.tooltipContent !== "";
		},
		mergedListeners() {
			return {
				...this.$listeners,
				"apply-show": this.handleApplyShow,
				"apply-hide": this.handleApplyHide,
			};
		},
		hideAfterMs() {
			const value = Number(this.hideAfter);
			return Number.isFinite(value) && value > 0 ? value : 0;
		},
	},
	beforeDestroy() {
		this.clearHideTimer();
	},
	methods: {
		show(...args) {
			return this.$refs.tooltip?.show(...args);
		},
		hide(...args) {
			this.clearHideTimer();
			return this.$refs.tooltip?.hide(...args);
		},
		dispose(...args) {
			this.clearHideTimer();
			return this.$refs.tooltip?.dispose(...args);
		},
		handleApplyShow(...args) {
			this.emitListener("apply-show", args);
			this.startAutoHide();
		},
		handleApplyHide(...args) {
			this.clearHideTimer();
			this.emitListener("apply-hide", args);
		},
		startAutoHide() {
			this.clearHideTimer();
			if (!this.hideAfterMs)
				return;
			this.hideTimer = setTimeout(() => {
				this.hide();
			}, this.hideAfterMs);
		},
		clearHideTimer() {
			if (this.hideTimer) {
				clearTimeout(this.hideTimer);
				this.hideTimer = null;
			}
		},
		emitListener(name, args) {
			const listener = this.$listeners[name];
			if (typeof listener === "function") {
				listener(...args);
				return;
			}
			if (Array.isArray(listener)) {
				listener.forEach(fn => typeof fn === "function" && fn(...args));
			}
		},
	},
};
</script>

<style lang="scss">
.v-popper--theme-white-tooltip .v-popper__inner {
	background: #ffffff;
	color: rgba(0, 0, 0, 0.88);
	border-radius: 8px;
	padding: 8px 12px;
	box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
	border: 1px solid rgba(0, 0, 0, 0.06);
}

.v-popper--theme-white-tooltip .v-popper__arrow-outer {
	border-color: #ffffff;
}
.tool-tips-content-label {
	font-weight: 400;
	font-size: 12px;
	line-height: 20px;
	color: #000000e5;
}
</style>