From dcf74f17171d9d8859fd2e5f3bf916ae0f4a8afd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Gu=CC=88ell=20Segarra?= Date: Tue, 14 Jan 2025 12:16:27 +0100 Subject: [PATCH] fix: improve ShareUrlButton functionality and design https://github.com/gisce/webclient/issues/1612 - Refactored the copyToClipboard function to use a temporary textarea for better compatibility across browsers and avoid SSL restrictions - Updated the ShareUrlButton component to include new icons and improved styling. - Added new translations for "Copy to clipboard" in ca_ES, en_US, and es_ES locale files. - Adjusted the popover content padding for a cleaner UI. --- src/actionbar/ShareUrlButton.tsx | 79 +++++++++++++++++++------------- src/locales/ca_ES.ts | 1 + src/locales/en_US.ts | 1 + src/locales/es_ES.ts | 1 + 4 files changed, 51 insertions(+), 31 deletions(-) diff --git a/src/actionbar/ShareUrlButton.tsx b/src/actionbar/ShareUrlButton.tsx index ef7b35250..2eca16c9c 100644 --- a/src/actionbar/ShareUrlButton.tsx +++ b/src/actionbar/ShareUrlButton.tsx @@ -1,15 +1,11 @@ import { useState, useCallback } from "react"; -import { Button, Input, message, Space, Popover } from "antd"; -import { - ShareAltOutlined, - CopyOutlined, - CheckOutlined, - LinkOutlined, -} from "@ant-design/icons"; +import { Button, Input, message, Space, Popover, theme } from "antd"; +import { CopyOutlined, CheckOutlined } from "@ant-design/icons"; import { useLocale } from "@gisce/react-formiga-components"; import { createShareOpenUrl } from "@/helpers/shareUrlHelper"; import { ViewType } from "@/types"; import ActionButton from "./ActionButton"; +import { IconExternalLink, IconShare2 } from "@tabler/icons-react"; export type ShareUrlButtonProps = { action_id?: number; @@ -22,6 +18,7 @@ export function ShareUrlButton({ view_type, res_id, }: ShareUrlButtonProps) { + const { token } = theme.useToken(); const { t } = useLocale(); const [isCopied, setIsCopied] = useState(false); @@ -38,12 +35,31 @@ export function ShareUrlButton({ moreDataNeededForCopying = !action_id || !res_id; } - const copyToClipboard = useCallback(async () => { + const copyToClipboard = useCallback(() => { try { - await navigator.clipboard.writeText(shareUrl); - setIsCopied(true); - message.success(t("urlCopiedToClipboard")); - setTimeout(() => setIsCopied(false), 2000); + // Create a temporary textarea element + const tempInput = document.createElement("textarea"); + tempInput.value = shareUrl; + document.body.appendChild(tempInput); + + // Select the text in the textarea + tempInput.select(); + tempInput.setSelectionRange(0, 99999); // For mobile devices + + // Copy the text using execCommand + const successful = document.execCommand("copy"); + + // Clean up the temporary element + document.body.removeChild(tempInput); + + // Handle success or failure + if (successful) { + setIsCopied(true); + message.success(t("urlCopiedToClipboard")); + setTimeout(() => setIsCopied(false), 2000); + } else { + throw new Error("Copy command was unsuccessful."); + } } catch (err) { console.error("Error copying to clipboard:", err); message.error(t("errorCopyingToClipboard")); @@ -51,7 +67,7 @@ export function ShareUrlButton({ }, [shareUrl, setIsCopied, t]); const popoverContent = ( -
+
- {isSecureContext && ( -