Skip to content

Commit 75a2495

Browse files
authored
Merge pull request #9 from jpudysz/feture/transform
Nested objects
2 parents 429dddd + 3dc73df commit 75a2495

File tree

2 files changed

+108
-13
lines changed

2 files changed

+108
-13
lines changed

src/utils/styles.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,79 @@ describe('styles', () => {
110110
fontWeight: 'bold'
111111
})
112112
})
113+
114+
it('should correctly parse transform styles', () => {
115+
const screenSize: ScreenSize = {
116+
width: 400,
117+
height: 800
118+
}
119+
const breakpoint = 'sm'
120+
const breakpoints = {
121+
xs: 0,
122+
sm: 400,
123+
md: 800
124+
}
125+
const style = {
126+
transform: [
127+
{
128+
translateX: {
129+
sm: 120,
130+
md: 200
131+
},
132+
translateY: 200
133+
}
134+
]
135+
}
136+
137+
expect(parseStyle(style as CustomNamedStyles<typeof style, typeof breakpoints>, breakpoint, screenSize, breakpoints)).toEqual({
138+
transform: [
139+
{
140+
translateX: 120,
141+
translateY: 200
142+
}
143+
]
144+
})
145+
})
146+
147+
it('should correctly parse shadowOffset styles', () => {
148+
const screenSize: ScreenSize = {
149+
width: 400,
150+
height: 800
151+
}
152+
const breakpoint = 'sm'
153+
const breakpoints = {
154+
xs: 0,
155+
sm: 400,
156+
md: 800
157+
}
158+
const style = {
159+
shadowOffset: {
160+
width: 0,
161+
height: 4
162+
}
163+
}
164+
const styleWithBreakpoints = {
165+
shadowOffset: {
166+
width: 0,
167+
height: {
168+
sm: 10,
169+
md: 20
170+
}
171+
}
172+
}
173+
174+
expect(parseStyle(style as CustomNamedStyles<typeof style, typeof breakpoints>, breakpoint, screenSize, breakpoints)).toEqual({
175+
shadowOffset: {
176+
width: 0,
177+
height: 4
178+
}
179+
})
180+
expect(parseStyle(styleWithBreakpoints as CustomNamedStyles<typeof style, typeof breakpoints>, breakpoint, screenSize, breakpoints)).toEqual({
181+
shadowOffset: {
182+
width: 0,
183+
height: 10
184+
}
185+
})
186+
})
113187
})
114188
})

src/utils/styles.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,40 @@ export const parseStyle = <T, B extends Record<string, number>>(
6161
breakpoint: keyof B & string,
6262
screenSize: ScreenSize,
6363
breakpoints: B
64-
) => Object
65-
.fromEntries(Object
66-
.entries(style)
67-
.map(([key, value]) => {
68-
const isDynamicFunction = typeof value === 'function'
69-
const isValidStyle = typeof value !== 'object' || key === 'transform'
64+
): T => {
65+
const entries = Object.entries(style) as [[keyof T, CustomNamedStyles<T, B>]]
7066

71-
if (isDynamicFunction || isValidStyle) {
72-
return [key, value]
73-
}
67+
return Object
68+
.fromEntries(entries
69+
.map(([key, value]) => {
70+
const isNestedStyle = key === 'shadowOffset'
7471

75-
const valueWithBreakpoint = value as Record<keyof B & string, string | number>
72+
if (isNestedStyle) {
73+
return [
74+
key,
75+
parseStyle(value, breakpoint, screenSize, breakpoints)
76+
]
77+
}
7678

77-
return [key, getValueForBreakpoint<B>(valueWithBreakpoint, breakpoint, screenSize, breakpoints)]
78-
})
79-
)
79+
const isTransform = key === 'transform'
80+
81+
if (isTransform && Array.isArray(value)) {
82+
return [
83+
key,
84+
value.map(value => parseStyle(value, breakpoint, screenSize, breakpoints))
85+
]
86+
}
87+
88+
const isDynamicFunction = typeof value === 'function'
89+
const isValidStyle = typeof value !== 'object'
90+
91+
if (isDynamicFunction || isValidStyle) {
92+
return [key, value]
93+
}
94+
95+
const valueWithBreakpoint = value as Record<keyof B & string, string | number>
96+
97+
return [key, getValueForBreakpoint<B>(valueWithBreakpoint, breakpoint, screenSize, breakpoints)]
98+
})
99+
)
100+
}

0 commit comments

Comments
 (0)