Dialog 对话框
按需导入组件:
import 'mdui/components/dialog.js';
按需导入组件的 TypeScript 类型:
import type { Dialog } from 'mdui/components/dialog.js';
使用示例:
Dialog
关闭
打开对话框
<mdui-dialog class="example-dialog">
Dialog
<mdui-button>关闭</mdui-button>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-dialog");
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector("mdui-button");
openButton.addEventListener("click", () => dialog.open = true);
closeButton.addEventListener("click", () => dialog.open = false);
</script>
添加 close-on-overlay-click
属性,点击遮罩层时会关闭对话框。
Dialog
打开对话框
<mdui-dialog close-on-overlay-click class="example-overlay">Dialog</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-overlay");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码
添加 close-on-esc
属性,按下 ESC 键时会关闭对话框。
Dialog
打开对话框
<mdui-dialog
close-on-esc
close-on-overlay-click
class="example-ecs"
>Dialog</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-ecs");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码
添加 fullscreen
属性,对话框会全屏显示。
Dialog
关闭
打开对话框
<mdui-dialog fullscreen class="example-fullscreen">
Dialog
<mdui-button>关闭</mdui-button>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-fullscreen");
const openButton = dialog.nextElementSibling;
const closeButton = dialog.querySelector("mdui-button");
openButton.addEventListener("click", () => dialog.open = true);
closeButton.addEventListener("click", () => dialog.open = false);
</script>
源码
设置 icon
属性,对话框上方会添加 Material Icons 图标。
Dialog
打开对话框
<mdui-dialog
icon="restart_alt"
close-on-overlay-click
class="example-icon"
>Dialog</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-icon");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码也可以通过 icon
slot 在对话框上方添加图标元素。
Dialog
打开对话框
<mdui-dialog close-on-overlay-click class="example-icon-slot">
Dialog
<mdui-icon slot="icon" name="restart_alt"></mdui-icon>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-icon-slot");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码
通过 headline
和 description
属性设置对话框的标题和描述。
打开对话框
<mdui-dialog
headline="Delete selected images?"
description="Images will be permenantly removed from you account and all synced devices."
close-on-overlay-click
class="example-headline"
></mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-headline");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码也可以通过 headline
和 description
slot 来设置对话框的标题元素和描述元素。
Delete selected images?
Images will be permenantly removed from you account and all synced devices.
打开对话框
<mdui-dialog close-on-overlay-click class="example-headline-slot">
<span slot="headline">Delete selected images?</span>
<span slot="description">Images will be permenantly removed from you account and all synced devices.</span>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-headline-slot");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码
可以通过 action
slot 添加底部操作按钮。
取消
删除
打开对话框
<mdui-dialog
close-on-overlay-click
headline="Delete selected images?"
class="example-action"
>
<mdui-button slot="action" variant="text">取消</mdui-button>
<mdui-button slot="action" variant="tonal">删除</mdui-button>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-action");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码添加 stacked-actions
属性,使底部操作按钮垂直排列。
Turn on speed boost
No thanks
打开对话框
<mdui-dialog
stacked-actions
close-on-overlay-click
headline="Use location service?"
class="example-stacked-actions"
>
<mdui-button slot="action" variant="text">Turn on speed boost</mdui-button>
<mdui-button slot="action" variant="text">No thanks</mdui-button>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-stacked-actions");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码
可以通过 header
slot 设置对话框顶部内容。
打开对话框
<mdui-dialog close-on-overlay-click class="example-header">
<mdui-top-app-bar slot="header">
<mdui-button-icon icon="close"></mdui-button-icon>
<mdui-top-app-bar-title>Title</mdui-top-app-bar-title>
<mdui-button variant="text">Save</mdui-button>
</mdui-top-app-bar>
<div style="height: 100px"></div>
</mdui-dialog>
<mdui-button>打开对话框</mdui-button>
<script>
const dialog = document.querySelector(".example-header");
const openButton = dialog.nextElementSibling;
openButton.addEventListener("click", () => dialog.open = true);
</script>
源码