From 6b12fdb293e0f59e66cea3218a0d974dd777fef8 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 31 Jul 2023 12:21:36 +0800 Subject: [PATCH] fix: modal close when router change --- src/components/layout/header/config.ts | 7 +++++++ src/providers/root/modal-stack-provider.tsx | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/components/layout/header/config.ts b/src/components/layout/header/config.ts index 928a6a9e46..76471dfbb8 100644 --- a/src/components/layout/header/config.ts +++ b/src/components/layout/header/config.ts @@ -64,6 +64,13 @@ export const headerMenuConfig: IHeaderMenu[] = [ icon: h(FaSolidCircle), path: '/timeline?memory=1', }, + { + title: '专栏', + path: '/notes/topics', + icon: h('i', { + className: 'icon-[mingcute--align-bottom-fill] flex center', + }), + }, ], }, { diff --git a/src/providers/root/modal-stack-provider.tsx b/src/providers/root/modal-stack-provider.tsx index d98b10a643..9d7045e1b1 100644 --- a/src/providers/root/modal-stack-provider.tsx +++ b/src/providers/root/modal-stack-provider.tsx @@ -12,6 +12,7 @@ import { } from 'react' import { AnimatePresence, m, useAnimationControls } from 'framer-motion' import { atom, useAtomValue, useSetAtom } from 'jotai' +import { usePathname } from 'next/navigation' import type { Target, Transition } from 'framer-motion' import type { FC, PropsWithChildren, SyntheticEvent } from 'react' @@ -43,6 +44,13 @@ interface ModalProps { const modalStackAtom = atom([] as (ModalProps & { id: string })[]) +const useDismissAllWhenRouterChange = () => { + const pathname = usePathname() + useEffect(() => { + actions.dismissAll() + }, [pathname]) +} + export const useModalStack = () => { const id = useId() const currentCount = useRef(0) @@ -97,6 +105,7 @@ const ModalStack = () => { const stack = useAtomValue(modalStackAtom) const isClient = useIsClient() + useDismissAllWhenRouterChange() if (!isClient) return null return ( @@ -117,9 +126,11 @@ const initialStyle: Target = { scale: 0.96, opacity: 0, } + const modalTransition: Transition = { ...microReboundPreset, } + const Modal: Component<{ item: ModalProps & { id: string } index: number