drop-target.mdx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ---
  2. sidebar_position: 2
  3. slug: /drop-target
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # Drop target
  9. The `@uppy/drop-target` plugin lets your users drag-and-drop files on any
  10. element on the page, for example the whole page, `document.body`.
  11. Can be used together with Uppy Dashboard or Drag & Drop plugins, or your custom
  12. solution targeting any DOM element.
  13. :::tip
  14. [Try out the live example](/examples) or take it for a spin in
  15. [CodeSandbox](https://codesandbox.io/s/uppy-drag-drop-gyewzx?file=/src/index.js).
  16. :::
  17. ## When should I use this?
  18. When you want to allow users to drag and drop files in your own UI, rather than
  19. in the [`Dashboard`](/docs/dashboard) UI, or catch dropped files from anywhere
  20. on the page.
  21. ## Install
  22. <Tabs>
  23. <TabItem value="npm" label="NPM" default>
  24. ```shell
  25. npm install @uppy/drop-target
  26. ```
  27. </TabItem>
  28. <TabItem value="yarn" label="Yarn">
  29. ```shell
  30. yarn add @uppy/drop-target
  31. ```
  32. </TabItem>
  33. <TabItem value="cdn" label="CDN">
  34. <UppyCdnExample>
  35. {`
  36. import { DropTarget } from "{{UPPY_JS_URL}}"
  37. const DropTarget = new Uppy().use(DropTarget)
  38. `}
  39. </UppyCdnExample>
  40. </TabItem>
  41. </Tabs>
  42. ## Use
  43. This module has one default export: the `DropTarget` plugin class.
  44. ```js {8-10} showLineNumbers
  45. import Uppy from '@uppy/core';
  46. import DropTarget from '@uppy/drop-target';
  47. import '@uppy/core/dist/style.css';
  48. import '@uppy/drop-target/dist/style.css';
  49. const uppy = new Uppy();
  50. uppy.use(DropTarget, {
  51. target: document.body,
  52. });
  53. ```
  54. ## API
  55. ### Options
  56. #### `onDragLeave`
  57. Event listener for the [`dragleave` event][].
  58. ```js {3-5} showLineNumbers
  59. uppy.use(DropTarget, {
  60. target: document.body,
  61. onDragLeave: (event) => {
  62. event.stopPropagation();
  63. },
  64. });
  65. ```
  66. #### `onDragOver`
  67. Event listener for the [`dragover` event][].
  68. #### `onDrop`
  69. Event listener for the [`drop` event][].
  70. #### `target`
  71. DOM element, CSS selector, or plugin to place the drag and drop area into
  72. (`string`, `Element`, `Function`, or `UIPlugin`, default: `null`).
  73. [`dragover` event]:
  74. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragover_event
  75. [`dragleave` event]:
  76. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dragleave_event
  77. [`drop` event]:
  78. https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/drop_event