From a2795a7d97b32c1f759ea940f92c720b08d9e48f Mon Sep 17 00:00:00 2001 From: Bhav Beri Date: Fri, 20 Dec 2024 16:13:50 +0530 Subject: [PATCH] Download button in DocItem to download rather than opening in new tab --- src/components/docs/DocItem.jsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/components/docs/DocItem.jsx b/src/components/docs/DocItem.jsx index 320a4a98..b91f031b 100644 --- a/src/components/docs/DocItem.jsx +++ b/src/components/docs/DocItem.jsx @@ -36,8 +36,18 @@ export default function DocItem({ const handleDownload = () => { const fileUrl = getFile(buildFileName(file, version), true, true); - window.open(fileUrl, "_blank"); + + // Create an anchor element + const anchor = document.createElement("a"); + anchor.href = fileUrl; + anchor.download = buildFileName(file, version); + + // Append it to the body (necessary for some browsers) + document.body.appendChild(anchor); + anchor.click(); + document.body.removeChild(anchor); }; + const handleVersionChange = (event) => { versionChange(event.target.value); };