跳转到正文

RModal 与弹窗 ​

RModal 继承 Naive UI Modal 的 show、preset、关闭事件和插槽,增加预设宽度和全屏卡片。声明式弹窗通过 show / onUpdateShow 或 v-model:show 控制。

tsx
import { RModal } from '@/components'
import { NButton } from 'naive-ui'
import { defineComponent, ref } from 'vue'

export default defineComponent({
  setup() {
    const show = ref(false)
    return () => (
      <>
        <NButton
          onClick={() => {
            show.value = true
          }}
        >
          查看报表
        </NButton>
        <RModal
          show={show.value}
          onUpdateShow={(value) => {
            show.value = value
          }}
          preset="card"
          title="库存报表"
          cardWidth={960}
        >
          报表内容
        </RModal>
      </>
    )
  },
})
属性默认作用范围
width600未指定预设时的宽度。
cardWidth600preset="card" 的宽度。
dialogWidth446preset="dialog" 的宽度。
fullscreenfalse仅 card 预设生效。

宽度接受数字或 CSS 尺寸。fullscreen 让卡片占满窗口,正文在溢出时滚动;它不调用浏览器全屏。default、header、footer 等插槽依 Naive UI 预设处理,组件原样转发。

交互示例 ​

查看示例源码
tsx
import { RModal, useModal } from '@/components'
import { useI18n } from '@/hooks'
import { NButton, NCard, NFlex, NInputNumber, NTag } from 'naive-ui'
import DemoPage from '../components/DemoPage'
import DemoResult from '../components/DemoResult'

const variants = ['card', 'dialog', 'fullscreen'] as const

export default defineComponent({
  name: 'ModalDemo',
  setup() {
    const { t } = useI18n('demoDetails')
    const variant = ref<(typeof variants)[number]>('card')
    const visible = ref(false)
    const width = ref<number | null>(480)
    const last = ref<string>()
    const { create } = useModal()

    const open = (mode: (typeof variants)[number]) => {
      variant.value = mode
      visible.value = true
    }
    const content = () => (
      <DemoResult label={t('modal.record')}>
        <strong>RAY-024</strong>
      </DemoResult>
    )

    return () => (
      <DemoPage title={t('modal.title')} description={t('modal.intro')}>
        <div class="r-demo-grid">
          {variants.map((mode) => (
            <NCard key={mode} title={t(`modal.${mode}`)}>
              <div class="r-demo-card-stack">
                <div
                  class={[
                    'r-demo-modal-sketch',
                    `r-demo-modal-sketch--${mode}`,
                  ]}
                  aria-hidden="true"
                >
                  <span />
                </div>
                <NButton type="primary" secondary onClick={() => open(mode)}>
                  {t(`modal.open.${mode}`)}
                </NButton>
              </div>
            </NCard>
          ))}
          <NCard title={t('modal.width')}>
            <div class="r-demo-card-stack">
              <NFlex align="center">
                <NInputNumber
                  v-model:value={width.value}
                  min={280}
                  max={960}
                  step={40}
                  aria-label={t('modal.width')}
                />
                <NTag bordered={false}>px</NTag>
              </NFlex>
              <NButton onClick={() => open('card')}>
                {t('modal.applyWidth')}
              </NButton>
            </div>
          </NCard>
          <NCard class="r-demo-grid__wide" title="useModal">
            <NFlex align="center">
              <NButton
                onClick={() =>
                  create({ preset: 'card', title: t('modal.card'), content })
                }
              >
                {t('modal.createCard')}
              </NButton>
              <NButton
                onClick={() =>
                  create({
                    preset: 'dialog',
                    title: t('modal.dialog'),
                    content,
                  })
                }
              >
                {t('modal.createDialog')}
              </NButton>
            </NFlex>
          </NCard>
        </div>

        <DemoResult label={t('modal.last')}>
          {last.value ? t(`modal.${last.value}`) : t('empty')}
        </DemoResult>

        <RModal
          v-model:show={visible.value}
          preset={variant.value === 'dialog' ? 'dialog' : 'card'}
          title={t(`modal.${variant.value}`)}
          fullscreen={variant.value === 'fullscreen'}
          draggable={variant.value !== 'fullscreen'}
          cardWidth={`min(${width.value ?? 480}px, calc(100vw - 32px))`}
          dialogWidth="min(380px, calc(100vw - 32px))"
          closable
          onAfterLeave={() => {
            last.value = variant.value
          }}
        >
          {content()}
        </RModal>
      </DemoPage>
    )
  },
})

命令式弹窗 ​

@/components 的 useModal() 已弃用,兼容代码仍可通过 create(options) 和 destroyAll() 调用。create 返回 Naive UI 弹窗句柄,支持 fullscreen、width、cardWidth、dialogWidth 等兼容配置。新代码从 naive-ui 导入 useModal,在 NModalProvider 的后代调用;需要模板专用全屏样式时用声明式 RModal。提示用户确认危险操作时也可以用全局 Dialog Provider。

组件源码 · 兼容 Hook · 回归用例