๐Ÿ“ฆ juspay / blend-design-system

๐Ÿ“„ FIGMA_CODE_CONNECT.md ยท 526 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526# Figma Code Connect - Complete Guide

## Table of Contents

1. [Current Status](#current-status)
2. [Quick Start](#quick-start)
3. [Overview](#overview)
4. [Monorepo Setup](#monorepo-setup)
5. [Configuration](#configuration)
6. [Adding Code Connect to Components](#adding-code-connect-to-components)
7. [Prop Mapping Guide](#prop-mapping-guide)
8. [Component Examples](#component-examples)
9. [Publishing](#publishing)
10. [Troubleshooting](#troubleshooting)
11. [Best Practices](#best-practices)
12. [Migration Guide](#migration-guide)

## Current Status

### โœ… Setup Complete & Published

- **Configuration**: `figma.config.json` configured with correct include paths
- **Dependencies**: `@figma/code-connect` installed
- **Scripts**: Added to package.json (`figma:validate`, `figma:publish`, `figma:unpublish`)
- **Environment**: `.env` file created with Figma access token
- **Migration**: Updated repository URLs from old to new repo
- **Published**: All Code Connect files successfully published to Figma

### Existing Code Connect Components

1. Button - `apps/storybook/stories/components/Button/Button.figma.tsx`
2. ButtonGroup - `apps/storybook/stories/components/Button/ButtonGroup.figma.tsx`
3. Breadcrumb - `apps/storybook/stories/components/Breadcrumb/Breadcrumb.figma.tsx`
4. Tags - `apps/storybook/stories/components/Tags/Tags.figma.tsx`
5. SplitTag - `apps/storybook/stories/components/Tags/SplitTag.figma.tsx`
6. Checkbox - `apps/storybook/stories/components/Checkbox/Checkbox.figma.tsx` โœ…
7. Switch - `apps/storybook/stories/components/Switch/Switch.figma.tsx` โœ…
8. Radio - `apps/storybook/stories/components/Radio/Radio.figma.tsx` โœ…
9. Alert - `apps/storybook/stories/components/Alert/Alert.figma.tsx` โœ…
10. Modal - `apps/storybook/stories/components/Modal/Modal.figma.tsx` โœ…
11. StatCard - `apps/storybook/stories/components/StatCard/StatCard.figma.tsx` โœ…
12. Tabs (TabsTrigger) - `apps/storybook/stories/components/Tabs/Tabs.figma.tsx` โœ…
13. Avatar - `apps/storybook/stories/components/Avatar/Avatar.figma.tsx` โœ…
14. AvatarGroup - `apps/storybook/stories/components/Avatar/AvatarGroup.figma.tsx` โœ…
15. Popover - `apps/storybook/stories/components/Popover/Popover.figma.tsx` โœ…
16. Tooltip - `apps/storybook/stories/components/Tooltip/Tooltip.figma.tsx` โœ…
17. TextInput - `apps/storybook/stories/components/TextInput/TextInput.figma.tsx` โœ…
18. DropdownInput - `apps/storybook/stories/components/TextInput/DropdownInput.figma.tsx` โœ…
19. OTPInput - `apps/storybook/stories/components/TextInput/OTPInput.figma.tsx` โœ…
20. TextArea - `apps/storybook/stories/components/TextInput/TextArea.figma.tsx` โœ…
21. SearchInput - `apps/storybook/stories/components/TextInput/SearchInput.figma.tsx` โœ…
22. NumberInput - `apps/storybook/stories/components/TextInput/NumberInput.figma.tsx` โœ…
23. UnitInput - `apps/storybook/stories/components/TextInput/UnitInput.figma.tsx` โœ…
24. MultiValueInput - `apps/storybook/stories/components/TextInput/MultiValueInput.figma.tsx` โœ…
25. DateRangePicker - `apps/storybook/stories/components/DateRangePicker/DateRangePicker.figma.tsx` โœ…
26. Charts - `apps/storybook/stories/components/Charts/Charts.figma.tsx` โœ…
27. DataTable - `apps/storybook/stories/components/DataTable/DataTable.figma.tsx` โœ…
28. Sidebar - `apps/storybook/stories/components/Sidebar/Sidebar.figma.tsx` โœ…

## Quick Start

### For New Components

```typescript
import { figma } from '@figma/code-connect'
import { YourComponent } from '@juspay/blend-design-system'

figma.connect(
  YourComponent,
  'YOUR_FIGMA_URL_HERE', // Right-click component โ†’ Copy link
  {
    props: {
      // Map your props here
      text: figma.string('text'),
      variant: figma.enum('variant', {
        'primary': 'primary',
        'secondary': 'secondary'
      }),
      disabled: figma.boolean('disabled'),
      icon: figma.boolean('hasIcon', {
        true: figma.instance('icon'),
        false: undefined
      })
    },
    example: ({ text, variant, disabled, icon }) => (
      <YourComponent
        text={text}
        variant={variant}
        disabled={disabled}
        icon={icon}
      />
    ),
    imports: ["import { YourComponent } from '@juspay/blend-design-system'"],
    links: [
      {
        name: "GitHub",
        url: "https://github.com/juspay/blend-design-system/tree/main/packages/blend/lib/components/YourComponent"
      },
      {
        name: "Storybook",
        url: "https://juspay.design/storybook/?path=/docs/components-yourcomponent--docs"
      }
    ]
  }
);
```

### Commands

```bash
# Validate Code Connect files
pnpm figma:validate

# Publish to Figma
pnpm figma:publish

# Remove from Figma
pnpm figma:unpublish
```

## Overview

Figma Code Connect bridges design and development by showing production-ready code snippets in Figma's Dev Mode. When developers inspect components, they see:

- Generated code snippet with correct props
- Import statements
- Links to GitHub and Storybook
- **NOT visible**: Code comments, annotations, or descriptions

## Monorepo Setup

### File Structure

```
blend-design-system/
โ”œโ”€โ”€ figma.config.json          # Figma Code Connect configuration
โ”œโ”€โ”€ .env                       # Your Figma access token (git ignored)
โ”œโ”€โ”€ .env.figma.example         # Template for token
โ”œโ”€โ”€ package.json               # Contains figma:* scripts
โ”œโ”€โ”€ apps/
โ”‚   โ””โ”€โ”€ storybook/
โ”‚       โ””โ”€โ”€ stories/
โ”‚           โ””โ”€โ”€ components/    # .figma.tsx files here
โ””โ”€โ”€ packages/
    โ””โ”€โ”€ blend/
        โ””โ”€โ”€ lib/
            โ””โ”€โ”€ components/    # Or as separate .figma.tsx files
```

### Initial Setup

1. **Install Dependencies** (Already done)

    ```bash
    pnpm add -D @figma/code-connect -w
    ```

2. **Get Figma Access Token**
    - Go to Figma โ†’ Settings โ†’ Account โ†’ Personal access tokens
    - Create token with scopes: File content (read-only), Code Connect (write)
    - Add to `.env` file

3. **Environment Setup**
    ```bash
    # Copy example file
    cp .env.figma.example .env
    # Add your token to .env
    ```

## Configuration

### figma.config.json

```json
{
    "codeConnect": {
        "include": [
            "apps/storybook/stories/**/*.figma.{ts,tsx}",
            "apps/storybook/stories/**/*.stories.{ts,tsx}",
            "packages/blend/lib/**/*.figma.{ts,tsx}"
        ],
        "exclude": [
            "**/node_modules/**",
            "**/dist/**",
            "**/build/**",
            "apps/ascent/**",
            "apps/site/**"
        ],
        "parser": "react"
    }
}
```

### Package.json Scripts

```json
{
    "scripts": {
        "figma:validate": "figma connect validate",
        "figma:publish": "figma connect publish",
        "figma:unpublish": "figma connect unpublish"
    }
}
```

## Adding Code Connect to Components

### Location Options

1. **Separate .figma.tsx Files** (Recommended)

    ```
    apps/storybook/stories/components/Button/Button.figma.tsx
    ```

2. **In Storybook Stories**
    ```
    apps/storybook/stories/components/Button/Button.stories.tsx
    ```

### Getting Figma Component URL

1. Open Figma file
2. Select the component (not an instance)
3. Right-click โ†’ "Copy link to selection"
4. URL format: `https://www.figma.com/design/FILE_ID/FILE_NAME?node-id=NODE_ID`

## Prop Mapping Guide

### Basic Types

```typescript
// String props
text: figma.string('text'),
placeholder: figma.string('Placeholder'),

// Boolean props
disabled: figma.boolean('disabled'),
loading: figma.boolean('loading'),

// Enum props
size: figma.enum('size', {
  'Small': 'sm',
  'Medium': 'md',
  'Large': 'lg'
}),

// Instance props (for nested components)
leftIcon: figma.instance('Left Icon'),
rightIcon: figma.instance('Right Icon')
```

### Advanced Mappings

```typescript
// Conditional props - map boolean to component
icon: figma.boolean('hasIcon', {
  true: figma.instance('icon'),
  false: undefined
}),

// Map Figma state to multiple props
state: figma.enum('State', {
  'Default': { disabled: false, loading: false },
  'Disabled': { disabled: true, loading: false },
  'Loading': { disabled: false, loading: true }
}),

// Handle Figma-only variants
subType: figma.enum('subType', {
  'default': 'default',
  'iconOnly': 'icon-only',
  // 'plainIcon' exists in Figma but not in code - excluded
})
```

## Component Examples

### Button Component

```typescript
import { figma } from '@figma/code-connect'
import { Button, ButtonType, ButtonSize, ButtonSubType } from '@juspay/blend-design-system'

figma.connect(
  Button,
  'https://www.figma.com/design/fHb0XUhWXZErq97C6N9uG3/-BETA--Dashboard-Design-System?node-id=14667-834',
  {
    props: {
      text: figma.string('text'),

      buttonType: figma.enum('buttonType', {
        'primary': ButtonType.PRIMARY,
        'secondary': ButtonType.SECONDARY,
        'danger': ButtonType.DANGER,
        'success': ButtonType.SUCCESS
      }),

      size: figma.enum('size', {
        'sm': ButtonSize.SMALL,
        'md': ButtonSize.MEDIUM,
        'lg': ButtonSize.LARGE
      }),

      disabled: figma.enum('state', {
        'disabled': true,
        'default': false,
        'hover': false,
        'active': false,
        'focussed': false
      }),

      leftIcon: figma.boolean('hasLeftIcon', {
        true: figma.instance('leftIcon'),
        false: undefined
      }),

      rightIcon: figma.boolean('hasRightIcon', {
        true: figma.instance('rightIcon'),
        false: undefined
      })
    },

    example: ({ text, buttonType, size, disabled, leftIcon, rightIcon }) => (
      <Button
        text={text}
        buttonType={buttonType}
        size={size}
        disabled={disabled}
        leftIcon={leftIcon}
        rightIcon={rightIcon}
      />
    ),

    imports: ["import { Button } from '@juspay/blend-design-system'"],

    links: [
      {
        name: "GitHub",
        url: "https://github.com/juspay/blend-design-system/tree/main/packages/blend/lib/components/Button"
      },
      {
        name: "Storybook",
        url: "https://juspay.design/storybook/?path=/docs/components-button-buttonv2--docs"
      }
    ]
  }
);
```

### Tag Component

```typescript
figma.connect(
  Tag,
  'https://www.figma.com/design/fHb0XUhWXZErq97C6N9uG3/-BETA--Dashboard-Design-System?node-id=2021-8332',
  {
    props: {
      text: figma.string('text'),

      variant: figma.enum('variant', {
        'subtle': 'subtle',
        'attentive': 'attentive',
        'noFill': 'no-fill'
      }),

      color: figma.enum('color', {
        'neutral': 'neutral',
        'primary': 'primary',
        'success': 'success',
        'error': 'error',
        'warning': 'warning',
        'purple': 'purple'
      }),

      size: figma.enum('size', {
        'xs': 'xs',
        'sm': 'sm',
        'md': 'md',
        'lg': 'lg'
      })
    },

    example: ({ text, variant, color, size }) => (
      <Tag
        text={text}
        variant={variant}
        color={color}
        size={size}
      />
    ),

    imports: ["import { Tag } from '@juspay/blend-design-system'"],

    links: [
      {
        name: "GitHub",
        url: "https://github.com/juspay/blend-design-system/tree/main/packages/blend/lib/components/Tags"
      },
      {
        name: "Storybook",
        url: "https://juspay.design/storybook/?path=/docs/components-tags--docs"
      }
    ]
  }
);
```

## Publishing

### Validate First

Always validate before publishing:

```bash
pnpm figma:validate
```

### Publish to Figma

```bash
pnpm figma:publish
```

### What Happens

1. Validates all Code Connect files
2. Uploads to Figma
3. Shows list of published components with their Figma URLs
4. Components are now visible in Figma Dev Mode

## Troubleshooting

### Common Issues

#### Code Connect Not Showing in Figma

- Ensure you're in Dev Mode
- Refresh the Figma file
- Verify component URL is correct (not an instance)
- Check publish was successful

#### Props Not Mapping

- Property names must match exactly (case-sensitive)
- Check Figma property names in Dev Mode
- Enum values must match Figma variants

#### "No Code Connect files found"

- Check `figma.config.json` includes correct paths
- Ensure files have `.figma.tsx` extension
- Verify files are in included directories

#### Publishing Errors

- Ensure `.env` file exists with valid token
- Token needs correct scopes
- Check network connectivity

### Debugging Tips

1. **Check Figma Properties**

    ```typescript
    example: (props) => {
      console.log('Figma props:', props);
      return <Component {...props} />;
    }
    ```

2. **Validate Individual Files**
    ```bash
    pnpm figma:validate path/to/file.figma.tsx
    ```

## Best Practices

1. **File Organization**
    - Keep `.figma.tsx` files next to component stories
    - Use consistent naming: `ComponentName.figma.tsx`

2. **Prop Mapping**
    - Map all visual properties from Figma
    - Handle Figma-only properties gracefully
    - Document complex mappings with comments

3. **Maintenance**
    - Update Code Connect when components change
    - Keep Figma and code props in sync
    - Validate before every publish

4. **Documentation**
    - Add comments explaining prop differences
    - Document Figma vs code discrepancies
    - Keep this guide updated

## Migration Guide

### Repository Migration

The repository migration has been completed. All Code Connect files now use the new repository URL:

- New: `https://github.com/juspay/blend-design-system`

### Adding to Existing Components

1. Create `.figma.tsx` file next to component
2. Copy template from Quick Start section
3. Update component import and props
4. Get Figma URL for component
5. Map all props
6. Validate and publish

## Known Issues

- **Auto-generated Links**: Manual links may be overridden
- **Links Point to Wrong Paths**: Known Code Connect limitation
- Only generated code, imports, and links visible in Figma (no comments)
- **Snackbar Component**: Not supported due to being a provider component that uses `addSnackbar()` function instead of direct component usage. Causes "Error loading Code Connect" in Figma.

## Resources

- [Figma Code Connect Docs](https://www.figma.com/developers/code-connect)
- [Example: Button.figma.tsx](apps/storybook/stories/components/Button/Button.figma.tsx)