From 7218a060796fe132b8833de5513c38b35abc56c3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Fri, 14 Apr 2023 15:05:03 +0200 Subject: [PATCH] Simplify closeHandler in Dialog --- src/components/feedback/Dialog.tsx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/feedback/Dialog.tsx b/src/components/feedback/Dialog.tsx index f3b2d89cc..c19dd5c8c 100644 --- a/src/components/feedback/Dialog.tsx +++ b/src/components/feedback/Dialog.tsx @@ -74,7 +74,7 @@ const DialogNext = function Dialog({ // Forwarded to Panel buttons, icon, - onClose, + onClose = noop, paddingSize = 'md', scrollable = true, title, @@ -87,12 +87,15 @@ const DialogNext = function Dialog({ ); const [transitionComponentVisible, setTransitionComponentVisible] = useState(false); - // If a TransitionComponent was provided, closing the Dialog should just set - // it to not visible. The TransitionComponent will take care of actually - // closing the dialog once the "out" transition has finished - const closeHandler = TransitionComponent - ? () => setTransitionComponentVisible(false) - : onClose ?? noop; + + const closeHandler = useCallback(() => { + if (TransitionComponent) { + setTransitionComponentVisible(false); + } else { + onClose(); + } + }, [onClose, TransitionComponent]); + const setInitialFocus = useCallback(() => { if (initialFocus === 'manual') { return; @@ -117,12 +120,13 @@ const DialogNext = function Dialog({ modalRef.current?.focus(); } }, [initialFocus, modalRef]); + const onTransitionEnd = (direction: 'in' | 'out') => { if (direction === 'in') { // We can't check the initial focus until transition "in" has finished setInitialFocus(); } else { - onClose?.(); + onClose(); } };