|
1 | 1 | 'use client'; |
2 | 2 |
|
| 3 | +import { useEffect, useRef, useState } from 'react'; |
| 4 | + |
| 5 | +type WatermarkMode = 'none' | 'draft' | 'confidential' | 'review' | 'internal'; |
| 6 | + |
| 7 | +const OPTIONS: { value: WatermarkMode; label: string }[] = [ |
| 8 | + { value: 'none', label: 'No watermark' }, |
| 9 | + { value: 'draft', label: 'DRAFT' }, |
| 10 | + { value: 'confidential', label: 'CONFIDENTIAL' }, |
| 11 | + { value: 'review', label: 'FOR REVIEW' }, |
| 12 | + { value: 'internal', label: 'INTERNAL ONLY' }, |
| 13 | +]; |
| 14 | + |
3 | 15 | export function ExportPdf({ className }: { className?: string }) { |
| 16 | + const [open, setOpen] = useState(false); |
| 17 | + const wrapRef = useRef<HTMLDivElement>(null); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + if (!open) return; |
| 21 | + const onClick = (e: MouseEvent) => { |
| 22 | + if (wrapRef.current && !wrapRef.current.contains(e.target as Node)) setOpen(false); |
| 23 | + }; |
| 24 | + const onKey = (e: KeyboardEvent) => { |
| 25 | + if (e.key === 'Escape') setOpen(false); |
| 26 | + }; |
| 27 | + window.addEventListener('mousedown', onClick); |
| 28 | + window.addEventListener('keydown', onKey); |
| 29 | + return () => { |
| 30 | + window.removeEventListener('mousedown', onClick); |
| 31 | + window.removeEventListener('keydown', onKey); |
| 32 | + }; |
| 33 | + }, [open]); |
| 34 | + |
| 35 | + const exportWith = (mode: WatermarkMode) => { |
| 36 | + setOpen(false); |
| 37 | + if (typeof window === 'undefined') return; |
| 38 | + const root = document.documentElement; |
| 39 | + if (mode !== 'none') { |
| 40 | + const label = OPTIONS.find((o) => o.value === mode)?.label ?? mode.toUpperCase(); |
| 41 | + root.setAttribute('data-watermark', mode); |
| 42 | + root.style.setProperty('--watermark-text', `'${label}'`); |
| 43 | + } else { |
| 44 | + root.removeAttribute('data-watermark'); |
| 45 | + root.style.removeProperty('--watermark-text'); |
| 46 | + } |
| 47 | + setTimeout(() => { |
| 48 | + window.print(); |
| 49 | + window.setTimeout(() => { |
| 50 | + root.removeAttribute('data-watermark'); |
| 51 | + root.style.removeProperty('--watermark-text'); |
| 52 | + }, 250); |
| 53 | + }, 16); |
| 54 | + }; |
| 55 | + |
4 | 56 | return ( |
5 | | - <button |
6 | | - type="button" |
7 | | - className={className} |
8 | | - onClick={() => { |
9 | | - if (typeof window !== 'undefined') window.print(); |
10 | | - }} |
11 | | - aria-label="Export deck as PDF" |
12 | | - > |
13 | | - <svg width="13" height="13" viewBox="0 0 13 13" fill="none" aria-hidden> |
14 | | - <path |
15 | | - d="M6.5 1.5V8.5M6.5 8.5L3.5 5.5M6.5 8.5L9.5 5.5M2 10.5V11.5H11V10.5" |
16 | | - stroke="currentColor" |
17 | | - strokeWidth="1.3" |
18 | | - strokeLinecap="round" |
19 | | - strokeLinejoin="round" |
20 | | - /> |
21 | | - </svg> |
22 | | - <span>Export PDF</span> |
23 | | - </button> |
| 57 | + <div className="export-pdf" ref={wrapRef}> |
| 58 | + <button |
| 59 | + type="button" |
| 60 | + className={className} |
| 61 | + onClick={() => setOpen((v) => !v)} |
| 62 | + aria-haspopup="menu" |
| 63 | + aria-expanded={open} |
| 64 | + aria-label="Export deck as PDF" |
| 65 | + > |
| 66 | + <svg width="13" height="13" viewBox="0 0 13 13" fill="none" aria-hidden> |
| 67 | + <path |
| 68 | + d="M6.5 1.5V8.5M6.5 8.5L3.5 5.5M6.5 8.5L9.5 5.5M2 10.5V11.5H11V10.5" |
| 69 | + stroke="currentColor" |
| 70 | + strokeWidth="1.3" |
| 71 | + strokeLinecap="round" |
| 72 | + strokeLinejoin="round" |
| 73 | + /> |
| 74 | + </svg> |
| 75 | + <span>Export PDF</span> |
| 76 | + </button> |
| 77 | + {open ? ( |
| 78 | + <ul className="export-pdf__menu" role="menu"> |
| 79 | + {OPTIONS.map((opt) => ( |
| 80 | + <li key={opt.value}> |
| 81 | + <button |
| 82 | + type="button" |
| 83 | + role="menuitem" |
| 84 | + className="export-pdf__item" |
| 85 | + onClick={() => exportWith(opt.value)} |
| 86 | + > |
| 87 | + {opt.label} |
| 88 | + </button> |
| 89 | + </li> |
| 90 | + ))} |
| 91 | + </ul> |
| 92 | + ) : null} |
| 93 | + </div> |
24 | 94 | ); |
25 | 95 | } |
0 commit comments