Skip to content

Commit c248131

Browse files
committed
[@mantine/core] Add onEnterTranstionEnd and onExitTransitionEnd props support to Modal, Drawer and Popover components
1 parent 12ef5d8 commit c248131

File tree

6 files changed

+53
-3
lines changed

6 files changed

+53
-3
lines changed

packages/@mantine/core/src/components/Modal/Modal.story.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable no-console */
12
import { useDisclosure } from '@mantine/hooks';
23
import { Button } from '../Button';
34
import { Menu } from '../Menu';
@@ -31,7 +32,13 @@ export function Usage() {
3132
<Button onClick={open}>Open modal</Button>
3233
{content}
3334
<Button onClick={open}>Open modal</Button>
34-
<Modal opened={opened} onClose={close} title="Just a Modal" zIndex={73812}>
35+
<Modal
36+
opened={opened}
37+
onClose={close}
38+
title="Just a Modal"
39+
transitionProps={{ duration: 500, onExited: () => console.log('onExited') }}
40+
onExitTransitionEnd={() => console.log('onExitTransitionEnd')}
41+
>
3542
<input data-autofocus />
3643
</Modal>
3744
</div>

packages/@mantine/core/src/components/ModalBase/ModalBase.context.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface ModalBaseContextValue {
1010
getTitleId: () => string;
1111
getBodyId: () => string;
1212
transitionProps: Partial<TransitionOverride> | undefined;
13+
onExitTransitionEnd: (() => void) | undefined;
14+
onEnterTransitionEnd: (() => void) | undefined;
1315
zIndex: string | number | undefined;
1416

1517
opened: boolean;

packages/@mantine/core/src/components/ModalBase/ModalBase.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ export interface ModalBaseProps extends BoxProps, ElementProps<'div', 'title'> {
5454
/** Props added to the `Transition` component that used to animate overlay and body, use to configure duration and animation type, `{ duration: 200, transition: 'pop' }` by default */
5555
transitionProps?: TransitionOverride;
5656

57+
/** Called when exit transition ends */
58+
onExitTransitionEnd?: () => void;
59+
60+
/** Called when enter transition ends */
61+
onEnterTransitionEnd?: () => void;
62+
5763
/** Determines whether `onClose` should be called when user presses the escape key, `true` by default */
5864
closeOnEscape?: boolean;
5965

@@ -84,6 +90,8 @@ export const ModalBase = forwardRef<HTMLDivElement, ModalBaseProps>(
8490
onClose,
8591
id,
8692
transitionProps,
93+
onExitTransitionEnd,
94+
onEnterTransitionEnd,
8795
trapFocus,
8896
closeOnEscape,
8997
returnFocus,
@@ -114,6 +122,8 @@ export const ModalBase = forwardRef<HTMLDivElement, ModalBaseProps>(
114122
opened,
115123
onClose,
116124
closeOnClickOutside,
125+
onExitTransitionEnd,
126+
onEnterTransitionEnd,
117127
transitionProps: { ...transitionProps, keepMounted },
118128
getTitleId: () => `${_id}-title`,
119129
getBodyId: () => `${_id}-body`,

packages/@mantine/core/src/components/ModalBase/ModalBaseContent.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export const ModalBaseContent = forwardRef<HTMLDivElement, _ModalBaseContentProp
3131
mounted={ctx.opened}
3232
transition="pop"
3333
{...ctx.transitionProps}
34+
onExited={() => {
35+
ctx.onExitTransitionEnd?.();
36+
ctx.transitionProps?.onExited?.();
37+
}}
38+
onEntered={() => {
39+
ctx.onEnterTransitionEnd?.();
40+
ctx.transitionProps?.onEntered?.();
41+
}}
3442
{...transitionProps}
3543
>
3644
{(transitionStyles) => (

packages/@mantine/core/src/components/Popover/Popover.story.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export function Uncontrolled() {
2828
}}
2929
>
3030
<div style={{ padding: 40 }}>
31-
<Popover onClose={() => console.log('closed')} onOpen={() => console.log('opened')}>
31+
<Popover
32+
onClose={() => console.log('closed')}
33+
onOpen={() => console.log('opened')}
34+
onExitTransitionEnd={() => console.log('exited')}
35+
onEnterTransitionEnd={() => console.log('entered')}
36+
>
3237
<Popover.Target>
3338
<button type="button">Toggle popover</button>
3439
</Popover.Target>

packages/@mantine/core/src/components/Popover/Popover.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export interface __PopoverProps {
6161
/** Props passed down to the `Transition` component that used to animate dropdown presence, use to configure duration and animation type, `{ duration: 150, transition: 'fade' }` by default */
6262
transitionProps?: TransitionOverride;
6363

64+
/** Called when exit transition ends */
65+
onExitTransitionEnd?: () => void;
66+
67+
/** Called when enter transition ends */
68+
onEnterTransitionEnd?: () => void;
69+
6470
/** Dropdown width, or `'target'` to make dropdown width the same as target element, `'max-content'` by default */
6571
width?: PopoverWidth;
6672

@@ -186,6 +192,8 @@ export function Popover(_props: PopoverProps) {
186192
positionDependencies,
187193
opened,
188194
transitionProps,
195+
onExitTransitionEnd,
196+
onEnterTransitionEnd,
189197
width,
190198
middlewares,
191199
withArrow,
@@ -279,6 +287,16 @@ export function Popover(_props: PopoverProps) {
279287
[popover.floating.refs.setFloating]
280288
);
281289

290+
const onExited = useCallback(() => {
291+
transitionProps?.onExited?.();
292+
onExitTransitionEnd?.();
293+
}, [transitionProps?.onExited, onExitTransitionEnd]);
294+
295+
const onEntered = useCallback(() => {
296+
transitionProps?.onEntered?.();
297+
onEnterTransitionEnd?.();
298+
}, [transitionProps?.onEntered, onEnterTransitionEnd]);
299+
282300
return (
283301
<PopoverContextProvider
284302
value={{
@@ -293,7 +311,7 @@ export function Popover(_props: PopoverProps) {
293311
arrowY: popover.floating?.middlewareData?.arrow?.y,
294312
opened: popover.opened,
295313
arrowRef,
296-
transitionProps,
314+
transitionProps: { ...transitionProps, onExited, onEntered },
297315
width,
298316
withArrow,
299317
arrowSize: arrowSize!,

0 commit comments

Comments
 (0)