Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@/climatemappedafrica Hero page map colour codes #993

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 43 additions & 39 deletions apps/climatemappedafrica/src/components/Hero/Hero.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import dynamic from "next/dynamic";
import PropTypes from "prop-types";
import React, { useState } from "react";

import Legend from "./Legend";

import heroBg from "@/climatemappedafrica/assets/images/bg-map-white.jpg";
import DropdownSearch from "@/climatemappedafrica/components/DropdownSearch";
import Image from "@/climatemappedafrica/components/Image";
Expand All @@ -23,6 +25,8 @@ function Hero({
properties,
level,
explorePageSlug,
averageTemperature,
legend,
...props
}) {
const isUpLg = useMediaQuery((theme) => theme.breakpoints.up("lg"));
Expand Down Expand Up @@ -99,45 +103,44 @@ function Hero({
</RichTypography>
</Grid>
{/* Since map is dynamic-ally loaded, no need for implementation="css" */}
<Box
sx={{
display: {
xs: "none",
md: "block",
},
}}
>
<Grid item md={5}>
{center ? (
<Map
center={[center[1], center[0]]}
zoom={zoom}
tileLayer={{
url: "https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.png",
}}
onLayerMouseOver={setHoverGeo}
featuredLocations={featuredLocations}
explorePageSlug={explorePageSlug}
{...props}
/>
) : null}
<Box sx={{ height: 80 }}>
<RichTypography
variant="h6"
sx={{
lineHeight: 23 / 18,
lineSpacing: "0.9px",
fontWeight: "normal",
textTransform: "capitalize",
display: "flex",
justifyContent: "flex-end",
}}
>
{hoverGeo}
</RichTypography>
</Box>
</Grid>
</Box>

<Grid item md={6} xs={12} justifyContent="center">
{center ? (
<Map
center={[center[1], center[0]]}
zoom={zoom}
tileLayer={{
url: "https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.png",
}}
onLayerMouseOver={setHoverGeo}
featuredLocations={featuredLocations}
explorePageSlug={explorePageSlug}
{...props}
/>
) : null}
<Box
display="flex"
justifyContent="space-between"
alignItems="center"
gap={2}
sx={{ height: 80, width: "100%" }}
>
<Legend title={averageTemperature} data={legend} />
<RichTypography
variant="h6"
sx={{
lineHeight: 23 / 18,
lineSpacing: "0.9px",
fontWeight: "normal",
textTransform: "capitalize",
display: "flex",
justifyContent: "flex-end",
}}
>
{hoverGeo}
</RichTypography>
</Box>
</Grid>
</Grid>
</Section>
</Box>
Expand All @@ -154,6 +157,7 @@ Hero.propTypes = {
properties: PropTypes.shape({}),
level: PropTypes.string,
explorePageSlug: PropTypes.string,
averageTemperature: PropTypes.string,
};

export default Hero;
48 changes: 44 additions & 4 deletions apps/climatemappedafrica/src/components/Hero/Hero.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,54 @@ exports[`<Hero /> renders unchanged 1`] = `
</div>
</div>
<div
class="MuiBox-root css-h9r63z"
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-md-6 css-1uu4n02-MuiGrid-root"
>
<div
class="MuiGrid-root MuiGrid-item MuiGrid-grid-md-5 css-1f9gci5-MuiGrid-root"
class="MuiBox-root css-ljsg2c"
>
<div
class="MuiBox-root css-4tnojn"
/>
class="MuiBox-root css-0"
>
<div
class="MuiBox-root css-70qvj9"
>
<div
aria-label="10 - 13"
class="MuiBox-root css-1wok3bd"
data-mui-internal-clone-element="true"
/>
<div
aria-label="13 - 16"
class="MuiBox-root css-1fg12k3"
data-mui-internal-clone-element="true"
/>
<div
aria-label="16 - 19"
class="MuiBox-root css-55biq3"
data-mui-internal-clone-element="true"
/>
<div
aria-label="19 - 22"
class="MuiBox-root css-9qafv7"
data-mui-internal-clone-element="true"
/>
<div
aria-label="22 - 25"
class="MuiBox-root css-5srxm6"
data-mui-internal-clone-element="true"
/>
<div
aria-label="25 - 28"
class="MuiBox-root css-1kw6r8l"
data-mui-internal-clone-element="true"
/>
<div
aria-label="28 - 31"
class="MuiBox-root css-oi5eih"
data-mui-internal-clone-element="true"
/>
</div>
</div>
</div>
</div>
</div>
Expand Down
9 changes: 9 additions & 0 deletions apps/climatemappedafrica/src/components/Hero/Hero.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ const defaultProps = {
},
variant: "explore",
icon: null,
legend: [
{ min: 10, max: 13, color: "#021AFE" },
{ min: 13, max: 16, color: "#5455FF" },
{ min: 16, max: 19, color: "#928EFD" },
{ min: 19, max: 22, color: "#B494DF" },
{ min: 22, max: 25, color: "#FA9B9B" },
{ min: 25, max: 28, color: "#F96264" },
{ min: 28, max: 31, color: "#F80701" },
],
};

describe("<Hero />", () => {
Expand Down
65 changes: 65 additions & 0 deletions apps/climatemappedafrica/src/components/Hero/Legend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { RichTypography } from "@commons-ui/legacy";
import { Box, Tooltip } from "@mui/material";
import PropTypes from "prop-types";
import React, { useState, forwardRef } from "react";

const Legend = forwardRef(function Legend({ data, title }, ref) {
const [hoveredValue, setHoveredValue] = useState(null);

const handleMouseEnter = (value) => {
setHoveredValue(value);
};

const handleMouseLeave = () => {
setHoveredValue(null);
};

return (
<Box>
<RichTypography variant="caption" sx={{ fontWeight: "bold", mb: 1 }}>
{title}
</RichTypography>
<Box ref={ref} display="flex" alignItems="center">
{data.map((item) => (
<Tooltip
key={`${item.min} - ${item.max}`}
title={`${item.min} - ${item.max}`}
placement="top"
arrow
>
<Box
onMouseEnter={() => handleMouseEnter(`${item.min} - ${item.max}`)}
onMouseLeave={handleMouseLeave}
sx={{
backgroundColor: item.color,
height: 24,
minWidth: 24,
cursor: "pointer",
}}
/>
</Tooltip>
))}
{hoveredValue && (
<RichTypography
variant="caption"
sx={{ marginLeft: 2, whiteSpace: "nowrap" }}
>
{hoveredValue}
</RichTypography>
)}
</Box>
</Box>
);
});

Legend.propTypes = {
data: PropTypes.arrayOf(
PropTypes.shape({
min: PropTypes.number.isRequired,
max: PropTypes.number.isRequired,
color: PropTypes.string.isRequired,
}),
).isRequired,
};

export default Legend;
79 changes: 41 additions & 38 deletions apps/climatemappedafrica/src/components/Hero/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ function Map({
onLayerMouseOver,
featuredLocations,
explorePageSlug,
choropleth,
}) {
const router = useRouter();

const countyCodes = featuredLocations?.map(({ code }) => code);

const regionCodes = featuredLocations?.map(({ code }) => code);
const theme = useTheme();
const onEachFeature = (feature, layer) => {
const choroplethColor = choropleth?.find?.(
(c) => c.code.toLowerCase() === feature.properties.code.toLowerCase(),
);
layer.setStyle({
fillColor: theme.palette.background.default,
...choroplethColor,
fillOpacity: 1,
});

if (countyCodes.includes(feature.properties.code?.toLowerCase())) {
if (regionCodes.includes(feature.properties.code?.toLowerCase())) {
layer.setStyle({
weight: 1.5,
dashArray: 0,
Expand All @@ -44,17 +48,14 @@ function Map({
onLayerMouseOver(feature.properties.name.toLowerCase());
if (explorePageSlug) {
layer.setStyle({
fillColor: theme.palette.primary.main,
fillOpacity: 0.5,
fillColor: choroplethColor?.fillColor,
fillOpacity: 0.4,
});
}
});
layer.on("mouseout", () => {
onLayerMouseOver(null);
layer.setStyle({
fillOpacity: 1,
fillColor: theme.palette.background.default,
});
layer.setStyle({ ...choroplethColor, fillOpacity: 1 });
});
layer.on("click", () => {
if (explorePageSlug) {
Expand All @@ -67,36 +68,38 @@ function Map({
};

return (
<Box
sx={{
position: "relative",
height: { sm: "350px", lg: "471px" },
width: { sm: "300px", lg: "500px" },
marginTop: { sm: "55px", lg: "42px" },
"& .leaflet-container": {
background: "transparent",
},
}}
>
<MapContainer
center={center}
zoom={zoom}
boxZoom={false}
dragging={false}
doubleClickZoom={false}
zoomControl={false}
scrollWheelZoom={false}
touchZoom={false}
trackResize={false}
zoomSnap={0.25}
style={styles}
<Box display="flex" justifyContent="center">
<Box
sx={{
position: "relative",
height: { sm: "350px", lg: "471px" },
width: { xs: "100%", sm: "300px", lg: "500px" },
marginTop: { sm: "55px", lg: "42px" },
"& .leaflet-container": {
background: "transparent",
},
}}
>
<GeoJSON
data={boundary}
style={geoJSONStyles}
onEachFeature={onEachFeature}
/>
</MapContainer>
<MapContainer
center={center}
zoom={zoom}
boxZoom={false}
dragging={false}
doubleClickZoom={false}
zoomControl={false}
scrollWheelZoom={false}
touchZoom={false}
trackResize={false}
zoomSnap={0.25}
style={styles}
>
<GeoJSON
data={boundary}
style={geoJSONStyles}
onEachFeature={onEachFeature}
/>
</MapContainer>
</Box>
</Box>
);
}
Expand Down
Loading
Loading