본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Dialog — 대화상자

원문 보기 위키 갱신

플로팅 레이어 위에 띄워 사용자에게 안내 메시지를 보여 주거나 동작을 확인받는 모달 대화상자예요. 함수 호출 방식과 컴포넌트(템플릿) 호출 방식 두 가지로 쓸 수 있어요.

출처: Dialog — doc.en-US.md

소개

모달 대화상자는 플로팅 레이어에 표시되어 사용자에게 관련 동작을 안내해요. 메시지 안내, 메시지 확인, 또는 현재 페이지에서 특정 인터랙션을 마치는 용도로 자주 쓰여요.

팝업 컴포넌트는 함수 호출과 컴포넌트 호출을 모두 지원해요.

설치

import { createApp, createVNode } from 'vue'
import { Dialog } from '@nutui/nutui'

const app = createApp()
app.use(Dialog)

함수로 사용하기

:::demo

<template>
  <nut-cell-group title="Function Use">
    <nut-cell title="Basic Usage" @click="baseClick"></nut-cell>
    <nut-cell title="Transparent Dialog" @click="transparentClick"></nut-cell>
    <nut-cell title="Use html" @click="htmlClick"></nut-cell>
    <nut-cell title="Before Close" @click="beforeCloseClick"></nut-cell>
    <nut-cell title="No Title" @click="noTitleClick"></nut-cell>
    <nut-cell title="Tips Dialog" @click="tipsClick"></nut-cell>
    <nut-cell title="Bottom button vertical use" @click="verticalClick"></nut-cell>
  </nut-cell-group>
</template>
<script setup>
import { ref, createVNode } from 'vue'
import { showDialog } from '@nutui/nutui'
import '@nutui/nutui/dist/packages/dialog/style'
const onCancel = () => {
  console.log('event cancel')
}
const onOk = () => {
  console.log('event ok')
}
const baseClick = () => {
  showDialog({
    title: 'Basic spring frame',
    content: createVNode('span', { style: { color: 'red' } }, 'I can be a custom component'),
    onCancel,
    onOk
  })
}
const transparentClick = () => {
  showDialog({
    overlayStyle: { background: 'rgba(0,0,0,0)' },
    title: 'Transparent Dialog',
    content: 'Content',
    onCancel,
    onOk
  })
}
const htmlClick = () => {
  showDialog({
    title: 'Use html',
    content:
      "<p style='color:red'>html</p><img src='https://m.360buyimg.com/babel/jfs/t1/164410/22/25162/93384/616eac6cE6c711350/0cac53c1b82e1b05.gif' />",
    onCancel,
    onOk
  })
}
const beforeCloseClick = () => {
  showDialog({
    title: 'Before Close',
    content: 'Click confirm to close it in 1 second',
    onCancel,
    onOk,
    beforeClose: (action) => {
      return new Promise((r) => {
        setTimeout(() => {
          r(action == 'ok')
        }, 1000)
      })
    }
  })
}
const noTitleClick = () => {
  showDialog({
    content: 'Content',
    onCancel,
    onOk
  })
}
const tipsClick = () => {
  showDialog({
    title: 'Title',
    content: 'Function call and component call are supported.',
    noCancelBtn: true,
    onCancel,
    onOk
  })
}
const verticalClick = () => {
  showDialog({
    title: 'Title',
    content: 'Support vertical arrangement of bottom buttons.',
    footerDirection: 'vertical',
    onCancel,
    onOk
  })
}
</script>

:::

템플릿으로 사용하기

:::demo

<template>
  <nut-cell-group title="Template use">
    <nut-cell title="Template use" @click="componentClick"></nut-cell>
    <nut-dialog
      teleport="#app"
      title="Template use"
      content="Function call and template call are supported."
      v-model:visible="visible"
    >
    </nut-dialog>
    <nut-cell title="Bottom button vertical use" @click="componentvVrticalClick"></nut-cell>
    <nut-dialog
      footer-direction="vertical"
      teleport="#app"
      title="Template use"
      content="Function call and template call are supported."
      v-model:visible="visible1"
    >
    </nut-dialog>
  </nut-cell-group>
</template>
<script setup>
import { ref } from 'vue'
const visible = ref(false)
const visible1 = ref(false)
const componentClick = () => {
  visible.value = true
}
const componentvVrticalClick = () => {
  visible1.value = true
}
</script>

:::

Teleport 사용 — 지정한 요소 노드에 마운트

:::demo

<template>
  <nut-cell-group title="Teleport use, mount to the specified element node">
    <nut-cell title="body element node" @click="teleportClick('body')"></nut-cell>
    <nut-cell title="#app element node" @click="teleportClick('#app')"></nut-cell>
    <nut-cell title="demo class element node" @click="teleportClick('.demo')"></nut-cell>
  </nut-cell-group>
</template>
<script setup>
import { showDialog } from '@nutui/nutui'
import '@nutui/nutui/dist/packages/dialog/style'
const onCancel = () => {
  console.log('event cancel')
}
const teleportClick = (teleport) => {
  showDialog({
    teleport,
    title: 'teleport to ' + teleport,
    content: 'Open the developer tool and take a look at the Elements tab',
    noCancelBtn: true,
    onCancel
  })
}
</script>

:::

API

DialogOptions

Attribute Description Type Default
title Title string -
id Identifier, share one instance at the same time, default to multiple instances string | object new Date().getTime()
content Content, support HTML string -
teleport Specifies a target element where Dialog will be mounted string "body"
closeOnClickOverlay Whether to close when overlay is clicked boolean false
noFooter Hide bottom button bar boolean false
noOkBtn Hide OK button boolean false
noCancelBtn Hide cancel button boolean false
cancelText Cancel button text string "Cancel"
okText OK button text string "Confirm"
cancelAutoClose Click Cancel to close the popup boolean true
okAutoClose4.2.1 Check whether the button closes the popup by default boolean true
textAlign Text alignment direction, the optional value is the same as css text-align string "center"
closeOnPopstate Whether to close when popstate boolean false
customClass Custom dialog class string
overlayClass Custom mask classname string -
overlayStyle Custom mask styles CSSProperties -
popClass Custom popup classname string -
popStyle Custom popup styles CSSProperties -
onUpdate Update boolean false
onOk Emitted when the confirm button is clicked Function -
onCancel Emitted when the cancel button is clicked Function -
onOpened Emitted when Dialog is opened Function -
onClosed Emitted when Dialog is closed Function -
beforeClose Callback function before close support return promise Function(action: string) -

Props

Attribute Description Type Default
title Title string -
content Content, support HTML string -
teleport Specifies a target element where Dialog will be mounted string "body"
close-on-click-overlay Whether to close when overlay is clicked boolean false
no-footer Hide bottom button bar boolean false
no-ok-btn Hide OK button boolean false
no-cancel-btn Hide cancel button boolean false
cancel-text Cancel button text string "Cancel"
ok-text OK button text string "Confirm"
cancel-auto-close Click Cancel to close the popup boolean true
ok-auto-close4.2.1 Check whether the button closes the popup by default boolean true
text-align Text alignment direction, the optional value is the same as css text-align string "center"
close-on-popstate Whether to close when popstate boolean false
lock-scroll Whether to lock background scroll boolean false
footer-direction Use horizontal and vertical optional values string horizontal
overlay-class Custom mask classname string -
overlay-style Custom mask styles CSSProperties -
pop-class Custom popup classname string -
pop-style Custom popup styles CSSProperties -
custom-class Custom dialog class string -
before-close Callback function before close support return promise Function(action: string) -

Events

Event Description Type Default
ok Emitted when the confirm button is clicked Function -
cancel Emitted when the cancel button is clicked Function -
closed Emitted when Dialog is closed Function -
opened Emitted when Dialog is Opened Function -

Slots

Name Description
header Custom title
default Custom default
footer Custom footer

테마 (Theming)

CSS 변수

이 컴포넌트는 스타일을 커스터마이징할 때 쓸 수 있는 CSS 변수를 제공해요. 자세한 내용은 ConfigProvider 컴포넌트 문서를 참고하세요.

Name Default Value
--nut-dialog-width 296px
--nut-dialog-header-font-weight normal
--nut-dialog-header-color rgba(38, 38, 38, 1)
--nut-dialog-footer-justify-content space-around

더 알아보기 (Learn more)

이 문서는 NutUI v4 공식 영문 문서를 한국어로 옮긴 글이에요. 최신 API와 변경 사항은 원문에서 확인하는 게 가장 정확해요.