Skip to main content

useTeamColors: swatches from a hook

KC #e31837KC #ffb612LAC #007bc7LAC #ffc20eDEN #002244DEN #fb4f14LV #000000LV #a5acafBUF #00338dBUF #c60c30MIA #008e97MIA #f58220NYJ #003f2dNYJ #000000NE #002244NE #c60c30
import { STANDINGS } from "@sportsdataverse/examples/data";
import { useTeamColors } from "@sportsdataverse/sdvplot/react";
import type { ReactElement } from "react";

function Swatches(): ReactElement | null {
// { team: "#hex" } for the whole league; undefined until the league has loaded.
const primary = useTeamColors("nfl");
const secondary = useTeamColors("nfl", { which: "secondary" });
if (primary === undefined || secondary === undefined) return null;
return (
<svg width={STANDINGS.length * 44} height={88} role="img" aria-label="AFC team colours">
{STANDINGS.map((s, i) =>
[primary, secondary].map((colors, row) => (
<rect
key={`${s.team}-${row}`}
x={i * 44 + 2}
y={row * 44 + 2}
width={40}
height={40}
fill={colors[s.team] ?? "grey"}
>
<title>{`${s.team} ${colors[s.team] ?? "unknown"}`}</title>
</rect>
)),
)}
</svg>
);
}

<Swatches />;

Tags: react · useTeamColors · colors · nfl. All sdvplot examples.