Files
heroui-vue/apps/docs/demos/alert-dialog-controlled.vue
T

150 lines
4.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { ref } from "vue";
import { useOverlayState } from "@heroui-vue/components";
import {
AlertDialogBackdrop,
AlertDialogBody,
AlertDialogClose,
AlertDialogCloseTrigger,
AlertDialogContainer,
AlertDialogDialog,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogHeading,
AlertDialogIcon,
Button,
} from "@heroui-vue/components";
const isOpen = ref(false);
const state = useOverlayState();
</script>
<template>
<div class="flex max-w-md flex-col gap-8">
<div class="flex flex-col gap-3">
<h3 class="text-lg font-semibold text-foreground">配合 ref / v-model</h3>
<p class="text-sm leading-relaxed text-pretty text-muted">
使用 Vue 的
<code class="text-foreground">ref</code>
管理对话框开关,适合简单场景。
</p>
<div
class="flex flex-col items-start gap-3 rounded-2xl bg-surface p-4 shadow-sm"
>
<div class="flex w-full items-center justify-between">
<p class="text-xs text-muted">
状态:
<span class="font-mono font-medium text-foreground">
{{ isOpen ? "打开" : "关闭" }}
</span>
</p>
</div>
<div class="flex gap-2">
<Button size="sm" variant="secondary" @click="isOpen = true">
打开对话框
</Button>
<Button size="sm" variant="tertiary" @click="isOpen = !isOpen">
切换
</Button>
</div>
</div>
<AlertDialogBackdrop v-model:is-open="isOpen">
<AlertDialogContainer>
<AlertDialogDialog class="sm:max-w-[400px]">
<AlertDialogCloseTrigger />
<AlertDialogHeader>
<AlertDialogIcon status="accent" />
<AlertDialogHeading>由 ref 控制</AlertDialogHeading>
</AlertDialogHeader>
<AlertDialogBody>
<p>
该警告对话框由 Vue 的
<code>ref</code>
控制。
</p>
</AlertDialogBody>
<AlertDialogFooter>
<AlertDialogClose as-child>
<Button variant="tertiary">取消</Button>
</AlertDialogClose>
<AlertDialogClose as-child>
<Button>确认</Button>
</AlertDialogClose>
</AlertDialogFooter>
</AlertDialogDialog>
</AlertDialogContainer>
</AlertDialogBackdrop>
</div>
<div class="flex flex-col gap-3">
<h3 class="text-lg font-semibold text-foreground">
配合 useOverlayState()
</h3>
<p class="text-sm leading-relaxed text-pretty text-muted">
使用
<code class="text-foreground">useOverlayState</code>
获得更简洁的 API,内置
<code>open()</code>
、
<code>close()</code>
、
<code>toggle()</code>
等方法。
</p>
<div
class="flex flex-col items-start gap-3 rounded-2xl bg-surface p-4 shadow-sm"
>
<div class="flex w-full items-center justify-between">
<p class="text-xs text-muted">
状态:
<span class="font-mono font-medium text-foreground">
{{ state.isOpen.value ? "打开" : "关闭" }}
</span>
</p>
</div>
<div class="flex gap-2">
<Button size="sm" variant="secondary" @click="state.open()">
打开对话框
</Button>
<Button size="sm" variant="tertiary" @click="state.toggle()">
切换
</Button>
</div>
</div>
<AlertDialogBackdrop :state="state">
<AlertDialogContainer>
<AlertDialogDialog class="sm:max-w-[400px]">
<AlertDialogCloseTrigger />
<AlertDialogHeader>
<AlertDialogIcon status="success" />
<AlertDialogHeading>由 useOverlayState() 控制</AlertDialogHeading>
</AlertDialogHeader>
<AlertDialogBody>
<p>
<code>useOverlayState</code>
为常见操作提供专用方法,直接使用
<code>state.open()</code>
、
<code>state.close()</code>
或
<code>state.toggle()</code>
即可。
</p>
</AlertDialogBody>
<AlertDialogFooter>
<AlertDialogClose as-child>
<Button variant="tertiary">取消</Button>
</AlertDialogClose>
<AlertDialogClose as-child>
<Button>确认</Button>
</AlertDialogClose>
</AlertDialogFooter>
</AlertDialogDialog>
</AlertDialogContainer>
</AlertDialogBackdrop>
</div>
</div>
</template>