/* ============================================================================ * Copyright (c) Palo Alto Networks * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * ========================================================================== */ import React, { useCallback, useState, useRef, useEffect } from "react"; import { translate } from "@docusaurus/Translate"; import clsx from "clsx"; interface CopyButtonProps { code: string; className?: string; } async function copyToClipboard(text: string) { // The clipboard API is only defined in secure contexts (HTTPS / localhost). // See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard if (navigator.clipboard) { return navigator.clipboard.writeText(text); } // Fall back to copy-text-to-clipboard for non-secure contexts (e.g. HTTP // on a local network). The fallback is lazily loaded to avoid bundle // overhead for the common HTTPS case. const { default: copy } = await import("copy-text-to-clipboard"); return copy(text); } export default function CopyButton({ code, className, }: CopyButtonProps): React.JSX.Element { const [isCopied, setIsCopied] = useState(false); const copyTimeout = useRef(undefined); const handleCopyCode = useCallback(async () => { await copyToClipboard(code); setIsCopied(true); copyTimeout.current = window.setTimeout(() => { setIsCopied(false); }, 1000); }, [code]); useEffect(() => () => window.clearTimeout(copyTimeout.current), []); return ( ); }