Skip to content

Commit 33aaafc

Browse files
committed
Support responsive components
1 parent d2f9ead commit 33aaafc

7 files changed

Lines changed: 617 additions & 53 deletions

File tree

src/code-impl.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ResponsiveCodegen } from './codegen/responsive/ResponsiveCodegen'
33
import { exportDevup, importDevup } from './commands/devup'
44
import { exportAssets } from './commands/exportAssets'
55
import { exportComponents } from './commands/exportComponents'
6+
import { getComponentName } from './utils'
67

78
export function extractImports(
89
componentsCodes: ReadonlyArray<readonly [string, string]>,
@@ -87,6 +88,27 @@ export function registerCodegen(ctx: typeof figma) {
8788
const codegen = new Codegen(node)
8889
await codegen.run()
8990
const componentsCodes = codegen.getComponentsCodes()
91+
92+
// Generate responsive component codes if viewport variant exists
93+
let responsiveComponentsCodes: ReadonlyArray<
94+
readonly [string, string]
95+
> = []
96+
if (codegen.hasViewportVariant() && node.type === 'COMPONENT_SET') {
97+
try {
98+
const componentName = getComponentName(node)
99+
responsiveComponentsCodes =
100+
await ResponsiveCodegen.generateViewportResponsiveComponents(
101+
node,
102+
componentName,
103+
)
104+
} catch (e) {
105+
console.error(
106+
'[responsive] Error generating responsive component code:',
107+
e,
108+
)
109+
}
110+
}
111+
90112
console.info(`[benchmark] devup-ui end ${Date.now() - time}ms`)
91113

92114
const parentSection = ResponsiveCodegen.hasParentSection(node)
@@ -144,6 +166,17 @@ export function registerCodegen(ctx: typeof figma) {
144166
},
145167
] as const)
146168
: []),
169+
...(responsiveComponentsCodes.length > 0
170+
? [
171+
{
172+
title: `${node.name} - Components Responsive`,
173+
language: 'TYPESCRIPT' as const,
174+
code: responsiveComponentsCodes
175+
.map((code) => code[1])
176+
.join('\n\n'),
177+
},
178+
]
179+
: []),
147180
...responsiveResult,
148181
]
149182
}

src/codegen/Codegen.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ export class Codegen {
2525
private componentTrees: Map<SceneNode, ComponentTree> = new Map()
2626

2727
constructor(private node: SceneNode) {
28-
if (node.type === 'COMPONENT' && node.parent?.type === 'COMPONENT_SET') {
29-
this.node = node.parent
30-
} else {
31-
this.node = node
32-
}
28+
this.node = node
29+
// if (node.type === 'COMPONENT' && node.parent?.type === 'COMPONENT_SET') {
30+
// this.node = node.parent
31+
// } else {
32+
// this.node = node
33+
// }
34+
// if (node.type === 'COMPONENT' && node.parent?.type === 'COMPONENT_SET') {
35+
// this.node = node.parent
36+
// } else {
37+
// this.node = node
38+
// }
3339
}
3440

3541
getCode() {
@@ -293,6 +299,7 @@ export class Codegen {
293299
*/
294300
async getTree(): Promise<NodeTree> {
295301
if (!this.tree) {
302+
console.log('buildTree', this.node)
296303
this.tree = await this.buildTree(this.node)
297304
}
298305
return this.tree
@@ -344,6 +351,16 @@ export class Codegen {
344351
})
345352
}
346353

354+
/**
355+
* Check if the node is a COMPONENT_SET with viewport variant.
356+
*/
357+
hasViewportVariant(): boolean {
358+
if (this.node.type !== 'COMPONENT_SET') return false
359+
return Object.keys(
360+
(this.node as ComponentSetNode).componentPropertyDefinitions,
361+
).some((key) => key.toLowerCase() === 'viewport')
362+
}
363+
347364
/**
348365
* Render a NodeTree to JSX string.
349366
* Static method so it can be used independently.

0 commit comments

Comments
 (0)