叠加管理器
用于以编程方式控制叠加层组件
用法
¥Usage
createOverlay
函数创建一个可以通过编程控制的新叠加层组件。
¥The createOverlay
function creates a new overlay component that can be
programmatically controlled.
import { createOverlay } from "@chakra-ui/react"
const dialog = createOverlay<DialogProps>((props) => {
const { title, description, content, ...rest } = props
return (
<Dialog.Root {...rest}>
<Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content>
{title && (
<Dialog.Header>
<Dialog.Title>{title}</Dialog.Title>
</Dialog.Header>
)}
<Dialog.Body spaceY="4">
{description && (
<Dialog.Description>{description}</Dialog.Description>
)}
{content}
</Dialog.Body>
</Dialog.Content>
</Dialog.Positioner>
</Portal>
</Dialog.Root>
)
})
然后渲染 Viewport
组件以查看叠加层。
¥Then render the Viewport
component to see the overlay.
<dialog.Viewport />
示例
¥Examples
对话框
¥Dialog
以下是可以通过编程控制的对话框组件的示例。
¥Here's an example of a dialog component that can be programmatically controlled.
"use client"
import { Button, Dialog, Portal, createOverlay } from "@chakra-ui/react"
interface DialogProps {
title: string
description?: string
content?: React.ReactNode
}
const dialog = createOverlay<DialogProps>((props) => {
const { title, description, content, ...rest } = props
return (
<Dialog.Root {...rest}>
<Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content>
{title && (
<Dialog.Header>
<Dialog.Title>{title}</Dialog.Title>
</Dialog.Header>
)}
<Dialog.Body spaceY="4">
{description && (
<Dialog.Description>{description}</Dialog.Description>
)}
{content}
</Dialog.Body>
</Dialog.Content>
</Dialog.Positioner>
</Portal>
</Dialog.Root>
)
})
const Demo = () => {
return (
<>
<Button
onClick={() => {
dialog.open("a", {
title: "Dialog Title",
description: "Dialog Description",
})
}}
>
Open Modal
</Button>
<dialog.Viewport />
</>
)
}
抽屉
¥Drawer
以下是可以通过编程控制的抽屉组件的示例。
¥Here's an example of a drawer component that can be programmatically controlled.
"use client"
import { Button, Drawer, Portal, createOverlay } from "@chakra-ui/react"
interface DialogProps {
title: string
description?: string
content?: React.ReactNode
placement?: Drawer.RootProps["placement"]
}
const drawer = createOverlay<DialogProps>((props) => {
const { title, description, content, ...rest } = props
return (
<Drawer.Root {...rest}>
<Portal>
<Drawer.Backdrop />
<Drawer.Positioner>
<Drawer.Content>
{title && (
<Drawer.Header>
<Drawer.Title>{title}</Drawer.Title>
</Drawer.Header>
)}
<Drawer.Body spaceY="4">
{description && (
<Drawer.Description>{description}</Drawer.Description>
)}
{content}
</Drawer.Body>
</Drawer.Content>
</Drawer.Positioner>
</Portal>
</Drawer.Root>
)
})
const Demo = () => {
return (
<>
<Button
onClick={() => {
drawer.open("a", {
title: "Drawer Title",
description: "Drawer Description",
placement: "end",
})
}}
>
Open Drawer
</Button>
<drawer.Viewport />
</>
)
}
更新
¥Update
使用 .update
方法更新叠加层的 props。
¥Use the .update
method to update the props of an overlay.
"use client"
import { Box, Button, Dialog, Portal } from "@chakra-ui/react"
import { createOverlay } from "@chakra-ui/react"
interface DialogProps {
title: string
description?: string
content?: React.ReactNode
}
const dialog = createOverlay<DialogProps>((props) => {
const { title, description, content, ...rest } = props
return (
<Dialog.Root {...rest}>
<Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content>
{title && (
<Dialog.Header>
<Dialog.Title>{title}</Dialog.Title>
</Dialog.Header>
)}
<Dialog.Body spaceY="4">
{description && (
<Dialog.Description>{description}</Dialog.Description>
)}
{content}
</Dialog.Body>
</Dialog.Content>
</Dialog.Positioner>
</Portal>
</Dialog.Root>
)
})
const Demo = () => {
return (
<>
<Button
onClick={async () => {
dialog.open("a", {
title: "Initial Modal Title",
content: (
<Box textStyle="sm">This text will update in 2 seconds.</Box>
),
})
setTimeout(() => {
dialog.update("a", {
title: "Updated Modal Title",
content: (
<Box textStyle="sm" color="fg.muted">
This is the updated content of the modal.
</Box>
),
})
}, 2000)
}}
>
Open Modal
</Button>
<dialog.Viewport />
</>
)
}
返回值
¥Return Value
等待 .open()
方法的结果返回传递给 .close()
方法的值。
¥Awaiting the result of the .open()
method returns the value passed to the
.close()
method.
额外内容:你还可以使用 .waitForExit()
方法等待退出动画完成后再打开新的叠加层。
¥Bonus: You can also use the .waitForExit()
method to wait for the exit
animation to complete before opening a new overlay.
"use client"
import { Button, Dialog, Portal } from "@chakra-ui/react"
import { createOverlay } from "@chakra-ui/react"
interface DialogProps {
title: string
description: string
content?: React.ReactNode
}
const dialog = createOverlay<DialogProps>((props) => {
const { title, description, content, ...rest } = props
return (
<Dialog.Root {...rest}>
<Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Content>
{title && (
<Dialog.Header>
<Dialog.Title>{title}</Dialog.Title>
</Dialog.Header>
)}
<Dialog.Body spaceY="4">
{description && (
<Dialog.Description>{description}</Dialog.Description>
)}
{content}
</Dialog.Body>
</Dialog.Content>
</Dialog.Positioner>
</Portal>
</Dialog.Root>
)
})
const Demo = () => {
return (
<>
<Button
onClick={async () => {
const returnValue = await dialog.open("a", {
title: "Dialog Title",
description: "Dialog Description",
content: (
<Button
onClick={() => {
const returnValue = { message: "Welcome" }
dialog.close("a", returnValue)
}}
>
Close
</Button>
),
})
await dialog.waitForExit("a")
dialog.open("b", {
title: returnValue.message,
description: "Next Dialog Description",
})
}}
>
Open Modal
</Button>
<dialog.Viewport />
</>
)
}
API
属性
¥Props
通过 createOverlay
函数注入到覆盖组件的属性:
¥Props that are injected into the overlay component by the createOverlay
function:
-
open
:当前是否打开覆盖层 -
onOpenChange
:当叠加层的打开状态改变时触发回调 -
onExitComplete
:当叠加层的退出动画完成时触发回调
方法
¥Methods
Viewport
渲染所有活动叠加层的根组件。
¥The root component that renders all active overlays.
open(id, props)
使用给定的 ID 和属性打开一个新的叠加层。返回一个可解析为任意值的 Promise。
¥Opens a new overlay with the given id and props. Returns a promise that resolves with any value.
close(id, value)
使用给定的 ID 关闭叠加层,并返回一个在关闭时解析的 Promise。
¥Closes the overlay with the given id and returns a promise that resolves when closed.
update(id, props)
使用给定 ID 更新叠加层的 props。
¥Updates the props of the overlay with the given id.
remove(id)
移除指定 ID 的叠加层。
¥Removes the overlay with the given id.
removeAll()
删除所有覆盖层。
¥Removes all overlays.
get(id)
获取具有指定 ID 的叠加层的属性。
¥Gets the props of the overlay with the given id.
getSnapshot()
获取叠加层的当前快照。
¥Gets the current snapshot of the overlays.
waitForExit(id)
等待具有给定 ID 的叠加层的退出动画完成。
¥Waits for the exit animation to complete for the overlay with the given id.