-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update helpers to support multi-window use cases (#509)
- Loading branch information
Showing
32 changed files
with
210 additions
and
46 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,28 @@ | ||
--- | ||
"@dnd-kit/core": patch | ||
"@dnd-kit/utilities": patch | ||
--- | ||
|
||
Helpers have been updated to support rendering in foreign `window` contexts (via `ReactDOM.render` or `ReactDOM.createPortal`). | ||
|
||
For example, checking if an element is an instance of an `HTMLElement` is normally done like so: | ||
|
||
```ts | ||
if (element instanceof HTMLElement) | ||
``` | ||
|
||
However, when rendering in a different window, this can return false even if the element is indeed an HTMLElement, because this code is equivalent to: | ||
|
||
```ts | ||
if (element instanceof window.HTMLElement) | ||
``` | ||
|
||
And in this case, the `window` of the `element` is different from the main execution context `window`, because we are rendering via a portal into another window. | ||
|
||
This can be solved by finding the local window of the element: | ||
|
||
```ts | ||
const elementWindow = element.ownerDocument.defaultView; | ||
|
||
if (element instanceof elementWindow.HTMLElement) | ||
``` |
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
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
9 changes: 6 additions & 3 deletions
9
packages/core/src/sensors/utilities/getEventListenerTarget.ts
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 |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import {getOwnerDocument} from '../../utilities'; | ||
import {getOwnerDocument, getWindow} from '@dnd-kit/utilities'; | ||
|
||
export function getEventListenerTarget( | ||
element: EventTarget | null | ||
target: EventTarget | null | ||
): EventTarget | Document { | ||
// If the `event.target` element is removed from the document events will still be targeted | ||
// at it, and hence won't always bubble up to the window or document anymore. | ||
// If there is any risk of an element being removed while it is being dragged, | ||
// the best practice is to attach the event listeners directly to the target. | ||
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget | ||
return element instanceof EventTarget ? element : getOwnerDocument(element); | ||
|
||
const {EventTarget} = getWindow(target); | ||
|
||
return target instanceof EventTarget ? target : getOwnerDocument(target); | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/core/src/utilities/coordinates/getRelativeTransformOrigin.ts
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
26 changes: 22 additions & 4 deletions
26
packages/core/src/utilities/scroll/getScrollableElement.ts
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {hasViewportRelativeCoordinates} from './hasViewportRelativeCoordinates'; | ||
export {isKeyboardEvent} from './isKeyboardEvent'; | ||
export {isTouchEvent} from './isTouchEvent'; |
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,13 @@ | ||
import {getWindow} from '../execution-context'; | ||
|
||
export function isKeyboardEvent( | ||
event: Event | undefined | null | ||
): event is KeyboardEvent { | ||
if (!event) { | ||
return false; | ||
} | ||
|
||
const {KeyboardEvent} = getWindow(event.target); | ||
|
||
return KeyboardEvent && event instanceof KeyboardEvent; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,13 @@ | ||
export function isTouchEvent(event: Event): event is TouchEvent { | ||
return window?.TouchEvent && event instanceof TouchEvent; | ||
import {getWindow} from '../execution-context'; | ||
|
||
export function isTouchEvent( | ||
event: Event | undefined | null | ||
): event is TouchEvent { | ||
if (!event) { | ||
return false; | ||
} | ||
|
||
const {TouchEvent} = getWindow(event.target); | ||
|
||
return TouchEvent && event instanceof TouchEvent; | ||
} |
File renamed without changes.
25 changes: 25 additions & 0 deletions
25
packages/utilities/src/execution-context/getOwnerDocument.ts
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,25 @@ | ||
import {isWindow, isHTMLElement, isDocument, isNode} from '../type-guards'; | ||
|
||
export function getOwnerDocument(target: Event['target']): Document { | ||
if (!target) { | ||
return document; | ||
} | ||
|
||
if (isWindow(target)) { | ||
return target.document; | ||
} | ||
|
||
if (!isNode(target)) { | ||
return document; | ||
} | ||
|
||
if (isDocument(target)) { | ||
return target; | ||
} | ||
|
||
if (isHTMLElement(target)) { | ||
return target.ownerDocument; | ||
} | ||
|
||
return document; | ||
} |
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,18 @@ | ||
import {isWindow} from '../type-guards/isWindow'; | ||
import {isNode} from '../type-guards/isNode'; | ||
|
||
export function getWindow(target: Event['target']): typeof window { | ||
if (!target) { | ||
return window; | ||
} | ||
|
||
if (isWindow(target)) { | ||
return target; | ||
} | ||
|
||
if (!isNode(target)) { | ||
return window; | ||
} | ||
|
||
return target.ownerDocument?.defaultView ?? window; | ||
} |
1 change: 1 addition & 0 deletions
1
...ages/core/src/utilities/document/index.ts → .../utilities/src/execution-context/index.ts
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export {canUseDOM} from './canUseDOM'; | ||
export {getOwnerDocument} from './getOwnerDocument'; | ||
export {getWindow} from './getWindow'; |
Oops, something went wrong.