Skip to content

Commit c3ebb83

Browse files
authored
fix: some styles parsing after performance improvements (#80)
1 parent c6a8c73 commit c3ebb83

4 files changed

Lines changed: 73 additions & 34 deletions

File tree

packages/uniwind/specs/spacing.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ describe('Converts tailwind spacings', () => {
2525
const { UniwindStore } = await import('../src/core/native')
2626
const styles = UniwindStore.getStyles(className).styles
2727

28-
expect(styles).toHaveProperty('paddingHorizontalStart', 16)
29-
expect(styles).toHaveProperty('paddingHorizontalEnd', 16)
28+
expect(styles).toHaveProperty('paddingLeft', 16)
29+
expect(styles).toHaveProperty('paddingRight', 16)
3030
expect(styles).toHaveProperty('marginTop', 8)
3131
expect(styles).toHaveProperty('marginBottom', 8)
3232
expect(styles).toHaveProperty('marginLeft', 8)

packages/uniwind/src/core/native/parsers/transforms.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ export const parseTransformsMutation = (styles: Record<string, any>) => {
4747

4848
if (transformsResult.length > 0) {
4949
Object.defineProperty(styles, 'transform', {
50+
configurable: true,
51+
enumerable: true,
5052
value: transformsResult,
5153
})
5254
}

packages/uniwind/src/core/native/store.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,24 @@ export class UniwindStoreBuilder {
153153
if (result.lineHeight !== undefined && result.lineHeight < 6) {
154154
Object.defineProperty(result, 'lineHeight', {
155155
value: result.fontSize * result.lineHeight,
156+
configurable: true,
157+
enumerable: true,
156158
})
157159
}
158160

159161
if (result.boxShadow !== undefined) {
160162
Object.defineProperty(result, 'boxShadow', {
161163
value: parseBoxShadow(result.boxShadow),
164+
configurable: true,
165+
enumerable: true,
162166
})
163167
}
164168

165-
if (result.visibility !== undefined && result.visibility === 'hidden') {
166-
Object.defineProperty(result, 'visibility', {
167-
value: 'hidden',
169+
if (result.visibility === 'hidden') {
170+
Object.defineProperty(result, 'display', {
171+
value: 'none',
172+
configurable: true,
173+
enumerable: true,
168174
})
169175
}
170176

@@ -173,12 +179,16 @@ export class UniwindStoreBuilder {
173179
) {
174180
Object.defineProperty(result, 'borderColor', {
175181
value: '#000000',
182+
configurable: true,
183+
enumerable: true,
176184
})
177185
}
178186

179187
if (result.fontVariant !== undefined) {
180188
Object.defineProperty(result, 'fontVariant', {
181189
value: parseFontVariant(result.fontVariant),
190+
configurable: true,
191+
enumerable: true,
182192
})
183193
}
184194

@@ -187,6 +197,8 @@ export class UniwindStoreBuilder {
187197
if (result.experimental_backgroundImage !== undefined) {
188198
Object.defineProperty(result, 'experimental_backgroundImage', {
189199
value: resolveGradient(result.experimental_backgroundImage),
200+
configurable: true,
201+
enumerable: true,
190202
})
191203
}
192204

packages/uniwind/src/metro/processor/rn.ts

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -208,48 +208,73 @@ export class RN {
208208
}
209209

210210
if (typeof value === 'object') {
211-
const properties = Object.keys(value)
212-
// border properties are border{X}Color instead of borderColor{X}
213-
const propertyEnd = property.includes('border')
214-
? property.split('border').at(-1) ?? ''
215-
: ''
216-
const transformedProperty = property.replace(propertyEnd, '')
217-
218-
if (properties.every(property => ['row', 'column'].includes(property))) {
219-
return {
220-
rowGap: value.row,
221-
columnGap: value.column,
222-
}
211+
const transformed = this.transformObjectProperty(property, value)
212+
213+
if (transformed) {
214+
return transformed
223215
}
216+
}
224217

225-
if (properties.every(property => ['start', 'end'].includes(property))) {
226-
return {
227-
[`${transformedProperty}Start${propertyEnd}`]: value.start,
228-
[`${transformedProperty}End${propertyEnd}`]: value.end,
229-
}
218+
return {
219+
[property]: value,
220+
}
221+
}
222+
223+
private transformObjectProperty(property: string, value: Record<string, any>) {
224+
const properties = Object.keys(value)
225+
// border properties are border{X}Color instead of borderColor{X}
226+
const propertyEnd = property.includes('border')
227+
? property.split('border').at(-1) ?? ''
228+
: ''
229+
const transformedProperty = property.replace(propertyEnd, '')
230+
const isSpacing = property.includes('margin') || property.includes('padding')
231+
232+
const wrapProperty = (prop: string) => `${transformedProperty}${prop}${propertyEnd}`
233+
234+
if (properties.every(property => ['row', 'column'].includes(property))) {
235+
return {
236+
rowGap: value.row,
237+
columnGap: value.column,
230238
}
239+
}
231240

232-
if (properties.every(property => ['top', 'right', 'bottom', 'left'].includes(property))) {
241+
if (properties.every(property => ['start', 'end'].includes(property))) {
242+
if (isSpacing && property.includes('Horizontal')) {
233243
return {
234-
[`${transformedProperty}Top${propertyEnd}`]: value.top,
235-
[`${transformedProperty}Right${propertyEnd}`]: value.right,
236-
[`${transformedProperty}Bottom${propertyEnd}`]: value.bottom,
237-
[`${transformedProperty}Left${propertyEnd}`]: value.left,
244+
[`${property.replace('Horizontal', 'Left')}`]: value.start,
245+
[`${property.replace('Horizontal', 'Right')}`]: value.end,
238246
}
239247
}
240248

241-
if (properties.every(property => ['topLeft', 'topRight', 'bottomRight', 'bottomLeft'].includes(property))) {
249+
if (isSpacing && property.includes('Vertical')) {
242250
return {
243-
[`${transformedProperty}TopLeft${propertyEnd}`]: value.topLeft,
244-
[`${transformedProperty}TopRight${propertyEnd}`]: value.topRight,
245-
[`${transformedProperty}BottomRight${propertyEnd}`]: value.bottomRight,
246-
[`${transformedProperty}BottomLeft${propertyEnd}`]: value.bottomLeft,
251+
[`${property.replace('Vertical', 'Top')}`]: value.start,
252+
[`${property.replace('Vertical', 'Bottom')}`]: value.end,
247253
}
248254
}
255+
256+
return {
257+
[wrapProperty('Start')]: value.start,
258+
[wrapProperty('End')]: value.end,
259+
}
249260
}
250261

251-
return {
252-
[property]: value,
262+
if (properties.every(property => ['top', 'right', 'bottom', 'left'].includes(property))) {
263+
return {
264+
[wrapProperty('Top')]: value.top,
265+
[wrapProperty('Right')]: value.right,
266+
[wrapProperty('Bottom')]: value.bottom,
267+
[wrapProperty('Left')]: value.left,
268+
}
269+
}
270+
271+
if (properties.every(property => ['topLeft', 'topRight', 'bottomRight', 'bottomLeft'].includes(property))) {
272+
return {
273+
[wrapProperty('TopLeft')]: value.topLeft,
274+
[wrapProperty('TopRight')]: value.topRight,
275+
[wrapProperty('BottomRight')]: value.bottomRight,
276+
[wrapProperty('BottomLeft')]: value.bottomLeft,
277+
}
253278
}
254279
}
255280
}

0 commit comments

Comments
 (0)