Spaces:
Running
Running
File size: 3,853 Bytes
c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 2be44dc c308906 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | "use client";
import React, { useRef, useEffect, useCallback, useId } from "react";
import { X, Download } from "lucide-react";
import { exportChartAsPng } from "@/utils/chartExport";
import type { ChartLegendItem } from "@/utils/chartHelpers";
interface ChartModalProps {
title: string;
unit?: string;
isOpen: boolean;
onClose: () => void;
children: React.ReactNode;
stationName?: string;
stationId?: string;
country?: string;
dateRangeLabel?: string;
legendItems?: ChartLegendItem[];
headerExtra?: React.ReactNode;
}
/**
* Fullscreen chart modal using native <dialog> for built-in focus trapping,
* Esc-key handling, and top-layer stacking. Desktop only.
*/
export const ChartModal: React.FC<ChartModalProps> = ({
title,
unit,
isOpen,
onClose,
children,
stationName,
stationId,
country,
dateRangeLabel,
legendItems,
headerExtra,
}) => {
const dialogRef = useRef<HTMLDialogElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const headingId = useId();
// Sync open state with native <dialog> API
useEffect(() => {
const dialog = dialogRef.current;
if (!dialog) return;
if (isOpen && !dialog.open) {
dialog.showModal();
} else if (!isOpen && dialog.open) {
dialog.close();
}
}, [isOpen]);
// Close on backdrop click (click on <dialog> itself, not its content)
const handleBackdropClick = useCallback(
(e: React.MouseEvent<HTMLDialogElement>) => {
if (e.target === dialogRef.current) {
onClose();
}
},
[onClose]
);
// Handle native close event (e.g. Esc key)
const handleClose = useCallback(() => {
onClose();
}, [onClose]);
const handleDownload = useCallback(() => {
if (contentRef.current) {
exportChartAsPng(contentRef.current, {
title,
unit,
stationName,
stationId,
country,
dateRangeLabel,
legendItems,
});
}
}, [title, unit, stationName, stationId, country, dateRangeLabel, legendItems]);
const headingText = unit ? `${title} (${unit})` : title;
return (
<dialog
ref={dialogRef}
aria-labelledby={headingId}
className="chart-modal m-auto"
onClick={handleBackdropClick}
onClose={handleClose}
>
<div className="flex flex-col w-[90vw] h-[85vh] bg-white rounded-2xl overflow-hidden">
{/* Header */}
<div className="flex items-start justify-between gap-4 px-6 py-4 border-b border-slate-100">
<div className="flex flex-col gap-1.5 min-w-0">
<h2
id={headingId}
className="text-sm font-bold text-slate-700 uppercase tracking-wider"
>
{headingText}
</h2>
{headerExtra}
</div>
<div className="flex items-center gap-1 shrink-0">
<button
className="p-2 min-w-[44px] min-h-[44px] flex items-center justify-center rounded-lg text-slate-400 hover:text-slate-600 hover:bg-slate-100 transition-colors"
aria-label={`Download ${title} as image`}
onClick={handleDownload}
>
<Download size={16} aria-hidden="true" />
</button>
<button
className="p-2 min-w-[44px] min-h-[44px] flex items-center justify-center rounded-lg text-slate-400 hover:text-slate-600 hover:bg-slate-100 transition-colors"
aria-label="Close expanded view"
onClick={onClose}
>
<X size={18} aria-hidden="true" />
</button>
</div>
</div>
{/* Chart content — flex-1 lets ResponsiveContainer fill the available space */}
<div ref={contentRef} className="flex-1 min-h-0 p-6">
{isOpen && children}
</div>
</div>
</dialog>
);
};
|