@chakra-ui/modal

中文文档
2.3.1发布于 2023/09/09MITTypeScriptESM + CJS近 30 天下载 128.7万

An accessible dialog (modal) component for React & Chakra UI

原始使用文档
文档目录13 节
  1. Installation
  2. Import components
  3. Usage
  4. Control Focus when Modal closes
  5. Installation
  6. Import components
  7. Basic usage
  8. Usage
  9. Usage with Typescript
  10. Installation
  11. Import components
  12. Basic usage
  13. Usage

@chakra-ui/modal

A modal is a window overlaid on either the primary window or another dialog window. Contents behind a modal dialog are inert meaning that users cannot interact with content behind the dialog.

Installation

yarn add @chakra-ui/modal

# or

npm i @chakra-ui/modal

Import components

import {
  Modal,
  ModalOverlay,
  ModalContent,
  ModalHeader,
  ModalFooter,
  ModalBody,
  ModalCloseButton,
} from "@chakra-ui/react"

Usage

When the modal opens, focus is sent into the modal and set to the first tabbable element. If there are no tabbed element, focus is set on the ModalContent.

function BasicUsage() {
  const { isOpen, onOpen, onClose } = useDisclosure()
  return (
    <>
      <Button onClick={onOpen}>Open Modal</Button>

      <Modal isOpen={isOpen} onClose={onClose}>
        <ModalOverlay>
          <ModalContent>
            <ModalHeader>Modal Title</ModalHeader>
            <ModalCloseButton />

            <ModalBody>
              <Lorem count={2} />
            </ModalBody>

            <ModalFooter>
              <Button colorScheme="blue" mr={3} onClick={onClose}>
                Close
              </Button>
              <Button variant="ghost">Secondary Action</Button>
            </ModalFooter>
          </ModalContent>
        </ModalOverlay>
      </Modal>
    </>
  )
}

Control Focus when Modal closes

When the dialog closes, it returns focus to the element that triggered. Set finalFocusRef to element that should receive focus when the modal opens.

function ReturnFocus() {
  const { isOpen, onOpen, onClose } = useDisclosure()
  const finalRef = React.useRef()

  return (
    <>
      <Box ref={finalRef} tabIndex={-1} aria-label="Focus moved to this box">
        Some other content that'll receive focus on close.
      </Box>

      <Button mt={4} onClick={onOpen}>
        Open Modal
      </Button>

      <Modal finalFocusRef={finalRef} isOpen={isOpen} onClose={onClose}>
        <ModalOverlay>
          <ModalContent>
            <ModalHeader>Modal Title</ModalHeader>
            <ModalCloseButton />
            <ModalBody>
              <Lorem count={2} />
            </ModalBody>

            <ModalFooter>
              <Button colorScheme="blue" mr={3} onClick={onClose}>
                Close
              </Button>
              <Button variant="ghost">Secondary Action</Button>
            </ModalFooter>
          </ModalContent>
        </ModalOverlay>
      </Modal>
    </>
  )
}

Alert Dialog

AlertDialog component is used interrupt the user with a mandatory confirmation or action.

Installation

yarn add @chakra-ui/alert-dialog

# or

npm i @chakra-ui/alert-dialog

Import components

import {
  AlertDialog,
  AlertDialogBody,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogContent,
  AlertDialogOverlay,
} from "@chakra-ui/react"

Basic usage

Usage

AlertDialog requires that you provide the leastDestructiveRef prop.

Based on WAI-ARIA specifications, focus should be placed on the least destructive element when the dialog opens, to prevent users from accidentally confirming the destructive action.

function AlertDialogExample() {
  const [isOpen, setIsOpen] = React.useState()
  const onClose = () => setIsOpen(false)
  const cancelRef = React.useRef()

  return (
    <>
      <Button colorScheme="red" onClick={() => setIsOpen(true)}>
        Delete Customer
      </Button>

      <AlertDialog
        isOpen={isOpen}
        leastDestructiveRef={cancelRef}
        onClose={onClose}
      >
        <AlertDialogOverlay>
          <AlertDialogContent>
            <AlertDialogHeader fontSize="lg" fontWeight="bold">
              Delete Customer
            </AlertDialogHeader>

            <AlertDialogBody>
              Are you sure? You can't undo this action afterwards.
            </AlertDialogBody>

            <AlertDialogFooter>
              <Button ref={cancelRef} onClick={onClose}>
                Cancel
              </Button>
              <Button colorScheme="red" onClick={onClose} ml={3}>
                Delete
              </Button>
            </AlertDialogFooter>
          </AlertDialogContent>
        </AlertDialogOverlay>
      </AlertDialog>
    </>
  )
}

Usage with Typescript

Using the above example will throw a typescript error. You'll want to modify the type of the useRef hook to the HTML element you're placing the focus on and default the value to null.

const cancelRef = React.useRef<HTMLButtonElement>(null)

Drawer

The Drawer component is a panel that slides out from the edge of the screen. It can be useful when you need users to complete a task or view some details without leaving the current page.

Installation

yarn add @chakra-ui/modal

# or

npm i @chakra-ui/modal

Import components

import {
  Drawer,
  DrawerOverlay,
  DrawerContent,
  DrawerHeader,
  DrawerFooter,
  DrawerBody,
  DrawerCloseButton,
} from "@chakra-ui/react"

Basic usage

Usage

By default, focus is placed on DrawerCloseButton when the drawer opens.

function DrawerExample() {
  const { isOpen, onOpen, onClose } = useDisclosure()
  const btnRef = React.useRef()

  return (
    <>
      <Button ref={btnRef} colorScheme="teal" onClick={onOpen}>
        Open
      </Button>
      <Drawer
        isOpen={isOpen}
        placement="right"
        onClose={onClose}
        finalFocusRef={btnRef}
      >
        <DrawerOverlay />
        <DrawerContent>
          <DrawerCloseButton />
          <DrawerHeader>Create your account</DrawerHeader>

          <DrawerBody>
            <Input placeholder="Type here..." />
          </DrawerBody>

          <DrawerFooter>
            <Button variant="outline" mr={3} onClick={onClose}>
              Cancel
            </Button>
            <Button color="blue">Save</Button>
          </DrawerFooter>
        </DrawerContent>
      </Drawer>
    </>
  )
}

版本与发布记录

@chakra-ui/modal 共发布 511 个版本,首次发布于 2020/06/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.3.1latest2023/09/093 年前
0.0.0-dev-20230908144305dev2023/09/083 年前
0.0.0-dev-202308301338212023/08/303 年前
0.0.0-dev-202308301206422023/08/303 年前
0.0.0-dev-202308300937472023/08/303 年前
0.0.0-dev-202308221344342023/08/223 年前

其他发布通道:canary → 0.0.0-canary-2021118164051,pr → 0.0.0-pr-202212011232,next → 2.0.0-next.3,next-dev → 0.0.0-next-dev-202241614737,dev → 0.0.0-dev-20230908144305。

关于 @chakra-ui/modal 的常见问题

@chakra-ui/modal 是什么?

An accessible dialog (modal) component for React & Chakra UI

如何安装 @chakra-ui/modal?

在项目目录运行 npm install @chakra-ui/modal,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=18)、react-dom(>=18)、framer-motion(>=4.0.0)、@chakra-ui/system(>=2.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@chakra-ui/modal 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chakra-ui/modal 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@chakra-ui/modal 可以商用吗?

@chakra-ui/modal 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chakra-ui/modal 的下载量有多少?

根据 npm 官方下载统计,@chakra-ui/modal 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,286,501 次,日均约 42,883 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@chakra-ui/modal 还在维护吗?

最新版本 2.3.1 发布于 2023/09/09(3 年前);至今共发布 511 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/06/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。