Vue3气泡卡片(Popover)

简介: 这是一个基于Vue的气泡卡片组件(Popover)介绍,提供了在线预览链接及详细API参数说明,包括maxWidth、title、content等,并支持自定义样式。

效果如下图:在线预览

在这里插入图片描述

APIs

Popover

参数 说明 类型 默认值
maxWidth 弹出卡片最大宽度,单位 px string | number ‘auto’
title 卡片标题 string | slot undefined
titleStyle 卡片标题样式 CSSProperties {}
content 卡片内容 string | slot undefined
contentStyle 卡片内容样式 CSSProperties {}
bgColor 弹出卡片背景颜色 string ‘#fff’
popoverStyle 卡片容器样式 CSSProperties {}
arrow 是否显示箭头 boolean true
trigger 弹出卡片触发方式 ‘hover’ | ‘click’ ‘hover’
showDelay 弹出卡片显示的延迟时间,单位 ms number 100
hideDelay 弹出卡片隐藏的延迟时间,单位 ms number 100
show v-model 弹出卡片是否显示 boolean false

Events

名称 说明 类型
openChange 显示隐藏的回调 (visible: boolean) => void

创建气泡卡片组件Popover.vue

其中引入使用了以下工具函数:

<script setup lang="ts">
import { ref, watch, watchEffect, computed } from 'vue'
import type { CSSProperties } from 'vue'
import { useSlotsExist, rafTimeout, cancelRaf } from '../utils'
interface Props {
  maxWidth?: string | number // 弹出卡片最大宽度,单位 px
  title?: string // 卡片标题 string | slot
  titleStyle?: CSSProperties // 卡片标题样式
  content?: string // 卡片内容 string | slot
  contentStyle?: CSSProperties // 卡片内容样式
  bgColor?: string // 弹出卡片背景颜色
  popoverStyle?: CSSProperties // 卡片容器样式
  arrow?: boolean // 是否显示箭头
  trigger?: 'hover' | 'click' // 弹出卡片触发方式
  showDelay?: number // 弹出卡片显示的延迟时间,单位 ms
  hideDelay?: number // 弹出卡片隐藏的延迟时间,单位 ms
  show?: boolean // (v-model) 弹出卡片是否显示
}
const props = withDefaults(defineProps<Props>(), {
  maxWidth: 'auto',
  title: undefined,
  titleStyle: () => ({}),
  content: undefined,
  contentStyle: () => ({}),
  bgColor: '#fff',
  popoverStyle: () => ({}),
  arrow: true,
  trigger: 'hover',
  showDelay: 100,
  hideDelay: 100,
  show: false
})
const visible = ref(false)
const top = ref(0) // 提示框top定位
const left = ref(0) // 提示框left定位
const defaultRef = ref() // 声明一个同名的模板引用
const popoverRef = ref() // 声明一个同名的模板引用
const hideTimer = ref() // 延迟调用 ID
const activeBlur = ref(false) // 是否激活 blur 事件
const emits = defineEmits(['update:show', 'openChange'])
const slotsExist = useSlotsExist(['title', 'content'])
const popoverMaxWidth = computed(() => {
  if (typeof props.maxWidth === 'number') {
    return props.maxWidth + 'px'
  }
  return props.maxWidth
})
const showTitle = computed(() => {
  return slotsExist.title || props.title
})
const showContent = computed(() => {
  return slotsExist.content || props.content
})
watch(
  popoverMaxWidth,
  () => {
    getPosition()
  },
  {
    flush: 'post'
  }
)
watchEffect(() => {
  visible.value = props.show
})
function getPosition() {
  const defaultWidth = defaultRef.value.offsetWidth // 展示文本宽度
  const popWidth = popoverRef.value.offsetWidth // 提示文本宽度
  const popHeight = popoverRef.value.offsetHeight // 提示文本高度
  top.value = popHeight + (props.arrow ? 4 : 6)
  left.value = (popWidth - defaultWidth) / 2
}
function onShow() {
  hideTimer.value && cancelRaf(hideTimer.value)
  if (!visible.value) {
    getPosition()
    rafTimeout(() => {
      visible.value = true
      emits('update:show', true)
      emits('openChange', true)
    }, props.showDelay)
  }
}
function onHide(): void {
  hideTimer.value = rafTimeout(() => {
    visible.value = false
    emits('update:show', false)
    emits('openChange', false)
  }, props.hideDelay)
}
function toggleVisible() {
  if (!visible.value) {
    onShow()
  } else {
    onHide()
  }
}
function onEnter() {
  activeBlur.value = false
}
function onLeave() {
  activeBlur.value = true
  popoverRef.value.focus()
}
function onBlur() {
  visible.value = false
  emits('update:show', false)
  emits('openChange', false)
}
</script>
<template>
  <div
    class="m-popover-wrap"
    @mouseenter="trigger === 'hover' ? onShow() : () => false"
    @mouseleave="trigger === 'hover' ? onHide() : () => false"
  >
    <div
      ref="popoverRef"
      tabindex="1"
      class="m-pop-content"
      :class="{ 'popover-padding': arrow, 'popover-visible': visible }"
      :style="`max-width: ${popoverMaxWidth}; --popover-background-color: ${bgColor}; transform-origin: 50% ${top}px; top: ${-top}px; left: ${-left}px;`"
      @blur="trigger === 'click' && activeBlur ? onBlur() : () => false"
      @mouseenter="trigger === 'hover' ? onShow() : () => false"
      @mouseleave="trigger === 'hover' ? onHide() : () => false"
    >
      <div class="m-popover" :style="popoverStyle">
        <div v-if="showTitle" class="popover-title" :style="titleStyle">
          <slot name="title">{
  { title }}</slot>
        </div>
        <div v-if="showContent" class="popover-content" :style="contentStyle">
          <slot name="content">{
  { content }}</slot>
        </div>
      </div>
      <div v-if="arrow" class="popover-arrow"></div>
    </div>
    <div
      ref="defaultRef"
      @click="trigger === 'click' ? toggleVisible() : () => false"
      @mouseenter="trigger === 'click' && visible ? onEnter() : () => false"
      @mouseleave="trigger === 'click' && visible ? onLeave() : () => false"
    >
      <slot></slot>
    </div>
  </div>
</template>
<style lang="less" scoped>
.m-popover-wrap {
  position: relative;
  display: inline-block;
  .m-pop-content {
    position: absolute;
    z-index: 999;
    width: max-content;
    pointer-events: none;
    transform: scale(0.8);
    opacity: 0;
    transition:
      transform 0.15s cubic-bezier(0.78, 0.14, 0.15, 0.86),
      opacity 0.15s cubic-bezier(0.78, 0.14, 0.15, 0.86);
    outline: none;
    .m-popover {
      padding: 12px;
      font-size: 14px;
      color: rgba(0, 0, 0, 0.88);
      line-height: 1.5714285714285714;
      text-align: start;
      text-decoration: none;
      word-break: break-all;
      cursor: auto;
      user-select: text;
      background-color: var(--popover-background-color);
      border-radius: 8px;
      box-shadow:
        0 6px 16px 0 rgba(0, 0, 0, 0.08),
        0 3px 6px -4px rgba(0, 0, 0, 0.12),
        0 9px 28px 8px rgba(0, 0, 0, 0.05);
      .popover-title {
        min-width: 176px;
        color: rgba(0, 0, 0, 0.88);
        font-weight: 600;
        &:not(:last-child) {
          margin-bottom: 8px;
        }
      }
      .popover-content {
        color: rgba(0, 0, 0, 0.88);
      }
    }
    .popover-arrow {
      position: absolute;
      z-index: 9;
      left: 50%;
      bottom: 12px;
      transform: translateX(-50%) translateY(100%) rotate(180deg);
      display: block;
      pointer-events: none;
      width: 16px;
      height: 16px;
      overflow: hidden;
      &::before {
        position: absolute;
        bottom: 0;
        inset-inline-start: 0;
        width: 16px;
        height: 8px;
        background-color: var(--popover-background-color);
        clip-path: path(
          'M 0 8 A 4 4 0 0 0 2.82842712474619 6.82842712474619 L 6.585786437626905 3.0710678118654755 A 2 2 0 0 1 9.414213562373096 3.0710678118654755 L 13.17157287525381 6.82842712474619 A 4 4 0 0 0 16 8 Z'
        );
        content: '';
      }
      &::after {
        position: absolute;
        width: 8.970562748477143px;
        height: 8.970562748477143px;
        bottom: 0;
        inset-inline: 0;
        margin: auto;
        border-radius: 0 0 2px 0;
        transform: translateY(50%) rotate(-135deg);
        box-shadow: 3px 3px 7px rgba(0, 0, 0, 0.1);
        z-index: 0;
        background: transparent;
        content: '';
      }
    }
  }
  .popover-padding {
    padding-bottom: 12px;
  }
  .popover-visible {
    pointer-events: auto;
    transform: scale(1);
    opacity: 1;
  }
}
</style>

在要使用的页面引入

其中引入使用了以下组件:

<script setup lang="ts">
import Popover from './Popover.vue'
function openChange(visible: boolean) {
  console.log('visible:', visible)
}
</script>
<template>
  <div class="ml60">
    <h1>{
  { $route.name }} {
  { $route.meta.title }}</h1>
    <h2 class="mt30 mb10">基本使用</h2>
    <Popover title="Title" @open-change="openChange">
      <template #content>
        <p>Content</p>
        <p>Content</p>
      </template>
      <Button type="primary">Hover me</Button>
    </Popover>
    <h2 class="mt30 mb10">自定义样式</h2>
    <Popover
      :max-width="180"
      :title-style="{ fontSize: '16px', fontWeight: 'bold', color: '#1677ff' }"
      :content-style="{ color: '#fff' }"
      bg-color="rgba(0, 0, 0.8)"
      :popover-style="{ padding: '12px 18px', borderRadius: '12px' }"
    >
      <template #title> Custom Title </template>
      <template #content>
        <p>Custom Content</p>
        <p>Custom Content</p>
      </template>
      <Button type="primary">Hover me</Button>
    </Popover>
    <h2 class="mt30 mb10">不同的触发方式</h2>
    <Space>
      <Popover title="Hover Title">
        <template #content>
          <p>Content</p>
          <p>Content</p>
        </template>
        <Button type="primary">Hover Me</Button>
      </Popover>
      <Popover title="Click Title" trigger="click">
        <template #content>
          <p>Content</p>
          <p>Content</p>
        </template>
        <Button type="primary">Click Me</Button>
      </Popover>
    </Space>
    <h2 class="mt30 mb10">延迟显示隐藏</h2>
    <Space>
      <Popover :show-delay="300" :hide-delay="300" title="delay 300ms" content="Vue Amazing UI">
        <Button type="primary">Delay 300ms Popover</Button>
      </Popover>
      <Popover :show-delay="500" :hide-delay="500" title="delay 500ms" content="Vue Amazing UI">
        <Button type="primary">Delay 500ms Popover</Button>
      </Popover>
    </Space>
    <h2 class="mt30 mb10">隐藏箭头</h2>
    <Popover :arrow="false" content="Vue Amazing UI">
      <Button type="primary">Hide Arrow</Button>
    </Popover>
  </div>
</template>
相关文章
|
10天前
|
JavaScript 前端开发 安全
Vue 3
Vue 3以组合式API、Proxy响应式系统和全面TypeScript支持,重构前端开发范式。性能优化与生态协同并进,兼顾易用性与工程化,引领Web开发迈向高效、可维护的新纪元。(238字)
373 139
|
4天前
|
缓存 JavaScript 算法
Vue 3性能优化
Vue 3 通过 Proxy 和编译优化提升性能,但仍需遵循最佳实践。合理使用 v-if、key、computed,避免深度监听,利用懒加载与虚拟列表,结合打包优化,方可充分发挥其性能优势。(239字)
61 1
|
1月前
|
开发工具 iOS开发 MacOS
基于Vite7.1+Vue3+Pinia3+ArcoDesign网页版webos后台模板
最新版研发vite7+vue3.5+pinia3+arco-design仿macos/windows风格网页版OS系统Vite-Vue3-WebOS。
245 11
|
19天前
|
JavaScript 安全
vue3使用ts传参教程
Vue 3结合TypeScript实现组件传参,提升类型安全与开发效率。涵盖Props、Emits、v-model双向绑定及useAttrs透传属性,建议明确声明类型,保障代码质量。
146 0
|
2月前
|
缓存 前端开发 大数据
虚拟列表在Vue3中的具体应用场景有哪些?
虚拟列表在 Vue3 中通过仅渲染可视区域内容,显著提升大数据列表性能,适用于 ERP 表格、聊天界面、社交媒体、阅读器、日历及树形结构等场景,结合 `vue-virtual-scroller` 等工具可实现高效滚动与交互体验。
346 1
|
2月前
|
缓存 JavaScript UED
除了循环引用,Vue3还有哪些常见的性能优化技巧?
除了循环引用,Vue3还有哪些常见的性能优化技巧?
186 0
|
3月前
|
JavaScript
vue3循环引用自已实现
当渲染大量数据列表时,使用虚拟列表只渲染可视区域的内容,显著减少 DOM 节点数量。
117 0
|
1月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
223 2
|
6天前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
211 137
|
4月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
653 0

热门文章

最新文章