| 1 | "use client"; |
| 2 | |
| 3 | import { useMemo } from "react"; |
| 4 | import { DndProvider } from "react-dnd"; |
| 5 | import { HTML5Backend } from "react-dnd-html5-backend"; |
| 6 | import { |
| 7 | TouchBackend, |
| 8 | type TouchBackendOptions, |
| 9 | } from "react-dnd-touch-backend"; |
| 10 | |
| 11 | const TOUCH_BACKEND_OPTIONS: Partial<TouchBackendOptions> = { |
| 12 | enableMouseEvents: true, |
| 13 | }; |
| 14 | |
| 15 | function supportsTouch() { |
| 16 | if (typeof window === "undefined") { |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | return ( |
| 21 | "ontouchstart" in window || |
| 22 | window.navigator.maxTouchPoints > 0 || |
| 23 | window.matchMedia?.("(pointer: coarse)").matches === true |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | export default function TouchAwareDndProvider({ |
| 28 | children, |
| 29 | }: { |
| 30 | children: React.ReactNode; |
| 31 | }) { |
| 32 | const isTouchDevice = useMemo(supportsTouch, []); |
| 33 | |
| 34 | return ( |
| 35 | <DndProvider |
| 36 | backend={isTouchDevice ? TouchBackend : HTML5Backend} |
| 37 | options={isTouchDevice ? TOUCH_BACKEND_OPTIONS : undefined} |
| 38 | > |
| 39 | {children} |
| 40 | </DndProvider> |
| 41 | ); |
| 42 | } |
| 43 |