|
| 1 | +# Figma to React Generator |
| 2 | + |
| 3 | +Custom system for generating high-quality React components from Figma designs. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The system is already configured in the project. No external dependencies needed - it uses our custom built tools: |
| 8 | +- `tools/figma/advanced-mapper.ts` - Figma to React conversion |
| 9 | +- `tools/figma/code-generator.ts` - TypeScript/JSX code generation |
| 10 | +- `tools/figma/figmaClient.ts` - Figma API client |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +### Basic Command |
| 15 | + |
| 16 | +```bash |
| 17 | +pnpm figma:builder --url "FIGMA_URL" --feature FEATURE_NAME --name COMPONENT_NAME |
| 18 | +``` |
| 19 | + |
| 20 | +### Examples |
| 21 | + |
| 22 | +```bash |
| 23 | +# With Figma URL |
| 24 | +pnpm figma:builder \ |
| 25 | + --url "https://www.figma.com/design/GhUCE2ZLNtDvUSVOa4w0Xg/FoodBlog--Admin-dashboard?node-id=83-50804" \ |
| 26 | + --feature dashboard \ |
| 27 | + --name HeaderComponent |
| 28 | + |
| 29 | +# With fileKey and query |
| 30 | +pnpm figma:builder \ |
| 31 | + --file GhUCE2ZLNtDvUSVOa4w0Xg \ |
| 32 | + --query "thumbnail|preview" \ |
| 33 | + --feature ui \ |
| 34 | + --name PreviewCard |
| 35 | + |
| 36 | +# With specific sectionId |
| 37 | +pnpm figma:builder \ |
| 38 | + --file GhUCE2ZLNtDvUSVOa4w0Xg \ |
| 39 | + --sectionId "1:2" \ |
| 40 | + --feature components \ |
| 41 | + --name ThumbnailCard |
| 42 | + |
| 43 | +# With page and section |
| 44 | +pnpm figma:builder \ |
| 45 | + --file GhUCE2ZLNtDvUSVOa4w0Xg \ |
| 46 | + --page "GOB-98 / Admin's dashboard" \ |
| 47 | + --section "stats" \ |
| 48 | + --feature dashboard \ |
| 49 | + --name StatsWidget |
| 50 | +``` |
| 51 | + |
| 52 | +## Options |
| 53 | + |
| 54 | +| Option | Description | Example | |
| 55 | +|-------|------|----------| |
| 56 | +| `--url` | Full Figma URL (extracts fileKey and node-id) | `--url "https://figma.com/design/abc123?node-id=1-2"` | |
| 57 | +| `--file` | File key from Figma | `--file GhUCE2ZLNtDvUSVOa4w0Xg` | |
| 58 | +| `--page` | Page name (CANVAS) in Figma | `--page "Dashboard"` | |
| 59 | +| `--section` | Frame/component name to export | `--section "Header"` | |
| 60 | +| `--sectionId` | Frame ID (skips name search) | `--sectionId "1:2"` | |
| 61 | +| `--query` | Search pattern (supports 'a\|b\|c') | `--query "card\|widget"` | |
| 62 | +| `--feature` | **Required** - feature folder | `--feature dashboard` | |
| 63 | +| `--name` | **Required** - component name | `--name HeaderComponent` | |
| 64 | +| `--outDir` | Output directory | `--outDir src/features` (default) | |
| 65 | + |
| 66 | +## What It Generates |
| 67 | + |
| 68 | +The system generates a complete component structure: |
| 69 | + |
| 70 | +``` |
| 71 | +src/features/{feature}/components/{ComponentName}/ |
| 72 | +├── ComponentName.tsx # React component with TypeScript |
| 73 | +├── ComponentName.css # CSS styles with exact values from Figma |
| 74 | +├── ComponentName.stories.tsx # Storybook story |
| 75 | +└── ComponentName.test.tsx # Unit test |
| 76 | +``` |
| 77 | + |
| 78 | +### Additional Files |
| 79 | + |
| 80 | +- **Route**: `src/routes/{feature}-{ComponentName}/index.tsx` - automatic routing |
| 81 | +- **Export**: updates `src/features/{feature}/components/index.ts` |
| 82 | + |
| 83 | +## Features |
| 84 | + |
| 85 | +### 🎨 Precise Style Conversion |
| 86 | +- Precise dimensions from Figma (px, rem) |
| 87 | +- RGB/RGBA colours with opacity |
| 88 | +- Linear gradients |
| 89 | +- Shadows (box-shadow) |
| 90 | +- Border radius |
| 91 | +- Typography (font-size, weight, line-height) |
| 92 | + |
| 93 | +### 🧩 Intelligent Components |
| 94 | +- Automatic component type detection (Button, Input, Card, etc.) |
| 95 | +- Semantic JSX elements |
| 96 | +- Tailwind classes for layout |
| 97 | +- Custom CSS classes for detailed styles |
| 98 | + |
| 99 | +### 📱 Responsiveness |
| 100 | +- Flex layouts from Figma |
| 101 | +- Gap, padding, margin |
| 102 | +- Alignment (justify-content, align-items) |
| 103 | +- Breakpoints (can be extended) |
| 104 | + |
| 105 | +### ♿ Accessibility |
| 106 | +- Semantic HTML |
| 107 | +- Alt texts for images |
| 108 | +- ARIA attributes (in development) |
| 109 | +- Focus management (in development) |
| 110 | + |
| 111 | +## Frame Search Strategy |
| 112 | + |
| 113 | +The system automatically finds the best frame for conversion: |
| 114 | + |
| 115 | +1. **Exact sectionId** - if `--sectionId` is provided, uses it directly |
| 116 | +2. **Exact section name** - exact name matching |
| 117 | +3. **Query/loose match** - fuzzy search with scoring: |
| 118 | + - Exact match: 100 points |
| 119 | + - Contains term: 50 points |
| 120 | + - Word contains term: 25 points |
| 121 | +4. **Biggest frame** - if nothing matches, takes the largest |
| 122 | + |
| 123 | +## Configuration |
| 124 | + |
| 125 | +### Environment Variables |
| 126 | + |
| 127 | +```env |
| 128 | +FIGMA_API_TOKEN=your_figma_token_here |
| 129 | +``` |
| 130 | + |
| 131 | +### Configuration |
| 132 | + |
| 133 | +The generator is fully configured via CLI parameters - no config files needed: |
| 134 | + |
| 135 | +- `--url`: Full Figma URL (extracts fileKey and node-id automatically) |
| 136 | +- `--feature`: Target feature folder (e.g., 'dashboard') |
| 137 | +- `--name`: Component name (e.g., 'StatsGrid') |
| 138 | + |
| 139 | +The generator automatically uses: |
| 140 | +- **Framework**: React with TypeScript |
| 141 | +- **Styling**: Tailwind CSS with generated CSS files |
| 142 | +- **Output**: Barrel exports, Storybook stories, and tests |
| 143 | +``` |
| 144 | +
|
| 145 | +## Examples of Generated Components |
| 146 | +
|
| 147 | +### Simple Component |
| 148 | +
|
| 149 | +```tsx |
| 150 | +import React from 'react'; |
| 151 | +import './HeaderComponent.css'; |
| 152 | +
|
| 153 | +interface HeaderComponentProps { |
| 154 | + className?: string; |
| 155 | + children?: React.ReactNode; |
| 156 | +} |
| 157 | +
|
| 158 | +const HeaderComponent: React.FC<HeaderComponentProps> = ({ className = "", ...props }) => { |
| 159 | + return ( |
| 160 | + <div className="flex flex-row justify-between items-center figma-abc123"> |
| 161 | + <h1 className="text-2xl font-bold figma-def456">Dashboard</h1> |
| 162 | + <button className="bg-blue-500 text-white px-4 py-2 rounded figma-ghi789"> |
| 163 | + Settings |
| 164 | + </button> |
| 165 | + </div> |
| 166 | + ); |
| 167 | +}; |
| 168 | +
|
| 169 | +export default HeaderComponent; |
| 170 | +export { HeaderComponent }; |
| 171 | +``` |
| 172 | + |
| 173 | +### CSS with Precise Styles |
| 174 | + |
| 175 | +```css |
| 176 | +.figma-abc123 { |
| 177 | + width: 1200px; |
| 178 | + height: 80px; |
| 179 | + background-color: rgb(255, 255, 255); |
| 180 | + box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1); |
| 181 | + padding: 16px 24px; |
| 182 | +} |
| 183 | + |
| 184 | +.figma-def456 { |
| 185 | + font-size: 28px; |
| 186 | + font-weight: 700; |
| 187 | + line-height: 34px; |
| 188 | + color: rgb(31, 41, 55); |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +## Troubleshooting |
| 193 | + |
| 194 | +### Incorrect Styles |
| 195 | +- Check if the frame has all necessary properties in Figma |
| 196 | +- Ensure the design doesn't use library components (e.g., Safari toolbar) |
| 197 | + |
| 198 | +### Missing Frames |
| 199 | +- Check available pages: the system will display a list if none found |
| 200 | +- Use `--query` with alternatives: `--query "card|widget|component"` |
| 201 | + |
| 202 | +### API Errors |
| 203 | +- Check if `FIGMA_API_TOKEN` is correct |
| 204 | +- Ensure you have access to the Figma file |
| 205 | + |
| 206 | +## Comparison with Other Tools |
| 207 | + |
| 208 | +| Feature | Our Generator | Builder.io Visual Copilot | Figma Dev Mode | |
| 209 | +|---------|---------------|---------------------------|----------------| |
| 210 | +| Style Accuracy | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | |
| 211 | +| Automation | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ | |
| 212 | +| Customisation | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐ | |
| 213 | +| TypeScript | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | |
| 214 | +| Tailwind Integration | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐ | |
| 215 | +| Free Usage | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐⭐⭐⭐ | |
| 216 | +| CLI Automation | ⭐⭐⭐⭐⭐ | ⭐⭐ | ⭐ | |
| 217 | + |
| 218 | +## Development |
| 219 | + |
| 220 | +The system can be extended with: |
| 221 | +- More CSS frameworks (styled-components, emotion) |
| 222 | +- Advanced component detection patterns |
| 223 | +- Image optimization and conversion |
| 224 | +- Animation and interaction conversion |
| 225 | +- Design tokens extraction and generation |
| 226 | +- Better responsive breakpoint handling |
0 commit comments