Skip to content

Commit 14a0dd4

Browse files
committed
Use CSS color variables for map marker coloring
1 parent 477bec0 commit 14a0dd4

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/components/Map/ColoredIconMarker.tsx

+10-7
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import React from "react";
33

44
type Props = {
55
accessibilityGrade: string;
6+
backgroundColor?: string;
67
children?: React.ReactNode;
78
};
89

910
export default function ColoredIconMarker(
1011
props: React.SVGAttributes<SVGElement> & Props,
1112
) {
12-
const { accessibilityGrade, children } = props;
13+
const { accessibilityGrade, children, backgroundColor } = props;
1314
return (
1415
<svg
1516
width="1em"
@@ -23,37 +24,39 @@ export default function ColoredIconMarker(
2324
cx={12.5}
2425
cy={12.5}
2526
r={10.5}
26-
fill="#7EC512"
27+
fill={backgroundColor}
2728
fillRule="nonzero"
2829
filter="url(#halo)"
2930
/>
3031
)}
32+
3133
{accessibilityGrade === "limited" && (
3234
<path
33-
fill="#FC9B32"
35+
fill={backgroundColor}
3436
d="M6.743 2.364h11.55l5.776 10.003-5.775 10.003H6.743L.967 12.367z"
3537
fillRule="evenodd"
3638
filter="url(#halo)"
3739
/>
3840
)}
41+
3942
{accessibilityGrade === "no" && (
4043
<path
4144
d="M22 22V3.072H3v19z"
42-
fill="#F54B4B"
45+
fill={backgroundColor}
4346
fillRule="nonzero"
4447
filter="url(#halo)"
4548
/>
4649
)}
50+
4751
{accessibilityGrade === "unknown" && (
4852
<path
49-
strokeOpacity={0.25}
50-
stroke="#000"
5153
d="M.707 12.5l11.794 11.793 11.798-11.79L12.5.707.707 12.5z"
52-
fill="#efefef"
54+
fill={backgroundColor}
5355
fillRule="evenodd"
5456
filter="url(#halo)"
5557
/>
5658
)}
59+
5760
<defs>
5861
<filter id="halo">
5962
<feDropShadow

src/components/Map/useMapIconLoader.tsx

+31-20
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,34 @@ function loadIcon(
3636
const wasInCache = !!dataUrl;
3737
if (!dataUrl) {
3838
const div = document.createElement("div");
39+
3940
const root = createRoot(div);
40-
flushSync(() => {
41-
const [categoryIconName, accessibilityGrade] = finalIconName.split("-");
42-
const IconComponent =
43-
categoryIconName && accessibilityGrade
44-
? icons[categoryIconName]
45-
: icons[iconName];
46-
const element = accessibilityGrade ? (
47-
<ColoredIconMarker accessibilityGrade={accessibilityGrade}>
48-
<IconComponent />
49-
</ColoredIconMarker>
50-
) : (
41+
const [categoryIconName, accessibilityGrade] = finalIconName.split("-");
42+
const IconComponent =
43+
categoryIconName && accessibilityGrade
44+
? icons[categoryIconName]
45+
: icons[iconName];
46+
47+
// Marker color originates from a CSS variable value like `var(--rating-yes)`
48+
const style = getComputedStyle(document.documentElement);
49+
const backgroundColor = style.getPropertyValue(
50+
`--rating-${accessibilityGrade}`,
51+
);
52+
const foregroundColor = style.getPropertyValue(
53+
`--rating-${accessibilityGrade}-contrast`,
54+
);
55+
const element = accessibilityGrade ? (
56+
<ColoredIconMarker
57+
accessibilityGrade={accessibilityGrade}
58+
backgroundColor={backgroundColor}
59+
>
5160
<IconComponent />
52-
);
61+
</ColoredIconMarker>
62+
) : (
63+
<IconComponent />
64+
);
65+
66+
flushSync(() => {
5367
root.render(element);
5468
});
5569
const svgElement = div.querySelector("svg");
@@ -76,13 +90,12 @@ function loadIcon(
7690
`scale(${options.iconSize}, ${options.iconSize})`,
7791
);
7892
}
79-
if (options?.fill) {
80-
const filledElements = iconElement.querySelectorAll(
93+
const filledElements =
94+
iconElement?.querySelectorAll(
8195
"path, rect, circle, ellipse, line, polyline, polygon",
82-
);
83-
for (const e of filledElements) {
84-
e.setAttribute("fill", options.fill);
85-
}
96+
) ?? [];
97+
for (const e of filledElements) {
98+
e.setAttribute("fill", options.fill ?? foregroundColor);
8699
}
87100

88101
if (options.addShadow) {
@@ -147,13 +160,11 @@ export function loadIconsInMapInstance(mapInstance: MapBoxMap): void {
147160
for (const iconName of Object.keys(categoryIcons)) {
148161
for (const accessibilityGrade of ["yes", "no", "limited"]) {
149162
loadIcon(mapInstance, categoryIcons, iconName, {
150-
fill: "white",
151163
addShadow: true,
152164
suffix: `-${accessibilityGrade}`,
153165
});
154166
}
155167
loadIcon(mapInstance, categoryIcons, iconName, {
156-
fill: "#666",
157168
addShadow: true,
158169
suffix: "-unknown",
159170
iconSize: 0.8,

0 commit comments

Comments
 (0)