-
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
List View: Allow dragging outside the immediate area by passing down a dropZoneElement #50726
Merged
andrewserong
merged 13 commits into
trunk
from
try/list-view-drop-zone-allow-passing-in-a-drop-zone-ref
May 24, 2023
Merged
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0480537
List View: Allow dragging outside the immediate area by passing down …
andrewserong d925f68
Fix nesting logic so it will only nest if hovering over the bottom ha…
andrewserong f501706
Update widget sidebar, too
andrewserong 9d55b5c
Update comments
andrewserong 65ffea3
Update comments and types
andrewserong 868c57e
Use setTarget directly in the callback
andrewserong 970217e
Fix issue where sometimes the expanded drop zone area was not being r…
andrewserong 84ac583
Add tests for useDropZone
andrewserong 4eb834d
Update dependency array, we only need to look at the current key
andrewserong 81b78ee
Swap out dropZoneRef for dropZoneElement
andrewserong c5c1e61
Fix typo
andrewserong 6b362a8
Add README file
andrewserong 5ce23fa
Update packages/compose/src/hooks/use-drop-zone/README.md
andrewserong 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 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 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 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 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 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,63 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useDropZone from '../'; | ||
|
||
describe( 'useDropZone', () => { | ||
const ComponentWithWrapperDropZone = () => { | ||
const outerRef = useRef(); | ||
const dropZoneRef = useDropZone( { | ||
dropZoneRef: outerRef, | ||
} ); | ||
|
||
return ( | ||
<div role="main" ref={ outerRef }> | ||
<div role="region" ref={ dropZoneRef }> | ||
<div>Drop Zone</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const ComponentWithoutWrapperDropZone = () => { | ||
const dropZoneRef = useDropZone( {} ); | ||
|
||
return ( | ||
<div role="main"> | ||
<div role="region" ref={ dropZoneRef }> | ||
<div>Drop Zone</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
it( 'will attach dropzone to outer wrapper', () => { | ||
const { rerender } = render( <ComponentWithWrapperDropZone /> ); | ||
// Ensure `useEffect` has run. | ||
rerender( <ComponentWithWrapperDropZone /> ); | ||
|
||
expect( screen.getByRole( 'main' ) ).toHaveAttribute( | ||
'data-is-drop-zone' | ||
); | ||
} ); | ||
|
||
it( 'will attach dropzone to element with dropZoneRef attached', () => { | ||
const { rerender } = render( <ComponentWithoutWrapperDropZone /> ); | ||
// Ensure `useEffect` has run. | ||
rerender( <ComponentWithoutWrapperDropZone /> ); | ||
|
||
expect( screen.getByRole( 'region' ) ).toHaveAttribute( | ||
'data-is-drop-zone' | ||
); | ||
} ); | ||
} ); |
This file contains 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 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 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
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.
I'm wondering how this will work in terms of the cleanup of the hook.
Because it's a ref effect, I think the callback will typically only trigger when
elem
is unmounted, not whendropZoneRef.current
is unmounted.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.
Good question! I think this is working pretty well so far. To confirm I logged out
element
within the callback, and it appears to be the right element:As far as I can tell the behaviour is something like the following:
useDropZone
, butuseDropZone
also returns aref
. Thatref
is still used in the list view, to attach to theTreeGrid
component.TreeGrid
unmounts, that results in the cleanup callback being called.element
being the one passed in bydropZoneRef
the cleanup callback cleans up the passed in element.dropZoneRef?.current
in the dependencies array ofuseRefEffect
when the passed in ref changes, the cleanup appears to correctly fire, too. From checking the docs, this appears to be consistent with how useEffect works in that the cleanup fires not only when the component unmounts, but whenever any of the dependencies change. The doc comment foruseRefEffect
appears to confirm this here, too:gutenberg/packages/compose/src/hooks/use-ref-effect/index.ts
Lines 11 to 29 in 62b3b0e
So, all up, I don't think there are any stray event listeners not being removed, as far as I can tell.
Do let me know if you think I've missed anything here, though!
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.
Sorry if I'm making this more complicated. Given this is being introduced as a generic stable API, I'm mainly thinking that there could be different ways it's used (or misused):
dropZoneRef
could reference a completely separate part of the DOM that isn't mounted/unmounted at the same time as the effect's component.dropZoneRef
could reference a parent of the effect's component that isn't mounted/unmounted at the same time.I don't know if there are tests that can be added to assert these different cases work as expected.
It also might be worth expanding upon the docs for the prop to ensure that the right usage of this API is promoted. There's an example of this for Popover's
anchor
prop that recommends particular usage - https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/popover/README.md#anchor-element--virtualelement--null.This is the part that often trips me up. With
dropZoneRef
being a ref type, it won't trigger a re-render when it changes. I've sometimes opted to store an element usinguseState
for that reason (which is also whatPopover
recommends).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.
No apology necessary! I'm happy to go as slowly as needed with this PR — I always like to be a bit more cautious when making changes to lower-level packages such as
compose
, and it's important we get it right and don't introduce any issues 👍Good example use cases, it's worth digging in, I'll do a little more exploration to see if it's possible to come up with additional test cases. I think expanding the test cases there is probably a good path toward building confidence around the API change.
Good idea, for some reason
useDropzone
isn't documented in thecompose
package's readme, but I'll have a go at writing an entry for it.Ah, good point, because the ref reference itself isn't changing, only
current
🤔. In practice, I haven't yet run into any issues in this PR so far. In terms of usinguseState
, do you mean we'd do that internally withinuseDropZone
, or are you imagining that the consumer would useuseState
, and we'd pass the element directly touseDropZone
instead of aref
?Edit: I think you probably mean the latter — and I can see that the url popover for example, uses the state approach here:
gutenberg/packages/block-editor/src/components/url-popover/image-url-input-ui.js
Line 251 in 493eac0
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.
Yep, exactly that 👍
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.
Update: I've done the following:
dropZoneRef
for adropZoneElement
and updated usage to use state for storing the element before passing it along to theListView
(and then on touseDropZone
)useDropZone
— because the hook is still currently exported as experimental, I couldn't add directly to thecompose
package readme. I see a couple of other experimental hooks also include their own README files in thecompose
package, so this seemed like a decent place to store the documentation for now, while the hook is still "experimental". Happy for any feedback there, I found it a bit challenging coming up with straightforward wording 😅I haven't looked into exploring adding other tests just yet, but will dig in tomorrow.
Thanks again for the feedback!
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, didn't realise it was experimental. It must have been that way for ages!
My hunch is that using state to store the element will probably solve the issues, hopefully that's the case.