|
| 1 | +import { Typography } from "antd"; |
| 2 | +import SpoolIcon from "./spoolIcon"; |
| 3 | + |
| 4 | +interface ColorHexPreviewProps { |
| 5 | + colorHex?: string | null; |
| 6 | + multiColorHexes?: string | null; |
| 7 | + multiColorDirection?: string | null; |
| 8 | +} |
| 9 | + |
| 10 | +const SMALL_TEXT_STYLE = { |
| 11 | + fontSize: 12, |
| 12 | + color: "rgba(255,255,255,0.45)", |
| 13 | + lineHeight: 1.2, |
| 14 | +}; |
| 15 | + |
| 16 | +const normalizeHex = (value: string) => `#${value.replace("#", "").toUpperCase()}`; |
| 17 | + |
| 18 | +export default function ColorHexPreview({ colorHex, multiColorHexes, multiColorDirection }: Readonly<ColorHexPreviewProps>) { |
| 19 | + const colors = |
| 20 | + multiColorHexes |
| 21 | + ?.split(",") |
| 22 | + .map((hex) => hex.trim()) |
| 23 | + .filter((hex) => hex.length > 0) |
| 24 | + .map(normalizeHex) ?? []; |
| 25 | + |
| 26 | + if (colors.length <= 1) { |
| 27 | + const singleColor = colorHex ? normalizeHex(colorHex) : colors[0]; |
| 28 | + if (!singleColor) return null; |
| 29 | + return ( |
| 30 | + <div style={{ display: "flex", flexDirection: "column", gap: 4 }}> |
| 31 | + <SpoolIcon color={singleColor} size="large" no_margin /> |
| 32 | + <Typography.Text style={SMALL_TEXT_STYLE}>{singleColor}</Typography.Text> |
| 33 | + </div> |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + const isLongitudinal = multiColorDirection === "longitudinal"; |
| 38 | + if (isLongitudinal) { |
| 39 | + return ( |
| 40 | + <div style={{ display: "flex", flexDirection: "column", gap: 4 }}> |
| 41 | + {colors.map((hex, index) => ( |
| 42 | + <div key={`${hex}-${index}`} style={{ display: "flex", alignItems: "center", gap: 8 }}> |
| 43 | + <div |
| 44 | + style={{ |
| 45 | + width: 56, |
| 46 | + height: 22, |
| 47 | + borderRadius: 5, |
| 48 | + border: "1px solid rgba(255,255,255,0.22)", |
| 49 | + background: hex, |
| 50 | + }} |
| 51 | + /> |
| 52 | + <Typography.Text style={SMALL_TEXT_STYLE}>{hex}</Typography.Text> |
| 53 | + </div> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <div style={{ display: "flex", alignItems: "flex-start", gap: 0 }}> |
| 61 | + {colors.map((hex, index) => ( |
| 62 | + <div |
| 63 | + key={`${hex}-${index}`} |
| 64 | + style={{ |
| 65 | + display: "flex", |
| 66 | + flexDirection: "column", |
| 67 | + alignItems: "center", |
| 68 | + width: 64, |
| 69 | + gap: 4, |
| 70 | + }} |
| 71 | + > |
| 72 | + <SpoolIcon color={hex} size="large" no_margin /> |
| 73 | + <Typography.Text style={{ ...SMALL_TEXT_STYLE, textAlign: "center" }}>{hex}</Typography.Text> |
| 74 | + </div> |
| 75 | + ))} |
| 76 | + </div> |
| 77 | + ); |
| 78 | +} |
0 commit comments