Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SourceControlIcon.tsx

76 lines2.4 KB

1import type { SourceControlIcon as SourceControlIconArtwork } from "@t3tools/client-runtime/source-control-clients";
2import { useId } from "react";
3import Svg, { Defs, LinearGradient, Path, Stop } from "react-native-svg";
4import { withUniwind } from "uniwind";
5​
6const ThemedSvg = withUniwind(Svg);
7​
8/** Draws a host's mark from its definition's artwork; `currentColor` follows `color`. */
9export function SourceControlIcon(props: {
10 readonly icon: SourceControlIconArtwork | null;
11 readonly size?: number;
12 readonly color?: string;
13 readonly colorClassName?: string;
14}) {
15 // Gradient ids must not collide when one screen shows the same mark more than once.
16 const idPrefix = useId().replace(/[^\w-]/gu, "");
17 const size = props.size ?? 18;
18 const { icon } = props;
19 if (icon === null) return null;
20 const paint = (value: SourceControlIconArtwork["paths"][number]["fill"]) =>
21 value === undefined
22 ? "none"
23 : typeof value === "string"
24 ? value
25 : `url(#${idPrefix}-${value.gradient})`;
26​
27 return (
28 <ThemedSvg
29 width={size}
30 height={size}
31 viewBox={icon.viewBox}
32 color={props.color}
33 colorClassName={props.colorClassName}
34 fill="none"
35 >
36 {icon.gradients ? (
37 <Defs>
38 {icon.gradients.map((gradient) => (
39 <LinearGradient
40 key={gradient.id}
41 id={`${idPrefix}-${gradient.id}`}
42 x1={gradient.x1}
43 y1={gradient.y1}
44 x2={gradient.x2}
45 y2={gradient.y2}
46 gradientTransform={gradient.transform}
47 gradientUnits="userSpaceOnUse"
48 >
49 {gradient.stops.map((stop) => (
50 <Stop
51 key={stop.offset}
52 offset={stop.offset}
53 stopColor={stop.color}
54 stopOpacity={stop.opacity}
55 />
56 ))}
57 </LinearGradient>
58 ))}
59 </Defs>
60 ) : null}
61 {icon.paths.map((path, index) => (
62 <Path
63 // Paths repeat with different paints, so their data is not a key.
64 // oxlint-disable-next-line react/no-array-index-key
65 key={index}
66 d={path.d}
67 fill={paint(path.fill)}
68 fillRule={path.fillRule}
69 fillOpacity={path.fillOpacity}
70 stroke={path.stroke}
71 strokeWidth={path.strokeWidth}
72 />
73 ))}
74 </ThemedSvg>
75 );