"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 for built-in focus trapping, * Esc-key handling, and top-layer stacking. Desktop only. */ export const ChartModal: React.FC = ({ title, unit, isOpen, onClose, children, stationName, stationId, country, dateRangeLabel, legendItems, headerExtra, }) => { const dialogRef = useRef(null); const contentRef = useRef(null); const headingId = useId(); // Sync open state with native 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 itself, not its content) const handleBackdropClick = useCallback( (e: React.MouseEvent) => { 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 (
{/* Header */}

{headingText}

{headerExtra}
{/* Chart content — flex-1 lets ResponsiveContainer fill the available space */}
{isOpen && children}
); };