npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modalcc

v1.0.0

Published

Vue3模态框最佳实践

Downloads

70

Readme

Vue3模态框最佳实践

NPM    Github

🌈 Modalcc 的重大突破

  • 无需关心模态框的状态绑定
  • 无需要再把模态框书写到 template 中
  • 以命令式思维去调用模态框
  • 一个组件,多种行为

✨ 特性

  • 🌍 适用于任何版本的 Vue3
  • 🌈 极低的学习成本,仅仅只有4个 API
  • 📦 提供完整的 TypeScript 类型定义文件

⚙️ 如何使用

  • 安装依赖
npm install modalcc
// or
yarn add modalcc
  • 创建模态框模板modal.vue,后续的模态框默认都以这个模态框为基础,此处以antd的模态框为例
<template>
  <a-modal
    v-model:open="open"
    cancel-text="取消"
    ok-text="确定"
    v-bind="config"
  >
    <!-- 定义模态框内容插入的位置 -->
    <slot />
  </a-modal>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { onModalInit } from 'modalcc'

const open = ref(false)
// 动态绑定模态框属性
const config = ref({})

// onModalInit方法会在每个模态框初始化时执行,可能是组件渲染时
// 也可能是模态框展开时,取决于你何时渲染模态框内容
onModalInit(props => {
  config.value = props
})

// 模态框对外暴露的事件
defineExpose({
  show() {
    open.value = true
  },
})
</script>
  • 入口文件中配置 Modalcc
import Modal from './modal.vue'
import { createApp } from 'vue'
import { init } from 'modalcc'

const app = createApp(App)
// 配置modal.vue为模态框的模板
init(app, Modal)
app.mount('#app')
  • 需要拉起模态框的组件
<template>
  <button @click="onShow">打开弹窗</button>
</template>

<script lang="ts" setup>
import { useModal } from 'modalcc'
import Test from './Test.vue'

/**
 * modal可以拿到模态框模板中defineExpose暴露的属性
 * 而useModal的接收一个Vue组件为参数
 * 这个组件将会被渲染在模态框的default插槽中
 */
const modal = useModal(Test)
function onShow() {
  modal.show()
}
</script>
  • 需要以模态框形式弹出的组件
<template>
  <div>
    这是一个表格
  </div>
  <button @click="onClose">关闭弹窗</button>
  <button @click="setTitle">改变标题</button>
</template>

<script setup lang="ts">
import { withModal } from 'modalcc'
import { reactive } from 'vue'

const props = reactive({
  title: 'hello'
})

/**
 * modal拿到模态框模板中defineExpose暴露的属性
 * props为模态框模板绑定的props,这边使用reactive是为了可以出发响应式更新
 * 使用普通对象也行,但是不具备响应式
 */
const modal = await withModal(props)

function onClose() {
  modal.close()
}

// 设置模态框的title属性
function setTitle() {
  props.title = 'world'
}
</script>
  • 效果展示

20240831-173051

✨ 扩展能力

自定义模态框模板,useModel接受两个参数,第一个是要嵌入模态框的组件(必填),第二个是自定义模态框模板(选填,会覆盖init配置的默认模态框)