2019-10-21 11:40:24 +00:00
|
|
|
import React, { useRef, useEffect } from "react";
|
2021-03-29 15:47:22 +00:00
|
|
|
import styled, { ThemeProvider } from "styled-components";
|
2019-10-21 11:40:24 +00:00
|
|
|
import { createPortal } from "react-dom";
|
2020-06-04 13:49:22 +00:00
|
|
|
import PopperJS, { Placement, PopperOptions } from "popper.js";
|
2021-03-29 15:47:22 +00:00
|
|
|
import { noop } from "utils/AppsmithUtils";
|
|
|
|
|
import { draggableElement } from "./utils";
|
|
|
|
|
import { ReactComponent as DragHandleIcon } from "assets/icons/ads/app-icons/draghandler.svg";
|
|
|
|
|
import { Colors } from "constants/Colors";
|
|
|
|
|
import { getThemeDetails, ThemeMode } from "selectors/themeSelectors";
|
|
|
|
|
import { AppState } from "reducers";
|
|
|
|
|
import { useSelector } from "react-redux";
|
2020-06-04 13:49:22 +00:00
|
|
|
|
2021-03-29 15:47:22 +00:00
|
|
|
export type PopperProps = {
|
2020-06-04 13:49:22 +00:00
|
|
|
zIndex: number;
|
2019-10-21 11:40:24 +00:00
|
|
|
isOpen: boolean;
|
2021-03-29 15:47:22 +00:00
|
|
|
themeMode?: ThemeMode;
|
2020-01-02 12:42:02 +00:00
|
|
|
targetNode?: Element;
|
2021-04-28 10:28:39 +00:00
|
|
|
children: JSX.Element | null;
|
2020-06-04 13:49:22 +00:00
|
|
|
placement: Placement;
|
|
|
|
|
modifiers?: Partial<PopperOptions["modifiers"]>;
|
2021-03-29 15:47:22 +00:00
|
|
|
isDraggable?: boolean;
|
|
|
|
|
disablePopperEvents?: boolean;
|
|
|
|
|
position?: {
|
|
|
|
|
top: number;
|
|
|
|
|
left: number;
|
|
|
|
|
};
|
|
|
|
|
onPositionChange?: (position: { top: number; left: number }) => void;
|
2019-10-21 11:40:24 +00:00
|
|
|
};
|
|
|
|
|
|
2020-06-04 13:49:22 +00:00
|
|
|
const PopperWrapper = styled.div<{ zIndex: number }>`
|
2020-12-24 04:32:25 +00:00
|
|
|
z-index: ${(props) => props.zIndex};
|
2019-10-21 11:40:24 +00:00
|
|
|
position: absolute;
|
|
|
|
|
`;
|
|
|
|
|
|
2021-03-29 15:47:22 +00:00
|
|
|
const DragHandleBlock = styled.div`
|
|
|
|
|
padding: 6px;
|
|
|
|
|
height: 28px;
|
|
|
|
|
background-color: ${(props) =>
|
|
|
|
|
props.theme.colors?.propertyPane?.bg || Colors.BLACK};
|
|
|
|
|
cursor: grab;
|
|
|
|
|
box-shadow: 0px 0px 2px rgb(0 0 0 / 10%), 0px 2px 10px rgb(0 0 0 / 10%);
|
|
|
|
|
clip-path: inset(-2px 0px -2px -2px);
|
|
|
|
|
`;
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
export function PopperDragHandle() {
|
2021-03-29 15:47:22 +00:00
|
|
|
return (
|
|
|
|
|
<DragHandleBlock>
|
|
|
|
|
<DragHandleIcon />
|
|
|
|
|
</DragHandleBlock>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2021-03-29 15:47:22 +00:00
|
|
|
|
2019-10-21 11:40:24 +00:00
|
|
|
/* eslint-disable react/display-name */
|
|
|
|
|
export default (props: PopperProps) => {
|
|
|
|
|
const contentRef = useRef(null);
|
2021-03-29 15:47:22 +00:00
|
|
|
const {
|
|
|
|
|
isDraggable = false,
|
|
|
|
|
disablePopperEvents = false,
|
|
|
|
|
position,
|
|
|
|
|
onPositionChange = noop,
|
|
|
|
|
themeMode = props.themeMode || ThemeMode.LIGHT,
|
|
|
|
|
} = props;
|
|
|
|
|
const popperTheme = useSelector((state: AppState) =>
|
|
|
|
|
getThemeDetails(state, themeMode),
|
|
|
|
|
);
|
2019-10-21 11:40:24 +00:00
|
|
|
useEffect(() => {
|
2020-01-02 12:42:02 +00:00
|
|
|
const parentElement = props.targetNode && props.targetNode.parentElement;
|
2019-12-27 10:10:04 +00:00
|
|
|
if (
|
|
|
|
|
parentElement &&
|
|
|
|
|
parentElement.parentElement &&
|
2020-01-02 12:42:02 +00:00
|
|
|
props.targetNode &&
|
2019-12-27 10:10:04 +00:00
|
|
|
props.isOpen
|
|
|
|
|
) {
|
2020-01-08 04:11:23 +00:00
|
|
|
// TODO: To further optimize this, we can go through the popper API
|
|
|
|
|
// and figure out a way to keep an app instance level popper instance
|
|
|
|
|
// which we can update to have different references when called here.
|
|
|
|
|
// However, the performance benefit gained by such an optimization
|
|
|
|
|
// remaines to be discovered.
|
2020-01-07 12:28:58 +00:00
|
|
|
const _popper = new PopperJS(
|
2020-01-02 12:42:02 +00:00
|
|
|
props.targetNode,
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
(contentRef.current as unknown) as Element,
|
|
|
|
|
{
|
2021-03-29 15:47:22 +00:00
|
|
|
...(isDraggable && disablePopperEvents
|
|
|
|
|
? {}
|
|
|
|
|
: { placement: props.placement }),
|
|
|
|
|
onCreate: (popperData) => {
|
|
|
|
|
const elementRef: any = popperData.instance.popper;
|
|
|
|
|
if (isDraggable && position) {
|
|
|
|
|
const initPositon =
|
|
|
|
|
position || elementRef.getBoundingClientRect();
|
|
|
|
|
elementRef.style.transform = "unset";
|
|
|
|
|
elementRef.style.top = initPositon.top + "px";
|
|
|
|
|
elementRef.style.left = initPositon.left + "px";
|
|
|
|
|
}
|
|
|
|
|
},
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
modifiers: {
|
|
|
|
|
flip: {
|
|
|
|
|
behavior: ["right", "left", "bottom", "top"],
|
|
|
|
|
},
|
|
|
|
|
keepTogether: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
|
|
|
|
arrow: {
|
|
|
|
|
enabled: false,
|
|
|
|
|
},
|
2020-01-15 04:49:36 +00:00
|
|
|
preventOverflow: {
|
|
|
|
|
enabled: true,
|
|
|
|
|
boundariesElement: "viewport",
|
|
|
|
|
},
|
2020-06-04 13:49:22 +00:00
|
|
|
...props.modifiers,
|
2019-10-21 11:40:24 +00:00
|
|
|
},
|
|
|
|
|
},
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
);
|
2021-03-29 15:47:22 +00:00
|
|
|
if (isDraggable) {
|
|
|
|
|
disablePopperEvents && _popper.disableEventListeners();
|
|
|
|
|
draggableElement(
|
|
|
|
|
"popper",
|
|
|
|
|
_popper.popper,
|
|
|
|
|
onPositionChange,
|
|
|
|
|
position,
|
|
|
|
|
() => (
|
|
|
|
|
<ThemeProvider theme={popperTheme}>
|
2021-04-28 10:28:39 +00:00
|
|
|
<PopperDragHandle />
|
2021-03-29 15:47:22 +00:00
|
|
|
</ThemeProvider>
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 12:28:58 +00:00
|
|
|
return () => {
|
|
|
|
|
_popper.destroy();
|
|
|
|
|
};
|
Property Pane Controls
- Fixes #121, #122, #123, #124, #90, #46, #65, #100, #101, #68, #102
2019-10-24 05:24:45 +00:00
|
|
|
}
|
2021-03-29 15:47:22 +00:00
|
|
|
}, [
|
|
|
|
|
props.targetNode,
|
|
|
|
|
props.isOpen,
|
|
|
|
|
props.modifiers,
|
|
|
|
|
props.placement,
|
|
|
|
|
disablePopperEvents,
|
|
|
|
|
]);
|
2019-10-21 11:40:24 +00:00
|
|
|
return createPortal(
|
2020-06-04 13:49:22 +00:00
|
|
|
<PopperWrapper ref={contentRef} zIndex={props.zIndex}>
|
|
|
|
|
{props.children}
|
|
|
|
|
</PopperWrapper>,
|
2019-10-21 11:40:24 +00:00
|
|
|
document.body,
|
|
|
|
|
);
|
|
|
|
|
};
|