|
13 | 13 |
|
14 | 14 | # Table of Contents |
15 | 15 |
|
16 | | -* [Installation](#installation) |
17 | | -* [Usage](#usage) |
18 | | -* [Browser Support](#browser-support) |
19 | | -* [Contribute](#contribute) |
20 | | -* [Credits](#credits) |
21 | | -* [Support](#support) |
22 | | -* [License](#license) |
23 | | - |
| 16 | +- [Installation](#installation) |
| 17 | +- [Usage](#usage) |
| 18 | +- [Browser Support](#browser-support) |
| 19 | +- [Contribute](#contribute) |
| 20 | +- [Credits](#credits) |
| 21 | +- [Support](#support) |
| 22 | +- [License](#license) |
24 | 23 |
|
25 | 24 | ## Installation |
| 25 | + |
26 | 26 | You can install this package from [NPM](https://www.npmjs.com): |
| 27 | + |
27 | 28 | ```bash |
28 | 29 | npm add file-selector |
29 | 30 | ``` |
30 | 31 |
|
31 | 32 | ### CDN |
| 33 | + |
32 | 34 | For CDN usage, load the ESM build directly from a CDN such as [esm.sh](https://esm.sh) or [jsDelivr](https://www.jsdelivr.com/package/npm/file-selector): |
| 35 | + |
33 | 36 | ```html |
34 | 37 | <script type="module"> |
35 | | - import {fromEvent} from 'https://esm.sh/file-selector'; |
| 38 | + import {fromEvent} from "https://esm.sh/file-selector"; |
36 | 39 |
|
37 | | - document.addEventListener('drop', async evt => { |
38 | | - const files = await fromEvent(evt); |
39 | | - console.log(files); |
40 | | - }); |
| 40 | + document.addEventListener("drop", async evt => { |
| 41 | + const files = await fromEvent(evt); |
| 42 | + console.log(files); |
| 43 | + }); |
41 | 44 | </script> |
42 | 45 | ``` |
43 | 46 |
|
44 | | - |
45 | 47 | ## Usage |
46 | 48 |
|
47 | 49 | ### ES6 |
| 50 | + |
48 | 51 | Convert a [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent) to File objects: |
| 52 | + |
49 | 53 | ```ts |
50 | | -import {fromEvent} from 'file-selector'; |
51 | | -document.addEventListener('drop', async evt => { |
52 | | - const files = await fromEvent(evt); |
53 | | - console.log(files); |
| 54 | +import {fromEvent} from "file-selector"; |
| 55 | +document.addEventListener("drop", async evt => { |
| 56 | + const files = await fromEvent(evt); |
| 57 | + console.log(files); |
54 | 58 | }); |
55 | 59 | ``` |
56 | 60 |
|
57 | 61 | Convert a [change event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) for an input type file to File objects: |
| 62 | + |
58 | 63 | ```ts |
59 | | -import {fromEvent} from 'file-selector'; |
60 | | -const input = document.getElementById('myInput'); |
61 | | -input.addEventListener('change', async evt => { |
62 | | - const files = await fromEvent(evt); |
63 | | - console.log(files); |
| 64 | +import {fromEvent} from "file-selector"; |
| 65 | +const input = document.getElementById("myInput"); |
| 66 | +input.addEventListener("change", async evt => { |
| 67 | + const files = await fromEvent(evt); |
| 68 | + console.log(files); |
64 | 69 | }); |
65 | 70 | ``` |
66 | 71 |
|
67 | 72 | Convert [FileSystemFileHandle](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle) items to File objects: |
| 73 | + |
68 | 74 | ```ts |
69 | | -import {fromEvent} from 'file-selector'; |
| 75 | +import {fromEvent} from "file-selector"; |
70 | 76 |
|
71 | 77 | // Open file picker |
72 | 78 | const handles = await window.showOpenFilePicker({multiple: true}); |
73 | 79 | // Get the files |
74 | 80 | const files = await fromEvent(handles); |
75 | 81 | console.log(files); |
76 | 82 | ``` |
| 83 | + |
77 | 84 | **NOTE** The above is experimental and subject to change. |
78 | 85 |
|
79 | 86 | ### CommonJS |
| 87 | + |
80 | 88 | Convert a `DragEvent` to File objects: |
| 89 | + |
81 | 90 | ```ts |
82 | | -const {fromEvent} = require('file-selector'); |
83 | | -document.addEventListener('drop', async evt => { |
84 | | - const files = await fromEvent(evt); |
85 | | - console.log(files); |
| 91 | +const {fromEvent} = require("file-selector"); |
| 92 | +document.addEventListener("drop", async evt => { |
| 93 | + const files = await fromEvent(evt); |
| 94 | + console.log(files); |
86 | 95 | }); |
87 | 96 | ``` |
88 | 97 |
|
89 | 98 | ### MIME types |
| 99 | + |
90 | 100 | When the browser doesn't set a File's `type`, `fromEvent` infers one from the file extension. By default it uses a small built-in table of the most common types, keeping the bundle lean. |
91 | 101 |
|
92 | 102 | If you need broader coverage, import the full extension-to-MIME table from the `file-selector/mime` subpath and pass it via the `mimeTypes` option. Because it's a separate entry point, the full table (~1,200 entries) is only included in your bundle when you import it: |
| 103 | + |
93 | 104 | ```ts |
94 | | -import {fromEvent} from 'file-selector'; |
95 | | -import {COMMON_MIME_TYPES} from 'file-selector/mime'; |
| 105 | +import {fromEvent} from "file-selector"; |
| 106 | +import {COMMON_MIME_TYPES} from "file-selector/mime"; |
96 | 107 |
|
97 | 108 | const files = await fromEvent(evt, {mimeTypes: COMMON_MIME_TYPES}); |
98 | 109 | ``` |
99 | 110 |
|
100 | 111 | You can also pass your own `Map<extension, mimeType>` to restrict or extend the lookup: |
| 112 | + |
101 | 113 | ```ts |
102 | | -const files = await fromEvent(evt, {mimeTypes: new Map([['dwg', 'image/vnd.dwg']])}); |
| 114 | +const files = await fromEvent(evt, {mimeTypes: new Map([["dwg", "image/vnd.dwg"]])}); |
103 | 115 | ``` |
104 | 116 |
|
105 | | - |
106 | 117 | ## Browser Support |
| 118 | + |
107 | 119 | Most browser support basic File selection with drag 'n' drop or file input: |
108 | | -* [File API](https://developer.mozilla.org/en-US/docs/Web/API/File#Browser_compatibility) |
109 | | -* [Drag Event](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Browser_compatibility) |
110 | | -* [DataTransfer](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer#Browser_compatibility) |
111 | | -* [`<input type="file">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Browser_compatibility) |
| 120 | + |
| 121 | +- [File API](https://developer.mozilla.org/en-US/docs/Web/API/File#Browser_compatibility) |
| 122 | +- [Drag Event](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Browser_compatibility) |
| 123 | +- [DataTransfer](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer#Browser_compatibility) |
| 124 | +- [`<input type="file">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Browser_compatibility) |
112 | 125 |
|
113 | 126 | For folder drop we use the [FileSystem API](https://developer.mozilla.org/en-US/docs/Web/API/FileSystem) which has very limited support: |
114 | | -* [DataTransferItem.getAsFile()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFile#Browser_compatibility) |
115 | | -* [DataTransferItem.webkitGetAsEntry()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry#Browser_compatibility) |
116 | | -* [FileSystemEntry](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry#Browser_compatibility) |
117 | | -* [FileSystemFileEntry.file()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file#Browser_compatibility) |
118 | | -* [FileSystemDirectoryEntry.createReader()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader#Browser_compatibility) |
119 | | -* [FileSystemDirectoryReader.readEntries()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries#Browser_compatibility) |
120 | 127 |
|
| 128 | +- [DataTransferItem.getAsFile()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/getAsFile#Browser_compatibility) |
| 129 | +- [DataTransferItem.webkitGetAsEntry()](https://developer.mozilla.org/en-US/docs/Web/API/DataTransferItem/webkitGetAsEntry#Browser_compatibility) |
| 130 | +- [FileSystemEntry](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemEntry#Browser_compatibility) |
| 131 | +- [FileSystemFileEntry.file()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file#Browser_compatibility) |
| 132 | +- [FileSystemDirectoryEntry.createReader()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/createReader#Browser_compatibility) |
| 133 | +- [FileSystemDirectoryReader.readEntries()](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryReader/readEntries#Browser_compatibility) |
121 | 134 |
|
122 | 135 | ## Contribute |
| 136 | + |
123 | 137 | Checkout the organization [CONTRIBUTING.md](https://github.com/react-dropzone/.github/blob/main/CONTRIBUTING.md). |
124 | 138 |
|
125 | 139 | ### Development |
126 | 140 |
|
127 | 141 | Development requires [Node.js](https://nodejs.org) >= 20. Install the dependencies with: |
| 142 | + |
128 | 143 | ```bash |
129 | 144 | npm install |
130 | 145 | ``` |
131 | 146 |
|
132 | 147 | The project is built with [tsdown](https://tsdown.dev) (Rolldown), type-checked with [TypeScript](https://www.typescriptlang.org), tested with [Vitest](https://vitest.dev) and linted/formatted with [oxlint](https://oxc.rs) and [oxfmt](https://oxc.rs). |
133 | 148 |
|
134 | | -| Command | Description | |
135 | | -| --- | --- | |
136 | | -| `npm test` | Run the test suite in watch mode. | |
137 | | -| `npm run test:cov` | Run the tests once with coverage (runs type-check, lint and format check first). | |
138 | | -| `npm run type-check` | Type-check the sources without emitting. | |
139 | | -| `npm run lint` | Lint the sources (oxlint). | |
140 | | -| `npm run lint:fix` | Apply safe lint fixes. | |
141 | | -| `npm run lint:type-aware` | Type-aware lint via tsgolint (alpha). | |
142 | | -| `npm run format` | Format the sources (oxfmt). | |
143 | | -| `npm run format:check` | Check formatting without writing. | |
144 | | -| `npm run build` | Build the outputs into `dist/`. | |
145 | | -| `npm run dev` | Build in watch mode. | |
146 | | -| `npm run docs:dev` | Run the [Vocs](https://vocs.dev) docs site locally. | |
147 | | -| `npm run docs:build` | Build the docs site into `site/`. | |
148 | | -| `npm run docs:preview` | Preview the built docs site. | |
| 149 | +| Command | Description | |
| 150 | +| ------------------------- | -------------------------------------------------------------------------------- | |
| 151 | +| `npm test` | Run the test suite in watch mode. | |
| 152 | +| `npm run test:cov` | Run the tests once with coverage (runs type-check, lint and format check first). | |
| 153 | +| `npm run type-check` | Type-check the sources without emitting. | |
| 154 | +| `npm run lint` | Lint the sources (oxlint). | |
| 155 | +| `npm run lint:fix` | Apply safe lint fixes. | |
| 156 | +| `npm run lint:type-aware` | Type-aware lint via tsgolint (alpha). | |
| 157 | +| `npm run format` | Format the sources (oxfmt). | |
| 158 | +| `npm run format:check` | Check formatting without writing. | |
| 159 | +| `npm run build` | Build the outputs into `dist/`. | |
| 160 | +| `npm run dev` | Build in watch mode. | |
| 161 | +| `npm run docs:dev` | Run the [Vocs](https://vocs.dev) docs site locally. | |
| 162 | +| `npm run docs:build` | Build the docs site into `site/`. | |
| 163 | +| `npm run docs:preview` | Preview the built docs site. | |
149 | 164 |
|
150 | 165 | The build emits into `dist/`: |
| 166 | + |
151 | 167 | - `dist/index.js` — ES module |
152 | 168 | - `dist/index.cjs` — CommonJS module |
153 | 169 | - `dist/index.d.ts` — TypeScript declarations |
154 | 170 |
|
155 | 171 | ## Credits |
156 | | -* [html5-file-selector](https://github.com/quarklemotion/html5-file-selector) |
| 172 | + |
| 173 | +- [html5-file-selector](https://github.com/quarklemotion/html5-file-selector) |
157 | 174 |
|
158 | 175 | ## Support |
159 | 176 |
|
160 | 177 | ### Backers |
| 178 | + |
161 | 179 | Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/react-dropzone#backer)] |
162 | 180 |
|
163 | 181 | <a href="https://opencollective.com/react-dropzone#backers" target="_blank"><img src="https://opencollective.com/react-dropzone/backers.svg?width=890"></a> |
164 | 182 |
|
165 | 183 | ### Sponsors |
| 184 | + |
166 | 185 | Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/react-dropzone#sponsor)] |
167 | 186 |
|
168 | 187 | <a href="https://opencollective.com/react-dropzone#sponsors" target="_blank"><img src="https://opencollective.com/react-dropzone/sponsors.svg?width=890"></a> |
169 | 188 |
|
170 | 189 | ## License |
| 190 | + |
171 | 191 | MIT |
0 commit comments