Skip to content

Commit 659795a

Browse files
author
figma-bot
committed
Code Connect v1.2.0
1 parent 233ec33 commit 659795a

File tree

56 files changed

+2017
-805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2017
-805
lines changed

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# Code Connect v1.2.0
2+
3+
## Features
4+
5+
### General
6+
- The interactive setup now offers AI support for accurate prop mapping between Figma and code components. Users will now be given the option to use AI during the setup process, which if chosen will assist in creating Code Connect files and attempting to accurately map your code to Figma properties.
7+
8+
Data is used only for mapping and is not stored or used for training. To learn more, visit https://help.figma.com/hc/en-us/articles/23920389749655-Code-Connect
9+
10+
### React
11+
- Added support for returning strings or React components from the `example` function, in addition to JSX
12+
- Added `getProps` on `figma.instance()` which can be used to access props of a nested connected component
13+
- Added `render` on `figma.instance()` which can be used to render a nested connected component dynamically
14+
- Added support for including any custom props in the `props` object, that can be accessed with `getProps` in a parent component
15+
16+
## Fixed
17+
18+
### HTML
19+
- Case of attribute names is now preserved to support Angular (fixes https://github.com/figma/code-connect/issues/172)
20+
- Fixed a bug with `nestedProps` (fixes https://github.com/figma/code-connect/issues/176)
21+
122
# Code Connect v1.1.4 (26th September 2024)
223

324
## Fixed

cli/package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@figma/code-connect",
3-
"version": "1.1.4",
3+
"version": "1.2.0",
44
"description": "A tool for connecting your design system components in code with your design system in Figma",
55
"keywords": [],
66
"author": "Figma",
@@ -38,7 +38,7 @@
3838
"dev": "tsx src/cli.ts",
3939
"build": "rm -rf dist && npm run typecheck && tsc",
4040
"build:web": "pnpm build",
41-
"build:webpack": "webpack --mode production",
41+
"build:webpack": "cross-env NODE_OPTIONS=\"--max-old-space-size=4096\" webpack --mode production",
4242
"test": "npm run test:no-coverage -- --coverage",
4343
"test:no-coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest",
4444
"test:fast": "npm run test -- --testPathIgnorePatterns=template_rendering.test.ts --testPathIgnorePatterns=e2e_parse_command_swift.test.ts --testPathIgnorePatterns=e2e_wizard_swift.test.ts",
@@ -62,7 +62,7 @@
6262
},
6363
"devDependencies": {
6464
"@types/cross-spawn": "^6.0.6",
65-
"@types/jest": "^29.5.5",
65+
"@types/jest": "^29.5.13",
6666
"@types/jsdom": "^21.1.7",
6767
"@types/lodash": "^4.17.0",
6868
"@types/node": "^20.14.0",
@@ -73,17 +73,17 @@
7373
"jest": "^29.7.0",
7474
"pkg": "^5.8.1",
7575
"react": "18.2.0",
76-
"ts-jest": "^29.1.1",
76+
"ts-jest": "^29.2.5",
7777
"ts-loader": "^9.5.1",
7878
"tsx": "^4.11.0",
7979
"webpack": "^5.91.0",
8080
"webpack-cli": "^5.1.4"
8181
},
8282
"dependencies": {
83-
"@babel/core": "^7.24.7",
84-
"@babel/generator": "^7.24.7",
85-
"@babel/parser": "^7.24.7",
86-
"@babel/types": "^7.24.7",
83+
"@babel/core": "^7.25.2",
84+
"@babel/generator": "^7.25.2",
85+
"@babel/parser": "^7.25.2",
86+
"@babel/types": "^7.25.2",
8787

8888
"@storybook/csf-tools": "^7.6.7",
8989
"axios": "^1.7.4",
@@ -104,6 +104,7 @@
104104
"prettier": "^2.8.8",
105105
"prompts": "^2.4.2",
106106
"strip-ansi": "^6.0.0",
107+
"ts-morph": "^23.0.0",
107108
"typescript": "5.4.2",
108109
"zod": "^3.23.6",
109110
"zod-validation-error": "^3.2.0"

cli/scripts/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Code Connect for icons
22

3-
This folder includes example scripts for generating Code Connect files for your components. This is useful for icons, where you might have tons of icons that you don't want to manually connect one by one.
3+
This folder includes example scripts for generating Code Connect files for your components. This is the recommended way of connecting icons, where you might have tons of icons that you don't want to manually connect one by one.
44

55
## Usage
66

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// change to: "import { client } from '@figma/code-connect'"
2+
import { client } from '../src/react/index_react'
3+
import fs from 'fs'
4+
5+
async function generateIcons() {
6+
// fetch components from a figma file. If the `node-id` query parameter is used,
7+
// only components within those frames will be included. This is useful if your
8+
// file is very large, as this will speed up the query by a lot
9+
let components = await client.getComponents(
10+
'https://figma.com/file/ABc123IjkLmnOPq?node-id=41-41',
11+
)
12+
13+
// Finds all components starting with 'icon' (this assumes icons are named e.g: 'icon-list')
14+
components = components.filter(({ name }) => {
15+
return name.includes('icon')
16+
})
17+
18+
// map each icon to a figma.connect call that looks like this:
19+
// figma.connect('https://figma.com/file/ABc123IjkLmnOPq?node-id=41-41', {
20+
// example: () => "icon-list"
21+
// })
22+
fs.writeFileSync(
23+
'icons.figma.tsx',
24+
`\
25+
import figma from '@figma/code-connect'
26+
27+
${components
28+
.map(
29+
(c) => `figma.connect('${c.figmaUrl}', {
30+
example: () => "${c.name}"
31+
})`,
32+
)
33+
.join('\n')}
34+
`,
35+
)
36+
}
37+
38+
generateIcons()

cli/src/connect/__test__/e2e/e2e_parse_command/swift_package/Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/connect/__test__/e2e/e2e_parse_command/swift_parser/swift_parser.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/connect/__test__/e2e/e2e_parse_command/swift_wizard/swift_wizard.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/connect/__test__/e2e/e2e_parse_command_html.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ ${path.join(testPath, 'test-component.figma.ts')}${maybeLabelMessage}`,
4040
},
4141
])
4242

43-
expect(json[0].template.startsWith('function _fcc_renderHtmlValue')).toBe(true)
43+
expect(json[0].template.startsWith('function _fcc_templateString')).toBe(true)
4444
// We don't care about checking the contents of the function as this can change
4545
expect(
4646
json[0].template.endsWith('export default figma.html`<my-component></my-component>`\n'),

cli/src/connect/api.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,47 @@ export type EnumValue =
88
| Function
99
| Object
1010

11+
/**
12+
* These types are intended to be returned by figma helper functions for exposing the
13+
* supported output modifiers for that type. There's no implementation for these types,
14+
* they are resolved to primitive types when the `props` object is passed to `example`.
15+
*/
16+
export interface ConnectedComponent {
17+
/**
18+
* Returns the resolved props of the connected component. This is useful for accessing
19+
* the `props` object of a child in a parent context. For example:
20+
* ```ts
21+
* figma.connect("parent", {
22+
* props: {
23+
* iconProps: figma.instance("Icon").getProps(),
24+
* },
25+
* example: (iconProps) => <IconButton iconId={iconProps.iconId} />,
26+
* }
27+
*/
28+
getProps<T = any>(): T
29+
/**
30+
* Renders the instance with the provided render function. The function is passed the resolved
31+
* `props` of the nested connected component. This is useful for dynamically rendering a child
32+
* component depending on parent context. For example:
33+
* ```ts
34+
* figma.connect("parent", {
35+
* props: {
36+
* icon: figma.instance("Icon").render(({ iconId }) => <Button.Icon iconId={iconId} />),
37+
* },
38+
* example: ({ icon }) => <Button icon={icon}/>,
39+
* }
40+
*/
41+
render<T = unknown>(renderFunction: (props: T) => JSX.Element): JSX.Element
42+
}
43+
1144
// This contains the base API interface for figma.connect calls across React and
1245
// HTML. Any parts which are platform-specific (either the function signature or
1346
// its docblock) are specified in the individual index_<platform> and added to
1447
// this using union typing.
1548
//
1649
// To override a docblock, all signatures of that function must be moved into
1750
// the platform-specific index file.
18-
export interface FigmaConnectAPI<InstanceChildrenT> {
51+
export interface FigmaConnectAPI<InstanceChildrenT, ChildrenT> {
1952
/**
2053
* Maps a Figma Variant property to a set if values for the connected component. This prop is replaced
2154
* with values from the Figma instance when viewed in Dev Mode. For example:
@@ -60,7 +93,7 @@ export interface FigmaConnectAPI<InstanceChildrenT> {
6093
*
6194
* @param figmaPropName The name of the property on the Figma component
6295
*/
63-
instance(figmaPropName: string): InstanceChildrenT
96+
instance<T = InstanceChildrenT>(figmaPropName: string): T
6497

6598
/**
6699
* Maps a Figma instance layer to a nested code example. For example:
@@ -82,7 +115,7 @@ export interface FigmaConnectAPI<InstanceChildrenT> {
82115
*
83116
* @param figmaPropName The name of the property on the Figma component
84117
*/
85-
children(layerNames: string | string[]): InstanceChildrenT
118+
children(layerNames: string | string[]): ChildrenT
86119

87120
/**
88121
* Creates a className string by joining an array of strings. The argument supports both
@@ -130,7 +163,12 @@ export interface FigmaConnectLink {
130163
// ExampleFnReturnT is the return type of an example function.
131164
// ExtraExampleT allows us to pass in an extra type to the `example` property,
132165
// so that in Storybook, we can use strings to refer to non-hoisted functions
133-
export interface FigmaConnectMeta<T = {}, ExampleFnReturnT = unknown, ExtraExampleT = never> {
166+
export interface FigmaConnectMeta<
167+
PropsT = {},
168+
ResolvedPropsT = {},
169+
ExampleFnReturnT = unknown,
170+
ExtraExampleT = never,
171+
> {
134172
/**
135173
* Restricts this figma connect to any variants that fullfill the given filter.
136174
* The filter is a map of Figma variant names to values. Example:
@@ -155,15 +193,15 @@ export interface FigmaConnectMeta<T = {}, ExampleFnReturnT = unknown, ExtraExamp
155193
* }),
156194
* }
157195
*/
158-
props?: T
196+
props?: PropsT
159197

160198
/**
161199
* The code example to display in Figma. Any mapped `props` are passed to the component,
162200
* where those values will be replaced with the mapped value when inspecting that instance in Figma.
163201
* @param props
164202
* @returns
165203
*/
166-
example?: ((props: T) => ExampleFnReturnT) | ExtraExampleT
204+
example?: ((props: ResolvedPropsT) => ExampleFnReturnT) | ExtraExampleT
167205

168206
/**
169207
* A list of links that will display in Figma along with the examples

cli/src/connect/external_types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PropMapping, ValueOf, EnumValue } from './api'
1+
import { PropMapping, ValueOf, EnumValue, ConnectedComponent } from './api'
22

33
export function booleanType(_figmaPropName: string): boolean
44
export function booleanType<V extends EnumValue>(
@@ -33,3 +33,7 @@ export function classNameType(_className: (string | undefined)[]) {
3333
export function textContentType(_layer: string) {
3434
return ''
3535
}
36+
37+
export function instanceType<T = ConnectedComponent>(_figmaPropName: string): T {
38+
return undefined as T
39+
}

0 commit comments

Comments
 (0)