@rexezuge/web 1.5.2 → 1.6.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.
@@ -29,6 +29,13 @@
29
29
  * A false success sends the operator away believing they have something they do
30
30
  * not.
31
31
  *
32
+ * ## `reset`
33
+ *
34
+ * The timer is cleared on unmount, but that is not the only way the affordance
35
+ * goes away: a clone menu that closes on an outside click hides the button
36
+ * while its feedback window is still running. `reset` covers that, and cancels
37
+ * the pending timer so the later unmount has nothing left to clear.
38
+ *
32
39
  * ## Timer handling
33
40
  *
34
41
  * The reset timer is a ref and is cleared on unmount. Local `setTimeout` handles
@@ -49,6 +56,17 @@ interface UseCopyToClipboardResult {
49
56
  Writes `text`, or does nothing if the clipboard is unavailable.
50
57
  */
51
58
  copy: (text: string) => void;
59
+ /**
60
+ * Clear the feedback early, cancelling the pending reset.
61
+ *
62
+ * For a host that hides the affordance itself — a clone menu that closes on
63
+ * an outside click, a tab that is no longer showing the field. Without this
64
+ * the only way to clear `copied` is to wait out the timer, and a consumer
65
+ * that hides the button is left holding a stale `true` for a copy whose
66
+ * confirmation is no longer on screen. Cancelling the timer here is what
67
+ * keeps a later unmount from clearing state twice.
68
+ */
69
+ reset: () => void;
52
70
  }
53
71
  declare function useCopyToClipboard(): UseCopyToClipboardResult;
54
72
  export { useCopyToClipboard };
@@ -1 +1 @@
1
- {"version":3,"file":"useCopyToClipboard.d.ts","sourceRoot":"","sources":["../src/useCopyToClipboard.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,UAAU,wBAAwB;IAChC;;;;;OAKG;IACH,MAAM,EAAE,OAAO,CAAC;IAChB;;MAEE;IACF,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAC9B;AAED,iBAAS,kBAAkB,IAAI,wBAAwB,CAuCtD;AAED,OAAO,EAAE,kBAAkB,EAAE,CAAC;AAC9B,YAAY,EAAE,wBAAwB,EAAE,CAAC"}
1
+ {"version":3,"file":"useCopyToClipboard.d.ts","sourceRoot":"","sources":["../src/useCopyToClipboard.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AAEH,UAAU,wBAAwB;IAChC;;;;;OAKG;IACH,MAAM,EAAE,OAAO,CAAC;IAChB;;MAEE;IACF,IAAI,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;IAC7B;;;;;;;;;OASG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,iBAAS,kBAAkB,IAAI,wBAAwB,CA6CtD;AAED,OAAO,EAAE,kBAAkB,EAAE,CAAC;AAC9B,YAAY,EAAE,wBAAwB,EAAE,CAAC"}
@@ -1 +1 @@
1
- var i=Object.defineProperty;var u=(t,r)=>i(t,"name",{value:r,configurable:!0});import{useCallback as l,useEffect as s,useRef as a,useState as f}from"react";import{COPY_FEEDBACK_TIMEOUT_MS as p}from"./constants.js";function T(){const[t,r]=f(!1),e=a(null);s(()=>()=>{e.current!==null&&clearTimeout(e.current)},[]);const c=l(n=>{const o=globalThis.navigator?.clipboard;o&&o.writeText(n).then(()=>{r(!0),e.current!==null&&clearTimeout(e.current),e.current=setTimeout(()=>{e.current=null,r(!1)},p)}).catch(()=>{})},[]);return{copied:t,copy:c}}u(T,"useCopyToClipboard");export{T as useCopyToClipboard};
1
+ var s=Object.defineProperty;var c=(t,r)=>s(t,"name",{value:r,configurable:!0});import{useCallback as n,useEffect as a,useRef as f,useState as p}from"react";import{COPY_FEEDBACK_TIMEOUT_MS as T}from"./constants.js";function m(){const[t,r]=p(!1),e=f(null);a(()=>()=>{e.current!==null&&clearTimeout(e.current)},[]);const o=n(i=>{const u=globalThis.navigator?.clipboard;u&&u.writeText(i).then(()=>{r(!0),e.current!==null&&clearTimeout(e.current),e.current=setTimeout(()=>{e.current=null,r(!1)},T)}).catch(()=>{})},[]),l=n(()=>{e.current!==null&&clearTimeout(e.current),e.current=null,r(!1)},[]);return{copied:t,copy:o,reset:l}}c(m,"useCopyToClipboard");export{m as useCopyToClipboard};
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/useCopyToClipboard.ts"],
4
- "sourcesContent": ["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { COPY_FEEDBACK_TIMEOUT_MS } from './constants.ts';\n\n/**\n * Copy text to the clipboard, reporting success only when it happened.\n *\n * ## Provenance\n *\n * `hooks/useCopyToClipboard.ts` in **Durable-DAV** — the one copy that had\n * solved the two failures the others carried. The other three handlers were\n * inlined into `ReadOnlyField` (Edge-Git, Mail-Otter, Durable-DAV-Router) and\n * `TypeToConfirmModal`, and were byte-identical to each other.\n *\n * ## The two live bugs this converges away\n *\n * 1. **`navigator.clipboard` is absent in an insecure context.** A plain\n * `navigator.clipboard.writeText` throws *synchronously* there, so the\n * handler dies before setting state. Where it was guarded with\n * `if (clipboard)`, the guard fell through to `setCopied(true)` anyway and\n * showed a checkmark for a copy that never happened.\n * 2. **The write is a promise that rejects on a denied permission.** Awaiting\n * it is not enough; the rejection has to be handled, or it surfaces as an\n * unhandled rejection *and* the checkmark still appears.\n *\n * That second case is why this is a hook rather than a helper. The honest\n * answer — \"nothing was copied, so there is nothing to undo\" — is expressed by\n * `copied` simply never becoming true, which is state a helper function cannot\n * hold.\n *\n * Every call site in the family is a secret reveal: a bucket credential's\n * copy-once password, a resource name typed to confirm a destructive action.\n * A false success sends the operator away believing they have something they do\n * not.\n *\n * ## Timer handling\n *\n * The reset timer is a ref and is cleared on unmount. Local `setTimeout` handles\n * in the inlined copies leaked both ways: a modal closed inside the feedback\n * window set state on an unmounted component, and repeated clicks each replaced\n * the timer without cancelling the previous one, so an early timer could clear\n * the checkmark while a later copy was still pending.\n */\n\ninterface UseCopyToClipboardResult {\n /**\n * True only after a write that actually resolved.\n *\n * Never optimistically set: a denied permission or an insecure context leaves\n * this `false`, which is the answer that matters — there is nothing to undo.\n */\n copied: boolean;\n /**\n Writes `text`, or does nothing if the clipboard is unavailable.\n */\n copy: (text: string) => void;\n}\n\nfunction useCopyToClipboard(): UseCopyToClipboardResult {\n const [copied, setCopied] = useState(false);\n const resetTimer = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n useEffect(\n () => () => {\n if (resetTimer.current !== null) clearTimeout(resetTimer.current);\n },\n [],\n );\n\n const copy = useCallback((text: string): void => {\n // Optional chaining rather than an assumption: `navigator.clipboard` is\n // genuinely absent over plain HTTP, and for a self-hosted deployment over\n // plain HTTP that is a normal configuration, not an error to surface.\n const clipboard = globalThis.navigator?.clipboard as Clipboard | undefined;\n // Nothing was copied, so there is nothing to report and nothing to undo.\n if (!clipboard) return;\n\n void clipboard\n .writeText(text)\n .then(() => {\n setCopied(true);\n // Cancel any pending reset before arming a new one. Replacing the\n // handle without clearing leaves the earlier timer live, and it can\n // clear the checkmark for a copy that is still being reported.\n if (resetTimer.current !== null) clearTimeout(resetTimer.current);\n resetTimer.current = setTimeout(() => {\n resetTimer.current = null;\n setCopied(false);\n }, COPY_FEEDBACK_TIMEOUT_MS);\n })\n // Leave `copied` false rather than flashing and clearing it: a rejection\n // means the text is still on screen and unread, so the affordance must\n // not claim otherwise.\n .catch(() => {});\n }, []);\n\n return { copied, copy };\n}\n\nexport { useCopyToClipboard };\nexport type { UseCopyToClipboardResult };\n"],
5
- "mappings": "+EAAA,OAAS,eAAAA,EAAa,aAAAC,EAAW,UAAAC,EAAQ,YAAAC,MAAgB,QACzD,OAAS,4BAAAC,MAAgC,iBAwDzC,SAASC,GAAkB,CACzB,KAAM,CAACC,EAAQC,CAAS,EAAIJ,EAAS,EAAK,EACpCK,EAAaN,EAA6C,IAAI,EAEpED,EACE,IAAM,IAAK,CACLO,EAAW,UAAY,MAAM,aAAaA,EAAW,OAAO,CAClE,EACA,CAAA,CAAE,EAGJ,MAAMC,EAAOT,EAAaU,GAAsB,CAI9C,MAAMC,EAAY,WAAW,WAAW,UAEnCA,GAEAA,EACF,UAAUD,CAAI,EACd,KAAK,IAAK,CACTH,EAAU,EAAI,EAIVC,EAAW,UAAY,MAAM,aAAaA,EAAW,OAAO,EAChEA,EAAW,QAAU,WAAW,IAAK,CACnCA,EAAW,QAAU,KACrBD,EAAU,EAAK,CACjB,EAAGH,CAAwB,CAC7B,CAAC,EAIA,MAAM,IAAK,CAAE,CAAC,CACnB,EAAG,CAAA,CAAE,EAEL,MAAO,CAAE,OAAAE,EAAQ,KAAAG,CAAI,CACvB,CAvCSG,EAAAP,EAAA",
6
- "names": ["useCallback", "useEffect", "useRef", "useState", "COPY_FEEDBACK_TIMEOUT_MS", "useCopyToClipboard", "copied", "setCopied", "resetTimer", "copy", "text", "clipboard", "__name"]
4
+ "sourcesContent": ["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { COPY_FEEDBACK_TIMEOUT_MS } from './constants.ts';\n\n/**\n * Copy text to the clipboard, reporting success only when it happened.\n *\n * ## Provenance\n *\n * `hooks/useCopyToClipboard.ts` in **Durable-DAV** — the one copy that had\n * solved the two failures the others carried. The other three handlers were\n * inlined into `ReadOnlyField` (Edge-Git, Mail-Otter, Durable-DAV-Router) and\n * `TypeToConfirmModal`, and were byte-identical to each other.\n *\n * ## The two live bugs this converges away\n *\n * 1. **`navigator.clipboard` is absent in an insecure context.** A plain\n * `navigator.clipboard.writeText` throws *synchronously* there, so the\n * handler dies before setting state. Where it was guarded with\n * `if (clipboard)`, the guard fell through to `setCopied(true)` anyway and\n * showed a checkmark for a copy that never happened.\n * 2. **The write is a promise that rejects on a denied permission.** Awaiting\n * it is not enough; the rejection has to be handled, or it surfaces as an\n * unhandled rejection *and* the checkmark still appears.\n *\n * That second case is why this is a hook rather than a helper. The honest\n * answer — \"nothing was copied, so there is nothing to undo\" — is expressed by\n * `copied` simply never becoming true, which is state a helper function cannot\n * hold.\n *\n * Every call site in the family is a secret reveal: a bucket credential's\n * copy-once password, a resource name typed to confirm a destructive action.\n * A false success sends the operator away believing they have something they do\n * not.\n *\n * ## `reset`\n *\n * The timer is cleared on unmount, but that is not the only way the affordance\n * goes away: a clone menu that closes on an outside click hides the button\n * while its feedback window is still running. `reset` covers that, and cancels\n * the pending timer so the later unmount has nothing left to clear.\n *\n * ## Timer handling\n *\n * The reset timer is a ref and is cleared on unmount. Local `setTimeout` handles\n * in the inlined copies leaked both ways: a modal closed inside the feedback\n * window set state on an unmounted component, and repeated clicks each replaced\n * the timer without cancelling the previous one, so an early timer could clear\n * the checkmark while a later copy was still pending.\n */\n\ninterface UseCopyToClipboardResult {\n /**\n * True only after a write that actually resolved.\n *\n * Never optimistically set: a denied permission or an insecure context leaves\n * this `false`, which is the answer that matters — there is nothing to undo.\n */\n copied: boolean;\n /**\n Writes `text`, or does nothing if the clipboard is unavailable.\n */\n copy: (text: string) => void;\n /**\n * Clear the feedback early, cancelling the pending reset.\n *\n * For a host that hides the affordance itself — a clone menu that closes on\n * an outside click, a tab that is no longer showing the field. Without this\n * the only way to clear `copied` is to wait out the timer, and a consumer\n * that hides the button is left holding a stale `true` for a copy whose\n * confirmation is no longer on screen. Cancelling the timer here is what\n * keeps a later unmount from clearing state twice.\n */\n reset: () => void;\n}\n\nfunction useCopyToClipboard(): UseCopyToClipboardResult {\n const [copied, setCopied] = useState(false);\n const resetTimer = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n useEffect(\n () => () => {\n if (resetTimer.current !== null) clearTimeout(resetTimer.current);\n },\n [],\n );\n\n const copy = useCallback((text: string): void => {\n // Optional chaining rather than an assumption: `navigator.clipboard` is\n // genuinely absent over plain HTTP, and for a self-hosted deployment over\n // plain HTTP that is a normal configuration, not an error to surface.\n const clipboard = globalThis.navigator?.clipboard as Clipboard | undefined;\n // Nothing was copied, so there is nothing to report and nothing to undo.\n if (!clipboard) return;\n\n void clipboard\n .writeText(text)\n .then(() => {\n setCopied(true);\n // Cancel any pending reset before arming a new one. Replacing the\n // handle without clearing leaves the earlier timer live, and it can\n // clear the checkmark for a copy that is still being reported.\n if (resetTimer.current !== null) clearTimeout(resetTimer.current);\n resetTimer.current = setTimeout(() => {\n resetTimer.current = null;\n setCopied(false);\n }, COPY_FEEDBACK_TIMEOUT_MS);\n })\n // Leave `copied` false rather than flashing and clearing it: a rejection\n // means the text is still on screen and unread, so the affordance must\n // not claim otherwise.\n .catch(() => {});\n }, []);\n\n const reset = useCallback((): void => {\n if (resetTimer.current !== null) clearTimeout(resetTimer.current);\n resetTimer.current = null;\n setCopied(false);\n }, []);\n\n return { copied, copy, reset };\n}\n\nexport { useCopyToClipboard };\nexport type { UseCopyToClipboardResult };\n"],
5
+ "mappings": "+EAAA,OAAS,eAAAA,EAAa,aAAAC,EAAW,UAAAC,EAAQ,YAAAC,MAAgB,QACzD,OAAS,4BAAAC,MAAgC,iBA0EzC,SAASC,GAAkB,CACzB,KAAM,CAACC,EAAQC,CAAS,EAAIJ,EAAS,EAAK,EACpCK,EAAaN,EAA6C,IAAI,EAEpED,EACE,IAAM,IAAK,CACLO,EAAW,UAAY,MAAM,aAAaA,EAAW,OAAO,CAClE,EACA,CAAA,CAAE,EAGJ,MAAMC,EAAOT,EAAaU,GAAsB,CAI9C,MAAMC,EAAY,WAAW,WAAW,UAEnCA,GAEAA,EACF,UAAUD,CAAI,EACd,KAAK,IAAK,CACTH,EAAU,EAAI,EAIVC,EAAW,UAAY,MAAM,aAAaA,EAAW,OAAO,EAChEA,EAAW,QAAU,WAAW,IAAK,CACnCA,EAAW,QAAU,KACrBD,EAAU,EAAK,CACjB,EAAGH,CAAwB,CAC7B,CAAC,EAIA,MAAM,IAAK,CAAE,CAAC,CACnB,EAAG,CAAA,CAAE,EAECQ,EAAQZ,EAAY,IAAW,CAC/BQ,EAAW,UAAY,MAAM,aAAaA,EAAW,OAAO,EAChEA,EAAW,QAAU,KACrBD,EAAU,EAAK,CACjB,EAAG,CAAA,CAAE,EAEL,MAAO,CAAE,OAAAD,EAAQ,KAAAG,EAAM,MAAAG,CAAK,CAC9B,CA7CSC,EAAAR,EAAA",
6
+ "names": ["useCallback", "useEffect", "useRef", "useState", "COPY_FEEDBACK_TIMEOUT_MS", "useCopyToClipboard", "copied", "setCopied", "resetTimer", "copy", "text", "clipboard", "reset", "__name"]
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rexezuge/web",
3
- "version": "1.5.2",
3
+ "version": "1.6.0",
4
4
  "private": false,
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -25,8 +25,8 @@
25
25
  "dist"
26
26
  ],
27
27
  "dependencies": {
28
- "@rexezuge/errors": "1.5.2",
29
- "@rexezuge/shared": "1.5.2"
28
+ "@rexezuge/shared": "1.6.0",
29
+ "@rexezuge/errors": "1.6.0"
30
30
  },
31
31
  "peerDependencies": {
32
32
  "lucide-react": "^1.49.0",