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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<HTMLDivElement>(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 (
|
||||
<div className="roi-slide">
|
||||
<div className="roi-slide-card">
|
||||
<div className="roi-slide-card-wrap">
|
||||
<button
|
||||
type="button"
|
||||
className="roi-slide-download"
|
||||
onClick={download}
|
||||
disabled={downloading}
|
||||
aria-label="Download slide as image"
|
||||
title="Download as image"
|
||||
>
|
||||
{downloading ? (
|
||||
<svg viewBox="0 0 24 24">
|
||||
<path d="M12 2a10 10 0 0 1 10 10" />
|
||||
</svg>
|
||||
) : (
|
||||
<svg viewBox="0 0 24 24">
|
||||
<path d="M12 3v12m0 0l-4-4m4 4l4-4M4 17v2a2 2 0 002 2h12a2 2 0 002-2v-2" />
|
||||
</svg>
|
||||
)}
|
||||
</button>
|
||||
<div className="roi-slide-card" ref={cardRef}>
|
||||
<div className="roi-slide-hex" dangerouslySetInnerHTML={{ __html: hexMotif }} />
|
||||
<div className="roi-slide-top">
|
||||
<img src="/logo.svg" alt="BlackDice" className="roi-slide-logo" />
|
||||
@@ -78,6 +118,7 @@ export default function RoiSlide({ values, results }: { values: DeckValues; resu
|
||||
<span>{values.subs !== '—' ? `${values.subs} subscribers` : 'Operator estimate'}</span>
|
||||
<span>BlackDice Cyber · Network Protection ROI Calculator</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user