-
Notifications
You must be signed in to change notification settings - Fork 359
feat(dialog): add sizeDraggable to props #3875
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
Open
FlowerBlackG
wants to merge
12
commits into
Tencent:develop
Choose a base branch
from
FlowerBlackG:feat/sizeDraggableDialog
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fb20b7b
feat(dialog): add sizeDraggable to props
FlowerBlackG 8b53c6f
style(dialog): lint
FlowerBlackG 5bfe008
fix(dialog): rename SizeDragLimit to DialogSizeDragLimit so it won't …
FlowerBlackG 67bc72b
chore(dialog): fix docs
FlowerBlackG 0145f5f
Merge branch 'Tencent:develop' into feat/sizeDraggableDialog
FlowerBlackG c049de5
fix(dialog): no detect border when rendered somewhere strange
FlowerBlackG ce96eb5
Update packages/components/dialog/hooks/useDialogResize.ts
FlowerBlackG dc558d6
fix(dialog): set default minWidth to 0 (useDialogResize)
FlowerBlackG 51ad547
fix(dialog): fix a bug that resizer might change cursor style when dr…
FlowerBlackG 1b33898
Merge branch 'feat/sizeDraggableDialog' of github.com:FlowerBlackG/td…
FlowerBlackG de1e364
fix(dialog): change some != to !==
FlowerBlackG ad769b8
fix(dialog): restore "lazy: true" to defaultProps
FlowerBlackG 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| import React from "react"; | ||
| import useMouseEvent from "../../hooks/useMouseEvent"; | ||
| import { DialogSizeDragLimit } from "../type"; | ||
|
|
||
|
|
||
| const RESIZE_BORDER_WIDTH = 8; // 边框宽度,拖拽时的感应区域 | ||
|
|
||
|
|
||
| interface DialogResizeProps { | ||
| dialogCardRef: React.MutableRefObject<HTMLDivElement | null>; | ||
| sizeDraggableProps: boolean | DialogSizeDragLimit; | ||
| onDragResizeChange: (resizing: boolean) => void; | ||
| } | ||
|
|
||
|
|
||
| type ResizeDirection = 'n' | 's' | 'e' | 'w' | 'ne' | 'nw' | 'se' | 'sw' | false; | ||
|
|
||
|
|
||
| function mouseOnBorder( | ||
| e: React.MouseEvent, | ||
| dialogCardRef: React.MutableRefObject<HTMLDivElement | null> | ||
| ): ResizeDirection { | ||
| const mouseX = e.clientX; | ||
| const mouseY = e.clientY; | ||
| const { left, top, width, height } = dialogCardRef.current.getBoundingClientRect(); | ||
|
|
||
| const borderWidth = RESIZE_BORDER_WIDTH; | ||
| const onLeftBorder = mouseX >= left - borderWidth && mouseX <= left + borderWidth; | ||
| const onRightBorder = mouseX >= left + width - borderWidth && mouseX <= left + width + borderWidth; | ||
| const onTopBorder = mouseY >= top - borderWidth && mouseY <= top + borderWidth; | ||
| const onBottomBorder = mouseY >= top + height - borderWidth && mouseY <= top + height + borderWidth; | ||
| if (onLeftBorder && onTopBorder) | ||
| return 'nw'; | ||
| if (onRightBorder && onBottomBorder) | ||
| return 'se'; | ||
| if (onRightBorder && onTopBorder) | ||
| return 'ne'; | ||
| if (onLeftBorder && onBottomBorder) | ||
| return 'sw'; | ||
| if (onLeftBorder) | ||
| return 'w'; | ||
| if (onRightBorder) | ||
| return 'e'; | ||
| if (onTopBorder) | ||
| return 'n'; | ||
| if (onBottomBorder) | ||
| return 's'; | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| const useDialogResize = (props: DialogResizeProps) => { | ||
| const { dialogCardRef, sizeDraggableProps } = props; | ||
|
|
||
| const validWindow = typeof window === 'object'; | ||
|
|
||
| const getWindowHeight = () => (validWindow ? window.innerHeight || document.documentElement.clientHeight : undefined); | ||
| const getWindowWidth = () => (validWindow ? window.innerWidth || document.documentElement.clientWidth : undefined); | ||
|
|
||
| const dialogSize = React.useRef({ x: 0, y: 0, width: 0, height: 0 }); | ||
|
|
||
| const resizingDirection = React.useRef<ResizeDirection>(false); | ||
|
|
||
| const minWidth = React.useRef(0); | ||
| const maxWidth = React.useRef(Number.MAX_VALUE); | ||
| const minHeight = React.useRef<number | undefined>(undefined); // If undefined, set to current height on first drag (lower than which causes buttons overflow). | ||
| const maxHeight = React.useRef(Number.MAX_VALUE); | ||
|
|
||
|
|
||
| React.useEffect(() => { | ||
| if (sizeDraggableProps === undefined || typeof sizeDraggableProps === 'boolean') { | ||
| minWidth.current = 0; | ||
| maxWidth.current = Number.MAX_VALUE; | ||
| minHeight.current = 0; | ||
| maxHeight.current = Number.MAX_VALUE; | ||
| return; | ||
| } | ||
|
|
||
| if (sizeDraggableProps.maxHeight !== undefined) { | ||
| maxHeight.current = sizeDraggableProps.maxHeight; | ||
| } | ||
|
|
||
| if (sizeDraggableProps.minHeight !== undefined) { | ||
| minHeight.current = sizeDraggableProps.minHeight; | ||
| } | ||
|
|
||
| if (sizeDraggableProps.maxWidth !== undefined) { | ||
| maxWidth.current = sizeDraggableProps.maxWidth; | ||
| } | ||
|
|
||
| if (sizeDraggableProps.minWidth !== undefined) { | ||
| minWidth.current = sizeDraggableProps.minWidth; | ||
| } | ||
|
|
||
| }, [sizeDraggableProps]); | ||
|
|
||
|
|
||
| useMouseEvent(dialogCardRef, { | ||
| enabled: sizeDraggableProps !== undefined && sizeDraggableProps !== false, | ||
| alwaysEmitOnMove: sizeDraggableProps !== undefined && sizeDraggableProps !== false, | ||
| onDown: (e: React.MouseEvent) => { | ||
| if (minHeight.current === undefined && dialogCardRef.current) { | ||
| minHeight.current = dialogCardRef.current.offsetHeight; | ||
| } | ||
|
|
||
| resizingDirection.current = mouseOnBorder(e, dialogCardRef); | ||
| if (resizingDirection.current !== false) { | ||
| dialogSize.current = { | ||
| x: dialogCardRef.current.offsetLeft, | ||
| y: dialogCardRef.current.offsetTop, | ||
| width: dialogCardRef.current.offsetWidth, | ||
| height: dialogCardRef.current.offsetHeight, | ||
| }; | ||
|
|
||
| props.onDragResizeChange(true); | ||
|
|
||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
| } | ||
| }, | ||
| onMove: (e: React.MouseEvent) => { | ||
| if (dialogCardRef.current.style.cursor === 'move') | ||
| return; | ||
|
|
||
| // Check whether we should update cursor style. | ||
| const direction = mouseOnBorder(e, dialogCardRef); | ||
| if (direction) { | ||
| let cursor = ''; | ||
| if (direction === 'n' || direction === 's') | ||
| cursor = `${direction}-resize`; | ||
| else if (direction === 'e' || direction === 'w') | ||
| cursor = `${direction}-resize`; | ||
| else if (direction === 'ne' || direction === 'sw') | ||
| cursor = 'nesw-resize'; | ||
| else if (direction === 'nw' || direction === 'se') | ||
| cursor = 'nwse-resize'; | ||
| dialogCardRef.current.style.cursor = cursor; | ||
| } else if (resizingDirection.current === false) | ||
| dialogCardRef.current.style.cursor = 'default'; | ||
|
|
||
| if (resizingDirection.current === false) | ||
| return; | ||
|
|
||
|
|
||
| // Do resize. | ||
| const dir = resizingDirection.current; | ||
| const {style} = dialogCardRef.current; | ||
|
|
||
| style.position = 'absolute'; | ||
|
|
||
| const mouseX = e.clientX; | ||
| const mouseY = e.clientY; | ||
|
|
||
| if (mouseX <= 4 || mouseY <= 4 || mouseX >= getWindowWidth() - 4 || mouseY >= getWindowHeight() - 4) { | ||
| return; // Just ignore. | ||
| } | ||
|
|
||
| let nextHeight = dialogCardRef.current.offsetHeight; | ||
| let nextWidth = dialogCardRef.current.offsetWidth; | ||
| let nextTop = dialogCardRef.current.offsetTop; | ||
| let nextLeft = dialogCardRef.current.offsetLeft; | ||
|
|
||
|
|
||
| if (dir.includes('n')) { // upper | ||
| const deltaY = mouseY - dialogSize.current.y; | ||
| nextHeight = dialogSize.current.height - deltaY; | ||
| nextTop = dialogSize.current.y + deltaY; | ||
| } | ||
| else if (dir.includes('s')) { // lower | ||
| const deltaY = mouseY - dialogSize.current.y - dialogSize.current.height; | ||
| nextHeight = dialogSize.current.height + deltaY; | ||
| } | ||
|
|
||
| if (dir.includes('w')) { // left | ||
| const deltaX = mouseX - dialogSize.current.x; | ||
| nextWidth = dialogSize.current.width - deltaX; | ||
| nextLeft = dialogSize.current.x + deltaX; | ||
| } | ||
| else if (dir.includes('e')) { // right | ||
| const deltaX = mouseX - dialogSize.current.x - dialogSize.current.width; | ||
| nextWidth = dialogSize.current.width + deltaX; | ||
| } | ||
|
|
||
|
|
||
| style.left = `${nextLeft}px`; | ||
| style.width = `${nextWidth}px`; | ||
| dialogSize.current.x = nextLeft; | ||
| dialogSize.current.width = nextWidth; | ||
|
|
||
|
|
||
| if (nextHeight >= minHeight.current) { | ||
| style.top = `${nextTop}px`; | ||
| style.height = `${nextHeight}px`; | ||
| dialogSize.current.y = nextTop; | ||
| dialogSize.current.height = nextHeight; | ||
| } | ||
|
|
||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
|
|
||
| }, | ||
| onUp: (e: React.MouseEvent) => { | ||
|
|
||
| if (resizingDirection.current === false) | ||
| return; | ||
|
|
||
| resizingDirection.current = false; | ||
|
|
||
| // prevent triggering overlay click. | ||
| setTimeout(() => { | ||
| props.onDragResizeChange(false); | ||
| }, 0); | ||
|
|
||
| e.stopPropagation(); | ||
| e.preventDefault(); | ||
| }, | ||
| }); | ||
| }; | ||
|
|
||
| export default useDialogResize; | ||
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.
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.
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.
Inconsistent handling of undefined values. Line 72 sets
minWidth.current = undefinedbut line 74 setsminHeight.current = 0. Both should handle undefined consistently or there should be a clear reason for the difference.