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

Modal: Ensure drag from within Modal does not accidentally dismiss it #1594

Merged
merged 1 commit into from
Sep 26, 2022
Merged
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
14 changes: 10 additions & 4 deletions packages/sage-react/lib/Modal/Modal.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { ModalBody } from './ModalBody';
Expand All @@ -24,6 +24,7 @@ export const Modal = ({
size,
...rest
}) => {
const [mouseDownSrc, setMouseDownSrc] = useState(null);
const classNames = classnames(
'sage-modal',
className,
Expand Down Expand Up @@ -56,10 +57,14 @@ export const Modal = ({
}
}

const handleBackgroundClick = (evt) => {
if (evt.target === evt.currentTarget) {
const handleMouseDown = (evt) => setMouseDownSrc(evt.target);

const handleMouseUp = (evt) => {
if (evt.target === evt.currentTarget && evt.target === mouseDownSrc) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

onExit(true);
}

setMouseDownSrc(null);
};

const handleBackgroundKeypress = () => {
Expand All @@ -70,7 +75,8 @@ export const Modal = ({
const attrs = {};

if (!disableBackgroundDismiss) {
attrs.onClick = handleBackgroundClick;
attrs.onMouseDown = handleMouseDown;
attrs.onMouseUp = handleMouseUp;
attrs.onKeyPress = handleBackgroundKeypress;
attrs.role = 'button';
attrs.tabIndex = '0';
Expand Down
2 changes: 1 addition & 1 deletion packages/sage-react/lib/Modal/Modal.story.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ export const Wired = (args) => {
Take An Action
</Button>
<Modal
{...args}
active={active}
animation={{ direction: Modal.ANIMATION_DIRECTIONS.BOTTOM }}
onExit={onExit}
{...args}
>
<DefaultBody onExit={onExit} />
</Modal>
Expand Down
23 changes: 16 additions & 7 deletions packages/sage-system/lib/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Sage.modal = (function() {
const EVENT_OPEN = "sage.modal.open";
let selectorLastFocused;
let containerInitialContent;
let mouseDownSrc;

// ==================================================
// Functions
Expand All @@ -29,21 +30,29 @@ Sage.modal = (function() {
// If a modal has 'data-js-modal-disable-close' return early and don't init any handlers
if (el.hasAttribute(SELECTOR_MODAL_DISABLE_CLOSE)) return;

el.addEventListener("click", function(evt) {
let el = evt.target;
// In order to prevent accidentally closing modals
// after dragging within the modal to the outside
// we store the target of a mousedown
el.addEventListener("mousedown", (evt) => mouseDownSrc = evt.target);

el.addEventListener("mouseup", (evt) => {
let el = evt.target;

// A JS Event is dispatched to call closeModal,
// this allows Products to hook into that call
// and perform cleanup actions like removing the modal's content.

// Modal Container has been clicked
if (el.hasAttribute(SELECTOR_MODAL)) {
// Modal Container has been clicked and its not a drag event from within the modal
if (el.hasAttribute(SELECTOR_MODAL) && el === mouseDownSrc) {
dispatchCloseAll();

// Modal Close Button has been clicked
// Modal Close Button has been clicked
} else if (el.hasAttribute(SELECTOR_MODAL_CLOSE) || evt.target.parentElement.hasAttribute(SELECTOR_MODAL_CLOSE)) {
dispatchCloseAll();
}

// Clear out the mouseDownSrc
mouseDownSrc = null;
});
}

Expand Down