fix: present modal in modal exit animation will stuck

- Updated modal animation styles to include perspective and z-index adjustments for improved depth perception.
- Refactored modal component to apply 3D transform styles and ensure consistent animation behavior.
- Modified MCPPresetCard and MCPPresetSelectionModal components to use new background styles for better visual consistency.

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-09-01 18:02:42 +08:00
parent e2d007fd9c
commit e992612de6
9 changed files with 46 additions and 22 deletions
@@ -1,10 +1,14 @@
import { createEventBus } from "@follow/utils/event-bus"
export const ModalEventBus = createEventBus<{
MODAL_DISPATCH: ModalDisposeEvent
DISMISS: ModalDisposeEvent
RE_PRESENT: ModalRePresentEvent
}>()
export type ModalDisposeEvent = {
type: "dismiss"
id: string
}
export type ModalRePresentEvent = {
id: string
}
@@ -2,12 +2,15 @@ import { Spring } from "@follow/components/constants/spring.js"
import type { MotionProps, TargetAndTransition } from "motion/react"
const enterStyle: TargetAndTransition = {
scale: 1,
opacity: 1,
// Draw the modal towards the viewer from depth
transformPerspective: 1200,
z: 0,
}
const initialStyle: TargetAndTransition = {
scale: 0.96,
opacity: 0,
transformPerspective: 1200,
z: -48,
}
export const modalMontionConfig = {
@@ -36,8 +36,7 @@ const actions = {
return jotaiStore.get(modalStackAtom).find((item) => item.id === id)
},
dismiss(id: string) {
ModalEventBus.dispatch("MODAL_DISPATCH", {
type: "dismiss",
ModalEventBus.dispatch("DISMISS", {
id,
})
},
@@ -4,14 +4,12 @@ import { useAnimationControls } from "motion/react"
import { useCallback, useEffect, useLayoutEffect } from "react"
import { useEventCallback } from "usehooks-ts"
import { ModalEventBus } from "../bus"
import { modalMontionConfig } from "../constants"
export interface ModalAnimateControls {
/** Controller for modal animations */
animateController: ReturnType<typeof useAnimationControls>
/** Play notice animation when modal can't be dismissed */
playNoticeAnimation: () => void
/** Play exit animation and return promise that resolves when complete */
playExitAnimation: () => Promise<void>
}
@@ -19,26 +17,37 @@ export interface ModalAnimateControls {
* @internal
* Hook for managing modal animations including enter, notice, and exit animations
*/
export const useModalAnimate = (isTop: boolean): ModalAnimateControls => {
export const useModalAnimate = (
isTop: boolean,
modalId: string,
setIsClosing: (isClosing: boolean) => void,
): ModalAnimateControls => {
const animateController = useAnimationControls()
// Initial enter animation
useEffect(() => {
ModalEventBus.subscribe("RE_PRESENT", (data) => {
if (data.id !== modalId) {
return
}
setIsClosing(false)
animateController.start(modalMontionConfig.animate)
})
nextFrame(() => {
animateController.start(modalMontionConfig.animate)
})
}, [animateController])
}, [animateController, modalId, setIsClosing])
// Notice animation for when modal can't be dismissed
const playNoticeAnimation = useCallback(() => {
animateController
.start({
scale: 1.01,
z: 6,
transition: Spring.snappy(0.06),
})
.then(() => {
animateController.start({
scale: 1,
z: 0,
})
})
}, [animateController])
@@ -47,14 +56,16 @@ export const useModalAnimate = (isTop: boolean): ModalAnimateControls => {
useLayoutEffect(() => {
if (isTop) return
animateController.start({
scale: 0.96,
y: 10,
z: -64,
rotateX: 2.5,
y: 8,
})
return () => {
try {
animateController.stop()
animateController.start({
scale: 1,
z: 0,
rotateX: 0,
y: 0,
})
} catch {
@@ -6,7 +6,7 @@ import type { ModalActionsInternal } from "../context"
/** @internal */
export const useModalSubscriber = (id: string, ctx: ModalActionsInternal) => {
useEffect(() => {
return ModalEventBus.subscribe("MODAL_DISPATCH", (data) => {
return ModalEventBus.subscribe("DISMISS", (data) => {
if (data.id === id) {
ctx.dismiss()
}
@@ -91,7 +91,11 @@ export const ModalInternal = memo(function Modal({
const [isClosing, setIsClosing] = useState(false)
// Animation controls
const { animateController, playNoticeAnimation, playExitAnimation } = useModalAnimate(!!isTop)
const { animateController, playNoticeAnimation, playExitAnimation } = useModalAnimate(
!!isTop,
item.id,
setIsClosing,
)
// Simple dismiss logic
const close = useEventCallback(async (forceClose = false) => {
@@ -105,7 +109,7 @@ export const ModalInternal = memo(function Modal({
// Custom modals handle their own animation
setStack((p) => p.filter((modal) => modal.id !== item.id))
} else {
// Play exit animation then remove from stack
// Play exit animation then remove from stack\
await playExitAnimation()
setStack((p) => p.filter((modal) => modal.id !== item.id))
}
@@ -332,6 +336,7 @@ export const ModalInternal = memo(function Modal({
onClick={handleClickOutsideToDismiss}
style={{
zIndex: currentModalZIndex,
perspective: 1200,
}}
tabIndex={-1}
>
@@ -339,7 +344,7 @@ export const ModalInternal = memo(function Modal({
<m.div
ref={modalElementRef}
style={modalStyle}
style={{ ...modalStyle, transformStyle: "preserve-3d" }}
{...modalMontionConfig}
animate={animateController}
className={cn(
@@ -7,6 +7,7 @@ import { getUISettings } from "~/atoms/settings/ui"
import { jotaiStore } from "~/lib/jotai"
import { modalStackAtom } from "./atom"
import { ModalEventBus } from "./bus"
import { PresentModalContextInternal } from "./context"
import { modalIdToPropsMap } from "./hooks"
import { ModalStack } from "./modal-stack"
@@ -35,6 +36,7 @@ export const ModalStackProvider: FC<PropsWithChildren> = ({ children }) => {
const index = p.indexOf(existingModal)
return [...p.slice(0, index), ...p.slice(index + 1), existingModal]
})
ModalEventBus.dispatch("RE_PRESENT", { id: modalId })
} else {
// NOTE: The props of the Command Modal are immutable, so we'll just take the store value and inject it.
// There is no need to inject `overlay` props, this is rendered responsively based on ui changes.
@@ -9,7 +9,7 @@ interface MCPPresetCardProps {
export const MCPPresetCard = ({ preset, onSelect }: MCPPresetCardProps) => {
return (
<div className="border-fill-secondary bg-fill hover:border-accent hover:bg-fill-secondary group rounded-lg border p-4 transition-all hover:shadow-md">
<div className="border-fill-secondary bg-material-medium hover:border-accent hover:bg-fill-quaternary group rounded-lg border p-4 transition-all hover:shadow-md">
<div className="flex flex-col items-center space-y-3 text-center">
{/* Icon */}
<div className="flex size-12 items-center justify-center">
@@ -22,7 +22,7 @@ export const MCPPresetSelectionModal = ({
))}
{/* Custom/Manual Configuration Card */}
<div className="border-fill-secondary bg-fill hover:border-accent hover:bg-fill-secondary group rounded-lg border p-4 transition-all hover:shadow-md">
<div className="border-fill-secondary bg-material-medium hover:border-accent hover:bg-fill-quaternary group rounded-lg border p-4 transition-all hover:shadow-md">
<div className="flex flex-col items-center space-y-3 text-center">
<div className="flex size-12 items-center justify-center">
<i className="i-mgc-settings-7-cute-re text-text size-8" />