Skip to content

Commit

Permalink
Better setYogaProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
giulioz committed Sep 3, 2020
1 parent e92c3f5 commit 745c66c
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const jsxPropToYogaProp = (s: string) => s.toUpperCase().replace('-', '_'
export const setYogaProperties = (node: YogaNode, props: R3FlexProps, scaleFactor: number) => {
return Object.keys(props).forEach((name) => {
const value = props[name]
const scaledValue = value * scaleFactor

if (typeof value === 'string') {
switch (name) {
Expand Down Expand Up @@ -42,42 +43,38 @@ export const setYogaProperties = (node: YogaNode, props: R3FlexProps, scaleFacto
return node.setFlexWrap(value)
case 'padding':
case 'p':
return node.setPadding(Yoga.EDGE_ALL, value * scaleFactor)
return node.setPadding(Yoga.EDGE_ALL, scaledValue)
case 'paddingLeft':
case 'pl':
return node.setPadding(Yoga.EDGE_LEFT, value * scaleFactor)
return node.setPadding(Yoga.EDGE_LEFT, scaledValue)
case 'paddingRight':
case 'pr':
return node.setPadding(Yoga.EDGE_RIGHT, value * scaleFactor)
return node.setPadding(Yoga.EDGE_RIGHT, scaledValue)
case 'paddingTop':
case 'pt':
return node.setPadding(Yoga.EDGE_TOP, value * scaleFactor)
return node.setPadding(Yoga.EDGE_TOP, scaledValue)
case 'paddingBottom':
case 'pb':
return node.setPadding(Yoga.EDGE_BOTTOM, value * scaleFactor)
return node.setPadding(Yoga.EDGE_BOTTOM, scaledValue)

case 'margin':
case 'm':
return node.setMargin(Yoga.EDGE_ALL, value * scaleFactor)
return node.setMargin(Yoga.EDGE_ALL, scaledValue)
case 'marginLeft':
case 'ml':
return node.setMargin(Yoga.EDGE_LEFT, value * scaleFactor)
return node.setMargin(Yoga.EDGE_LEFT, scaledValue)
case 'marginRight':
case 'mr':
return node.setMargin(Yoga.EDGE_RIGHT, value * scaleFactor)
return node.setMargin(Yoga.EDGE_RIGHT, scaledValue)
case 'marginTop':
case 'mt':
return node.setMargin(Yoga.EDGE_TOP, value * scaleFactor)
return node.setMargin(Yoga.EDGE_TOP, scaledValue)
case 'marginBottom':
case 'mb':
return node.setMargin(Yoga.EDGE_BOTTOM, value * scaleFactor)
return node.setMargin(Yoga.EDGE_BOTTOM, scaledValue)

default:
if (typeof value === 'number') {
return node[`set${capitalize(name)}`](value * scaleFactor)
} else {
return node[`set${capitalize(name)}`](value)
}
return node[`set${capitalize(name)}`](typeof value === 'number' ? scaledValue : value)
}
}
})
Expand Down

0 comments on commit 745c66c

Please sign in to comment.