|
| 1 | +import { useState } from 'react' |
| 2 | + |
| 3 | +import LayersIcon from '@mui/icons-material/Layers' |
| 4 | +import { Box, Button, Collapse } from '@mui/material' |
| 5 | +import { alpha } from '@mui/material/styles' |
| 6 | + |
| 7 | +import { OverlayType, WorldEFPState } from './types' |
| 8 | + |
| 9 | +const OVERLAY_LABELS: Record<OverlayType, string> = { |
| 10 | + [OverlayType.None]: 'None', |
| 11 | + [OverlayType.Precipitation]: 'Annual Precip.', |
| 12 | + [OverlayType.HistoricalMinTemp]: 'Min. Temp.', |
| 13 | + [OverlayType.HistoricalMaxTemp]: 'Max. Temp.', |
| 14 | +} |
| 15 | + |
| 16 | +const OVERLAY_TYPES = Object.values(OverlayType) |
| 17 | + |
| 18 | +type OverlaySelectorProps = { |
| 19 | + overlay: WorldEFPState['overlay'] |
| 20 | + onSelect: (overlay: WorldEFPState['overlay']) => void |
| 21 | +} |
| 22 | + |
| 23 | +const OverlaySelector = ({ overlay, onSelect }: OverlaySelectorProps) => { |
| 24 | + const [isOpen, setIsOpen] = useState(false) |
| 25 | + |
| 26 | + const handleSelect = (next: OverlayType) => { |
| 27 | + onSelect(next) |
| 28 | + setIsOpen(false) |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <Box |
| 33 | + sx={(theme) => ({ |
| 34 | + width: isOpen ? theme.spacing(15) : theme.spacing(5), |
| 35 | + borderRadius: theme.spacing(1), |
| 36 | + backgroundColor: alpha(theme.palette.background.active, 0.7), |
| 37 | + boxShadow: '0 8px 24px rgba(0, 0, 0, 0.18)', |
| 38 | + border: `1px solid ${alpha(theme.palette.background.active, 0.7)}`, |
| 39 | + backdropFilter: 'blur(8px)', |
| 40 | + WebkitBackdropFilter: 'blur(8px)', |
| 41 | + overflow: 'hidden', |
| 42 | + transition: 'width 220ms ease, box-shadow 220ms ease', |
| 43 | + })} |
| 44 | + > |
| 45 | + <Button |
| 46 | + variant='text' |
| 47 | + onClick={() => setIsOpen((open) => !open)} |
| 48 | + sx={(theme) => ({ |
| 49 | + width: '100%', |
| 50 | + justifyContent: 'flex-start', |
| 51 | + textTransform: 'none', |
| 52 | + color: |
| 53 | + overlay !== OverlayType.None |
| 54 | + ? theme.palette.primary.main |
| 55 | + : theme.palette.text.primary, |
| 56 | + padding: theme.spacing(1), |
| 57 | + '&:hover': { |
| 58 | + backgroundColor: alpha(theme.palette.background.active, 0.85), |
| 59 | + }, |
| 60 | + })} |
| 61 | + > |
| 62 | + <LayersIcon fontSize='small' /> |
| 63 | + <Box |
| 64 | + sx={(theme) => ({ |
| 65 | + marginLeft: theme.spacing(1), |
| 66 | + opacity: isOpen ? 1 : 0, |
| 67 | + maxWidth: isOpen ? theme.spacing(12) : 0, |
| 68 | + overflow: 'hidden', |
| 69 | + whiteSpace: 'nowrap', |
| 70 | + transition: 'opacity 200ms ease, max-width 220ms ease', |
| 71 | + })} |
| 72 | + > |
| 73 | + {OVERLAY_LABELS[overlay]} |
| 74 | + </Box> |
| 75 | + </Button> |
| 76 | + <Collapse in={isOpen} timeout={200} unmountOnExit> |
| 77 | + <Box |
| 78 | + sx={(theme) => ({ |
| 79 | + display: 'flex', |
| 80 | + flexDirection: 'column', |
| 81 | + gap: theme.spacing(0.5), |
| 82 | + padding: theme.spacing(0.5, 1, 1), |
| 83 | + })} |
| 84 | + > |
| 85 | + {OVERLAY_TYPES.map((type) => ( |
| 86 | + <Button |
| 87 | + key={type} |
| 88 | + variant='text' |
| 89 | + onClick={() => handleSelect(type)} |
| 90 | + sx={(theme) => ({ |
| 91 | + justifyContent: 'flex-start', |
| 92 | + textTransform: 'none', |
| 93 | + color: theme.palette.text.primary, |
| 94 | + padding: theme.spacing(0.5, 1), |
| 95 | + '&:hover': { |
| 96 | + backgroundColor: alpha(theme.palette.background.active, 0.45), |
| 97 | + }, |
| 98 | + ...(overlay === type && { |
| 99 | + fontWeight: 600, |
| 100 | + backgroundColor: alpha(theme.palette.background.active, 0.6), |
| 101 | + borderRadius: theme.spacing(0.75), |
| 102 | + '&:hover': { |
| 103 | + backgroundColor: alpha( |
| 104 | + theme.palette.background.active, |
| 105 | + 0.7 |
| 106 | + ), |
| 107 | + }, |
| 108 | + }), |
| 109 | + })} |
| 110 | + > |
| 111 | + {OVERLAY_LABELS[type]} |
| 112 | + </Button> |
| 113 | + ))} |
| 114 | + </Box> |
| 115 | + </Collapse> |
| 116 | + </Box> |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +export default OverlaySelector |
0 commit comments