Vue3弹出确认(Popconfirm)

news2024/9/22 13:35:52

效果如下图:在线预览

在这里插入图片描述

APIs

参数说明类型默认值必传
title确认框的标题string | slot‘’false
description确认框的内容描述string | slot‘’false
content展示的文本string | slot‘’false
icon自定义弹出确认框 Icon 图标string | slot‘’false
maxWidth弹出确认框内容最大宽度string | number‘auto’false
cancelText取消按钮文字string | slot‘取消’false
cancelType取消按钮类型string‘default’false
okText确认按钮文字string | slot‘确定’false
okType确认按钮类型string‘primary’false
showCancel是否显示取消按钮booleantruefalse

Events

事件名称说明参数
cancel点击取消的回调(e: Event) => void
ok点击确认的回调(e: Event) => void
openChange显示隐藏的回调(visible: boolean) => void

创建弹出确认组件Popconfirm.vue

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import type { Slot } from 'vue'
interface Props {
  title?: string|Slot // 确认框的标题
  description?: string|Slot // 确认框的内容描述
  content?: string|Slot // 展示的文本
  icon?: string|Slot // 自定义弹出确认框 Icon 图标
  maxWidth?: string|number // 弹出确认框内容最大宽度
  cancelText?: string|Slot // 取消按钮文字
  cancelType?: string // 取消按钮类型
  okText?: string|Slot // 确认按钮文字
  okType?: string // 确认按钮类型
  showCancel?: boolean // 是否显示取消按钮
}
const props = withDefaults(defineProps<Props>(), {
  title: '',
  description: '',
  content: '',
  icon: '',
  maxWidth: 'auto',
  cancelText: '取消',
  cancelType: 'default',
  okText: '确定',
  okType: 'primary',
  showCancel: true
})
const popMaxWidth = computed(() => {
  if (typeof props.maxWidth === 'number') {
    return props.maxWidth + 'px'
  }
  return props.maxWidth
})
const visible = ref(false)
const desc = ref()
const showDesc = ref(1)
onMounted(() => {
  showDesc.value = desc.value.offsetHeight
})
const top = ref(0) // 提示框top定位
const left = ref(0) // 提示框left定位
const contentRef = ref() // 声明一个同名的模板引用
const popRef = ref() // 声明一个同名的模板引用
function getPosition () {
  const contentWidth = contentRef.value.offsetWidth // 展示文本宽度
  const popWidth = popRef.value.offsetWidth // 提示文本宽度
  const popHeight = popRef.value.offsetHeight // 提示文本高度
  top.value = popHeight + 4
  left.value = (popWidth - contentWidth) / 2
}
const activeBlur = ref(false) // 是否激活 blur 事件
function onEnter () {
  activeBlur.value = false
}
function onLeave () {
  activeBlur.value = true
  popRef.value.focus()
}
function onBlur () {
  visible.value = false
  emits('openChange', false)
}
const emits = defineEmits(['cancel', 'ok', 'openChange'])
function onOpen () {
  visible.value = !visible.value
  if (visible.value) {
    getPosition()
  }
  emits('openChange', visible.value)
}
function onCancel (e: Event) {
  visible.value = false
  emits('openChange', false)
  emits('cancel', e)
}
function onOk (e: Event) {
  visible.value = false
  emits('openChange', false)
  emits('ok', e)
}
</script>
<template>
  <div class="m-popconfirm">
    <div
      ref="popRef"
      tabindex="1"
      class="m-pop-content"
      :class="{'show-pop': visible}"
      :style="`max-width: ${popMaxWidth}; top: ${-top}px; left: ${-left}px;`"
      @blur="activeBlur ? onBlur() : () => false">
      <div class="m-pop">
        <div class="m-pop-message">
          <span class="m-icon">
            <slot name="icon">
              <svg focusable="false" class="u-icon" data-icon="exclamation-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm-32 232c0-4.4 3.6-8 8-8h48c4.4 0 8 3.6 8 8v272c0 4.4-3.6 8-8 8h-48c-4.4 0-8-3.6-8-8V296zm32 440a48.01 48.01 0 010-96 48.01 48.01 0 010 96z"></path></svg>
            </slot>
          </span>
          <div class="m-title" :class="{'font-weight': showDesc}">
            <slot name="title">{{ title }}</slot>
          </div>
        </div>
        <div class="m-pop-description" ref="desc" v-if="showDesc">
          <slot name="description">{{ description }}</slot>
        </div>
        <div class="m-pop-buttons">
          <Button v-if="showCancel" @click="onCancel" size="small" :type=cancelType>{{ cancelText }}</Button>
          <Button @click="onOk" size="small" :type=okType>{{ okText }}</Button>
        </div>
      </div>
      <div class="m-pop-arrow">
        <span class="u-pop-arrow"></span>
      </div>
    </div>
    <div
      ref="contentRef"
      @click="onOpen"
      @mouseenter="onEnter"
      @mouseleave="onLeave">
      <slot>{{ content }}</slot>
    </div>
  </div>
</template>
<style lang="less" scoped>
.m-popconfirm {
  position: relative;
  display: inline-block;
  .m-pop-content {
    position: absolute;
    z-index: 999;
    width: max-content;
    padding-bottom: 12px;
    outline: none;
    pointer-events: none;
    opacity: 0;
    transform-origin: 50% 75%;
    transform: scale(.8); // 缩放变换
    -ms-transform: scale(.8); /* IE 9 */
    -webkit-transform: scale(.8); /* Safari and Chrome */
    transition: transform .25s, opacity .25s;
    .m-pop {
      min-width: 32px;
      min-height: 32px;
      padding: 12px;
      font-size: 14px;
      color: rgba(0, 0, 0, .88);
      line-height: 1.5714285714285714;
      text-align: start;
      text-decoration: none;
      word-wrap: break-word;
      cursor: auto;
      user-select: text;
      background-color: #FFF;
      border-radius: 8px;
      box-shadow: 0 6px 16px 0 rgba(0, 0, 0, .08), 0 3px 6px -4px rgba(0, 0, 0, .12), 0 9px 28px 8px rgba(0, 0, 0, .05);
      .m-pop-message {
        position: relative;
        margin-bottom: 8px;
        font-size: 14px;
        display: flex;
        flex-wrap: nowrap;
        align-items: start;
        .m-icon {
          content: '\f8f5';
          flex: none;
          line-height: 1;
          padding-top: 4px;
          display: inline-block;
          text-align: center;
          .u-icon {
            display: inline-block;
            line-height: 1;
            fill: #faad14;
          }
        }
        .m-title {
          flex: auto;
          margin-inline-start: 8px;
        }
        .font-weight {
          font-weight: 600;
        }
      }
      .m-pop-description {
        position: relative;
        margin-inline-start: 22px;
        margin-bottom: 8px;
        font-size: 14px;
      }
      .m-pop-buttons {
        text-align: end;
        & > .m-btn-wrap {
          margin-inline-start: 8px;
        }
      }
    }
    .m-pop-arrow {
      position: absolute;
      z-index: 9;
      left: 50%;
      bottom: 12px;
      transform: translateX(-50%) translateY(100%) rotate(180deg);
      display: block;
      border-radius: 0 0 2px;
      pointer-events: none;
      width: 32px;
      height: 32px;
      overflow: hidden;
      &::before {
        position: absolute;
        bottom: 0;
        inset-inline-start: 0;
        width: 32px;
        height: 8px;
        background: #FFF;
        clip-path: path('M 6.343145750507619 8 A 4 4 0 0 0 9.17157287525381 6.82842712474619 L 14.585786437626904 1.414213562373095 A 2 2 0 0 1 17.414213562373096 1.414213562373095 L 22.82842712474619 6.82842712474619 A 4 4 0 0 0 25.65685424949238 8 Z');
        content: "";
      }
      &::after {
        position: absolute;
        width: 11.31370849898476px;
        height: 11.31370849898476px;
        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, .1);
        z-index: 0;
        background: transparent;
        content: "";
      }
    }
  }
  .show-pop {
    pointer-events: auto;
    opacity: 1;
    transform: scale(1); // 缩放变换
    -ms-transform: scale(1); /* IE 9 */
    -webkit-transform: scale(1); /* Safari and Chrome */
  }
}
</style>

在要使用的页面引入

其中引入使用了 Vue3按钮(Button)、Vue3全局提示(Message)

<script setup lang="ts">
import Popconfirm from './Popconfirm.vue'
import Button from './Button.vue'
import Message from './Message.vue'
import { ref } from 'vue'
const message = ref()
const confirm = (e: MouseEvent) => {
  console.log(e)
  message.value.success('Click on Yes')
}
const cancel = (e: MouseEvent) => {
  console.log(e)
  message.value.error('Click on No')
}
</script>
<template>
  <div class="ml120">
    <h1>Popconfirm 弹出确认</h1>
    <h2 class="mt30 mb10">基本使用</h2>
    <Popconfirm
      title="Are you sure delete this task?"
      description="This will have other effects..."
      @ok="confirm"
      @cancel="cancel">
      <Button type="danger">Delete</Button>
    </Popconfirm>
    <h2 class="mt30 mb10">自定义按钮文字</h2>
    <Popconfirm title="Are you sure?" ok-text="Yes" cancel-text="No">
      <Button type="danger">Delete</Button>
    </Popconfirm>
    <h2 class="mt30 mb10">自定义 Icon 图标</h2>
    <Popconfirm title="Are you sure?">
      <template #icon>
        <svg focusable="false" class="u-svg" data-icon="question-circle" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M512 64C264.6 64 64 264.6 64 512s200.6 448 448 448 448-200.6 448-448S759.4 64 512 64zm0 820c-205.4 0-372-166.6-372-372s166.6-372 372-372 372 166.6 372 372-166.6 372-372 372z"></path><path d="M623.6 316.7C593.6 290.4 554 276 512 276s-81.6 14.5-111.6 40.7C369.2 344 352 380.7 352 420v7.6c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8V420c0-44.1 43.1-80 96-80s96 35.9 96 80c0 31.1-22 59.6-56.1 72.7-21.2 8.1-39.2 22.3-52.1 40.9-13.1 19-19.9 41.8-19.9 64.9V620c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8v-22.7a48.3 48.3 0 0130.9-44.8c59-22.7 97.1-74.7 97.1-132.5.1-39.3-17.1-76-48.3-103.3zM472 732a40 40 0 1080 0 40 40 0 10-80 0z"></path></svg>
      </template>
      <Button type="danger">Delete</Button>
    </Popconfirm>
    <h2 class="mt30 mb10">隐藏取消按钮</h2>
    <Popconfirm
      title="friendly reminder ..."
      :show-cancel="false">
      <Button type="primary">Hidden Cancel Btn</Button>
    </Popconfirm>
    <Message ref="message" />
  </div>
</template>
<style lang="less" scoped>
.u-svg {
  fill: #ff4d4f;
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/855528.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

《合成孔径雷达成像算法与实现》Figure3.8

与图3.7的代码区别只在于原始信号的表达式对了一个时间偏移 代码复现如下&#xff1a; clc clear all close all%参数设置 TBP 100; %时间带宽积 T 10e-6; %脉冲持续时间 tc …

SEED-Bench: Benchmarking Multimodal LLMs with Generative Comprehension

本文是LLM相关的文章&#xff0c;针对《SEED-Bench: Benchmarking Multimodal LLMs with Generative Comprehension》的翻译。 SEED-基准&#xff1a;用生成理解对多模式LLM进行基准测试 摘要1 引言2 相关工作3 SEED-Bench 摘要 基于强大的大型语言模型&#xff08;LLM&#…

【SpringBoot框架篇】33.优雅集成i18n实现国际化信息返回

文章目录 1.简介2.MessageSource配置和工具类封装2.1.配置MessageSource相关配置2.2.配置工具类2.3.测试返回国际级文本信息 3.不优雅的web调用示例(看看就行&#xff0c;别用)4.优雅使用示例4.1.错误响应消息枚举类4.2.ThreadLocal工具类配置4.2.1.ThreadLocal工具类数据封装4…

不再限制CPU,这才是Win11完全体的样子嘛

从 Win11 刚问世时&#xff0c;微软就宣称将给其许多 Win10没有也不会支持的新功能。 其中 Android 子系统的支持算得上是最期待&#xff0c;但结果难免有些失望的部分。 先不说使用怎么样&#xff0c;光是安装就困难重重。 绕过限制升级了 Win11 &#xff0c;但没想到安装 W…

idea双击启动无效,idea卡顿问题

idea双击启动无效&#xff1a;大概率是关机时没有正确关闭idea&#xff0c;再次开机导致无法正常启动idea 1.通过任务管理器杀死idea进程后重启idea 2.需要修改配置 打开 &#xff08;以各自电脑实际为准&#xff09;C:\Program Files\JetBrains\IntelliJ IDEA 2020.3.1\bin&am…

LabVIEW使用图像处理检测显微图像中的白血病

LabVIEW使用图像处理检测显微图像中的白血病 人体最重要的部分是血液&#xff0c;因为它使人活着。它执行许多重要功能&#xff0c;例如转移氧气&#xff0c;二氧化碳&#xff0c;矿物质等。血液量不足会极大地影响新陈代谢&#xff0c;如果不及早治疗&#xff0c;这可能是非常…

接口自动化测试框架及接口测试自动化主要知识点

接口自动化测试框架&#xff1a; 接口测试框架&#xff1a;使用最流行的Requests进行接口测试接口请求构造&#xff1a;常见的GET/POST/PUT/HEAD等HTTP请求构造 接口测试断言&#xff1a;状态码、返回内容等断言JSON/XML请求&#xff1a;发送json\xml请求JSON/XML响应断言&…

c语言经典例题讲解(输出菱形,喝汽水问题)

目录 一、输出菱形 二、喝汽水问题 方法1&#xff1a;一步一步来 方法二&#xff1a;直接套公式 一、输出菱形 输出类似于下图的菱形&#xff1a; 通过分析&#xff1a;1、先分为上下两部分输出 2.在输出前先输出空格 3.找规律进行输出 可知&#xff0c;可令上半部分lin…

Python Opencv实践 - 图像属性相关

import numpy as np import cv2 as cv import matplotlib.pyplot as pltimg cv.imread("../SampleImages/pomeranian.png", cv.IMREAD_COLOR) plt.imshow(img[:,:,::-1])#像素操作 pixel img[320,370] print(pixel)#只获取蓝色通道的值 pixel_blue img[320,370,0]…

openwrt dns ssh相关问题

DHCP/DNS中 的技术叫dnsmasq 可配置hosts和 自定义挟持域名配置 image.png image.png 拦截优先级为挟持域名最高&#xff0c;另外需要重启服务方可生效&#xff0c;在系统&#xff0c;启动项中重启dnsmasq ssh 使用root用户SSH登录服务器出现Access Denied错误 只输入root就出现…

Python-OpenCV中的图像处理-图像金字塔

Python-OpenCV中的图像处理-图像金字塔 图像金字塔高斯金字塔拉普拉斯金字塔 金字塔图像融合 图像金字塔 同一图像的不同分辨率的子图集合&#xff0c;如果把最大的图像放在底部&#xff0c;最小的放在顶部&#xff0c;看起来像一座金字塔&#xff0c;故而得名图像金字塔。cv2…

C语言的动态分配空间C++的动态分配空间问题

动态分配空间 C&#xff1a;1、malloc 2、calloc C&#xff1a;new运算符 一 malloc malloc()&#xff1a; 这个函数用于分配一块指定大小的内存块&#xff0c;并返回一个指向该内存块的指针。语法如下&#xff1a; void* malloc(size_t size); 示例&#xff1a; int* ptr …

欧拉操作系统添加磁盘

1、查看磁盘空间 fdisk -l 2、创建新磁盘分区 fdisk /dev/vda 欢迎使用 fdisk (util-linux 2.37.2)。 更改将停留在内存中&#xff0c;直到您决定将更改写入磁盘。 使用写入命令前请三思。 This disk is currently in use - repartitioning is probably a bad idea. Its r…

(力扣)用两个栈实现队列

这里是栈的源代码&#xff1a;栈和队列的实现 当然&#xff0c;自己也可以写一个栈来用&#xff0c;对题目来说不影响&#xff0c;只要符合栈的特点就行。 题目&#xff1a; 请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作&#xff08;push、pop、pe…

c++(空间配置器)[32]

空间配置器 一级空间配置器 || 二级空间配置器 默认先走二级然后判断 二级空间配置器 一个指针指向start_free然后start_free向后移动&#xff0c;相当于哈希桶的头删和头插 8byte&#xff1a;切大补小 C的二级空间配置器按照8字节&#xff08;或者更大的倍数&#xff09;切分…

《流浪地球3》预告片流出?!网友整活竟被郭导翻牌、央视点赞!

年初《流浪地球2》掀起了一股“科幻热”&#xff0c;而这股热潮直至今日还只增不减。这不&#xff0c;一位名叫“数字生命卡兹克”的博主已经开始“整活”了&#xff01;他利用AI技术&#xff0c;自制了《流浪地球3》的预告片&#xff0c;并迅速火遍全网。 更牛的是&#xff0c…

apple pencil二代值不值得买?好用的苹果平替笔推荐

自从苹果的Pencil系列问世以来&#xff0c;在国内电容笔市场的销量大增&#xff0c;而苹果的Pencil系列&#xff0c;其的售价更是贵的让人望而却步。现在市面上有很多平替的电容笔&#xff0c;都能取代苹果的Pencil&#xff0c;用来做笔记、做批注、写写字都绰绰有余了。在这里…

【Vue+Element-plus】记录后台首页多echart图静态页面

一、页面效果 二、完整代码 Index.vue <template><div><div><DateTime /><!-- {{username}} --></div><el-row :gutter"20"><el-col :span"8"><div class"grid-content bg-purple"><P…

Python-OpenCV中的图像处理-图像轮廓

Python-OpenCV中的图像处理-图像轮廓 轮廓什么是轮廓查找轮廓绘制轮廓 轮廓特征图像的矩轮廓面积轮廓周长&#xff08;弧长&#xff09;轮廓近似凸包轮廓边界矩形 轮廓 什么是轮廓 轮廓可以简单认为成将连续的点&#xff08;连着边界&#xff09;连在一起的曲线&#xff0c;具…

分布式 - 服务器Nginx:一小时入门系列之动静分离

文章目录 1. 动静分离的好处2. 分离静态文件3. 修改 Nginx 配置文件 1. 动静分离的好处 Apache Tocmat 严格来说是一款java EE服务器&#xff0c;主要是用来处理 servlet请求。处理css、js、图片这些静态文件的IO性能不够好&#xff0c;因此&#xff0c;将静态文件交给nginx处…