Skip to content

Commit 04cd91e

Browse files
authored
fix: playground current color stopped working (#294)
<img width="1439" alt="image" src="https://user-images.githubusercontent.com/22126563/200785190-dae8a79b-efe9-46d8-91df-199ba3ec1991.png"> Closes: #288
1 parent 0d74835 commit 04cd91e

4 files changed

+92
-25
lines changed

src/handler/expand.ts

+42-25
Original file line numberDiff line numberDiff line change
@@ -199,43 +199,52 @@ function normalizeColor(value: string | object) {
199199
}
200200

201201
export default function expand(
202-
style: Record<string, string | number>,
202+
style: Record<string, string | number> | undefined,
203203
inheritedStyle: Record<string, string | number>
204204
): Record<string, string | number> {
205205
const transformedStyle = {} as any
206206

207-
for (const prop in style) {
208-
// Internal properties.
209-
if (prop.startsWith('_')) {
210-
transformedStyle[prop] = style[prop]
211-
continue
212-
}
207+
if (style) {
208+
const currentColor = getCurrentColor(style.color as string, inheritedStyle.color as string)
213209

214-
const name = getPropertyName(prop)
215-
const currentColor = (style.color || inheritedStyle.color) as string
210+
transformedStyle.color = currentColor
216211

217-
try {
218-
const resolvedStyle =
219-
handleSpecialCase(name, style[prop], currentColor) ||
220-
handleFallbackColor(
221-
name,
222-
getStylesForProperty(name, purify(name, style[prop]), true),
223-
style[prop] as string,
224-
currentColor
225-
)
212+
for (const prop in style) {
213+
// Internal properties.
214+
if (prop.startsWith('_')) {
215+
transformedStyle[prop] = style[prop]
216+
continue
217+
}
226218

227-
Object.assign(transformedStyle, resolvedStyle)
228-
} catch (err) {
229-
throw new Error(
230-
err.message +
219+
if (prop === 'color') {
220+
continue
221+
}
222+
223+
const name = getPropertyName(prop)
224+
225+
try {
226+
const resolvedStyle =
227+
handleSpecialCase(name, style[prop], currentColor) ||
228+
handleFallbackColor(
229+
name,
230+
getStylesForProperty(name, purify(name, style[prop]), true),
231+
style[prop] as string,
232+
currentColor
233+
)
234+
235+
Object.assign(transformedStyle, resolvedStyle)
236+
} catch (err) {
237+
throw new Error(
238+
err.message +
231239
// Attach the extra information of the rule itself if it's not included in
232240
// the error message.
233241
(err.message.includes(style[prop])
234242
? '\n ' + getErrorHint(name)
235243
: `\n in CSS rule \`${name}: ${style[prop]}\`.${getErrorHint(
236-
name
237-
)}`)
238-
)
244+
name
245+
)}`)
246+
)
247+
}
239248
}
240249
}
241250

@@ -337,3 +346,11 @@ export default function expand(
337346

338347
return transformedStyle
339348
}
349+
350+
function getCurrentColor(color: string | undefined, inheritedColor: string) {
351+
if (color && color.toLowerCase() !== 'currentcolor') {
352+
return color
353+
}
354+
355+
return inheritedColor
356+
}

test/color-models.test.tsx

+50
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,56 @@ describe('Color Models', () => {
169169
expect(toImage(svg, 100)).toMatchImageSnapshot()
170170
})
171171

172+
it('should support inherit color', async () => {
173+
const svg = await satori(
174+
<div
175+
style={{
176+
display: 'flex',
177+
backgroundColor: 'pink',
178+
color: 'red',
179+
height: '100%',
180+
width: '100%',
181+
}}
182+
>
183+
<div style={{ display: 'flex' }}>
184+
red
185+
<div>red</div>
186+
</div>
187+
</div>,
188+
{
189+
width: 100,
190+
height: 100,
191+
fonts,
192+
}
193+
)
194+
expect(toImage(svg, 100)).toMatchImageSnapshot()
195+
})
196+
197+
it('should support currentcolor when inherit', async () => {
198+
const svg = await satori(
199+
<div
200+
style={{
201+
display: 'flex',
202+
backgroundColor: 'pink',
203+
color: 'red',
204+
height: '100%',
205+
width: '100%',
206+
}}
207+
>
208+
<div style={{ display: 'flex', color: 'currentcolor' }}>
209+
red
210+
<div>red</div>
211+
</div>
212+
</div>,
213+
{
214+
width: 100,
215+
height: 100,
216+
fonts,
217+
}
218+
)
219+
expect(toImage(svg, 100)).toMatchImageSnapshot()
220+
})
221+
172222
// TODO: add `currentcolor` support to css-to-react-native lib
173223
// it('should support currentcolor', async () => {
174224
// const svg = await satori(

0 commit comments

Comments
 (0)