-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: break out react-dnd's internal package structure * chore: cut major semver due to re-layout * fix: remove extraneous deps * fix: jest tests * chore: cut semver * fix: tsconfig updates * refactor: roll pkgs back into react-dnd using a flattened folder structure * refactor: update DragDropManager construction; fix tests/linting * chore: update semver document * ci: remove redundant build script * build: nvm * fix: top-level build steps * docs: brush up some api doc pages * docs: move monitoring state towards bottom of article list * chore: cut semver * docs: update alex config
- Loading branch information
1 parent
0f6e8c5
commit a850aa0
Showing
22 changed files
with
387 additions
and
157 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"allow": ["hook", "hooks"] | ||
} |
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,2 @@ | ||
declined: | ||
- react-dnd-documentation |
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
File renamed without changes.
File renamed without changes.
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
34 changes: 34 additions & 0 deletions
34
packages/docsite/markdown/docs/03 Using Hooks/DndProvider.md
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,34 @@ | ||
--- | ||
path: '/docs/api/dnd-provider' | ||
title: 'DndProvider' | ||
--- | ||
|
||
_New to React DnD? [Read the overview](/docs/overview) before jumping into the docs._ | ||
|
||
# DndProvider | ||
|
||
The DndProvider component provides React-DnD capabilities to your application. This must be | ||
injected with a backend via the `backend` prop, but it may be injected with a `window` object. | ||
|
||
### Usage | ||
|
||
```jsx | ||
import { HTML5Backend } from 'react-dnd-html5-backend' | ||
import { DndProvider } from 'react-dnd' | ||
|
||
export default class YourApp { | ||
render() { | ||
return ( | ||
<DndProvider backend={HTML5Backend}> | ||
/* Your Drag-and-Drop Application */ | ||
</DndProvider> | ||
) | ||
} | ||
} | ||
``` | ||
|
||
### Props | ||
|
||
- **`backend`**: Required. A React DnD backend. Unless you're writing a custom one, you probably want to use the [HTML5 backend](/docs/backends/html5) that ships with React DnD. | ||
- **`context`**: Optional. The backend context used to configure the backend. This is dependent on the backend implementation. | ||
- **`options`**: Optional. An options object used to configure the backend. This is dependent on the backend implementation. |
36 changes: 36 additions & 0 deletions
36
packages/docsite/markdown/docs/03 Using Hooks/DragPreviewImage.md
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,36 @@ | ||
--- | ||
path: '/docs/api/drag-preview-image' | ||
title: 'DragPreviewImage' | ||
--- | ||
|
||
_New to React DnD? [Read the overview](/docs/overview) before jumping into the docs._ | ||
|
||
# DragPreviewImage | ||
|
||
A Component to render an HTML Image element as a disconnected drag preview. | ||
|
||
### Usage | ||
|
||
```jsx | ||
import { DragSource, DragPreviewImage } from 'react-dnd' | ||
|
||
function DraggableHouse({ connectDragSource, connectDragPreview }) { | ||
return ( | ||
<> | ||
<DragPreviewImage src="house_dragged.png" connect={connectDragPreview} /> | ||
<div ref={connectDragSource}>🏠</div> | ||
</> | ||
) | ||
} | ||
export default DragSource( | ||
/* ... */ | ||
(connect, monitor) => ({ | ||
connectDragSource: connect.dragSource(), | ||
connectDragPreview: connect.dragPreview() | ||
}) | ||
) | ||
``` | ||
|
||
### Props | ||
|
||
- **`connect`**: Required. The drag preview connector function |
75 changes: 75 additions & 0 deletions
75
packages/docsite/markdown/docs/03 Using Hooks/HooksOverview.md
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,75 @@ | ||
--- | ||
path: '/docs/api/hooks-overview' | ||
title: 'Overview' | ||
--- | ||
|
||
_New to React DnD? [Read the overview](/docs/overview) before jumping into the docs._ | ||
|
||
# Using the Hooks API | ||
|
||
The hooks-based React DnD API leverages one of the significant newer features of React. Hooks have dramatically impacted how most people write their Reoct components, and are the recommended approach for writing stateful and effectful code within React. Prior to hooks, the React community poured a lot of effort into Higher Order Components and Decorator-based libraries. After hooks were introduced to the React community, there has been a dramatic shift in switching towards approaches and libraries that utilize hooks over decorator-based techniques. | ||
|
||
In the overview page, it is pointed out that Drag-and-drop interations are inherently _stateful_. Because of this React DnD has been designed to take advantage of the Flux data pattern and model drag-and-drop state using actions and reducers (independent of React). Hooks are the perfect way to utilize a stateful data source in React. In fact, this is the approach taken by many state-management libraries in React! | ||
|
||
There are three primary hooks that are provided to wire your components into React DnD, and a fourth is provided to give you a seam into React DnD (for testing or development purposes) | ||
|
||
- [`useDrag`](/docs/api/use-drag) | ||
- [`useDrop`](/docs/api/use-drop) | ||
- [`useDragLayer`](/docs/api/use-drag-layer) | ||
- [`useDragDropManager`](/docs/api/use-drag-drop-manager) (_dev/test hook_) | ||
|
||
## Basic Example | ||
|
||
To start using hooks, let's make a box draggable. | ||
|
||
```jsx | ||
import { useDrag } from 'react-dnd' | ||
|
||
function Box() { | ||
const [{ isDragging }, drag, dragPreview] = useDrag({ | ||
// "type" is required. It is used by the "accept" specification of drop targets. | ||
item: { type: 'BOX' }, | ||
// The collect function utilizes a "monitor" instance (see the Overview for what this is) | ||
// to pull important pieces of state from the DnD system. | ||
collect: (monitor) => ({ | ||
isDragging: monitor.isDragging() | ||
}) | ||
|
||
return ( | ||
{/* This is optional. The dragPreview will be attached to the dragSource by default */} | ||
<div ref={dragPreview} style={{ opacity: isDragging ? 0.5 : 1}}> | ||
{/* The drag ref marks this node as being the "pick-up" node */} | ||
<div role="Handle" ref={drag}> | ||
</div> | ||
) | ||
}) | ||
} | ||
``` | ||
|
||
Now, let's make something for this to drag into. | ||
|
||
```jsx | ||
function Bucket() { | ||
const [{ canDrop, isOver }, drop] = useDrop({ | ||
// The type (or types) to accept - strings or symbols | ||
accept: 'BOX', | ||
// Props to collect | ||
collect: (monitor) => ({ | ||
isOver: monitor.isOver(), | ||
canDrop: monitor.canDrop() | ||
}) | ||
}) | ||
|
||
return ( | ||
<div | ||
ref={drop} | ||
role={'Dustbin'} | ||
style={{ backgroundColor: isActive ? 'red' : 'white' }} | ||
> | ||
{isActive ? 'Release to drop' : 'Drag a box here'} | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
To explore further, read the individual hook API documentation, or check out the [hook-based examples](https://github.com/react-dnd/react-dnd/tree/main/packages/examples-hooks) on GitHub. |
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
Oops, something went wrong.