Skip to content

Commit 2fdd2f6

Browse files
committed
Fix assets w
1 parent 1c7f141 commit 2fdd2f6

File tree

7 files changed

+85
-5
lines changed

7 files changed

+85
-5
lines changed

src/codegen/Codegen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class Codegen {
8282
const assetNode = checkAssetNode(node)
8383
if (assetNode) {
8484
const props = await getProps(node)
85+
console.log('props', props)
8586
props.src = `/${assetNode === 'svg' ? 'icons' : 'images'}/${node.name}.${assetNode}`
8687
if (assetNode === 'svg') {
8788
const maskColor = await checkSameColor(node)

src/codegen/props/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getVisibilityProps } from './visibility'
2222
export async function getProps(
2323
node: SceneNode,
2424
): Promise<Record<string, unknown>> {
25+
console.log('getProps', getLayoutProps(node))
2526
return {
2627
...getAutoLayoutProps(node),
2728
...getMinMaxProps(node),
@@ -56,8 +57,9 @@ export function filterPropsWithComponent(
5657
for (const [key, value] of Object.entries(props)) {
5758
switch (component) {
5859
case 'Flex':
59-
// Only skip display if it's exactly 'flex' (not responsive array or other value)
60+
// Only skip display/flexDir if it's exactly the default value (not responsive array)
6061
if (key === 'display' && value === 'flex') continue
62+
if (key === 'flexDir' && value === 'row') continue
6163
break
6264
case 'Grid':
6365
// Only skip display if it's exactly 'grid' (not responsive array or other value)
@@ -66,9 +68,11 @@ export function filterPropsWithComponent(
6668
case 'Center':
6769
if (['alignItems', 'justifyContent'].includes(key)) continue
6870
if (key === 'display' && value === 'flex') continue
71+
if (key === 'flexDir' && value === 'row') continue
6972
break
7073
case 'VStack':
71-
if (key === 'flexDir') continue
74+
// Only skip flexDir if it's exactly 'column' (not responsive array or other value)
75+
if (key === 'flexDir' && value === 'column') continue
7276
if (key === 'display' && value === 'flex') continue
7377
break
7478

src/codegen/props/layout.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { addPx } from '../utils/add-px'
2+
import { checkAssetNode } from '../utils/check-asset-node'
23
import { getPageNode } from '../utils/get-page-node'
34
import { isChildWidthShrinker } from '../utils/is-child-width-shrinker'
45
import { canBeAbsolute } from './position'
@@ -52,7 +53,9 @@ function _getLayoutProps(
5253
(node.parent &&
5354
'width' in node.parent &&
5455
node.parent.width > node.width)
55-
? undefined
56+
? checkAssetNode(node)
57+
? addPx(node.width)
58+
: undefined
5659
: '100%',
5760
// if node does not have children, it is a single node, so it should be 100%
5861
h:

src/codegen/props/position.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ export function canBeAbsolute(node: SceneNode): boolean {
1717
(isFreelayout(node.parent) &&
1818
'width' in node.parent &&
1919
'height' in node.parent &&
20-
!isPageRoot(node.parent as SceneNode)))
20+
node.width !== node.parent.width &&
21+
node.height !== node.parent.height &&
22+
!isPageRoot(node as SceneNode)))
2123
)
2224
}
2325

src/codegen/responsive/__tests__/mergePropsToResponsive.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,59 @@ describe('mergePropsToResponsive', () => {
170170
display: ['none', null, 'flex'],
171171
},
172172
},
173+
{
174+
name: 'flexDir column at mobile, row at tablet and pc should produce responsive array',
175+
input: new Map<BreakpointKey, Record<string, unknown>>([
176+
['mobile', { flexDir: 'column' }],
177+
['tablet', { flexDir: 'row' }],
178+
['pc', { flexDir: 'row' }],
179+
]),
180+
expected: {
181+
flexDir: ['column', null, 'row'],
182+
},
183+
},
184+
{
185+
name: 'alignItems with default value at first should become null',
186+
input: new Map<BreakpointKey, Record<string, unknown>>([
187+
['mobile', { alignItems: 'flex-start' }],
188+
['tablet', { alignItems: 'center' }],
189+
['pc', { alignItems: 'center' }],
190+
]),
191+
expected: {
192+
alignItems: [null, null, 'center'],
193+
},
194+
},
195+
{
196+
name: 'justifyContent with default value at first should become null',
197+
input: new Map<BreakpointKey, Record<string, unknown>>([
198+
['mobile', { justifyContent: 'flex-start' }],
199+
['tablet', { justifyContent: 'center' }],
200+
['pc', { justifyContent: 'center' }],
201+
]),
202+
expected: {
203+
justifyContent: [null, null, 'center'],
204+
},
205+
},
206+
{
207+
name: 'flexDir with default value (row) at first should become null',
208+
input: new Map<BreakpointKey, Record<string, unknown>>([
209+
['mobile', { flexDir: 'row' }],
210+
['tablet', { flexDir: 'column' }],
211+
['pc', { flexDir: 'column' }],
212+
]),
213+
expected: {
214+
flexDir: [null, null, 'column'],
215+
},
216+
},
217+
{
218+
name: 'all default values should be omitted (empty result)',
219+
input: new Map<BreakpointKey, Record<string, unknown>>([
220+
['mobile', { alignItems: 'flex-start', justifyContent: 'flex-start' }],
221+
['tablet', { alignItems: 'flex-start', justifyContent: 'flex-start' }],
222+
['pc', { alignItems: 'flex-start', justifyContent: 'flex-start' }],
223+
]),
224+
expected: {},
225+
},
173226
]
174227

175228
cases.forEach(({ name, input, expected }) => {

src/codegen/responsive/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDefaultProp } from '../utils/is-default-prop'
2+
13
// Breakpoint thresholds (by width)
24
// Array indices: mobile=0, sm=1, tablet=2, lg=3, pc=4
35
// Always 5 slots
@@ -93,6 +95,7 @@ function isEqual(a: PropValue, b: PropValue): boolean {
9395
* 1. If only index 0 has a value and the rest are null, return single value.
9496
* 2. Consecutive identical values keep the first, later ones become null.
9597
* 3. Remove trailing nulls only.
98+
* 4. If the first value is default for that prop, replace with null.
9699
*
97100
* Examples:
98101
* ["100px", null, null] -> "100px" (only first has value)
@@ -101,9 +104,11 @@ function isEqual(a: PropValue, b: PropValue): boolean {
101104
* [null, null, "none"] -> [null, null, "none"] (keeps leading nulls)
102105
* [null, null, "none", null, null] -> [null, null, "none"] (trim trailing null)
103106
* ["100px", "200px", "200px"] -> ["100px", "200px"] (trailing equal treated as trailing null)
107+
* ["flex-start", null, "center"] -> [null, null, "center"] (first value is default for alignItems)
104108
*/
105109
export function optimizeResponsiveValue(
106110
arr: (PropValue | null)[],
111+
key?: string,
107112
): PropValue | (PropValue | null)[] {
108113
const nonNullValues = arr.filter((v) => v !== null)
109114
if (nonNullValues.length === 0) return null
@@ -123,11 +128,21 @@ export function optimizeResponsiveValue(
123128
}
124129
}
125130

131+
// If the first value is default for that prop, replace with null.
132+
if (key && optimized[0] !== null && isDefaultProp(key, optimized[0])) {
133+
optimized[0] = null
134+
}
135+
126136
// Remove trailing nulls.
127137
while (optimized.length > 0 && optimized[optimized.length - 1] === null) {
128138
optimized.pop()
129139
}
130140

141+
// If empty array after optimization, return null.
142+
if (optimized.length === 0) {
143+
return null
144+
}
145+
131146
// If only index 0 has value, return single value.
132147
if (optimized.length === 1 && optimized[0] !== null) {
133148
return optimized[0]
@@ -209,7 +224,7 @@ export function mergePropsToResponsive(
209224
}
210225

211226
// Optimize: single when all same, otherwise array.
212-
const optimized = optimizeResponsiveValue(valuesToOptimize)
227+
const optimized = optimizeResponsiveValue(valuesToOptimize, key)
213228

214229
if (optimized !== null) {
215230
result[key] = optimized

src/codegen/utils/is-default-prop.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const DEFAULT_PROPS_MAP = {
2424
gap: /\b0(px)?\b/,
2525
} as const
2626
export function isDefaultProp(prop: string, value: unknown) {
27+
// Don't filter arrays (responsive values)
28+
if (Array.isArray(value)) return false
2729
return (
2830
prop in DEFAULT_PROPS_MAP &&
2931
DEFAULT_PROPS_MAP[prop as keyof typeof DEFAULT_PROPS_MAP].test(

0 commit comments

Comments
 (0)