-
Notifications
You must be signed in to change notification settings - Fork 28
Make all Studio modals draggable by their header #3931
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
assets/js/src/core/components/modal/hooks/draggable-modal-render.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /** | ||
| * This source file is available under the terms of the | ||
| * Pimcore Open Core License (POCL) | ||
| * Full copyright and license information is available in | ||
| * LICENSE.md which is distributed with this source code. | ||
| * | ||
| * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) | ||
| * @license Pimcore Open Core License (POCL) | ||
| */ | ||
|
|
||
| import React from 'react' | ||
| import { useDraggableModal, type UseDraggableModalOptions } from './use-draggable-modal' | ||
|
|
||
| export interface DraggableModalRenderProps extends Pick<UseDraggableModalOptions, 'handle' | 'cancel' | 'disabled'> { | ||
| children: React.ReactNode | ||
| } | ||
|
|
||
| /** | ||
| * Component wrapper around {@link useDraggableModal} for the imperative modal | ||
| * API (`modal.confirm` / `modal.info` / …), whose `modalRender` callback needs | ||
| * to return an element that can own its own drag state. Each open imperative | ||
| * modal is destroyed on close, so state resets naturally without an `open` | ||
| * prop. | ||
| * | ||
| * Compose with any caller-provided `modalRender`: | ||
| * modalRender: (node) => <DraggableModalRender>{userRender(node)}</DraggableModalRender> | ||
| */ | ||
| export const DraggableModalRender = ({ children, ...options }: DraggableModalRenderProps): React.JSX.Element => { | ||
| const { modalRender } = useDraggableModal(options) | ||
| return <>{modalRender(children)}</> | ||
| } | ||
|
|
||
| /** | ||
| * Wraps a `ModalFuncProps.modalRender` value so the imperative modal becomes | ||
| * draggable, preserving any existing render (e.g. autofocus behaviour). | ||
| */ | ||
| export const withDraggableModalRender = ( | ||
| existingRender?: (node: React.ReactNode) => React.ReactNode | ||
| ): ((node: React.ReactNode) => React.ReactNode) => { | ||
| const renderDraggableModal = (node: React.ReactNode): React.ReactNode => ( | ||
| <DraggableModalRender> | ||
| {existingRender !== undefined ? existingRender(node) : node} | ||
| </DraggableModalRender> | ||
| ) | ||
| renderDraggableModal.displayName = 'DraggableModalRenderCallback' | ||
|
|
||
| return renderDraggableModal | ||
| } |
26 changes: 26 additions & 0 deletions
26
assets/js/src/core/components/modal/hooks/use-draggable-modal.styles.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * This source file is available under the terms of the | ||
| * Pimcore Open Core License (POCL) | ||
| * Full copyright and license information is available in | ||
| * LICENSE.md which is distributed with this source code. | ||
| * | ||
| * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) | ||
| * @license Pimcore Open Core License (POCL) | ||
| */ | ||
|
|
||
| import { createStyles } from '@Pimcore/modules/ant-design/styles/create-styles' | ||
|
|
||
| export const useStyle = createStyles(({ css }) => { | ||
| return { | ||
| // Applied to the draggable wrapper around the modal content. Scopes the | ||
| // "grab" affordance to the header/title so the body keeps normal cursors, | ||
| // and prevents accidental text selection while dragging by the header. | ||
| draggableWrapper: css` | ||
| .ant-modal-header, | ||
| .ant-modal-confirm-title { | ||
| cursor: move; | ||
| user-select: none; | ||
| } | ||
| ` | ||
| } | ||
| }, { hashPriority: 'low' }) |
126 changes: 126 additions & 0 deletions
126
assets/js/src/core/components/modal/hooks/use-draggable-modal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * This source file is available under the terms of the | ||
| * Pimcore Open Core License (POCL) | ||
| * Full copyright and license information is available in | ||
| * LICENSE.md which is distributed with this source code. | ||
| * | ||
| * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) | ||
| * @license Pimcore Open Core License (POCL) | ||
| */ | ||
|
|
||
| import React, { useCallback, useEffect, useRef, useState } from 'react' | ||
| import Draggable, { type DraggableData, type DraggableEvent } from 'react-draggable' | ||
| import cn from 'classnames' | ||
| import { useStyle } from './use-draggable-modal.styles' | ||
|
|
||
| /** | ||
| * CSS selectors (relative to the modal content) that initiate a drag. | ||
| * Covers both the standard modal header and the imperative confirm/alert title. | ||
| */ | ||
| const DEFAULT_HANDLE = '.ant-modal-header, .ant-modal-confirm-title' | ||
|
|
||
| /** | ||
| * CSS selectors inside the handle that must NOT start a drag, so interactive | ||
| * header content (close button, links, form controls placed in the title) | ||
| * keeps working. `react-draggable` matches these against the mousedown target. | ||
| */ | ||
| const DEFAULT_CANCEL = '.ant-modal-close, button, a, input, textarea, select, [role="button"], .ant-select, .ant-input, .ant-input-number, .ant-picker' | ||
|
|
||
| export interface UseDraggableModalOptions { | ||
| /** When false the modal renders untouched (no drag behaviour, no wrapper). */ | ||
| disabled?: boolean | ||
| /** | ||
| * Controlled open state of the modal. When provided, the drag position is | ||
| * reset to origin every time the modal opens, so a modal that was dragged | ||
| * away does not reappear off-centre on the next open. Imperative modals are | ||
| * destroyed on close, so they get a fresh position automatically and can | ||
| * omit this. | ||
| */ | ||
| open?: boolean | ||
| /** Override the drag-handle selector. */ | ||
| handle?: string | ||
| /** Override the non-draggable selector. */ | ||
| cancel?: string | ||
| /** Extra class(es) merged onto the draggable wrapper element. */ | ||
| wrapperClassName?: string | ||
| } | ||
|
|
||
| export interface UseDraggableModalResult { | ||
| /** Pass to antd `Modal`'s / `ModalFuncProps`' `modalRender`. */ | ||
| modalRender: (modal: React.ReactNode) => React.ReactNode | ||
| /** The draggable container element; useful as a `getPopupContainer` target. */ | ||
| nodeRef: React.RefObject<HTMLDivElement> | ||
| } | ||
|
|
||
| /** | ||
| * Shared drag-by-header behaviour for every Studio modal. Wraps the modal | ||
| * content in `react-draggable` using the standard antd recipe (viewport bounds | ||
| * recomputed on drag start, `nodeRef` to stay React-18/19 safe) and drives the | ||
| * position in controlled mode so it can be reset between opens. | ||
| */ | ||
| export const useDraggableModal = (options: UseDraggableModalOptions = {}): UseDraggableModalResult => { | ||
| const { | ||
| disabled = false, | ||
| open, | ||
| handle = DEFAULT_HANDLE, | ||
| cancel = DEFAULT_CANCEL, | ||
| wrapperClassName | ||
| } = options | ||
|
|
||
| const { styles } = useStyle() | ||
| const nodeRef = useRef<HTMLDivElement>(null) | ||
| const [bounds, setBounds] = useState({ left: 0, top: 0, bottom: 0, right: 0 }) | ||
| const [position, setPosition] = useState({ x: 0, y: 0 }) | ||
|
|
||
| // Reset to origin (centred) whenever the modal transitions to open. | ||
| useEffect(() => { | ||
| if (open === true) { | ||
| setPosition({ x: 0, y: 0 }) | ||
| } | ||
| }, [open]) | ||
|
|
||
| const onStart = useCallback((_event: DraggableEvent, uiData: DraggableData): void => { | ||
| const { clientWidth, clientHeight } = window.document.documentElement | ||
| const targetRect = nodeRef.current?.getBoundingClientRect() | ||
| if (targetRect === undefined || targetRect === null) { | ||
| return | ||
| } | ||
| setBounds({ | ||
| left: -targetRect.left + uiData.x, | ||
| right: clientWidth - (targetRect.right - uiData.x), | ||
| top: -targetRect.top + uiData.y, | ||
| bottom: clientHeight - (targetRect.bottom - uiData.y) | ||
| }) | ||
| }, []) | ||
|
|
||
| const onDrag = useCallback((_event: DraggableEvent, uiData: DraggableData): void => { | ||
| setPosition({ x: uiData.x, y: uiData.y }) | ||
| }, []) | ||
|
|
||
| const modalRender = useCallback((modal: React.ReactNode): React.ReactNode => { | ||
| if (disabled) { | ||
| return modal | ||
| } | ||
|
|
||
| return ( | ||
| <Draggable | ||
| bounds={ bounds } | ||
| cancel={ cancel } | ||
| handle={ handle } | ||
| nodeRef={ nodeRef } | ||
| onDrag={ onDrag } | ||
| onStart={ onStart } | ||
| position={ position } | ||
| > | ||
| <div | ||
| className={ cn(styles.draggableWrapper, wrapperClassName) } | ||
| ref={ nodeRef } | ||
| > | ||
| {modal} | ||
| </div> | ||
| </Draggable> | ||
| ) | ||
| }, [bounds, cancel, handle, disabled, position, onDrag, onStart, styles.draggableWrapper, wrapperClassName]) | ||
|
|
||
| return { modalRender, nodeRef } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.