-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
List View: Allow dragging outside the immediate area by passing down …
…a dropZoneElement (#50726) * List View: Allow dragging outside the immediate area by passing down a dropZoneRef from outside the list view * Fix nesting logic so it will only nest if hovering over the bottom half of the item * Update widget sidebar, too * Update comments * Update comments and types * Use setTarget directly in the callback * Fix issue where sometimes the expanded drop zone area was not being respected * Add tests for useDropZone * Update dependency array, we only need to look at the current key * Swap out dropZoneRef for dropZoneElement * Fix typo * Add README file * Update packages/compose/src/hooks/use-drop-zone/README.md Co-authored-by: Daniel Richards <daniel.richards@automattic.com> --------- Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
- Loading branch information
1 parent
19594cd
commit 66f94d8
Showing
9 changed files
with
210 additions
and
17 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# useDropZone (experimental) | ||
|
||
A hook to facilitate drag and drop handling within a designated drop zone area. An optional `dropZoneElement` can be provided, however by default the drop zone is bound by the area where the returned `ref` is assigned. | ||
|
||
When using a `dropZoneElement`, it is expected that the `ref` will be attached to a node that is a descendent of the `dropZoneElement`. Additionally, the element passed to `dropZoneElement` should be stored in state rather than a plain ref to ensure reactive updating when it changes. | ||
|
||
## Usage | ||
|
||
```js | ||
import { useDropZone } from '@wordpress/compose'; | ||
import { useState } from '@wordpress/element'; | ||
|
||
const WithWrapperDropZoneElement = () => { | ||
const [ dropZoneElement, setDropZoneElement ] = useState( null ); | ||
|
||
const dropZoneRef = useDropZone( | ||
{ | ||
dropZoneElement, | ||
onDrop() => { | ||
console.log( 'Dropped within the drop zone.' ); | ||
}, | ||
onDragEnter() => { | ||
console.log( 'Dragging within the drop zone' ); | ||
} | ||
} | ||
) | ||
|
||
return ( | ||
<div className="outer-wrapper" ref={ setDropZoneElement }> | ||
<div ref={ dropZoneRef }> | ||
<p>Drop Zone</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const WithoutWrapperDropZoneElement = () => { | ||
const dropZoneRef = useDropZone( | ||
{ | ||
onDrop() => { | ||
console.log( 'Dropped within the drop zone.' ); | ||
}, | ||
onDragEnter() => { | ||
console.log( 'Dragging within the drop zone' ); | ||
} | ||
} | ||
) | ||
|
||
return ( | ||
<div ref={ dropZoneRef }> | ||
<p>Drop Zone</p> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Parameters | ||
|
||
- _props_ `Object`: Named parameters. | ||
- _props.dropZoneElement_ `HTMLElement`: Optional element to be used as the drop zone. | ||
- _props.isDisabled_ `boolean`: Whether or not to disable the drop zone. | ||
- _props.onDragStart_ `( e: DragEvent ) => void`: Called when dragging has started. | ||
- _props.onDragEnter_ `( e: DragEvent ) => void`: Called when the zone is entered. | ||
- _props.onDragOver_ `( e: DragEvent ) => void`: Called when the zone is moved within. | ||
- _props.onDragLeave_ `( e: DragEvent ) => void`: Called when the zone is left. | ||
- _props.onDragEnd_ `( e: MouseEvent ) => void`: Called when dragging has ended. | ||
- _props.onDrop_ `( e: DragEvent ) => void`: Called when dropping in the zone. | ||
|
||
_Returns_ | ||
|
||
- `RefCallback< HTMLElement >`: Ref callback to be passed to the drop zone element. |
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 { useState } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useDropZone from '../'; | ||
|
||
describe( 'useDropZone', () => { | ||
const ComponentWithWrapperDropZone = () => { | ||
const [ dropZoneElement, setDropZoneElement ] = useState( null ); | ||
const dropZoneRef = useDropZone( { | ||
dropZoneElement, | ||
} ); | ||
|
||
return ( | ||
<div role="main" ref={ setDropZoneElement }> | ||
<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
66f94d8
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.
Flaky tests detected in 66f94d8.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5074167024
📝 Reported issues:
No Behaviors
should be the default as defined in the core theme.json #50923 in/test/e2e/specs/editor/various/behaviors.spec.js
/test/e2e/specs/site-editor/style-book.spec.js