Skip to content
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

Added onAttemptClose handler to Modal #77

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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