-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ View: | |
name: widgets | ||
height: 200px | ||
width: 900px | ||
draggable: true | ||
- Button: | ||
label: show dialog | ||
onTap: | ||
|
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, | ||
|
@@ -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; | ||
|
@@ -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} | ||
|
@@ -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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = ( | ||
<> | ||
|
@@ -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={{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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