|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +function extractText(node: React.ReactNode): string { |
| 4 | + if (typeof node === 'string') { |
| 5 | + return node; |
| 6 | + } |
| 7 | + |
| 8 | + if (typeof node === 'number') { |
| 9 | + return String(node); |
| 10 | + } |
| 11 | + |
| 12 | + if (React.isValidElement<{ children?: React.ReactNode }>(node)) { |
| 13 | + return extractText(node.props.children); |
| 14 | + } |
| 15 | + |
| 16 | + if (Array.isArray(node)) { |
| 17 | + return node.map(extractText).join('\n'); |
| 18 | + } |
| 19 | + |
| 20 | + return ''; |
| 21 | +} |
| 22 | + |
| 23 | +type Props = { |
| 24 | + title: string; |
| 25 | + children: React.ReactNode; |
| 26 | +}; |
| 27 | + |
| 28 | +export default function ThemeColors({ title, children }: Props) { |
| 29 | + const text = extractText(children); |
| 30 | + |
| 31 | + const colors: { name: string; value: string }[] = []; |
| 32 | + |
| 33 | + for (const line of text.split('\n')) { |
| 34 | + const match = line.trim().match(/^(\w+):\s*(.+)$/); |
| 35 | + |
| 36 | + if (match) { |
| 37 | + colors.push({ name: match[1], value: match[2] }); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + style={{ |
| 44 | + display: 'grid', |
| 45 | + gridTemplateColumns: 'repeat(auto-fill, minmax(100px, 1fr))', |
| 46 | + gap: 'var(--ifm-spacing-horizontal)', |
| 47 | + marginBottom: 'var(--ifm-spacing-vertical)', |
| 48 | + }} |
| 49 | + > |
| 50 | + {colors.map(({ name, value }) => ( |
| 51 | + <div key={name}> |
| 52 | + <div |
| 53 | + style={{ |
| 54 | + width: '100%', |
| 55 | + aspectRatio: '2 / 1', |
| 56 | + backgroundColor: value, |
| 57 | + borderRadius: 'var(--ifm-global-radius)', |
| 58 | + boxShadow: 'inset 0 0 0 1px rgba(0, 0, 0, 0.16)', |
| 59 | + marginBottom: 'calc(var(--ifm-spacing-vertical) / 2)', |
| 60 | + }} |
| 61 | + /> |
| 62 | + <div |
| 63 | + style={{ |
| 64 | + fontFamily: 'var(--ifm-font-family-monospace)', |
| 65 | + fontSize: '.675rem', |
| 66 | + }} |
| 67 | + > |
| 68 | + <div style={{ fontWeight: 'var(--ifm-heading-font-weight)' }}> |
| 69 | + {name} |
| 70 | + </div> |
| 71 | + <div style={{ opacity: 0.7 }}>{value}</div> |
| 72 | + </div> |
| 73 | + </div> |
| 74 | + ))} |
| 75 | + </div> |
| 76 | + ); |
| 77 | +} |
0 commit comments