Skip to content

Commit

Permalink
Simplify closeHandler in Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Apr 14, 2023
1 parent 0cf42f9 commit 7218a06
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/components/feedback/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const DialogNext = function Dialog({
// Forwarded to Panel
buttons,
icon,
onClose,
onClose = noop,
paddingSize = 'md',
scrollable = true,
title,
Expand All @@ -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;
Expand All @@ -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();
}
};

Expand Down

0 comments on commit 7218a06

Please sign in to comment.