Created
December 19, 2025 10:35
-
-
Save Wxh16144/4673ae719e3af65513158e27f9e4cdbf to your computer and use it in GitHub Desktop.
Ant Design Modal Animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Button, ConfigProvider, Modal } from "antd"; | |
| import React, { useEffect, useState } from "react"; | |
| import 'animate.css'; | |
| import clsx from "clsx"; | |
| // 动画持续时间 | |
| const ENTRY_DURATION = 800; | |
| const EXIT_DURATION = 600; | |
| const App = () => { | |
| const [open, setOpen] = useState(false); | |
| const slogan = "React + Ant Design + TypeScript + Vite"; | |
| const [realClosed, setRealClosed] = useState(false); | |
| const realOpen = !realClosed; | |
| useEffect(() => { | |
| if (!open) { | |
| const timer = setTimeout(() => { | |
| setRealClosed(true); | |
| }, EXIT_DURATION); | |
| return () => clearTimeout(timer); | |
| } else { | |
| // eslint-disable-next-line react-hooks/set-state-in-effect | |
| setRealClosed(false); | |
| } | |
| }, [open]); | |
| return ( | |
| <ConfigProvider | |
| theme={{ | |
| components: { | |
| Modal: { motion: false, algorithm: true } | |
| } | |
| }} | |
| > | |
| <Button type="primary" onClick={() => setOpen(true)}> | |
| Open Modal | |
| </Button> | |
| <Modal | |
| onCancel={() => { | |
| setOpen(false); | |
| }} | |
| open={realOpen} | |
| centered | |
| classNames={{ | |
| mask: clsx('animate__animated', !open && 'animate__fadeOut'), | |
| }} | |
| styles={{ | |
| mask: { | |
| '--animate-duration': `${EXIT_DURATION * 2}ms`, | |
| } | |
| }} | |
| afterOpenChange={globalThis.console.log} | |
| modalRender={node => React.createElement( | |
| 'div', | |
| { | |
| className: clsx( | |
| 'animate__animated', | |
| open | |
| ? 'animate__fadeInDown' | |
| : 'animate__fadeOutDown' | |
| ), | |
| style: { | |
| '--animate-duration': open ? `${ENTRY_DURATION}ms` : `${EXIT_DURATION}ms`, | |
| } | |
| }, | |
| node | |
| )} | |
| > | |
| <h3>{slogan}</h3> | |
| </Modal> | |
| </ConfigProvider> | |
| ); | |
| } | |
| export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment