@tokshare/pick 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +152 -2
- package/dist/index.cjs +2449 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +231 -0
- package/dist/index.d.ts +231 -0
- package/dist/index.js +2415 -0
- package/dist/index.js.map +1 -0
- package/dist/picker.global.js +250 -0
- package/dist/react.cjs +157 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +65 -0
- package/dist/react.d.ts +65 -0
- package/dist/react.js +140 -0
- package/dist/react.js.map +1 -0
- package/package.json +71 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Peter Torki
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,153 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tokshare/pick
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Let visitors send files from their phone to your website: a button, a QR code, and ordinary `File`
|
|
4
|
+
objects in your page. No app on the phone.
|
|
5
|
+
|
|
6
|
+
## Upload field (recommended)
|
|
7
|
+
|
|
8
|
+
A complete upload field: drop zone, **Browse files**, and **Use your phone**, which opens inside the
|
|
9
|
+
field (QR code, steps, live status). Files from the computer and the phone land in one list with
|
|
10
|
+
thumbnails and remove buttons, and in a real `<input type="file" name>` so your form submits them.
|
|
11
|
+
|
|
12
|
+
```html
|
|
13
|
+
<script src="https://cdn.jsdelivr.net/npm/@tokshare/pick/dist/picker.global.js" data-key="pk_live_…" defer></script>
|
|
14
|
+
|
|
15
|
+
<form method="post" enctype="multipart/form-data">
|
|
16
|
+
<tokshare-pick-field name="photos" accept="image/png,image/jpeg" max-size="5MB" max-files="6" multiple></tokshare-pick-field>
|
|
17
|
+
<button>Publish</button>
|
|
18
|
+
</form>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Attributes: `name`, `accept`, `multiple`, `max-files`, `max-size` (`5MB`, `500KB`), `resize` (longest
|
|
22
|
+
side in pixels for photos from the phone), `phone` (`inline`, `modal` or `off`), `locale`, `theme`,
|
|
23
|
+
`label`, `key`, `relay`. The element fires a `files` event with the current `File[]`.
|
|
24
|
+
|
|
25
|
+
From npm: `mountField(element, { key, name, accept, maxFileBytes, multiple })` returns
|
|
26
|
+
`{ files, input, on("change"), remove(file), clear(), destroy() }`, or call `defineFieldElement({ key })`
|
|
27
|
+
to register `<tokshare-pick-field>`. On a page that is itself open on a phone, "Use your phone" is
|
|
28
|
+
hidden (`phoneOnMobile: true` to keep it). `theme: "auto"` matches the page behind the field.
|
|
29
|
+
|
|
30
|
+
## Script tag: a button for an existing input
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<script src="https://cdn.jsdelivr.net/npm/@tokshare/pick/dist/picker.global.js" data-key="pk_live_…" defer></script>
|
|
34
|
+
|
|
35
|
+
<input type="file" id="photos" name="photos" accept="image/*" multiple>
|
|
36
|
+
<button type="button" data-tokshare-pick-for="photos">From my phone</button>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
The button fills the input (firing `input` and `change`), so your form works unchanged. Add
|
|
40
|
+
`data-mode="append"` to add to files already chosen.
|
|
41
|
+
|
|
42
|
+
## npm
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
npm install @tokshare/pick
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { pick, open, attach } from "@tokshare/pick";
|
|
50
|
+
|
|
51
|
+
// Promise: the files, or [] if the person cancels.
|
|
52
|
+
const files = await pick({ key: "pk_live_…", accept: "image/*,.pdf", maxFiles: 5 });
|
|
53
|
+
|
|
54
|
+
// Live events.
|
|
55
|
+
const picker = open({ key: "pk_live_…" });
|
|
56
|
+
picker.on("phone", ({ phone }) => console.log("connected", phone.label));
|
|
57
|
+
picker.on("progress", ({ name, sent, total, phase }) => console.log(name, phase, sent / total));
|
|
58
|
+
picker.on("file", (file) => console.log("received", file.name));
|
|
59
|
+
const all = await picker.done;
|
|
60
|
+
|
|
61
|
+
// Fill an existing input.
|
|
62
|
+
const fromPhone = attach(document.querySelector("#avatar"), { key: "pk_live_…" });
|
|
63
|
+
button.addEventListener("click", fromPhone);
|
|
64
|
+
|
|
65
|
+
// Your own UI: no modal; show picker.claimUrl as a QR and picker.code as text.
|
|
66
|
+
const headless = open({ key: "pk_live_…", ui: false });
|
|
67
|
+
headless.on("ready", ({ claimUrl, code }) => render(claimUrl, code));
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## React
|
|
71
|
+
|
|
72
|
+
`@tokshare/pick/react` (same package, React 17+). Set the key once, then drop components in:
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
import { TokShareProvider, TokSharePick, TokSharePickButton } from "@tokshare/pick/react";
|
|
76
|
+
|
|
77
|
+
export default function App() {
|
|
78
|
+
return (
|
|
79
|
+
<TokShareProvider pickKey="pk_live_…">
|
|
80
|
+
<ListingForm />
|
|
81
|
+
</TokShareProvider>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function ListingForm() {
|
|
86
|
+
const [photos, setPhotos] = useState<File[]>([]);
|
|
87
|
+
return (
|
|
88
|
+
<form>
|
|
89
|
+
{/* The full field: drop zone, Browse files, Use your phone. `name` makes a normal submit include the files. */}
|
|
90
|
+
<TokSharePick name="photos" accept="image/png,image/jpeg" maxFiles={6} maxFileBytes="5MB" onChange={setPhotos} />
|
|
91
|
+
|
|
92
|
+
{/* Or just a button: opens the phone window, gives you File[]. Takes every <button> prop. */}
|
|
93
|
+
<TokSharePickButton accept="image/*" onFiles={(files) => setPhotos((all) => [...all, ...files])} className="btn">
|
|
94
|
+
From my phone
|
|
95
|
+
</TokSharePickButton>
|
|
96
|
+
</form>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
- `pickKey` (React reserves `key`) and `relay`, `locale`, `theme` can go on the provider, or on any component
|
|
102
|
+
instead of a provider.
|
|
103
|
+
- `<TokSharePick>` takes the field's options as props: `name`, `accept`, `multiple`, `maxFiles`,
|
|
104
|
+
`maxFileBytes` (bytes or `"5MB"`), `resize` (longest side for phone photos), `phone` (`"inline"`,
|
|
105
|
+
`"modal"`, `false`), `label`, `strings`, plus `className`, `style`, `onChange(files)`. A ref gives
|
|
106
|
+
`files`, `input`, `remove(file)` and `clear()`. Changing an option rebuilds the field.
|
|
107
|
+
- Your own trigger: `const { pick, open } = useTokSharePick({ accept: "image/*" })`, then
|
|
108
|
+
`const files = await pick()` in a click handler (`[]` if the person cancels).
|
|
109
|
+
- Next.js: the components are client components (`"use client"` is built in); import them anywhere.
|
|
110
|
+
|
|
111
|
+
## Options
|
|
112
|
+
|
|
113
|
+
| Option | Default | |
|
|
114
|
+
|---|---|---|
|
|
115
|
+
| `key` | — | Your publishable key, bound to your site's origins. |
|
|
116
|
+
| `accept` | any | Like `<input accept>`: `"image/*,.pdf"`. Checked on the phone and on the relay, including the file's real type. |
|
|
117
|
+
| `multiple` | `true` | `false` for a single file. |
|
|
118
|
+
| `maxFiles` | 20 | Up to the plan's limit. |
|
|
119
|
+
| `maxFileBytes` | 50 MB | Up to the plan's limit. |
|
|
120
|
+
| `resizeImages` | off | `{ maxDimension: 1600, quality: 0.82 }` shrinks photos on the phone. |
|
|
121
|
+
| `confirmPhone` | `false` | Someone at the computer approves the phone (both show the same number). |
|
|
122
|
+
| `siteName` | your host | Shown on the phone: "*siteName* is asking for files". |
|
|
123
|
+
| `direct` | `true` | When the phone is on the same Wi-Fi as the computer, files go straight to the page over the local network (WebRTC) instead of the cloud: seconds instead of minutes on a slow internet upload. Falls back to the cloud by itself. `false` always uses the cloud. Field attribute `direct="off"`, script tag `data-direct="off"`. |
|
|
124
|
+
| `announce` | `true` | A small line about TokShare for desktop under the phone QR, which visitors can hide. `false` removes it. Field attribute `announce="off"`, script tag `data-announce="off"`. |
|
|
125
|
+
| `locale` | `"auto"` | `"en"` or `"ar"` (right to left). Auto follows `<html lang>`, then the browser. |
|
|
126
|
+
| `theme` | `"auto"` | `"light"`, `"dark"` or `"auto"`. |
|
|
127
|
+
| `strings` | — | Replace any modal text. |
|
|
128
|
+
| `relay` | hosted | Your own relay URL. |
|
|
129
|
+
| `signal` | — | An `AbortSignal` that cancels the picker. |
|
|
130
|
+
|
|
131
|
+
### Direct transfers (same Wi-Fi)
|
|
132
|
+
|
|
133
|
+
The phone and the page set up a WebRTC data channel through the relay. No STUN or TURN servers are
|
|
134
|
+
used, so only local-network addresses (which browsers hide behind random `.local` names) can connect:
|
|
135
|
+
it works on the same network and nowhere else, and neither side learns the other's public IP. The
|
|
136
|
+
relay still approves every file first (count, size, type, usage), and the page checks what arrives
|
|
137
|
+
exactly as the relay checks a cloud upload: the announced size, and contents that match `accept`.
|
|
138
|
+
If the link isn't there or drops mid-file, that file goes through the cloud under the same id. The
|
|
139
|
+
`direct` event (`{ connected }`) and "Connected over Wi-Fi" in the built-in UI show when it's on.
|
|
140
|
+
|
|
141
|
+
Style the modal with CSS variables on `[data-tokshare-pick]`: `--tsp-accent`, `--tsp-accent-text`,
|
|
142
|
+
`--tsp-bg`, `--tsp-text`, `--tsp-muted`, `--tsp-line`, `--tsp-radius`, `--tsp-font`.
|
|
143
|
+
|
|
144
|
+
## Content-Security-Policy
|
|
145
|
+
|
|
146
|
+
Add the relay to `connect-src` (HTTPS and WSS). The modal needs nothing else: it uses a Shadow DOM
|
|
147
|
+
with a constructable stylesheet and draws the QR on a canvas.
|
|
148
|
+
|
|
149
|
+
## Privacy
|
|
150
|
+
|
|
151
|
+
Files travel from the phone to your page through TokShare's relay and are deleted as soon as your page
|
|
152
|
+
receives them (at most 10 minutes after upload). Contents are never inspected beyond the first bytes
|
|
153
|
+
used to check the file type, and never logged.
|