Skip to content

Commit

Permalink
feat: add "dragged" event in Dialog (#8047)
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoCardoso authored Oct 30, 2024
1 parent 6d4a78d commit 19d414c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/dialog/src/vaadin-dialog-draggable-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,18 @@ export const DialogDraggableMixin = (superClass) =>

/** @private */
_stopDrag() {
this.dispatchEvent(
new CustomEvent('dragged', { bubbles: true, composed: true, detail: { top: this.top, left: this.left } }),
);
window.removeEventListener('mouseup', this._stopDrag);
window.removeEventListener('touchend', this._stopDrag);
window.removeEventListener('mousemove', this._drag);
window.removeEventListener('touchmove', this._drag);
}

/**
* Fired when the dialog drag is finished.
*
* @event dragged
*/
};
13 changes: 13 additions & 0 deletions packages/dialog/src/vaadin-dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type DialogResizeDimensions = {
contentHeight: string;
};

export type DialogPosition = {
top: string;
left: string;
};

/**
* Fired when the `opened` property changes.
*/
Expand All @@ -34,6 +39,11 @@ export type DialogOpenedChangedEvent = CustomEvent<{ value: boolean }>;
*/
export type DialogResizeEvent = CustomEvent<DialogResizeDimensions>;

/**
* Fired when the dialog drag is finished.
*/
export type DialogDraggedEvent = CustomEvent<DialogPosition>;

/**
* Fired when the dialog is closed.
*/
Expand All @@ -45,6 +55,8 @@ export interface DialogCustomEventMap {
closed: DialogClosedEvent;

resize: DialogResizeEvent;

dragged: DialogDraggedEvent;
}

export type DialogEventMap = DialogCustomEventMap & HTMLElementEventMap;
Expand Down Expand Up @@ -108,6 +120,7 @@ export type DialogEventMap = DialogCustomEventMap & HTMLElementEventMap;
* See [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.
*
* @fires {CustomEvent} resize - Fired when the dialog resize is finished.
* @fires {CustomEvent} dragged - Fired when the dialog drag is finished.
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} closed - Fired when the dialog is closed.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/dialog/src/vaadin-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export { DialogOverlay } from './vaadin-dialog-overlay.js';
* See [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.
*
* @fires {CustomEvent} resize - Fired when the dialog resize is finished.
* @fires {CustomEvent} dragged - Fired when the dialog drag is finished.
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} closed - Fired when the dialog is closed.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/dialog/test/draggable-resizable.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,17 @@ describe('draggable', () => {
expect(dialog.top).to.be.equal(bounds.top);
expect(dialog.left).to.be.equal(bounds.left);
});

it('should fire "dragged" event on drag', async () => {
const onDragged = sinon.spy();
dialog.addEventListener('dragged', onDragged);
drag(container);
await nextRender();
expect(onDragged.calledOnce).to.be.true;
const { detail } = onDragged.args[0][0];
expect(detail.top).to.be.equal(dialog.top);
expect(detail.left).to.be.equal(dialog.left);
});
});

describe('touch', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/dialog/test/typings/dialog.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import type { DialogResizableMixinClass } from '../../src/vaadin-dialog-resizabl
import type {
Dialog,
DialogClosedEvent,
DialogDraggedEvent,
DialogOpenedChangedEvent,
DialogPosition,
DialogRenderer,
DialogResizeDimensions,
DialogResizeEvent,
Expand Down Expand Up @@ -35,6 +37,11 @@ dialog.addEventListener('resize', (event) => {
assertType<DialogResizeDimensions>(event.detail);
});

dialog.addEventListener('dragged', (event) => {
assertType<DialogDraggedEvent>(event);
assertType<DialogPosition>(event.detail);
});

dialog.addEventListener('closed', (event) => {
assertType<DialogClosedEvent>(event);
});
Expand Down

0 comments on commit 19d414c

Please sign in to comment.