Skip to content

Commit 152afcf

Browse files
committed
Fix single child padding issue
1 parent ff7ec9b commit 152afcf

File tree

11 files changed

+138
-220
lines changed

11 files changed

+138
-220
lines changed

src/codegen/__tests__/codegen.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,25 @@ describe('Codegen', () => {
11801180
w="110px"
11811181
/>`,
11821182
},
1183+
{
1184+
title: 'renders padding from inferredAutoLayout',
1185+
node: {
1186+
type: 'FRAME',
1187+
name: 'InferredPaddingFrame',
1188+
children: [],
1189+
layoutSizingHorizontal: 'FIXED',
1190+
layoutSizingVertical: 'FIXED',
1191+
width: 100,
1192+
height: 50,
1193+
inferredAutoLayout: {
1194+
paddingTop: 10,
1195+
paddingRight: 20,
1196+
paddingBottom: 10,
1197+
paddingLeft: 20,
1198+
},
1199+
} as unknown as FrameNode,
1200+
expected: `<Box h="50px" px="20px" py="10px" w="100px" />`,
1201+
},
11831202
{
11841203
title: 'renders frame with border radius',
11851204
node: {

src/codegen/props/background.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BLEND_MODE_MAP } from '../utils/blend-mode-map'
12
import { paintToCSS } from '../utils/paint-to-css'
23

34
export async function getBackgroundProps(
@@ -37,27 +38,7 @@ export async function getBackgroundProps(
3738
const combinedBg = cssFills.join(', ')
3839
return {
3940
bg: node.type !== 'TEXT' || gradientText ? combinedBg : null,
40-
bgBlendMode: {
41-
NORMAL: null,
42-
MULTIPLY: 'multiply',
43-
SCREEN: 'screen',
44-
OVERLAY: 'overlay',
45-
DARKEN: 'darken',
46-
LINEAR_BURN: 'linear-burn',
47-
COLOR_BURN: 'colorBurn',
48-
LIGHTEN: 'lighten',
49-
LINEAR_DODGE: 'linear-dodge',
50-
COLOR_DODGE: 'color-dodge',
51-
SOFT_LIGHT: 'soft-light',
52-
HARD_LIGHT: 'hard-light',
53-
DIFFERENCE: 'difference',
54-
EXCLUSION: 'exclusion',
55-
HUE: 'hue',
56-
SATURATION: 'saturation',
57-
COLOR: 'color',
58-
LUMINOSITY: 'luminosity',
59-
PASS_THROUGH: null,
60-
}[backgroundBlend],
41+
bgBlendMode: BLEND_MODE_MAP[backgroundBlend],
6142
color: gradientText ? 'transparent' : undefined,
6243
bgClip: gradientText ? 'text' : undefined,
6344
}

src/codegen/props/blend.ts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { BLEND_MODE_MAP } from '../utils/blend-mode-map'
12
import { fmtPct } from '../utils/fmtPct'
23

34
export function getBlendProps(
@@ -6,28 +7,7 @@ export function getBlendProps(
67
if ('opacity' in node) {
78
return {
89
opacity: node.opacity < 1 ? fmtPct(node.opacity) : undefined,
9-
mixBlendMode: {
10-
// same as multiply
11-
PASS_THROUGH: null,
12-
NORMAL: null,
13-
DARKEN: 'darken',
14-
MULTIPLY: 'multiply',
15-
LINEAR_BURN: 'linearBurn',
16-
COLOR_BURN: 'colorBurn',
17-
LIGHTEN: 'lighten',
18-
SCREEN: 'screen',
19-
LINEAR_DODGE: 'linear-dodge',
20-
COLOR_DODGE: 'color-dodge',
21-
OVERLAY: 'overlay',
22-
SOFT_LIGHT: 'soft-light',
23-
HARD_LIGHT: 'hard-light',
24-
DIFFERENCE: 'difference',
25-
EXCLUSION: 'exclusion',
26-
HUE: 'hue',
27-
SATURATION: 'saturation',
28-
COLOR: 'color',
29-
LUMINOSITY: 'luminosity',
30-
}[node.blendMode],
10+
mixBlendMode: BLEND_MODE_MAP[node.blendMode],
3111
}
3212
}
3313
}

src/codegen/props/layout.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ function _getLayoutProps(
9191
? 1
9292
: undefined,
9393
w:
94-
rootNode === node &&
95-
node.width ===
96-
(getPageNode(node as BaseNode & ChildrenMixin) as SceneNode)?.width
94+
rootNode === node
9795
? undefined
9896
: wType === 'FIXED'
9997
? addPx(node.width)

src/codegen/props/padding.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ import { optimizeSpace } from '../utils/optimize-space'
33
export function getPaddingProps(
44
node: SceneNode,
55
): Record<string, boolean | string | number | undefined | null> | undefined {
6+
if (
7+
'inferredAutoLayout' in node &&
8+
node.inferredAutoLayout &&
9+
'paddingLeft' in node.inferredAutoLayout
10+
) {
11+
return optimizeSpace(
12+
'p',
13+
node.inferredAutoLayout.paddingTop,
14+
node.inferredAutoLayout.paddingRight,
15+
node.inferredAutoLayout.paddingBottom,
16+
node.inferredAutoLayout.paddingLeft,
17+
)
18+
}
619
if ('paddingLeft' in node) {
720
return optimizeSpace(
821
'p',

src/codegen/utils/add-px.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { formatNumber } from './format-number'
2+
13
export function addPx(value: unknown, fallback: string): string
24
export function addPx(value: unknown, fallback?: string): string | undefined
35
export function addPx(
@@ -6,12 +8,7 @@ export function addPx(
68
) {
79
if (typeof value !== 'number') return fallback
810

9-
// Round to 2 decimal places (same as fmtPct)
10-
const rounded = Math.round(value * 100) / 100
11-
const fixed = rounded.toFixed(2)
12-
13-
// Remove unnecessary trailing zeros
14-
const str = fixed.replace(/\.00$/, '').replace(/(\.\d)0$/, '$1')
11+
const str = formatNumber(value)
1512

1613
if (str === '0') return fallback
1714
return `${str}px`
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Figma BlendMode to CSS blend-mode mapping
3+
*/
4+
export const BLEND_MODE_MAP: Record<string, string | null> = {
5+
PASS_THROUGH: null,
6+
NORMAL: null,
7+
DARKEN: 'darken',
8+
MULTIPLY: 'multiply',
9+
LINEAR_BURN: 'linearBurn',
10+
COLOR_BURN: 'colorBurn',
11+
LIGHTEN: 'lighten',
12+
SCREEN: 'screen',
13+
LINEAR_DODGE: 'linear-dodge',
14+
COLOR_DODGE: 'color-dodge',
15+
OVERLAY: 'overlay',
16+
SOFT_LIGHT: 'soft-light',
17+
HARD_LIGHT: 'hard-light',
18+
DIFFERENCE: 'difference',
19+
EXCLUSION: 'exclusion',
20+
HUE: 'hue',
21+
SATURATION: 'saturation',
22+
COLOR: 'color',
23+
LUMINOSITY: 'luminosity',
24+
}

src/codegen/utils/fmtPct.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
export function fmtPct(n: number) {
2-
// Round to 2 decimal places
3-
const rounded = Math.round(n * 100) / 100
4-
5-
// Format with 2 decimal places
6-
const formatted = rounded.toFixed(2)
1+
import { formatNumber } from './format-number'
72

8-
// Remove unnecessary trailing zeros
9-
// .00 -> remove entirely (156.00 -> 156)
10-
// .X0 -> remove trailing 0 (156.30 -> 156.3)
11-
return formatted.replace(/\.00$/, '').replace(/(\.\d)0$/, '$1')
3+
export function fmtPct(n: number) {
4+
return formatNumber(n)
125
}

src/codegen/utils/format-number.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Round to 2 decimal places and remove unnecessary trailing zeros
3+
* Examples:
4+
* 156.00 -> "156"
5+
* 156.30 -> "156.3"
6+
* 156.35 -> "156.35"
7+
*/
8+
export function formatNumber(n: number): string {
9+
const rounded = Math.round(n * 100) / 100
10+
const formatted = rounded.toFixed(2)
11+
return formatted.replace(/\.00$/, '').replace(/(\.\d)0$/, '$1')
12+
}

src/codegen/utils/four-value-shortcut.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export function fourValueShortcut(
1010
return addPx(first, '0')
1111
if (first === third && second === fourth)
1212
return `${addPx(first, '0')} ${addPx(second, '0')}`
13-
if (first === third && second === fourth)
14-
return `${addPx(first, '0')} ${addPx(second, '0')}`
1513
if (second === fourth)
1614
return `${addPx(first, '0')} ${addPx(second, '0')} ${addPx(third, '0')}`
1715
return `${addPx(first, '0')} ${addPx(second, '0')} ${addPx(third, '0')} ${addPx(fourth, '0')}`

0 commit comments

Comments
 (0)