Skip to content

Commit 8c0e71d

Browse files
committed
fix: overlay and improve pref of modal
Signed-off-by: Innei <tukon479@gmail.com>
1 parent 3f191e7 commit 8c0e71d

File tree

3 files changed

+64
-57
lines changed

3 files changed

+64
-57
lines changed

src/components/universal/Modal/stack-context.tsx

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import type {
1111
import React, {
1212
createContext,
1313
createElement,
14-
useCallback,
1514
useContext,
16-
useMemo,
1715
useRef,
1816
useState,
1917
} from 'react'
18+
import { useStateToRef } from 'react-shortcut-guide'
2019

2120
import { useIsClient } from '~/hooks/use-is-client'
2221
import { useStore } from '~/store'
@@ -98,6 +97,8 @@ export const ModalStackProvider: FC<{
9897
}> = observer((props) => {
9998
const { children } = props
10099
const [modalStack, setModalStack] = useState<IModalStackStateType[]>([])
100+
const modalStackRef = useStateToRef(modalStack)
101+
101102
const [extraModalPropsMap, setExtraModalPropsMap] = useState<
102103
Map<
103104
string,
@@ -115,7 +116,7 @@ export const ModalStackProvider: FC<{
115116
new WeakMap<FunctionComponentElement<any>, () => any>(),
116117
)
117118

118-
const present = useCallback((comp: IModalStackComponent): Disposer => {
119+
const present = useRef((comp: IModalStackComponent): Disposer => {
119120
const {
120121
component,
121122
props,
@@ -143,24 +144,6 @@ export const ModalStackProvider: FC<{
143144
return () => null
144145
}
145146

146-
const $modalElement: FunctionComponentElement<any> = createElement(
147-
Modal,
148-
{
149-
...modalProps,
150-
modalId: id,
151-
useBottomDrawerInMobile,
152-
key: id,
153-
ref: (ins) => {
154-
modalRefMap.current.set($modalElement, ins!)
155-
},
156-
disposer: () => {
157-
dismissFnMapRef.current.delete($modalElement)
158-
setModalStack((prev) => prev.filter((item) => item.id !== id))
159-
},
160-
},
161-
modalChildren,
162-
)
163-
164147
const disposer = (immediately = false) => {
165148
const immediatelyDisposer = () => {
166149
setModalStack((stack) => {
@@ -179,6 +162,21 @@ export const ModalStackProvider: FC<{
179162
}
180163
}
181164

165+
const $modalElement: FunctionComponentElement<any> = createElement(
166+
Modal,
167+
{
168+
...modalProps,
169+
modalId: id,
170+
useBottomDrawerInMobile,
171+
key: id,
172+
ref: (ins) => {
173+
modalRefMap.current.set($modalElement, ins!)
174+
},
175+
disposer,
176+
},
177+
modalChildren,
178+
)
179+
182180
setModalStack((stack) => {
183181
return [
184182
...stack,
@@ -198,39 +196,36 @@ export const ModalStackProvider: FC<{
198196
return new Map(map)
199197
})
200198
return disposer
201-
}, [])
199+
}).current
202200

203-
const findCurrentByName = useCallback(
204-
(name: string) => {
205-
return modalStack.find((item) => item.name === name || item.id === name)
206-
},
207-
[modalStack],
208-
)
201+
const findCurrentByName = useRef((name: string) => {
202+
const modalStack = modalStackRef.current
203+
return modalStack.find((item) => item.name === name || item.id === name)
204+
}).current
209205

210-
const getStack = useCallback(() => {
206+
const getStack = useRef(() => {
207+
const modalStack = modalStackRef.current
211208
return modalStack.concat()
212-
}, [modalStack])
209+
}).current
213210

214-
const disposeAll = useCallback(
215-
async (immediately = false) => {
216-
const reversedStack = modalStack.concat().reverse()
217-
if (immediately) {
218-
reversedStack.forEach((current) => current.disposer())
219-
} else {
220-
for (const current of reversedStack) {
221-
const instance = modalRefMap.current.get(current.component)
211+
const disposeAll = useRef(async (immediately = false) => {
212+
const modalStack = modalStackRef.current
213+
const reversedStack = modalStack.concat().reverse()
214+
if (immediately) {
215+
reversedStack.forEach((current) => current.disposer())
216+
} else {
217+
for (const current of reversedStack) {
218+
const instance = modalRefMap.current.get(current.component)
222219

223-
if (!instance) {
224-
current.disposer()
225-
continue
226-
}
227-
await instance.dismiss()
220+
if (!instance) {
228221
current.disposer()
222+
continue
229223
}
224+
await instance.dismiss()
225+
current.disposer()
230226
}
231-
},
232-
[modalStack],
233-
)
227+
}
228+
}).current
234229

235230
const isClient = useIsClient()
236231

@@ -243,10 +238,9 @@ export const ModalStackProvider: FC<{
243238

244239
return (
245240
<ModalStackContext.Provider
246-
value={useMemo(
247-
() => ({ present, findCurrentByName, getStack, disposeAll }),
248-
[disposeAll, findCurrentByName, getStack, present],
249-
)}
241+
value={
242+
useRef({ present, findCurrentByName, getStack, disposeAll }).current
243+
}
250244
>
251245
{children}
252246

src/components/universal/Overlay/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ const OverLay: FC<OverlayProps> = (props) => {
5454
document.documentElement.style.overflow = show ? 'hidden' : ''
5555
}, [show])
5656

57+
useEffect(() => {
58+
return () => {
59+
document.documentElement.style.overflow = ''
60+
}
61+
}, [])
62+
5763
if (!isClient) {
5864
return null
5965
}

src/components/universal/Transition/bottom-up.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,32 @@ import { genSpringKeyframes } from '~/utils/spring'
66
import type { BaseTransitionViewProps } from './base'
77
import { BaseTransitionView } from './base'
88

9-
const name = `bottom-up-spring`
9+
const up_name = `bottom-up-spring`
10+
const down_name = `bottom-down-spring`
1011

1112
const defaultStyle = {
1213
opacity: 0,
1314
}
1415

1516
const transitionStyles = {
1617
entering: { opacity: 0 },
17-
entered: { animation: `${name} 1000ms linear both`, opacity: 1 },
18-
exiting: { animation: `${name} 1000ms linear both reverse`, opacity: 1 },
18+
entered: { animation: `${up_name} 1000ms linear both`, opacity: 1 },
19+
exiting: { animation: `${down_name} 1000ms linear both`, opacity: 1 },
1920
exited: { opacity: 0 },
2021
}
2122

2223
export const BottomUpTransitionView: FC<
2324
BaseTransitionViewProps & Partial<TransitionProps>
24-
> = BaseTransitionView(defaultStyle, transitionStyles, () =>
25+
> = BaseTransitionView(defaultStyle, transitionStyles, () => {
2526
genSpringKeyframes(
26-
name,
27+
up_name,
2728
{ translateY: '3em', opacity: 0 },
2829
{ translateY: '0em', opacity: 1 },
29-
),
30-
)
30+
)
31+
32+
genSpringKeyframes(
33+
down_name,
34+
{ translateY: '0em', opacity: 1 },
35+
{ translateY: '3em', opacity: 0 },
36+
)
37+
})

0 commit comments

Comments
 (0)