From 5232e6bd64a93476cf9f4a0cd03e99a730769225 Mon Sep 17 00:00:00 2001 From: Mehboob Khan Date: Mon, 19 Oct 2026 17:32:18 +0500 Subject: [PATCH] Add download button to the ROI slide card Lazy-loads html2canvas (separate chunk, only fetched on click) to export the card as a PNG. The button floats on the card's top-right corner, outside the captured element, so it never ends up in the exported image. Co-Authored-By: Claude Sonnet 5 --- package-lock.json | 50 +++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/site/roi/RoiSlide.tsx | 43 ++++++++++++++++++++++++++++++++- src/site/roi/roiSlide.css | 50 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 142 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 01f8919..575924c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "framer-motion": "^11.18.2", + "html2canvas": "^1.4.1", "react": "^18.3.1", "react-dom": "^18.3.1", "react-router-dom": "^6.26.2", @@ -1251,6 +1252,15 @@ "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0" } }, + "node_modules/base64-arraybuffer": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-1.0.2.tgz", + "integrity": "sha512-I3yl4r9QB5ZRY3XuJVEPfc2XhZO6YweFPI+UovAzn+8/hb3oJ6lnysaFcjVpkCPfVWFUDvoZ8kmVDP7WyRtYtQ==", + "license": "MIT", + "engines": { + "node": ">= 0.6.0" + } + }, "node_modules/baseline-browser-mapping": { "version": "2.10.40", "resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.10.40.tgz", @@ -1326,6 +1336,15 @@ "dev": true, "license": "MIT" }, + "node_modules/css-line-break": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/css-line-break/-/css-line-break-2.1.0.tgz", + "integrity": "sha512-FHcKFCZcAha3LwfVBhCQbW2nCNbkZXn7KVUJcsT5/P8YmfsVja0FMPJr0B903j/E69HUphKiV9iQArX8SDYA4w==", + "license": "MIT", + "dependencies": { + "utrie": "^1.0.2" + } + }, "node_modules/csstype": { "version": "3.2.3", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz", @@ -1565,6 +1584,19 @@ "node": ">=6.9.0" } }, + "node_modules/html2canvas": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/html2canvas/-/html2canvas-1.4.1.tgz", + "integrity": "sha512-fPU6BHNpsyIhr8yyMpTLLxAbkaK8ArIBcmZIRiBLiDhjeqvXolaEmDGmELFuX9I4xDcaKKcJl+TKZLqruBbmWA==", + "license": "MIT", + "dependencies": { + "css-line-break": "^2.1.0", + "text-segmentation": "^1.0.3" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/htmlparser2": { "version": "12.0.0", "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-12.0.0.tgz", @@ -1907,6 +1939,15 @@ "node": ">=0.10.0" } }, + "node_modules/text-segmentation": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/text-segmentation/-/text-segmentation-1.0.3.tgz", + "integrity": "sha512-iOiPUo/BGnZ6+54OsWxZidGCsdU8YbE4PSpdPinp7DeMtUJNJBoJ/ouUSTJjHkh1KntHaltHl/gDs2FC4i5+Nw==", + "license": "MIT", + "dependencies": { + "utrie": "^1.0.2" + } + }, "node_modules/tslib": { "version": "2.8.1", "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", @@ -1958,6 +1999,15 @@ "browserslist": ">= 4.21.0" } }, + "node_modules/utrie": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/utrie/-/utrie-1.0.2.tgz", + "integrity": "sha512-1MLa5ouZiOmQzUbjbu9VmjLzn1QLXBhwpUa7kdLUQK+KQ5KA9I1vk5U4YHe/X2Ch7PYnJfWuWT+VbuxbGwljhw==", + "license": "MIT", + "dependencies": { + "base64-arraybuffer": "^1.0.2" + } + }, "node_modules/vite": { "version": "5.4.21", "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.21.tgz", diff --git a/package.json b/package.json index 9366376..44cce9b 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ }, "dependencies": { "framer-motion": "^11.18.2", + "html2canvas": "^1.4.1", "react": "^18.3.1", "react-dom": "^18.3.1", "react-router-dom": "^6.26.2", diff --git a/src/site/roi/RoiSlide.tsx b/src/site/roi/RoiSlide.tsx index 1702f9a..86d21dc 100644 --- a/src/site/roi/RoiSlide.tsx +++ b/src/site/roi/RoiSlide.tsx @@ -1,3 +1,4 @@ +import { useRef, useState } from 'react' import './roiSlide.css' import hexMotif from './hexMotif.svg?raw' @@ -36,9 +37,48 @@ export default function RoiSlide({ values, results }: { values: DeckValues; resu const mids = CATS.map((c) => Math.max(0, (results[c.key].lo + results[c.key].hi) / 2)) const totalMid = mids.reduce((a, b) => a + b, 0) + const cardRef = useRef(null) + const [downloading, setDownloading] = useState(false) + + const download = async () => { + if (!cardRef.current || downloading) return + setDownloading(true) + try { + const { default: html2canvas } = await import('html2canvas') + const canvas = await html2canvas(cardRef.current, { backgroundColor: null, scale: 2 }) + const a = document.createElement('a') + a.download = `blackdice-roi-savings-${new Date().toISOString().slice(0, 10)}.png` + a.href = canvas.toDataURL('image/png') + a.click() + } catch { + /* best effort — leave the slide visible either way */ + } finally { + setDownloading(false) + } + } + return (
-
+
+ +
BlackDice @@ -78,6 +118,7 @@ export default function RoiSlide({ values, results }: { values: DeckValues; resu {values.subs !== '—' ? `${values.subs} subscribers` : 'Operator estimate'} BlackDice Cyber · Network Protection ROI Calculator
+
) diff --git a/src/site/roi/roiSlide.css b/src/site/roi/roiSlide.css index 9d4974f..0af3a46 100644 --- a/src/site/roi/roiSlide.css +++ b/src/site/roi/roiSlide.css @@ -13,9 +13,57 @@ justify-content: center; } -.roi-slide-card { +.roi-slide-card-wrap { + position: relative; width: 100%; max-width: 860px; +} + +.roi-slide-download { + position: absolute; + top: -14px; + right: -14px; + z-index: 2; + display: inline-flex; + align-items: center; + justify-content: center; + width: 34px; + height: 34px; + border-radius: 50%; + border: 1px solid rgba(255, 255, 255, 0.25); + background: var(--rs-teal2); + box-shadow: 0 4px 14px rgba(1, 59, 73, 0.4); + color: #fff; + cursor: pointer; + transition: background 0.15s, opacity 0.15s; +} +.roi-slide-download:hover:not(:disabled) { + background: var(--rs-teal3); +} +.roi-slide-download:disabled { + opacity: 0.5; + cursor: wait; +} +.roi-slide-download svg { + width: 16px; + height: 16px; + stroke: currentColor; + fill: none; + stroke-width: 2; + stroke-linecap: round; + stroke-linejoin: round; +} +.roi-slide-download:disabled svg { + animation: roi-slide-spin 0.8s linear infinite; +} +@keyframes roi-slide-spin { + to { + transform: rotate(360deg); + } +} + +.roi-slide-card { + width: 100%; aspect-ratio: 16 / 9; background: linear-gradient(135deg, var(--rs-teal) 0%, var(--rs-teal2) 60%, var(--rs-teal3) 100%); border-radius: 16px;