cloudflare/kumo
Publicmirrored fromhttps://github.com/cloudflare/kumoAvailable
packages/kumo-figma/src/parsers/component-registry.ts
126lines · modecode
| 1 | /** |
| 2 | * Component Registry Parser |
| 3 | * |
| 4 | * Parses component-registry.json to extract component specifications. |
| 5 | * Provides types and utilities for accessing component metadata. |
| 6 | */ |
| 7 | |
| 8 | // Import shared types from @cloudflare/kumo |
| 9 | import type { |
| 10 | ComponentRegistry, |
| 11 | ComponentSchema, |
| 12 | PropSchema, |
| 13 | SubComponentSchema, |
| 14 | } from "@cloudflare/kumo"; |
| 15 | |
| 16 | // Re-export with aliases for backwards compatibility |
| 17 | export type { ComponentRegistry }; |
| 18 | export type ComponentSpec = ComponentSchema; |
| 19 | export type ComponentProp = PropSchema; |
| 20 | export type SubComponent = SubComponentSchema; |
| 21 | |
| 22 | /** |
| 23 | * Parse component registry JSON |
| 24 | * |
| 25 | * @param registryJson - Raw JSON string or parsed object |
| 26 | * @returns Parsed component registry |
| 27 | */ |
| 28 | export function parseComponentRegistry( |
| 29 | registryJson: string | ComponentRegistry, |
| 30 | ): ComponentRegistry { |
| 31 | if (typeof registryJson === "string") { |
| 32 | return JSON.parse(registryJson) as ComponentRegistry; |
| 33 | } |
| 34 | return registryJson; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Get component specification by name |
| 39 | * |
| 40 | * @param registry - Component registry |
| 41 | * @param componentName - Name of component (e.g., "Button", "Badge") |
| 42 | * @returns Component specification or undefined if not found |
| 43 | */ |
| 44 | export function getComponentSpec( |
| 45 | registry: ComponentRegistry, |
| 46 | componentName: string, |
| 47 | ): ComponentSpec | undefined { |
| 48 | return registry.components[componentName]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get variant values for a component prop |
| 53 | * |
| 54 | * @param spec - Component specification |
| 55 | * @param propName - Prop name (e.g., "variant", "size") |
| 56 | * @returns Array of variant values or undefined if prop is not an enum |
| 57 | * |
| 58 | * @example |
| 59 | * const buttonSpec = getComponentSpec(registry, "Button"); |
| 60 | * const variants = getVariantValues(buttonSpec, "variant"); |
| 61 | * // ["primary", "secondary", "ghost", "destructive", "outline"] |
| 62 | */ |
| 63 | export function getVariantValues( |
| 64 | spec: ComponentSpec, |
| 65 | propName: string, |
| 66 | ): readonly string[] | undefined { |
| 67 | const prop = spec.props[propName]; |
| 68 | if (prop?.type === "enum" && prop.values) { |
| 69 | return prop.values; |
| 70 | } |
| 71 | return undefined; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get default value for a prop |
| 76 | * |
| 77 | * @param spec - Component specification |
| 78 | * @param propName - Prop name |
| 79 | * @returns Default value or undefined |
| 80 | */ |
| 81 | export function getDefaultValue( |
| 82 | spec: ComponentSpec, |
| 83 | propName: string, |
| 84 | ): string | number | boolean | undefined { |
| 85 | return spec.props[propName]?.default; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get variant descriptions |
| 90 | * |
| 91 | * @param spec - Component specification |
| 92 | * @param propName - Prop name |
| 93 | * @returns Map of variant value to description |
| 94 | */ |
| 95 | export function getVariantDescriptions( |
| 96 | spec: ComponentSpec, |
| 97 | propName: string, |
| 98 | ): Record<string, string> | undefined { |
| 99 | return spec.props[propName]?.descriptions; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get all semantic color tokens used by a component |
| 104 | * |
| 105 | * @param spec - Component specification |
| 106 | * @returns Array of color token names (e.g., ["bg-kumo-brand", "text-kumo-default"]) |
| 107 | */ |
| 108 | export function getComponentColors(spec: ComponentSpec): string[] { |
| 109 | return spec.colors || []; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Extract components by category |
| 114 | * |
| 115 | * @param registry - Component registry |
| 116 | * @param category - Category name (e.g., "Action", "Input", "Display") |
| 117 | * @returns Array of component specifications in that category |
| 118 | */ |
| 119 | export function getComponentsByCategory( |
| 120 | registry: ComponentRegistry, |
| 121 | category: string, |
| 122 | ): ComponentSpec[] { |
| 123 | return Object.values(registry.components).filter( |
| 124 | (spec) => spec.category === category, |
| 125 | ); |
| 126 | } |
| 127 | |