drag-drop.mdx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ---
  2. sidebar_position: 2
  3. slug: /drag-drop
  4. ---
  5. import Tabs from '@theme/Tabs';
  6. import TabItem from '@theme/TabItem';
  7. import UppyCdnExample from '/src/components/UppyCdnExample';
  8. # Drag & Drop
  9. The `@uppy/drag-drop` plugin renders a drag and drop area for file selection.
  10. :::tip
  11. [Try out the live example](/examples) or take it for a spin in
  12. [CodeSandbox](https://codesandbox.io/s/uppy-drag-drop-gyewzx?file=/src/index.js).
  13. :::
  14. ## When should I use this?
  15. It can be useful when you only want the local device as a file source, don’t
  16. need file previews and a UI for metadata editing, or the
  17. [Dashboard](/docs/dashboard/) is too much. But it can be too minimal too. By
  18. default it doesn’t show that a file has been added nor is there a progress bar.
  19. ## Install
  20. <Tabs>
  21. <TabItem value="npm" label="NPM" default>
  22. ```shell
  23. npm install @uppy/core @uppy/drag-drop
  24. ```
  25. </TabItem>
  26. <TabItem value="yarn" label="Yarn">
  27. ```shell
  28. yarn add @uppy/core @uppy/drag-drop
  29. ```
  30. </TabItem>
  31. <TabItem value="cdn" label="CDN">
  32. <UppyCdnExample>
  33. {`
  34. import { Uppy, DragDrop } from "{{UPPY_JS_URL}}"
  35. const uppy = new Uppy()
  36. uppy.use(DragDrop, { target: '#uppy' })
  37. `}
  38. </UppyCdnExample>
  39. </TabItem>
  40. </Tabs>
  41. ## Use
  42. ```js showLineNumbers
  43. import Uppy from '@uppy/core';
  44. import DragDrop from '@uppy/drag-drop';
  45. import '@uppy/core/dist/style.min.css';
  46. import '@uppy/drag-drop/dist/style.min.css';
  47. new Uppy().use(DragDrop, { target: '#drag-drop' });
  48. ```
  49. :::info
  50. Certain [restrictions](/docs/uppy#restrictions) set in Uppy’s options, namely
  51. `maxNumberOfFiles` and `allowedFileTypes`, affect the system file picker dialog.
  52. If `maxNumberOfFiles: 1`, users will only be able to select one file, and
  53. `allowedFileTypes: ['video/*', '.gif']` means only videos or gifs (files with
  54. `.gif` extension) will be selectable.
  55. :::
  56. ## API
  57. ### Options
  58. #### `id`
  59. A unique identifier for this plugin (`string`, Default: `'DragDrop'`).
  60. Use this if you need to add several DragDrop instances.
  61. #### `target`
  62. DOM element, CSS selector, or plugin to place the drag and drop area into
  63. (`string` or `Element`, default: `null`).
  64. #### `width`
  65. Drag and drop area width (`string`, default: `'100%'`).
  66. Set in inline CSS, so feel free to use percentage, pixels or other values that
  67. you like.
  68. #### `height`
  69. Drag and drop area height (`string`, default: `'100%'`).
  70. Set in inline CSS, so feel free to use percentage, pixels or other values that
  71. you like.
  72. #### `note`
  73. Optionally, specify a string of text that explains something about the upload
  74. for the user (`string`, default: `null`).
  75. This is a place to explain any `restrictions` that are put in place. For
  76. example: `'Images and video only, 2–3 files, up to 1 MB'`.
  77. #### `locale`
  78. ```js
  79. export default {
  80. strings: {
  81. // Text to show on the droppable area.
  82. // `%{browse}` is replaced with a link that opens the system file selection dialog.
  83. dropHereOr: 'Drop here or %{browse}',
  84. // Used as the label for the link that opens the system file selection dialog.
  85. browse: 'browse',
  86. },
  87. };
  88. ```
  89. #### `onDragOver(event)`
  90. Callback for the [`ondragover`][ondragover] event handler.
  91. #### `onDragLeave(event)`
  92. Callback for the [`ondragleave`][ondragleave] event handler.
  93. #### `onDrop(event)`
  94. Callback for the [`ondrop`][ondrop] event handler.
  95. [ondragover]:
  96. https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/ondragover
  97. [ondragleave]:
  98. https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/ondragleave
  99. [ondrop]:
  100. https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/ondrop