Modal
Modal (모달)
팝업으로 떠서 뭔가를 보여주는 컴포넌트예요.
For example:
<!-- App.vue -->
<n-modal-provider>
<content />
</n-modal-provider>
import { useModal } from 'naive-ui'
import { defineComponent } from 'vue'
// content
export default defineComponent({
setup() {
const modal = useModal()
return {
showModal() {
modal.create({
title: 'Title',
content: 'Content'
})
}
}
}
})
출처: 문서
본문
Demos
basic.vue
content-scrollable.vue
reactive.vue
controlled.vue
mask-closable.vue
custom-position.vue
preset-card.vue
preset-confirm.vue
preset-confirm-slot.vue
transform-origin.vue
draggable.vue
mask-visible.vue
API
ModalProvider Props
2.38.0부터 제공돼요.
| 이름 | 타입 | 기본값 | 설명 | 버전 |
|---|---|---|---|---|
| to | string | HTMLElement |
body |
모달 콘텐츠의 컨테이너 노드예요. | 2.38.0 |
useModal API
2.38.0부터 제공돼요.
| 이름 | 타입 | 설명 | 버전 |
|---|---|---|---|
| create | (options: ModalOptions) => ModalReactive |
모달을 만들어요. | 2.38.0 |
| destroyAll | () => void |
모든 모달을 제거해요. | 2.38.0 |
ModalOptions와 ModalReactive의 속성은 ModalProps와 같아요(속성은 camelCase를 사용해요. 예를 들어 auto-focus 속성은 옵션 속성으로 autoFocus를 사용해요).
Modal Props
| 이름 | 타입 | 기본값 | 설명 | 버전 |
|---|---|---|---|---|
| auto-focus | boolean |
true |
모달 안의 첫 번째 포커스 가능한 요소에 포커스를 맞출지 여부예요. | 2.24.2 |
| block-scroll | boolean |
true |
활성 상태일 때 body 스크롤을 막을지 여부예요. | 2.28.3 |
| close-on-esc | boolean |
true |
Esc 키를 눌렀을 때 모달을 닫을지 여부예요. | 2.24.2 |
| display-directive | 'if' | 'show' |
'if' |
모달 본문 렌더링을 어떤 디렉티브로 제어할지 정해요. | |
| draggable | boolean | { bounds?: 'window' } |
false |
모달을 드래그할 수 있게 할지 여부예요. bounds === 'none'을 쓰면 위치가 창 안에 갇히지 않아요. |
2.41.0 |
| mask-closable | boolean |
true |
마스크를 클릭했을 때 hide 이벤트를 발생시킬지 여부예요. |
|
| preset | 'dialog' | 'card' |
undefined |
n-modal의 프리셋이에요. |
|
| show | boolean |
false |
모달을 표시할지 여부예요. | |
| show-mask | boolean |
true |
마스크를 표시할지 여부예요. false로 지정하면 마스크 관련 API가 모두 비활성화되고, 포커스가 모달 안에 갇히지 않아요(그래서 Esc 같은 키보드 이벤트가 항상 동작하지는 않아요). |
2.43.0 |
| to | string | HTMLElement |
body |
모달 콘텐츠의 컨테이너 노드예요. | |
| transform-origin | 'mouse' | 'center' |
'mouse' |
모달 표시 애니메이션의 transform origin이에요. | |
| trap-focus | boolean |
true |
포커스를 모달 안에 가둘지(trap) 여부예요. | 2.24.2 |
| z-index | number |
undefined |
모달의 z-index예요. | 2.24.0 |
| on-after-enter | () => void |
undefined |
모달이 열린 뒤에 호출되는 콜백이에요. | |
| on-after-leave | () => void |
undefined |
모달이 닫힌 뒤에 호출되는 콜백이에요. | |
| on-esc | () => void |
undefined |
Esc 키를 눌렀고 포커스가 모달 안에 있을 때 발생하는 콜백이에요. | 2.24.2 |
| on-mask-click | () => void |
undefined |
마스크를 클릭했을 때 호출되는 콜백이에요. | |
| on-update:show | (value: boolean) => void |
undefined |
모달의 표시 상태가 바뀔 때 호출되는 콜백이에요. |
ModalProvider Props
2.38.0부터 제공돼요.
| 이름 | 타입 | 기본값 | 설명 | 버전 |
|---|---|---|---|---|
| to | string | HTMLElement |
body |
모달 콘텐츠의 컨테이너 노드예요. | 2.38.0 |
Modal with Preset Card Props
Card props를 참고하세요.
Modal with Preset Dialog Props
Dialog props를 참고하세요.
Modal without Preset Slots
| 이름 | 파라미터 | 설명 |
|---|---|---|
| default | () |
모달의 콘텐츠예요. |
Modal with Preset Card Slots
Card slots을 참고하세요.
default 슬롯의 파라미터는 다르며, (props: { draggableClass: string })이에요.
Modal with Preset Dialog Slots
Dialog slots을 참고하세요.
default 슬롯의 파라미터는 다르며, (props: { draggableClass: string })이에요.
Q & A
Use Modal Outside Setup
Option 1
createDiscreteApi를 사용해요. 이 방법을 쓰려면 주의사항(caveat)을 꼼꼼히 읽어보세요. 같은 앱에서 useModal과 함께 쓰지 않는 게 좋아요.
Option 2
useModal의 반환 값을 최상위 setup에서 window에 붙인 다음 호출해야 해요. 호출하기 전에 모달이 성공적으로 마운트되었는지 먼저 확인해야 하죠.<!-- App.vue -->
<n-modal-provider>
<content />
</n-modal-provider>
<!-- content.vue -->
<template>...</template>
<script>
import { useModal } from 'naive-ui'
import { defineComponent } from 'vue'
// content
export default defineComponent({
setup() {
window.$modal = useModal()
}
})
</script>
// xxx.js
export function handler() {
// You need to ensure that window.$modal = modal has been executed in setup
window.$modal.create({
title: 'Title',
content: 'Content'
})
}
더 알아보기 (Learn more)
모달과 비슷하지만 화면 가장자리에서 슬라이드로 열리는 형태는 Drawer 컴포넌트예요. 단순 확인/취소 대화상자는 Dialog도 함께 참고하세요.