Skip to content

Commit

Permalink
added onAttemptClose handler to Modal
Browse files Browse the repository at this point in the history
  • Loading branch information
denosaurabh committed Dec 14, 2023
1 parent 36a0cea commit d086446
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/components/Modal/Modal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ const StoryWrapper: StoryFn<typeof Modal> = args => {
<>
<Button label="Open Modal" onClick={() => toggleModal(!isOpen)} />
<AnimatePresence>
{isOpen && <Modal {...args} onClose={() => toggleModal(false)} />}
{isOpen && (
<Modal
{...args}
onClose={() => {
toggleModal(false)
}}
onAttemptClose={() => {
console.log('onAttemptClose')
}}
/>
)}
</AnimatePresence>
</>
)
Expand Down
10 changes: 10 additions & 0 deletions src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type ModalProps = {
disableAnimation?: boolean
isDismissible?: boolean
onClose?: () => void
onAttemptClose?: () => void
scroll?: boolean
overlayProps?: MotionProps
contentProps?: MotionProps
Expand All @@ -34,6 +35,7 @@ export const Modal = (props: PropsWithChildren<ModalProps>) => {
disableAnimation = false,
isDismissible = true,
onClose,
onAttemptClose,
scroll = true,
size = 'lg',
overlayProps,
Expand Down Expand Up @@ -71,6 +73,8 @@ export const Modal = (props: PropsWithChildren<ModalProps>) => {
className={styles.contentVariants({ autoHeight, size })}
forceMount
onEscapeKeyDown={ev => {
onAttemptClose?.()

if (isDismissible) {
onClose?.()
} else {
Expand All @@ -82,6 +86,9 @@ export const Modal = (props: PropsWithChildren<ModalProps>) => {
ev.preventDefault()
}
}}
onPointerDownOutside={_ => {
onAttemptClose?.()
}}
>
<motion.div
key="modal-content"
Expand Down Expand Up @@ -113,6 +120,9 @@ export const Modal = (props: PropsWithChildren<ModalProps>) => {
size="xs"
className={styles.close}
aria-label="Close"
onClick={() => {
onAttemptClose?.()
}}
/>
</ModalPrimitive.Close>
)}
Expand Down

0 comments on commit d086446

Please sign in to comment.