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

Fix #2042: Dialog better handling of draggable #3715

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
84 changes: 7 additions & 77 deletions components/lib/dialog/Dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import PrimeReact, { PrimeReactContext, localeOption } from '../api/Api';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { CSSTransition } from '../csstransition/CSSTransition';
import { ESC_KEY_HANDLING_PRIORITIES, useDisplayOrder, useEventListener, useGlobalOnEscapeKey, useMergeProps, useMountEffect, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { ESC_KEY_HANDLING_PRIORITIES, useDisplayOrder, useDraggable, useEventListener, useGlobalOnEscapeKey, useMergeProps, useMountEffect, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { TimesIcon } from '../icons/times';
import { WindowMaximizeIcon } from '../icons/windowmaximize';
import { WindowMinimizeIcon } from '../icons/windowminimize';
Expand All @@ -21,14 +21,14 @@ export const Dialog = React.forwardRef((inProps, ref) => {
const [maskVisibleState, setMaskVisibleState] = React.useState(false);
const [visibleState, setVisibleState] = React.useState(false);
const [maximizedState, setMaximizedState] = React.useState(props.maximized);
const [draggableState, setDraggableState] = React.useState(false);
const dialogRef = React.useRef(null);
const headerRef = React.useRef(null);
const maskRef = React.useRef(null);
const pointerRef = React.useRef(null);
const contentRef = React.useRef(null);
const headerRef = React.useRef(null);
const footerRef = React.useRef(null);
const closeRef = React.useRef(null);
const dragging = React.useRef(false);
const resizing = React.useRef(false);
const lastPageX = React.useRef(null);
const lastPageY = React.useRef(null);
Expand Down Expand Up @@ -60,11 +60,10 @@ export const Dialog = React.forwardRef((inProps, ref) => {
priority: [ESC_KEY_HANDLING_PRIORITIES.DIALOG, displayOrder]
});

const draggable = useDraggable({ targetRef: dialogRef, handleRef: headerRef, onDragStart: props.onDragStart, onDrag: props.onDrag, onDragEnd: props.onDragEnd, enabled: draggableState, keepInViewport: props.keepInViewport });
const [bindDocumentKeyDownListener, unbindDocumentKeyDownListener] = useEventListener({ type: 'keydown', listener: (event) => onKeyDown(event) });
const [bindDocumentResizeListener, unbindDocumentResizeListener] = useEventListener({ type: 'mousemove', target: () => window.document, listener: (event) => onResize(event) });
const [bindDocumentResizeEndListener, unbindDocumentResizEndListener] = useEventListener({ type: 'mouseup', target: () => window.document, listener: (event) => onResizeEnd(event) });
const [bindDocumentDragListener, unbindDocumentDragListener] = useEventListener({ type: 'mousemove', target: () => window.document, listener: (event) => onDrag(event) });
const [bindDocumentDragEndListener, unbindDocumentDragEndListener] = useEventListener({ type: 'mouseup', target: () => window.document, listener: (event) => onDragEnd(event) });

const onClose = (event) => {
props.onHide();
Expand Down Expand Up @@ -146,68 +145,6 @@ export const Dialog = React.forwardRef((inProps, ref) => {
}
};

const onDragStart = (event) => {
if (DomHandler.hasClass(event.target, 'p-dialog-header-icon') || DomHandler.hasClass(event.target.parentElement, 'p-dialog-header-icon')) {
return;
}

if (props.draggable) {
dragging.current = true;
lastPageX.current = event.pageX;
lastPageY.current = event.pageY;
dialogRef.current.style.margin = '0';
DomHandler.addClass(document.body, 'p-unselectable-text');

props.onDragStart && props.onDragStart(event);
}
};

const onDrag = (event) => {
if (dragging.current) {
const width = DomHandler.getOuterWidth(dialogRef.current);
const height = DomHandler.getOuterHeight(dialogRef.current);
const deltaX = event.pageX - lastPageX.current;
const deltaY = event.pageY - lastPageY.current;
const offset = dialogRef.current.getBoundingClientRect();
const leftPos = offset.left + deltaX;
const topPos = offset.top + deltaY;
const viewport = DomHandler.getViewport();
const computedStyle = getComputedStyle(dialogRef.current);
const leftMargin = parseFloat(computedStyle.marginLeft);
const topMargin = parseFloat(computedStyle.marginTop);

dialogRef.current.style.position = 'fixed';

if (props.keepInViewport) {
if (leftPos >= props.minX && leftPos + width < viewport.width) {
lastPageX.current = event.pageX;
dialogRef.current.style.left = leftPos - leftMargin + 'px';
}

if (topPos >= props.minY && topPos + height < viewport.height) {
lastPageY.current = event.pageY;
dialogRef.current.style.top = topPos - topMargin + 'px';
}
} else {
lastPageX.current = event.pageX;
dialogRef.current.style.left = leftPos - leftMargin + 'px';
lastPageY.current = event.pageY;
dialogRef.current.style.top = topPos - topMargin + 'px';
}

props.onDrag && props.onDrag(event);
}
};

const onDragEnd = (event) => {
if (dragging.current) {
dragging.current = false;
DomHandler.removeClass(document.body, 'p-unselectable-text');

props.onDragEnd && props.onDragEnd(event);
}
};

const onResizeStart = (event) => {
if (props.resizable) {
resizing.current = true;
Expand Down Expand Up @@ -284,6 +221,7 @@ export const Dialog = React.forwardRef((inProps, ref) => {

const onEnter = () => {
dialogRef.current.setAttribute(attributeSelector.current, '');
setDraggableState(props.draggable);
};

const onEntered = () => {
Expand All @@ -303,7 +241,7 @@ export const Dialog = React.forwardRef((inProps, ref) => {
};

const onExited = () => {
dragging.current = false;
setDraggableState(false);
ZIndexUtils.clear(maskRef.current);
setMaskVisibleState(false);
disableDocumentSettings();
Expand Down Expand Up @@ -361,11 +299,6 @@ export const Dialog = React.forwardRef((inProps, ref) => {
};

const bindGlobalListeners = () => {
if (props.draggable) {
bindDocumentDragListener();
bindDocumentDragEndListener();
}

if (props.resizable) {
bindDocumentResizeListener();
bindDocumentResizeEndListener();
Expand All @@ -375,8 +308,6 @@ export const Dialog = React.forwardRef((inProps, ref) => {
};

const unbindGlobalListeners = () => {
unbindDocumentDragListener();
unbindDocumentDragEndListener();
unbindDocumentResizeListener();
unbindDocumentResizEndListener();
unbindDocumentKeyDownListener();
Expand Down Expand Up @@ -555,8 +486,7 @@ export const Dialog = React.forwardRef((inProps, ref) => {
{
ref: headerRef,
style: props.headerStyle,
className: cx('header'),
onMouseDown: onDragStart
className: cx('header')
},
ptm('header')
);
Expand Down
2 changes: 2 additions & 0 deletions components/lib/hooks/Hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useClickOutside } from './useClickOutside';
import { useCounter } from './useCounter';
import { useDebounce } from './useDebounce';
import { useDisplayOrder } from './useDisplayOrder';
import { useDraggable } from './useDraggable';
import { useEventListener } from './useEventListener';
import { useFavicon } from './useFavicon';
import { ESC_KEY_HANDLING_PRIORITIES, useGlobalOnEscapeKey } from './useGlobalOnEscapeKey';
Expand All @@ -28,6 +29,7 @@ export {
useCounter,
useDebounce,
useDisplayOrder,
useDraggable,
useEventListener,
useFavicon,
useGlobalOnEscapeKey,
Expand Down
48 changes: 48 additions & 0 deletions components/lib/hooks/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,54 @@ interface ResizeEventOptions {
when?: boolean;
}

/**
* Custom draggable event options.
*/
interface DraggableOptions {
/**
* The target element to listen to.
*/
targetRef: React.Ref<HTMLElement>;
/**
* The draggable handle element.
*/
handleRef: React.Ref<HTMLElement>;
/**
* Callback to invoke when dragging dialog.
* @param {React.DragEvent<HTMLElement>} event - Browser event.
*/
onDrag?(event: React.DragEvent<HTMLElement>): void;
/**
* Callback to invoke when dialog dragging is completed.
* @param {React.DragEvent<HTMLElement>} event - Browser event.
*/
onDragEnd?(event: React.DragEvent<HTMLElement>): void;
/**
* Callback to invoke when dialog dragging is initiated.
* @param {React.DragEvent<HTMLElement>} event - Browser event.
*/
onDragStart?(event: React.DragEvent<HTMLElement>): void;
/**
* Enables the draggable feature.
* @defaultValue true
*/
enabled: true;
/**
* Flag to keep the draggable in the viewport, else let it go outside the bounds.
* @defaultValue false
*/
keepInViewport: false;
/**
* The reactangular limits if you want to keep draggable to a certain area.
*/
rectLimits?: DOMRect;
}

/**
* Custom hook to handle dragging.
* @param {DraggableOptions} options - The draggable options.
*/
export declare function useDraggable(options: DraggableOptions): any;
/**
* Custom hook to get the previous value of a property.
* @param {*} value - The value to compare.
Expand Down
Loading
Loading