Repository navigation
Modal: add exit animation #65203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modal: add exit animation #65203
Changes from all commits
aadbf93
d488176
5a87b55
d16d293
e1a0ed3
be87876
85668ad
352e057
4185a82
25d8538
11c0518
3686a86
d774215
bbb49a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| * External dependencies | ||
| */ | ||
| import clsx from 'clsx'; | ||
| import type { ForwardedRef, KeyboardEvent, RefObject, UIEvent } from 'react'; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the named imports in favour of using the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add an explicit
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I came across two issues when doing that:
I don't think the auto-fix makes sense, since we only want to import the types — but at the same time, I don't understand the first error, and I'm not sure if it's safe to ignore. |
||
|
|
||
| /** | ||
| * WordPress dependencies | ||
|
|
@@ -38,10 +37,11 @@ import StyleProvider from '../style-provider'; | |
| import type { ModalProps } from './types'; | ||
| import { withIgnoreIMEEvents } from '../utils/with-ignore-ime-events'; | ||
| import { Spacer } from '../spacer'; | ||
| import { useModalExitAnimation } from './use-modal-exit-animation'; | ||
|
|
||
| // Used to track and dismiss the prior modal when another opens unless nested. | ||
| type Dismissers = Set< | ||
| RefObject< ModalProps[ 'onRequestClose' ] | undefined > | ||
| React.RefObject< ModalProps[ 'onRequestClose' ] | undefined > | ||
| >; | ||
| const ModalContext = createContext< Dismissers >( new Set() ); | ||
|
|
||
|
|
@@ -50,7 +50,7 @@ const bodyOpenClasses = new Map< string, number >(); | |
|
|
||
| function UnforwardedModal( | ||
| props: ModalProps, | ||
| forwardedRef: ForwardedRef< HTMLDivElement > | ||
| forwardedRef: React.ForwardedRef< HTMLDivElement > | ||
| ) { | ||
| const { | ||
| bodyOpenClassName = 'modal-open', | ||
|
|
@@ -70,7 +70,7 @@ function UnforwardedModal( | |
| closeButtonLabel, | ||
| children, | ||
| style, | ||
| overlayClassName, | ||
| overlayClassName: overlayClassnameProp, | ||
| className, | ||
| contentLabel, | ||
| onKeyDown, | ||
|
|
@@ -184,6 +184,9 @@ function UnforwardedModal( | |
| }; | ||
| }, [ bodyOpenClassName ] ); | ||
|
|
||
| const { closeModal, frameRef, frameStyle, overlayClassname } = | ||
| useModalExitAnimation(); | ||
|
|
||
| // Calls the isContentScrollable callback when the Modal children container resizes. | ||
| useLayoutEffect( () => { | ||
| if ( ! window.ResizeObserver || ! childrenContainerRef.current ) { | ||
|
|
@@ -200,21 +203,21 @@ function UnforwardedModal( | |
| }; | ||
| }, [ isContentScrollable, childrenContainerRef ] ); | ||
|
|
||
| function handleEscapeKeyDown( event: KeyboardEvent< HTMLDivElement > ) { | ||
| function handleEscapeKeyDown( | ||
| event: React.KeyboardEvent< HTMLDivElement > | ||
| ) { | ||
| if ( | ||
| shouldCloseOnEsc && | ||
| ( event.code === 'Escape' || event.key === 'Escape' ) && | ||
| ! event.defaultPrevented | ||
| ) { | ||
| event.preventDefault(); | ||
| if ( onRequestClose ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| onRequestClose( event ); | ||
| } | ||
| closeModal().then( () => onRequestClose( event ) ); | ||
| } | ||
| } | ||
|
|
||
| const onContentContainerScroll = useCallback( | ||
| ( e: UIEvent< HTMLDivElement > ) => { | ||
| ( e: React.UIEvent< HTMLDivElement > ) => { | ||
| const scrollY = e?.currentTarget?.scrollTop ?? -1; | ||
|
|
||
| if ( ! hasScrolledContent && scrollY > 0 ) { | ||
|
|
@@ -248,7 +251,7 @@ function UnforwardedModal( | |
| const isSameTarget = target === pressTarget; | ||
| pressTarget = null; | ||
| if ( button === 0 && isSameTarget ) { | ||
| onRequestClose(); | ||
| closeModal().then( () => onRequestClose() ); | ||
| } | ||
| }, | ||
| }; | ||
|
|
@@ -259,7 +262,8 @@ function UnforwardedModal( | |
| ref={ useMergeRefs( [ ref, forwardedRef ] ) } | ||
| className={ clsx( | ||
| 'components-modal__screen-overlay', | ||
| overlayClassName | ||
| overlayClassname, | ||
| overlayClassnameProp | ||
| ) } | ||
| onKeyDown={ withIgnoreIMEEvents( handleEscapeKeyDown ) } | ||
| { ...( shouldCloseOnClickOutside ? overlayPressHandlers : {} ) } | ||
|
|
@@ -271,8 +275,12 @@ function UnforwardedModal( | |
| sizeClass, | ||
| className | ||
| ) } | ||
| style={ style } | ||
| style={ { | ||
| ...frameStyle, | ||
| ...style, | ||
| } } | ||
| ref={ useMergeRefs( [ | ||
| frameRef, | ||
| constrainedTabbingRef, | ||
| focusReturnRef, | ||
| focusOnMount !== 'firstContentElement' | ||
|
|
@@ -331,7 +339,13 @@ function UnforwardedModal( | |
| /> | ||
| <Button | ||
| size="small" | ||
| onClick={ onRequestClose } | ||
| onClick={ ( | ||
| event: React.MouseEvent< HTMLButtonElement > | ||
| ) => | ||
| closeModal().then( () => | ||
| onRequestClose( event ) | ||
| ) | ||
| } | ||
| icon={ close } | ||
| label={ | ||
| closeButtonLabel || | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useReducedMotion } from '@wordpress/compose'; | ||
| import { useCallback, useRef, useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { CONFIG } from '../utils'; | ||
| import warning from '@wordpress/warning'; | ||
|
|
||
| // Animation duration (ms) extracted to JS in order to be used on a setTimeout. | ||
| const FRAME_ANIMATION_DURATION = CONFIG.transitionDuration; | ||
| const FRAME_ANIMATION_DURATION_NUMBER = Number.parseInt( | ||
| CONFIG.transitionDuration | ||
| ); | ||
|
|
||
| const EXIT_ANIMATION_NAME = 'components-modal__disappear-animation'; | ||
|
|
||
| export function useModalExitAnimation() { | ||
| const frameRef = useRef< HTMLDivElement >(); | ||
| const [ isAnimatingOut, setIsAnimatingOut ] = useState( false ); | ||
| const isReducedMotion = useReducedMotion(); | ||
|
|
||
| const closeModal = useCallback( | ||
| () => | ||
| new Promise< void >( ( closeModalResolve ) => { | ||
| // Grab a "stable" reference of the frame element, since | ||
| // the value held by the react ref might change at runtime. | ||
| const frameEl = frameRef.current; | ||
|
|
||
| if ( isReducedMotion ) { | ||
| closeModalResolve(); | ||
| return; | ||
| } | ||
|
|
||
| if ( ! frameEl ) { | ||
| warning( | ||
| "wp.components.Modal: the Modal component can't be closed with an exit animation because of a missing reference to the modal frame element." | ||
| ); | ||
| closeModalResolve(); | ||
| return; | ||
| } | ||
|
|
||
| let handleAnimationEnd: | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I extracted |
||
| | undefined | ||
| | ( ( e: AnimationEvent ) => void ); | ||
|
|
||
| const startAnimation = () => | ||
| new Promise< void >( ( animationResolve ) => { | ||
| handleAnimationEnd = ( e: AnimationEvent ) => { | ||
| if ( e.animationName === EXIT_ANIMATION_NAME ) { | ||
| animationResolve(); | ||
| } | ||
| }; | ||
|
|
||
| frameEl.addEventListener( | ||
| 'animationend', | ||
| handleAnimationEnd | ||
| ); | ||
| setIsAnimatingOut( true ); | ||
| } ); | ||
| const animationTimeout = () => | ||
| new Promise< void >( ( timeoutResolve ) => { | ||
| setTimeout( | ||
| () => timeoutResolve(), | ||
| // Allow an extra 20% of the animation duration for the | ||
| // animationend event to fire, in case the animation frame is | ||
| // slightly delayes by some other events in the event loop. | ||
| FRAME_ANIMATION_DURATION_NUMBER * 1.2 | ||
| ); | ||
| } ); | ||
|
|
||
| Promise.race( [ startAnimation(), animationTimeout() ] ).then( | ||
| () => { | ||
| if ( handleAnimationEnd ) { | ||
| frameEl.removeEventListener( | ||
| 'animationend', | ||
| handleAnimationEnd | ||
| ); | ||
| } | ||
| setIsAnimatingOut( false ); | ||
| closeModalResolve(); | ||
| } | ||
| ); | ||
| } ), | ||
| [ isReducedMotion ] | ||
| ); | ||
|
|
||
| return { | ||
| overlayClassname: isAnimatingOut ? 'is-animating-out' : undefined, | ||
| frameRef, | ||
| frameStyle: { | ||
| '--modal-frame-animation-duration': `${ FRAME_ANIMATION_DURATION }`, | ||
| }, | ||
|
ciampo marked this conversation as resolved.
Resolved
|
||
| closeModal, | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.