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

Drag trap for popups #292

Merged
merged 4 commits into from
Sep 26, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to [`@bpmn-io/properties-panel`](https://github.com/bpmn-io/
___Note:__ Yet to be released changes appear here._

* `FEAT`: improve FEEL popup lifecycle events ([#294](https://github.com/bpmn-io/properties-panel/pull/294))
* `FEAT`: add drag trap to popup component ([#289](https://github.com/bpmn-io/properties-panel/issues/289))
* `FEAT`: allow listen to `feelPopup.dragstart`, `feelPopup.dragover` and `feelPopup.dragend` events ([#299](https://github.com/bpmn-io/properties-panel/pull/292))

## 3.7.1

Expand Down
2 changes: 1 addition & 1 deletion src/assets/properties-panel.css
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ textarea.bio-properties-panel-input {
--feel-popup-close-background-color: hsla(219, 99%, 53%, 1);
--feel-popup-gutters-background-color: hsla(0, 0%, 90%, 1);

position: absolute;
position: fixed;
nikku marked this conversation as resolved.
Show resolved Hide resolved
display: flex;
flex: auto;
flex-direction: column;
Expand Down
25 changes: 21 additions & 4 deletions src/components/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { useEffect, useRef } from 'preact/hooks';

import classNames from 'classnames';

import { throttle } from 'min-dash';

import * as focusTrap from 'focus-trap';

import { DragIcon } from './icons';
Expand Down Expand Up @@ -141,6 +139,7 @@ function Title(props) {
children,
className,
draggable,
emit = () => {},
title,
...rest
} = props;
Expand All @@ -156,7 +155,9 @@ function Title(props) {

const titleRef = useRef();

const onMove = throttle((_, delta) => {
const onMove = (event, delta) => {
cancel(event);

const { x: dx, y: dy } = delta;

const newPosition = {
Expand All @@ -168,25 +169,36 @@ function Title(props) {

popupParent.style.top = newPosition.y + 'px';
popupParent.style.left = newPosition.x + 'px';
});

// notify interested parties
emit('dragover', { newPosition, delta });
};

const onMoveStart = (event) => {

// initialize drag handler
const onDragStart = createDragger(onMove, dragPreviewRef.current);
onDragStart(event);

event.stopPropagation();

const popupParent = getPopupParent(titleRef.current);

const bounds = popupParent.getBoundingClientRect();
context.current.startPosition = {
x: bounds.left,
y: bounds.top
};

// notify interested parties
emit('dragstart');
};

const onMoveEnd = () => {
context.current.newPosition = null;

// notify interested parties
emit('dragend');
};

return (
Expand Down Expand Up @@ -248,4 +260,9 @@ function Footer(props) {

function getPopupParent(node) {
return node.closest('.bio-properties-panel-popup');
}

function cancel(event) {
event.preventDefault();
event.stopPropagation();
}
2 changes: 2 additions & 0 deletions src/components/entries/FEEL/FeelPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ function FeelPopupComponent(props) {
<Popup
container={ container }
className="bio-properties-panel-feel-popup"
emit={ emit }
position={ position }
title={ title }
onClose={ onClose }
Expand All @@ -198,6 +199,7 @@ function FeelPopupComponent(props) {
>
<Popup.Title
title={ title }
emit={ emit }
draggable />
<Popup.Body>
<div
Expand Down
11 changes: 9 additions & 2 deletions src/components/util/dragger.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export function createDragger(fn, dragPreview) {
// (2) setup drag listeners

// attach drag + cleanup event
document.addEventListener('dragover', onDrag);
// we need to do this to make sure we track cursor
// movements before we reach other drag event handlers,
// e.g. in child containers.
document.addEventListener('dragover', onDrag, true);
nikku marked this conversation as resolved.
Show resolved Hide resolved
document.addEventListener('dragenter', preventDefault, true);

document.addEventListener('dragend', onEnd);
document.addEventListener('drop', preventDefault);
}
Expand All @@ -55,7 +60,9 @@ export function createDragger(fn, dragPreview) {
}

function onEnd() {
document.removeEventListener('dragover', onDrag);
document.removeEventListener('dragover', onDrag, true);
document.removeEventListener('dragenter', preventDefault, true);

document.removeEventListener('dragend', onEnd);
document.removeEventListener('drop', preventDefault);
}
Expand Down
83 changes: 83 additions & 0 deletions test/spec/components/FeelPopup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,42 @@ describe('<FeelPopup>', function() {

describe('events', function() {

function expectDraggingEvent(event) {

it('should listen on <' + event + '>', async function() {

// given
const eventBus = new EventBus();

const spy = sinon.spy();

eventBus.on(event, spy);

createFeelPopup({ popupContainer: container, eventBus }, container);

// assume
expect(getFeelEditor(container)).to.not.exist;

// when
await act(() => {
eventBus.fire('feelPopup._open');
});

const header = domQuery('.bio-properties-panel-popup__header', container);
const dragger = domQuery('.bio-properties-panel-popup__drag-handle', header);
const draggerBounds = dragger.getBoundingClientRect();

// when
startDragging(dragger);
moveDragging(dragger, { clientX: draggerBounds.x + 20, clientY: draggerBounds.y });
endDragging(dragger);

// then
expect(spy).to.have.been.calledOnce;
});
}


it('should listen on <feelPopup.opened>', async function() {

// given
Expand Down Expand Up @@ -255,6 +291,15 @@ describe('<FeelPopup>', function() {
});


expectDraggingEvent('feelPopup.dragstart');


expectDraggingEvent('feelPopup.dragover');


expectDraggingEvent('feelPopup.dragend');


it('<feelPopup.open>', async function() {

// given
Expand Down Expand Up @@ -703,4 +748,42 @@ function getFeelEditor(container) {

function getFeelersEditor(container) {
return domQuery('.bio-properties-panel-feelers-editor-container', container);
}

function dispatchEvent(element, type, options = {}) {
const event = document.createEvent('Event');

event.initEvent(type, true, true);

Object.keys(options).forEach(key => event[ key ] = options[ key ]);

element.dispatchEvent(event);
}

function startDragging(node, position) {
if (!position) {
const bounds = node.getBoundingClientRect();
position = {
clientX: bounds.x,
clientY: bounds.y
};
}

dispatchEvent(node, 'dragstart', position);
}

function moveDragging(node, position) {
if (!position) {
const bounds = node.getBoundingClientRect();
position = {
clientX: bounds.x + 20,
clientY: bounds.y + 20
};
}

dispatchEvent(node, 'dragover', position);
}

function endDragging(node) {
dispatchEvent(node, 'dragend');
}
36 changes: 36 additions & 0 deletions test/spec/components/Popup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,42 @@ describe('<Popup>', function() {
expect(newBounds.x).to.eql(oldBounds.x + 20);
});


it('should not bubble dragging events to parent', function() {

// given
const dragStartSpy = sinon.spy();
const dragOverSpy = sinon.spy();
const dragEnterSpy = sinon.spy();

container.addEventListener('dragstart', dragStartSpy);
container.addEventListener('dragover', dragOverSpy);
container.addEventListener('dragenter', dragEnterSpy);

render(
<Popup container={ container }>
<Popup.Title draggable={ true } />
</Popup>,
{ container }
);

const header = domQuery('.bio-properties-panel-popup__header', container);
const dragger = domQuery('.bio-properties-panel-popup__drag-handle', header);

const draggerBounds = dragger.getBoundingClientRect();

// when
startDragging(dragger);
moveDragging(dragger, { clientX: draggerBounds.x + 20, clientY: draggerBounds.y });
endDragging(dragger);

// then
expect(dragStartSpy).to.not.have.been.called;
expect(dragOverSpy).to.not.have.been.called;
expect(dragEnterSpy).to.not.have.been.called;
});


});

});
Expand Down
Loading