-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix grid resizer drag over embed #64098
Conversation
Size Change: -12 B (0%) Total Size: 1.76 MB
ℹ️ View Unchanged
|
onPointerDown={ ( { target, pointerId } ) => { | ||
/* | ||
* Captures the pointer to avoid hiccups while dragging over objects | ||
* like iframes and ensures that the event to end the drag is | ||
* captured by the target (resize handle) whether or not it’s under | ||
* the pointer. | ||
*/ | ||
target.setPointerCapture( pointerId ); | ||
} } |
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.
I think it’d nice to put this in ResizableBox
as it should be okay for any use case.
I gave it a quick try but typing it perfectly is an exercise I didn’t complete (had to use `@ts-ignore`).
diff --git a/packages/components/src/resizable-box/index.tsx b/packages/components/src/resizable-box/index.tsx
index 1b05270ea0..96c3044b7b 100644
--- a/packages/components/src/resizable-box/index.tsx
+++ b/packages/components/src/resizable-box/index.tsx
@@ -9,7 +9,7 @@ import { forwardRef } from '@wordpress/element';
import clsx from 'clsx';
import { Resizable } from 're-resizable';
import type { ResizableProps } from 're-resizable';
-import type { ReactNode, ForwardedRef } from 'react';
+import type { ReactNode, ForwardedRef, PointerEventHandler } from 'react';
/**
* Internal dependencies
@@ -92,6 +92,7 @@ type ResizableBoxProps = ResizableProps & {
showHandle?: boolean;
__experimentalShowTooltip?: boolean;
__experimentalTooltipProps?: Parameters< typeof ResizeTooltip >[ 0 ];
+ onPointerDown: PointerEventHandler;
};
function UnforwardedResizableBox(
@@ -116,6 +117,17 @@ function UnforwardedResizableBox(
handleStyles={ HANDLE_STYLES }
ref={ ref }
{ ...props }
+ // @ts-ignore -- Ignore that ResizableProps does not include `onPointerDown`
+ onPointerDown={ ( event ) => {
+ /*
+ * Captures the pointer to avoid hiccups while dragging over objects
+ * like iframes and ensures that the event to end the drag is
+ * captured by the target (resize handle) whether or not it’s under
+ * the pointer.
+ */
+ event.target.setPointerCapture( event.pointerId );
+ props.onPointerDown( event );
+ } }
>
{ children }
{ showTooltip && <ResizeTooltip { ...tooltipProps } /> }
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.
Ideally this would be fixed in re-resizable
but you could do something like this as a workaround:
diff --git a/packages/components/src/resizable-box/index.tsx b/packages/components/src/resizable-box/index.tsx
index 1b05270ea0b..28d3a7cea74 100644
--- a/packages/components/src/resizable-box/index.tsx
+++ b/packages/components/src/resizable-box/index.tsx
@@ -9,7 +9,7 @@ import { forwardRef } from '@wordpress/element';
import clsx from 'clsx';
import { Resizable } from 're-resizable';
import type { ResizableProps } from 're-resizable';
-import type { ReactNode, ForwardedRef } from 'react';
+import type { ReactNode, ForwardedRef, PointerEventHandler } from 'react';
/**
* Internal dependencies
@@ -87,11 +87,19 @@ const HANDLE_STYLES = {
bottomLeft: HANDLE_STYLES_OVERRIDES,
};
+declare module 're-resizable' {
+ // eslint-disable-next-line @typescript-eslint/no-shadow
+ interface ResizableProps {
+ onPointerDown: PointerEventHandler;
+ }
+}
+
type ResizableBoxProps = ResizableProps & {
children: ReactNode;
showHandle?: boolean;
__experimentalShowTooltip?: boolean;
__experimentalTooltipProps?: Parameters< typeof ResizeTooltip >[ 0 ];
+ onPointerDown: PointerEventHandler;
};
function UnforwardedResizableBox(
@@ -116,6 +124,11 @@ function UnforwardedResizableBox(
handleStyles={ HANDLE_STYLES }
ref={ ref }
{ ...props }
+ onPointerDown={ ( event ) => {
+ const target = event.target as Element;
+ target.setPointerCapture( event.pointerId );
+ props.onPointerDown( event );
+ } }
>
{ children }
{ showTooltip && <ResizeTooltip { ...tooltipProps } /> }
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.
Thanks for the tip!
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
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.
This is great! TIL about setPointerCapture
. Love that you've gotten rid of the once
hack, too.
Tested that this fixes the bug and there's no regression of #61668.
What?
A bug fix for grid resizing.
Why?
To fix #63279 where dragging to resize grid items is impeded if the grid item is an object/embed/iframe.
How?
Uses
setPointerCapture
Testing Instructions
No flaky drag over embed with iframe
No regression of stuck dragging when releasing the drag with the pointer off of the drag handle
Testing Instructions for Keyboard
Screenshots or screencast
grid-resizer-drag-over-embed.mp4