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

Added the draggable property to modals #793

Open
wants to merge 1 commit into
base: main
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
6 changes: 6 additions & 0 deletions .changeset/orange-walls-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ensembleui/react-framework": patch
"@ensembleui/react-kitchen-sink": patch
---

Added the draggable property to modals
1 change: 1 addition & 0 deletions apps/kitchen-sink/src/ensemble/screens/forms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ View:
name: widgets
height: 200px
width: 900px
draggable: true
- Button:
label: show dialog
onTap:
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@
"runes2": "^1.1.4",
"tsconfig": "workspace:*",
"tsup": "^7.2.0"
},
"dependencies": {
"react-draggable": "^4.4.6"
Comment on lines +60 to +61
Copy link
Contributor

Choose a reason for hiding this comment

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

Please move to dev dependencies

}
}
43 changes: 42 additions & 1 deletion packages/runtime/src/runtime/modal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Modal } from "antd";
import type { PropsWithChildren } from "react";
import type { PropsWithChildren, ReactNode } from "react";
import {
createContext,
useCallback,
Expand All @@ -17,11 +17,16 @@ import { CloseOutlined } from "@ant-design/icons";
import { generateRandomString, useEvaluate } from "@ensembleui/react-framework";
import { isString, omit, pick } from "lodash-es";
import { useNavigate } from "react-router-dom";
import Draggable, {
type DraggableData,
type DraggableEvent,
} from "react-draggable";
import { getComponentStyles } from "../../shared/styles";
import { getCustomStyles, getFullScreenStyles } from "./utils";

export interface ModalProps {
title?: string | React.ReactNode;
draggable?: boolean;
maskClosable?: boolean;
mask?: boolean;
hideCloseIcon?: boolean;
Expand Down Expand Up @@ -284,6 +289,14 @@ export const ModalWrapper: React.FC<PropsWithChildren> = ({ children }) => {
</div>
);

const draggleRef = useRef<HTMLDivElement>(null);
const [bounds, setBounds] = useState({
left: 0,
top: 0,
bottom: 0,
right: 0,
});

return (
<ModalContext.Provider value={modalContext}>
{children}
Expand All @@ -292,6 +305,33 @@ export const ModalWrapper: React.FC<PropsWithChildren> = ({ children }) => {
if (!modal.visible) {
return null;
}
const modalRender = (modalBody: ReactNode) => {
return modal.options.draggable ? (
<Draggable
bounds={bounds}
disabled={false}
nodeRef={draggleRef}
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens if you have multiple draggable modals?

onStart={(_event: DraggableEvent, uiData: DraggableData) => {
const { clientWidth, clientHeight } =
window.document.documentElement;
const targetRect = draggleRef.current?.getBoundingClientRect();
if (!targetRect) {
return;
}
setBounds({
left: -targetRect.left + uiData.x,
right: clientWidth - (targetRect.right - uiData.x),
top: -targetRect.top + uiData.y,
bottom: clientHeight - (targetRect.bottom - uiData.y),
Comment on lines +321 to +325
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do? Would be nice to have a comment here.

});
}}
>
<div ref={draggleRef}>{modalBody}</div>
</Draggable>
) : (
<div ref={draggleRef}>{modalBody}</div>
);
};
const { options, key } = modal;
const modalContent = (
<>
Expand All @@ -307,6 +347,7 @@ export const ModalWrapper: React.FC<PropsWithChildren> = ({ children }) => {
key={modal.key}
mask={options.mask}
maskClosable={options.maskClosable}
modalRender={modalRender}
onCancel={(): void => closeModal(index)}
open={modal.visible}
style={{
Expand Down
8 changes: 3 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading