GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — embedded-wallet/src/components/CopyableText.tsx at develop · 1Shot-API/embedded-wallet Expand file tree/ Copy pathCopyableText.tsxMore file actions 202 lines (180 loc) · 5.32 KB / Copy pathCopyableText.tsxFile metadata and controls202 lines (180 loc) · 5.32 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 import { CheckIcon, CopyIcon } from "lucide-react"; import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { Button } from "@/components/ui/button"; import { copyText } from "@/lib/clipboard"; import { cn } from "@/lib/utils"; type CopyState = "idle" | "copied" | "failed"; const MIN_SIDE_CHARS = 4; const FEEDBACK_MS = 1500; export interface ICopyableTextProps { /** Full value to display (and copy). */ text: string; /** * When set, middle-truncate to fit the available width (keeps start + end). * The full `text` is still copied. */ truncate?: boolean; className?: string; disabled?: boolean; copyLabel?: string; copiedLabel?: string; copyFailedLabel?: string; } function measureTextWidth( text: string, font: string, canvas: HTMLCanvasElement, ): number { const ctx = canvas.getContext("2d"); if (!ctx) return text.length * 8; ctx.font = font; return ctx.measureText(text).width; } /** Fit `text` into `maxWidth` with a middle ellipsis; prefer showing more chars. */ function formatToWidth( text: string, maxWidth: number, font: string, canvas: HTMLCanvasElement, ): string { if (maxWidth <= 0) { if (text.length <= MIN_SIDE_CHARS * 2 + 1) return text; return `${text.slice(0, MIN_SIDE_CHARS)}…${text.slice(-MIN_SIDE_CHARS)}`; } if (measureTextWidth(text, font, canvas) <= maxWidth) return text; const ellipsis = "…"; const ellipsisWidth = measureTextWidth(ellipsis, font, canvas); if (ellipsisWidth >= maxWidth) return ellipsis; const maxSide = Math.floor((text.length - 1) / 2); let lo = MIN_SIDE_CHARS; let hi = maxSide; let best = Math.min(MIN_SIDE_CHARS, maxSide); while (lo <= hi) { const mid = Math.floor((lo + hi) / 2); const candidate = `${text.slice(0, mid)}${ellipsis}${text.slice(-mid)}`; if (measureTextWidth(candidate, font, canvas) <= maxWidth) { best = mid; lo = mid + 1; } else { hi = mid - 1; } } // If even the minimum sides overflow, shrink further. if ( measureTextWidth( `${text.slice(0, best)}${ellipsis}${text.slice(-best)}`, font, canvas, ) > maxWidth ) { for (let side = best - 1; side >= 1; side -= 1) { const candidate = `${text.slice(0, side)}${ellipsis}${text.slice(-side)}`; if (measureTextWidth(candidate, font, canvas) <= maxWidth) { return candidate; } } return ellipsis; } return `${text.slice(0, best)}${ellipsis}${text.slice(-best)}`; } /** * Mono text with an inline copy control and brief success/failure feedback. * Use `truncate` for compact address rows; omit it for full wrapped blocks (e.g. backup). */ export function CopyableText({ text, truncate = false, className, disabled = false, copyLabel = "Copy", copiedLabel = "Copied", copyFailedLabel = "Copy failed", }: ICopyableTextProps) { const [copyState, setCopyState] = useState<CopyState>("idle"); const [display, setDisplay] = useState(text); const resetRef = useRef<ReturnType<typeof setTimeout> | null>(null); const textRef = useRef<HTMLPreElement>(null); const canvasRef = useRef<HTMLCanvasElement | null>(null); const canCopy = Boolean(text) && text !== "—" && !disabled; useEffect(() => { return () => { if (resetRef.current) clearTimeout(resetRef.current); }; }, []); useLayoutEffect(() => { if (!truncate) { setDisplay(text); return; } const el = textRef.current; if (!el) { setDisplay(text); return; } if (!canvasRef.current) { canvasRef.current = document.createElement("canvas"); } const canvas = canvasRef.current; const update = () => { const font = getComputedStyle(el).font; const width = el.clientWidth; setDisplay(formatToWidth(text, width, font, canvas)); }; update(); const observer = new ResizeObserver(update); observer.observe(el); return () => observer.disconnect(); }, [text, truncate]); const feedbackLabel = copyState === "copied" ? copiedLabel : copyState === "failed" ? copyFailedLabel : copyLabel; const onCopy = () => { if (!canCopy) return; void copyText(text).then((ok) => { setCopyState(ok ? "copied" : "failed"); if (resetRef.current) clearTimeout(resetRef.current); resetRef.current = setTimeout(() => setCopyState("idle"), FEEDBACK_MS); }); }; return ( <div className={cn( "border-border bg-muted/40 flex min-w-0 gap-2 rounded-lg border", truncate ? "items-center px-3 py-2" : "items-start p-3", className, )} > <pre ref={textRef} className={cn( "m-0 min-w-0 flex-1 font-mono text-[0.8rem]", truncate ? "overflow-hidden whitespace-nowrap" : "max-h-40 overflow-x-hidden overflow-y-auto break-all whitespace-pre-wrap", )} title={truncate ? text : undefined} > {display} </pre> <Button type="button" variant="ghost" size="icon-sm" className="shrink-0" aria-label={feedbackLabel} title={feedbackLabel} disabled={!canCopy} onClick={onCopy} > {copyState === "copied" ? ( <CheckIcon className="size-3.5" /> ) : ( <CopyIcon className="size-3.5" /> )} </Button> </div> ); } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |