foxact 0.3.3 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "foxact",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "React Hooks/Utils done right. For browser, SSR, and React Server Components.",
5
5
  "homepage": "https://foxact.skk.moe",
6
6
  "repository": {
@@ -61,24 +61,6 @@
61
61
  "require": "./compose-context-provider/index.cjs",
62
62
  "default": "./compose-context-provider/index.cjs"
63
63
  },
64
- "./context-state": {
65
- "types": "./context-state/index.d.ts",
66
- "import": {
67
- "types": "./context-state/index.d.ts",
68
- "default": "./context-state/index.mjs"
69
- },
70
- "require": "./context-state/index.cjs",
71
- "default": "./context-state/index.cjs"
72
- },
73
- "./create-context-reducer": {
74
- "types": "./create-context-reducer/index.d.ts",
75
- "import": {
76
- "types": "./create-context-reducer/index.d.ts",
77
- "default": "./create-context-reducer/index.mjs"
78
- },
79
- "require": "./create-context-reducer/index.cjs",
80
- "default": "./create-context-reducer/index.cjs"
81
- },
82
64
  "./context-reducer": {
83
65
  "types": "./context-reducer/index.d.ts",
84
66
  "import": {
@@ -115,6 +97,24 @@
115
97
  "require": "./create-local-storage-state/index.cjs",
116
98
  "default": "./create-local-storage-state/index.cjs"
117
99
  },
100
+ "./create-context-reducer": {
101
+ "types": "./create-context-reducer/index.d.ts",
102
+ "import": {
103
+ "types": "./create-context-reducer/index.d.ts",
104
+ "default": "./create-context-reducer/index.mjs"
105
+ },
106
+ "require": "./create-context-reducer/index.cjs",
107
+ "default": "./create-context-reducer/index.cjs"
108
+ },
109
+ "./context-state": {
110
+ "types": "./context-state/index.d.ts",
111
+ "import": {
112
+ "types": "./context-state/index.d.ts",
113
+ "default": "./context-state/index.mjs"
114
+ },
115
+ "require": "./context-state/index.cjs",
116
+ "default": "./context-state/index.cjs"
117
+ },
118
118
  "./create-magic-portal": {
119
119
  "types": "./create-magic-portal/index.d.ts",
120
120
  "import": {
@@ -142,6 +142,15 @@
142
142
  "require": "./create-storage-hook/index.cjs",
143
143
  "default": "./create-storage-hook/index.cjs"
144
144
  },
145
+ "./create-session-storage-state": {
146
+ "types": "./create-session-storage-state/index.d.ts",
147
+ "import": {
148
+ "types": "./create-session-storage-state/index.d.ts",
149
+ "default": "./create-session-storage-state/index.mjs"
150
+ },
151
+ "require": "./create-session-storage-state/index.cjs",
152
+ "default": "./create-session-storage-state/index.cjs"
153
+ },
145
154
  "./create-storage-state-factory": {
146
155
  "types": "./create-storage-state-factory/index.d.ts",
147
156
  "import": {
@@ -169,14 +178,14 @@
169
178
  "require": "./email-protection/index.cjs",
170
179
  "default": "./email-protection/index.cjs"
171
180
  },
172
- "./fetch-jsonp": {
173
- "types": "./fetch-jsonp/index.d.ts",
181
+ "./is-safari": {
182
+ "types": "./is-safari/index.d.ts",
174
183
  "import": {
175
- "types": "./fetch-jsonp/index.d.ts",
176
- "default": "./fetch-jsonp/index.mjs"
184
+ "types": "./is-safari/index.d.ts",
185
+ "default": "./is-safari/index.mjs"
177
186
  },
178
- "require": "./fetch-jsonp/index.cjs",
179
- "default": "./fetch-jsonp/index.cjs"
187
+ "require": "./is-safari/index.cjs",
188
+ "default": "./is-safari/index.cjs"
180
189
  },
181
190
  "./invariant": {
182
191
  "types": "./invariant/index.d.ts",
@@ -187,14 +196,14 @@
187
196
  "require": "./invariant/index.cjs",
188
197
  "default": "./invariant/index.cjs"
189
198
  },
190
- "./is-safari": {
191
- "types": "./is-safari/index.d.ts",
199
+ "./fetch-jsonp": {
200
+ "types": "./fetch-jsonp/index.d.ts",
192
201
  "import": {
193
- "types": "./is-safari/index.d.ts",
194
- "default": "./is-safari/index.mjs"
202
+ "types": "./fetch-jsonp/index.d.ts",
203
+ "default": "./fetch-jsonp/index.mjs"
195
204
  },
196
- "require": "./is-safari/index.cjs",
197
- "default": "./is-safari/index.cjs"
205
+ "require": "./fetch-jsonp/index.cjs",
206
+ "default": "./fetch-jsonp/index.cjs"
198
207
  },
199
208
  "./merge-refs": {
200
209
  "types": "./merge-refs/index.d.ts",
@@ -205,15 +214,6 @@
205
214
  "require": "./merge-refs/index.cjs",
206
215
  "default": "./merge-refs/index.cjs"
207
216
  },
208
- "./magic-portal": {
209
- "types": "./magic-portal/index.d.ts",
210
- "import": {
211
- "types": "./magic-portal/index.d.ts",
212
- "default": "./magic-portal/index.mjs"
213
- },
214
- "require": "./magic-portal/index.cjs",
215
- "default": "./magic-portal/index.cjs"
216
- },
217
217
  "./no-ssr": {
218
218
  "types": "./no-ssr/index.d.ts",
219
219
  "import": {
@@ -223,14 +223,14 @@
223
223
  "require": "./no-ssr/index.cjs",
224
224
  "default": "./no-ssr/index.cjs"
225
225
  },
226
- "./create-session-storage-state": {
227
- "types": "./create-session-storage-state/index.d.ts",
226
+ "./magic-portal": {
227
+ "types": "./magic-portal/index.d.ts",
228
228
  "import": {
229
- "types": "./create-session-storage-state/index.d.ts",
230
- "default": "./create-session-storage-state/index.mjs"
229
+ "types": "./magic-portal/index.d.ts",
230
+ "default": "./magic-portal/index.mjs"
231
231
  },
232
- "require": "./create-session-storage-state/index.cjs",
233
- "default": "./create-session-storage-state/index.cjs"
232
+ "require": "./magic-portal/index.cjs",
233
+ "default": "./magic-portal/index.cjs"
234
234
  },
235
235
  "./noop": {
236
236
  "types": "./noop/index.d.ts",
@@ -241,15 +241,6 @@
241
241
  "require": "./noop/index.cjs",
242
242
  "default": "./noop/index.cjs"
243
243
  },
244
- "./noop-component": {
245
- "types": "./noop-component/index.d.ts",
246
- "import": {
247
- "types": "./noop-component/index.d.ts",
248
- "default": "./noop-component/index.mjs"
249
- },
250
- "require": "./noop-component/index.cjs",
251
- "default": "./noop-component/index.cjs"
252
- },
253
244
  "./nullthrow": {
254
245
  "types": "./nullthrow/index.d.ts",
255
246
  "import": {
@@ -259,14 +250,14 @@
259
250
  "require": "./nullthrow/index.cjs",
260
251
  "default": "./nullthrow/index.cjs"
261
252
  },
262
- "./rem": {
263
- "types": "./rem/index.d.ts",
253
+ "./noop-component": {
254
+ "types": "./noop-component/index.d.ts",
264
255
  "import": {
265
- "types": "./rem/index.d.ts",
266
- "default": "./rem/index.mjs"
256
+ "types": "./noop-component/index.d.ts",
257
+ "default": "./noop-component/index.mjs"
267
258
  },
268
- "require": "./rem/index.cjs",
269
- "default": "./rem/index.cjs"
259
+ "require": "./noop-component/index.cjs",
260
+ "default": "./noop-component/index.cjs"
270
261
  },
271
262
  "./open-new-tab": {
272
263
  "types": "./open-new-tab/index.d.ts",
@@ -277,6 +268,15 @@
277
268
  "require": "./open-new-tab/index.cjs",
278
269
  "default": "./open-new-tab/index.cjs"
279
270
  },
271
+ "./rem": {
272
+ "types": "./rem/index.d.ts",
273
+ "import": {
274
+ "types": "./rem/index.d.ts",
275
+ "default": "./rem/index.mjs"
276
+ },
277
+ "require": "./rem/index.cjs",
278
+ "default": "./rem/index.cjs"
279
+ },
280
280
  "./request-idle-callback": {
281
281
  "types": "./request-idle-callback/index.d.ts",
282
282
  "import": {
@@ -376,15 +376,6 @@
376
376
  "require": "./use-debounced-value/index.cjs",
377
377
  "default": "./use-debounced-value/index.cjs"
378
378
  },
379
- "./use-intersection": {
380
- "types": "./use-intersection/index.d.ts",
381
- "import": {
382
- "types": "./use-intersection/index.d.ts",
383
- "default": "./use-intersection/index.mjs"
384
- },
385
- "require": "./use-intersection/index.cjs",
386
- "default": "./use-intersection/index.cjs"
387
- },
388
379
  "./use-error-boundary": {
389
380
  "types": "./use-error-boundary/index.d.ts",
390
381
  "import": {
@@ -403,6 +394,15 @@
403
394
  "require": "./use-fast-click/index.cjs",
404
395
  "default": "./use-fast-click/index.cjs"
405
396
  },
397
+ "./use-intersection": {
398
+ "types": "./use-intersection/index.d.ts",
399
+ "import": {
400
+ "types": "./use-intersection/index.d.ts",
401
+ "default": "./use-intersection/index.mjs"
402
+ },
403
+ "require": "./use-intersection/index.cjs",
404
+ "default": "./use-intersection/index.cjs"
405
+ },
406
406
  "./use-is-client": {
407
407
  "types": "./use-is-client/index.d.ts",
408
408
  "import": {
@@ -412,15 +412,6 @@
412
412
  "require": "./use-is-client/index.cjs",
413
413
  "default": "./use-is-client/index.cjs"
414
414
  },
415
- "./use-is-online": {
416
- "types": "./use-is-online/index.d.ts",
417
- "import": {
418
- "types": "./use-is-online/index.d.ts",
419
- "default": "./use-is-online/index.mjs"
420
- },
421
- "require": "./use-is-online/index.cjs",
422
- "default": "./use-is-online/index.cjs"
423
- },
424
415
  "./use-isomorphic-layout-effect": {
425
416
  "types": "./use-isomorphic-layout-effect/index.d.ts",
426
417
  "import": {
@@ -430,6 +421,15 @@
430
421
  "require": "./use-isomorphic-layout-effect/index.cjs",
431
422
  "default": "./use-isomorphic-layout-effect/index.cjs"
432
423
  },
424
+ "./use-is-online": {
425
+ "types": "./use-is-online/index.d.ts",
426
+ "import": {
427
+ "types": "./use-is-online/index.d.ts",
428
+ "default": "./use-is-online/index.mjs"
429
+ },
430
+ "require": "./use-is-online/index.cjs",
431
+ "default": "./use-is-online/index.cjs"
432
+ },
433
433
  "./use-local-storage": {
434
434
  "types": "./use-local-storage/index.d.ts",
435
435
  "import": {
@@ -493,15 +493,6 @@
493
493
  "require": "./use-session-storage/index.cjs",
494
494
  "default": "./use-session-storage/index.cjs"
495
495
  },
496
- "./use-set": {
497
- "types": "./use-set/index.d.ts",
498
- "import": {
499
- "types": "./use-set/index.d.ts",
500
- "default": "./use-set/index.mjs"
501
- },
502
- "require": "./use-set/index.cjs",
503
- "default": "./use-set/index.cjs"
504
- },
505
496
  "./use-singleton": {
506
497
  "types": "./use-singleton/index.d.ts",
507
498
  "import": {
@@ -511,14 +502,14 @@
511
502
  "require": "./use-singleton/index.cjs",
512
503
  "default": "./use-singleton/index.cjs"
513
504
  },
514
- "./use-state-with-deps": {
515
- "types": "./use-state-with-deps/index.d.ts",
505
+ "./use-set": {
506
+ "types": "./use-set/index.d.ts",
516
507
  "import": {
517
- "types": "./use-state-with-deps/index.d.ts",
518
- "default": "./use-state-with-deps/index.mjs"
508
+ "types": "./use-set/index.d.ts",
509
+ "default": "./use-set/index.mjs"
519
510
  },
520
- "require": "./use-state-with-deps/index.cjs",
521
- "default": "./use-state-with-deps/index.cjs"
511
+ "require": "./use-set/index.cjs",
512
+ "default": "./use-set/index.cjs"
522
513
  },
523
514
  "./use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired": {
524
515
  "types": "./use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired/index.d.ts",
@@ -538,6 +529,15 @@
538
529
  "require": "./use-typescript-happy-callback/index.cjs",
539
530
  "default": "./use-typescript-happy-callback/index.cjs"
540
531
  },
532
+ "./use-state-with-deps": {
533
+ "types": "./use-state-with-deps/index.d.ts",
534
+ "import": {
535
+ "types": "./use-state-with-deps/index.d.ts",
536
+ "default": "./use-state-with-deps/index.mjs"
537
+ },
538
+ "require": "./use-state-with-deps/index.cjs",
539
+ "default": "./use-state-with-deps/index.cjs"
540
+ },
541
541
  "./use-uncontrolled": {
542
542
  "types": "./use-uncontrolled/index.d.ts",
543
543
  "import": {
package/sizes.json CHANGED
@@ -1 +1 @@
1
- {"total":{"raw":25768,"gzip":15616,"br":0},"exports":{"compose-context-provider":{"raw":177,"gzip":155,"br":122},"create-context-reducer":{"raw":223,"gzip":146,"br":117},"context-state":{"raw":402,"gzip":259,"br":218},"context-reducer":{"raw":557,"gzip":332,"br":319},"create-context-state":{"raw":145,"gzip":116,"br":96},"create-magic-portal":{"raw":268,"gzip":148,"br":121},"create-fixed-array":{"raw":452,"gzip":282,"br":248},"create-local-storage-state":{"raw":446,"gzip":222,"br":209},"create-stackless-error":{"raw":278,"gzip":198,"br":162},"current-year":{"raw":524,"gzip":355,"br":293},"create-storage-state-factory":{"raw":620,"gzip":341,"br":290},"email-protection":{"raw":426,"gzip":321,"br":272},"is-safari":{"raw":233,"gzip":164,"br":119},"invariant":{"raw":178,"gzip":156,"br":118},"fetch-jsonp":{"raw":890,"gzip":546,"br":429},"no-ssr":{"raw":338,"gzip":267,"br":219},"merge-refs":{"raw":525,"gzip":280,"br":235},"noop-component":{"raw":142,"gzip":130,"br":111},"create-storage-hook":{"raw":2462,"gzip":972,"br":852},"create-session-storage-state":{"raw":450,"gzip":223,"br":206},"noop":{"raw":33,"gzip":53,"br":37},"magic-portal":{"raw":1083,"gzip":536,"br":472},"nullthrow":{"raw":187,"gzip":163,"br":121},"types":{"raw":0,"gzip":20,"br":1},"open-new-tab":{"raw":335,"gzip":255,"br":187},"typescript-happy-forward-ref":{"raw":119,"gzip":103,"br":80},"request-idle-callback":{"raw":392,"gzip":221,"br":171},"use-abortable-effect":{"raw":215,"gzip":170,"br":144},"rem":{"raw":823,"gzip":395,"br":350},"use":{"raw":298,"gzip":197,"br":160},"use-array":{"raw":322,"gzip":236,"br":202},"use-component-will-receive-update":{"raw":216,"gzip":188,"br":154},"use-debounced-state":{"raw":401,"gzip":280,"br":242},"use-composition-input":{"raw":470,"gzip":286,"br":241},"use-debounced-value":{"raw":524,"gzip":342,"br":286},"use-clipboard":{"raw":1177,"gzip":650,"br":552},"use-error-boundary":{"raw":252,"gzip":195,"br":165},"use-is-client":{"raw":204,"gzip":153,"br":121},"use-fast-click":{"raw":622,"gzip":392,"br":317},"use-intersection":{"raw":1230,"gzip":649,"br":597},"use-isomorphic-layout-effect":{"raw":169,"gzip":137,"br":110},"use-is-online":{"raw":370,"gzip":258,"br":220},"use-local-storage":{"raw":428,"gzip":226,"br":198},"use-map":{"raw":339,"gzip":243,"br":208},"use-page-visibility":{"raw":359,"gzip":263,"br":210},"use-retimer":{"raw":199,"gzip":166,"br":127},"use-media-query":{"raw":648,"gzip":397,"br":332},"use-set":{"raw":340,"gzip":238,"br":203},"use-singleton":{"raw":149,"gzip":135,"br":116},"use-session-storage":{"raw":434,"gzip":225,"br":210},"use-readonly-search-params":{"raw":1164,"gzip":589,"br":496},"use-typescript-happy-callback":{"raw":107,"gzip":102,"br":77},"use-state-with-deps":{"raw":464,"gzip":321,"br":295},"use-uncontrolled":{"raw":388,"gzip":273,"br":230},"use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired":{"raw":450,"gzip":318,"br":249},"use-url-hash-state":{"raw":1121,"gzip":628,"br":556}}}
1
+ {"total":{"raw":27314,"gzip":16298,"br":0},"exports":{"create-context-state":{"raw":145,"gzip":116,"br":96},"create-local-storage-state":{"raw":446,"gzip":222,"br":209},"create-fixed-array":{"raw":452,"gzip":282,"br":248},"context-reducer":{"raw":557,"gzip":332,"br":319},"create-context-reducer":{"raw":223,"gzip":146,"br":117},"compose-context-provider":{"raw":177,"gzip":155,"br":122},"create-magic-portal":{"raw":268,"gzip":148,"br":121},"context-state":{"raw":402,"gzip":259,"br":218},"create-stackless-error":{"raw":278,"gzip":198,"br":162},"create-storage-state-factory":{"raw":620,"gzip":341,"br":290},"is-safari":{"raw":233,"gzip":164,"br":119},"current-year":{"raw":524,"gzip":355,"br":293},"invariant":{"raw":178,"gzip":156,"br":118},"create-session-storage-state":{"raw":450,"gzip":223,"br":206},"email-protection":{"raw":426,"gzip":321,"br":272},"create-storage-hook":{"raw":2462,"gzip":972,"br":852},"no-ssr":{"raw":338,"gzip":267,"br":219},"merge-refs":{"raw":525,"gzip":280,"br":235},"noop":{"raw":33,"gzip":53,"br":37},"fetch-jsonp":{"raw":890,"gzip":546,"br":429},"nullthrow":{"raw":187,"gzip":163,"br":121},"noop-component":{"raw":142,"gzip":130,"br":111},"open-new-tab":{"raw":335,"gzip":255,"br":187},"typescript-happy-forward-ref":{"raw":119,"gzip":103,"br":80},"magic-portal":{"raw":1083,"gzip":536,"br":472},"types":{"raw":0,"gzip":20,"br":1},"request-idle-callback":{"raw":392,"gzip":221,"br":171},"use":{"raw":298,"gzip":197,"br":160},"use-abortable-effect":{"raw":215,"gzip":170,"br":144},"rem":{"raw":823,"gzip":395,"br":350},"use-array":{"raw":322,"gzip":236,"br":202},"use-composition-input":{"raw":470,"gzip":286,"br":241},"use-component-will-receive-update":{"raw":216,"gzip":188,"br":154},"use-debounced-state":{"raw":401,"gzip":280,"br":242},"use-debounced-value":{"raw":524,"gzip":342,"br":286},"use-error-boundary":{"raw":252,"gzip":195,"br":165},"use-is-client":{"raw":204,"gzip":153,"br":121},"use-clipboard":{"raw":2723,"gzip":1332,"br":1140},"use-fast-click":{"raw":622,"gzip":392,"br":317},"use-isomorphic-layout-effect":{"raw":169,"gzip":137,"br":110},"use-is-online":{"raw":370,"gzip":258,"br":220},"use-local-storage":{"raw":428,"gzip":226,"br":198},"use-map":{"raw":339,"gzip":243,"br":208},"use-intersection":{"raw":1230,"gzip":649,"br":597},"use-retimer":{"raw":199,"gzip":166,"br":127},"use-page-visibility":{"raw":359,"gzip":263,"br":210},"use-media-query":{"raw":648,"gzip":397,"br":332},"use-singleton":{"raw":149,"gzip":135,"br":116},"use-readonly-search-params":{"raw":1164,"gzip":589,"br":496},"use-session-storage":{"raw":434,"gzip":225,"br":210},"use-set":{"raw":340,"gzip":238,"br":203},"use-typescript-happy-callback":{"raw":107,"gzip":102,"br":77},"use-state-with-deps":{"raw":464,"gzip":321,"br":295},"use-uncontrolled":{"raw":388,"gzip":273,"br":230},"use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired":{"raw":450,"gzip":318,"br":249},"use-url-hash-state":{"raw":1121,"gzip":628,"br":556}}}
@@ -1 +1 @@
1
- "use strict";require("client-only");var e=require("react"),r=require("../noop/index.cjs"),t=require("../use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired/index.cjs"),a=require("../use-typescript-happy-callback/index.cjs");class o extends Error{constructor(e){super(e),this.name="UseClipboardError"}}exports.UseClipboardError=o,exports.useClipboard=function(){let{timeout:l=1e3,usePromptAsFallback:i=!1,promptFallbackText:c="Failed to copy to clipboard automatically, please manually copy the text below.",onCopyError:n}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const[u,s]=e.useState(null),[p,d]=e.useState(!1),b=e.useRef(null),y=t.useStableHandler(n||r.noop),w=a.useCallback(e=>{b.current&&clearTimeout(b.current),e&&(b.current=window.setTimeout(()=>d(!1),l)),d(e)},[l]),h=a.useCallback(e=>{s(e),y(e)},[y]);return{copy:a.useCallback(async e=>{try{if("clipboard"in navigator)await navigator.clipboard.writeText(e),w(!0);else throw new o("[foxact/use-clipboard] navigator.clipboard is not supported")}catch(r){if(i)try{window.prompt(c,e)}catch(e){h(e)}else h(r)}},[w,c,h,i]),reset:a.useCallback(()=>{d(!1),s(null),b.current&&clearTimeout(b.current)},[]),error:u,copied:p}};
1
+ "use strict";require("client-only");var e=require("react"),t=require("../noop/index.cjs"),r=require("../use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired/index.cjs"),l=require("../use-typescript-happy-callback/index.cjs");class a extends Error{constructor(...e){var t;super(...e),(t="name")in this?Object.defineProperty(this,t,{value:"UseClipboardError",enumerable:!0,configurable:!0,writable:!0}):this[t]="UseClipboardError"}}async function o(e,t,r,l){let o;if("clipboard"in navigator)try{return await navigator.clipboard.writeText(e),!0}catch(e){(o=new a("[foxact/use-clipboard] Failed to copy using navigator.clipboard")).cause=e}else o=new a("[foxact/use-clipboard] navigator.clipboard is not supported");if(t){var n;let t;const r=document.activeElement,l=[],c=document.getSelection();let s=null,i=null!=(n=document.fullscreenElement)?n:document.body,u=r;for(;u;){if("DIALOG"===u.tagName&&u.open){i=u;break}u=u.parentElement}let d=null;try{if(c.rangeCount){for(let e=0;e<c.rangeCount;e++)l.push(c.getRangeAt(e));c.removeAllRanges()}((null==r?void 0:r.tagName)==="INPUT"||(null==r?void 0:r.tagName)==="TEXTAREA")&&(s=r).blur();const a=document.createRange();(d=document.createElement("span")).textContent=e,d.style.whiteSpace="pre",d.ariaHidden="true",d.style.all="unset",d.style.position="fixed",d.style.top="0",d.style.clip="rect(0, 0, 0, 0)",d.style.borderWidth="0",d.style.overflow="hidden",d.style.userSelect="text",d.style.webkitUserSelect="text",d.style.MozUserSelect="text",d.style.msUserSelect="text",d.addEventListener("copy",e=>{e.stopPropagation()}),i.appendChild(d),a.selectNodeContents(d),c.removeAllRanges(),c.addRange(a),t=document.execCommand("copy")}catch(e){(o=new a("[foxact/use-clipboard] Failed to copy using execCommand fallback")).cause=e,t=!1}finally{c.removeAllRanges(),d&&i.removeChild(d),"Caret"===c.type&&c.removeAllRanges(),c.rangeCount||l.forEach(e=>c.addRange(e)),null==s||s.focus()}if(t)return!0}return r?(window.prompt(l,e),!1):o}exports.UseClipboardError=a,exports.useClipboard=function(){let{timeout:a=1e3,useExecCommandAsFallback:n=!0,usePromptAsFallback:c=!1,promptFallbackText:s="Failed to copy to clipboard, please manually copy the text below.",onCopyError:i}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const[u,d]=e.useState(null),[p,y]=e.useState(!1),b=e.useRef(null),m=r.useStableHandler(i||t.noop),g=l.useCallback(e=>{b.current&&clearTimeout(b.current),e&&(b.current=window.setTimeout(()=>y(!1),a)),y(e)},[a]),f=l.useCallback(e=>{d(e),m(e)},[m]);return{copy:l.useCallback(async e=>{try{const t=await o(e,n,c,s);"boolean"==typeof t?t&&g(!0):f(t)}catch(e){f(e)}},[g,s,f,n,c]),reset:l.useCallback(()=>{y(!1),d(null),b.current&&clearTimeout(b.current)},[]),error:u,copied:p}};
@@ -1,14 +1,17 @@
1
1
  declare class UseClipboardError extends Error {
2
- constructor(message: string);
2
+ readonly name = "UseClipboardError";
3
3
  }
4
4
  interface UseClipboardOption {
5
5
  timeout?: number;
6
+ /** @default true */
7
+ useExecCommandAsFallback?: boolean;
8
+ /** @default false */
6
9
  usePromptAsFallback?: boolean;
7
10
  promptFallbackText?: string;
8
11
  onCopyError?: (error: Error) => void;
9
12
  }
10
13
  /** @see https://foxact.skk.moe/use-clipboard */
11
- declare function useClipboard({ timeout, usePromptAsFallback, promptFallbackText, onCopyError }?: UseClipboardOption): {
14
+ declare function useClipboard({ timeout, useExecCommandAsFallback, usePromptAsFallback, promptFallbackText, onCopyError }?: UseClipboardOption): {
12
15
  copy: (valueToCopy: string) => Promise<void>;
13
16
  reset: () => void;
14
17
  error: Error | null;
@@ -1 +1 @@
1
- import"client-only";import{useState as r,useRef as e}from"react";import{noop as o}from"../noop/index.mjs";import{useStableHandler as t}from"../use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired/index.mjs";import{useCallback as i}from"../use-typescript-happy-callback/index.mjs";class a extends Error{constructor(r){super(r),this.name="UseClipboardError"}}function l(){let{timeout:l=1e3,usePromptAsFallback:n=!1,promptFallbackText:c="Failed to copy to clipboard automatically, please manually copy the text below.",onCopyError:p}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const[s,u]=r(null),[d,m]=r(!1),y=e(null),w=t(p||o),b=i(r=>{y.current&&clearTimeout(y.current),r&&(y.current=window.setTimeout(()=>m(!1),l)),m(r)},[l]),h=i(r=>{u(r),w(r)},[w]);return{copy:i(async r=>{try{if("clipboard"in navigator)await navigator.clipboard.writeText(r),b(!0);else throw new a("[foxact/use-clipboard] navigator.clipboard is not supported")}catch(e){if(n)try{window.prompt(c,r)}catch(r){h(r)}else h(e)}},[b,c,h,n]),reset:i(()=>{m(!1),u(null),y.current&&clearTimeout(y.current)},[]),error:s,copied:d}}export{a as UseClipboardError,l as useClipboard};
1
+ import"client-only";import{useState as e,useRef as t}from"react";import{noop as o}from"../noop/index.mjs";import{useStableHandler as r}from"../use-stable-handler-only-when-you-know-what-you-are-doing-or-you-will-be-fired/index.mjs";import{useCallback as n}from"../use-typescript-happy-callback/index.mjs";class l extends Error{constructor(...e){var t;super(...e),(t="name")in this?Object.defineProperty(this,t,{value:"UseClipboardError",enumerable:!0,configurable:!0,writable:!0}):this[t]="UseClipboardError"}}function a(){let{timeout:l=1e3,useExecCommandAsFallback:a=!0,usePromptAsFallback:c=!1,promptFallbackText:s="Failed to copy to clipboard, please manually copy the text below.",onCopyError:u}=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};const[d,p]=e(null),[m,y]=e(!1),b=t(null),f=r(u||o),g=n(e=>{b.current&&clearTimeout(b.current),e&&(b.current=window.setTimeout(()=>y(!1),l)),y(e)},[l]),h=n(e=>{p(e),f(e)},[f]);return{copy:n(async e=>{try{const t=await i(e,a,c,s);"boolean"==typeof t?t&&g(!0):h(t)}catch(e){h(e)}},[g,s,h,a,c]),reset:n(()=>{y(!1),p(null),b.current&&clearTimeout(b.current)},[]),error:d,copied:m}}async function i(e,t,o,r){let n;if("clipboard"in navigator)try{return await navigator.clipboard.writeText(e),!0}catch(e){(n=new l("[foxact/use-clipboard] Failed to copy using navigator.clipboard")).cause=e}else n=new l("[foxact/use-clipboard] navigator.clipboard is not supported");if(t){var a;let t;const o=document.activeElement,r=[],i=document.getSelection();let c=null,s=null!=(a=document.fullscreenElement)?a:document.body,u=o;for(;u;){if("DIALOG"===u.tagName&&u.open){s=u;break}u=u.parentElement}let d=null;try{if(i.rangeCount){for(let e=0;e<i.rangeCount;e++)r.push(i.getRangeAt(e));i.removeAllRanges()}((null==o?void 0:o.tagName)==="INPUT"||(null==o?void 0:o.tagName)==="TEXTAREA")&&(c=o).blur();const n=document.createRange();(d=document.createElement("span")).textContent=e,d.style.whiteSpace="pre",d.ariaHidden="true",d.style.all="unset",d.style.position="fixed",d.style.top="0",d.style.clip="rect(0, 0, 0, 0)",d.style.borderWidth="0",d.style.overflow="hidden",d.style.userSelect="text",d.style.webkitUserSelect="text",d.style.MozUserSelect="text",d.style.msUserSelect="text",d.addEventListener("copy",e=>{e.stopPropagation()}),s.appendChild(d),n.selectNodeContents(d),i.removeAllRanges(),i.addRange(n),t=document.execCommand("copy")}catch(e){(n=new l("[foxact/use-clipboard] Failed to copy using execCommand fallback")).cause=e,t=!1}finally{i.removeAllRanges(),d&&s.removeChild(d),"Caret"===i.type&&i.removeAllRanges(),i.rangeCount||r.forEach(e=>i.addRange(e)),null==c||c.focus()}if(t)return!0}return o?(window.prompt(r,e),!1):n}export{l as UseClipboardError,a as useClipboard};