|
21 | 21 |
|
22 | 22 |
|
23 | 23 | ## Installation |
24 | | -You can install this package from [NPM](https://www.npmjs.com): |
| 24 | + |
| 25 | +You can install this package from [NPM](https://www.npmjs.com/package/file-selector): |
| 26 | + |
25 | 27 | ```bash |
26 | 28 | npm add file-selector |
27 | 29 | ``` |
28 | 30 |
|
29 | | -### CDN |
30 | | -For CDN, you can use [unpkg](https://unpkg.com): |
| 31 | +### Bundler |
31 | 32 |
|
32 | | -[https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js](https://unpkg.com/file-selector/dist/bundles/file-selector.umd.min.js) |
| 33 | +If you are using a bundler such as [Vite](https://vite.dev/) or [Webpack](https://webpack.js.org/) you can import the package directly: |
33 | 34 |
|
34 | | -The global namespace for file-selector is `fileSelector`: |
35 | 35 | ```js |
36 | | -const {fromEvent} = fileSelector; |
37 | | -document.addEventListener('drop', async evt => { |
38 | | - const files = await fromEvent(evt); |
39 | | - console.log(files); |
40 | | -}); |
| 36 | +import {fromEvent} from 'file-selector'; |
| 37 | +``` |
| 38 | + |
| 39 | +### Browser |
| 40 | + |
| 41 | +To include the package directly in the browser without a bundling step you can choose to either use a CDN, or include the required files in your own project. |
| 42 | + |
| 43 | +#### CDN |
| 44 | + |
| 45 | +If you want to use a CDN, you can use [Skypack](https://www.skypack.dev/), or any other CDN of your choice. Make sure you are specifying a compatible version range to avoid unexpected breaking changes. |
| 46 | + |
| 47 | +```html |
| 48 | +<script type="module"> |
| 49 | + import {fromEvent} from 'https://cdn.skypack.dev/file-selector@^1.0.0'; |
| 50 | +</script> |
| 51 | +``` |
| 52 | + |
| 53 | +#### Self-hosted |
| 54 | + |
| 55 | +Self hosting is also possible, make sure to copy or link the contents of the NPM package into your project and import them as you would any other module. |
| 56 | + |
| 57 | +```html |
| 58 | +<script type="module"> |
| 59 | + import {fromEvent} from './path/to/file-selector.js'; |
| 60 | +</script> |
41 | 61 | ``` |
42 | 62 |
|
| 63 | +#### Import maps |
| 64 | + |
| 65 | +To avoid repeating the import path and get an experience similar to a bundler you can opt to use an [import map](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script/type/importmap). |
| 66 | + |
| 67 | +```html |
| 68 | +<script type="importmap"> |
| 69 | + { |
| 70 | + "imports": { |
| 71 | + // Using the CDN |
| 72 | + "file-selector": "https://cdn.skypack.dev/file-selector@^1.0.0" |
| 73 | + // Or a path to your own self-hosted version. |
| 74 | + "file-selector": "./path/to/file-selector.js" |
| 75 | + } |
| 76 | + } |
| 77 | +</script> |
| 78 | +<script type="module"> |
| 79 | + import {fromEvent} from 'file-selector'; |
| 80 | +</script> |
| 81 | +``` |
43 | 82 |
|
44 | 83 | ## Usage |
45 | 84 |
|
46 | | -### ES6 |
47 | 85 | Convert a [DragEvent](https://developer.mozilla.org/en-US/docs/Web/API/DragEvent) to File objects: |
48 | | -```ts |
| 86 | + |
| 87 | +```js |
49 | 88 | import {fromEvent} from 'file-selector'; |
50 | | -document.addEventListener('drop', async evt => { |
51 | | - const files = await fromEvent(evt); |
| 89 | + |
| 90 | +document.addEventListener('drop', async (event) => { |
| 91 | + const files = await fromEvent(event); |
52 | 92 | console.log(files); |
53 | 93 | }); |
54 | 94 | ``` |
55 | 95 |
|
56 | 96 | Convert a [change event](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event) for an input type file to File objects: |
57 | | -```ts |
| 97 | + |
| 98 | +```js |
58 | 99 | import {fromEvent} from 'file-selector'; |
| 100 | + |
59 | 101 | const input = document.getElementById('myInput'); |
60 | | -input.addEventListener('change', async evt => { |
61 | | - const files = await fromEvent(evt); |
| 102 | +input.addEventListener('change', async (event) => { |
| 103 | + const files = await fromEvent(event); |
62 | 104 | console.log(files); |
63 | 105 | }); |
64 | 106 | ``` |
65 | 107 |
|
66 | 108 | Convert [FileSystemFileHandle](https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle) items to File objects: |
67 | | -```ts |
| 109 | + |
| 110 | +```js |
68 | 111 | import {fromEvent} from 'file-selector'; |
69 | 112 |
|
70 | | -// Open file picker |
71 | 113 | const handles = await window.showOpenFilePicker({multiple: true}); |
72 | | -// Get the files |
73 | 114 | const files = await fromEvent(handles); |
74 | 115 | console.log(files); |
75 | 116 | ``` |
76 | | -**NOTE** The above is experimental and subject to change. |
| 117 | + |
| 118 | +> [!NOTE] |
| 119 | +> The above is experimental and subject to change. |
77 | 120 |
|
78 | 121 | ### CommonJS |
| 122 | + |
79 | 123 | Convert a `DragEvent` to File objects: |
80 | | -```ts |
| 124 | + |
| 125 | +```js |
81 | 126 | const {fromEvent} = require('file-selector'); |
82 | | -document.addEventListener('drop', async evt => { |
83 | | - const files = await fromEvent(evt); |
| 127 | + |
| 128 | +document.addEventListener('drop', async (event) => { |
| 129 | + const files = await fromEvent(event); |
84 | 130 | console.log(files); |
85 | 131 | }); |
86 | 132 | ``` |
87 | 133 |
|
88 | | - |
89 | 134 | ## Browser Support |
90 | 135 | Most browser support basic File selection with drag 'n' drop or file input: |
91 | 136 | * [File API](https://developer.mozilla.org/en-US/docs/Web/API/File#Browser_compatibility) |
|
0 commit comments