From 202a506485e109a50cdd60b2f629452d46fc15aa Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Mon, 2 Sep 2024 19:37:44 -0400 Subject: [PATCH] add dialog for copy to clipboard fallback (fixes #152) --- .../site/src/components/copy-to-clipboard.tsx | 49 +++++++++++++++++++ beszel/site/src/components/ui/textarea.tsx | 23 +++++++++ beszel/site/src/lib/stores.ts | 3 ++ beszel/site/src/lib/utils.ts | 7 +-- beszel/site/src/main.tsx | 30 +++++++++--- beszel/site/tailwind.config.js | 10 ++-- 6 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 beszel/site/src/components/copy-to-clipboard.tsx create mode 100644 beszel/site/src/components/ui/textarea.tsx diff --git a/beszel/site/src/components/copy-to-clipboard.tsx b/beszel/site/src/components/copy-to-clipboard.tsx new file mode 100644 index 0000000..4e18688 --- /dev/null +++ b/beszel/site/src/components/copy-to-clipboard.tsx @@ -0,0 +1,49 @@ +import { useEffect, useMemo, useRef } from 'react' +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from './ui/dialog' +import { Textarea } from './ui/textarea' +import { $copyContent } from '@/lib/stores' + +export default function CopyToClipboard({ content }: { content: string }) { + return ( + + + + Could not copy to clipboard + Please copy the text manually. + + +

+ Clipboard API requires a secure context (https, localhost, or *.localhost) +

+
+
+ ) +} + +function CopyTextarea({ content }: { content: string }) { + const textareaRef = useRef(null) + + const rows = useMemo(() => { + return content.split('\n').length + }, [content]) + + useEffect(() => { + if (textareaRef.current) { + textareaRef.current.select() + } + }, [textareaRef]) + + useEffect(() => { + return () => $copyContent.set('') + }, []) + + return ( +