Skip to content

Commit c7e3eb3

Browse files
fix: resolve single box-shadow from runtime CSS variables
1 parent b0b35b1 commit c7e3eb3

File tree

2 files changed

+115
-15
lines changed

2 files changed

+115
-15
lines changed

src/__tests__/native/box-shadow.test.tsx

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,96 @@ test("shadow values - single nested variables", () => {
2828
});
2929
});
3030

31+
test("single shadow via runtime variable", () => {
32+
registerCSS(
33+
`.test {
34+
--my-shadow: 0 4px 6px -1px #000;
35+
box-shadow: var(--my-shadow);
36+
}`,
37+
{ inlineVariables: false },
38+
);
39+
40+
render(<View testID={testID} className="test" />);
41+
const component = screen.getByTestId(testID);
42+
43+
expect(component.props.style.boxShadow).toStrictEqual([
44+
{
45+
offsetX: 0,
46+
offsetY: 4,
47+
blurRadius: 6,
48+
spreadDistance: -1,
49+
color: "#000",
50+
},
51+
]);
52+
});
53+
54+
test("single shadow via multi-definition variable (theme switching)", () => {
55+
registerCSS(`
56+
:root { --themed-shadow: 0 4px 6px -1px #000; }
57+
.dark { --themed-shadow: 0 4px 6px -1px #fff; }
58+
.test { box-shadow: var(--themed-shadow); }
59+
`);
60+
61+
render(<View testID={testID} className="test" />);
62+
const component = screen.getByTestId(testID);
63+
64+
expect(component.props.style.boxShadow).toStrictEqual([
65+
{
66+
offsetX: 0,
67+
offsetY: 4,
68+
blurRadius: 6,
69+
spreadDistance: -1,
70+
color: "#000",
71+
},
72+
]);
73+
});
74+
75+
test("transparent shadow via runtime variable is filtered", () => {
76+
registerCSS(
77+
`.test {
78+
--my-shadow: 0 0 0 0 #0000;
79+
box-shadow: var(--my-shadow);
80+
}`,
81+
{ inlineVariables: false },
82+
);
83+
84+
render(<View testID={testID} className="test" />);
85+
const component = screen.getByTestId(testID);
86+
87+
expect(component.props.style.boxShadow).toStrictEqual([]);
88+
});
89+
90+
test("multi-shadow via runtime variables (comma-separated)", () => {
91+
registerCSS(
92+
`.test {
93+
--shadow-a: 0 4px 6px -1px #000;
94+
--shadow-b: 0 1px 2px 0 #333;
95+
box-shadow: var(--shadow-a), var(--shadow-b);
96+
}`,
97+
{ inlineVariables: false },
98+
);
99+
100+
render(<View testID={testID} className="test" />);
101+
const component = screen.getByTestId(testID);
102+
103+
expect(component.props.style.boxShadow).toStrictEqual([
104+
{
105+
offsetX: 0,
106+
offsetY: 4,
107+
blurRadius: 6,
108+
spreadDistance: -1,
109+
color: "#000",
110+
},
111+
{
112+
offsetX: 0,
113+
offsetY: 1,
114+
blurRadius: 2,
115+
spreadDistance: 0,
116+
color: "#333",
117+
},
118+
]);
119+
});
120+
31121
test("shadow values - multiple nested variables", () => {
32122
registerCSS(`
33123
:root {

src/native/styles/shorthands/box-shadow.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,32 @@ export const boxShadow: StyleFunctionResolver = (
4141

4242
if (!isStyleDescriptorArray(args)) {
4343
return args;
44-
} else {
45-
return args
46-
.flatMap(flattenShadowDescriptor)
47-
.map((shadows) => {
48-
if (shadows === undefined) {
49-
return;
50-
} else {
51-
return normalizeInsetValue(
52-
omitTransparentShadows(
53-
handler(resolveValue, shadows, get, options),
54-
),
55-
);
56-
}
57-
})
58-
.filter((v) => v !== undefined);
5944
}
45+
46+
// A flat array of primitives (e.g. [0, 4, 6, -1, "#000"]) is a single shadow
47+
// resolved from a runtime variable. Pass it directly to the pattern handler.
48+
// A nested array (e.g. [[0, 4, 6, -1, "#000"], [...]]) is multiple shadows.
49+
if (args.length > 0 && !Array.isArray(args[0])) {
50+
const result = handler(resolveValue, args, get, options);
51+
if (result === undefined) {
52+
return [];
53+
}
54+
const filtered = omitTransparentShadows(result);
55+
return filtered !== undefined ? [normalizeInsetValue(filtered)] : [];
56+
}
57+
58+
return args
59+
.flatMap(flattenShadowDescriptor)
60+
.map((shadows) => {
61+
if (shadows === undefined) {
62+
return;
63+
} else {
64+
return normalizeInsetValue(
65+
omitTransparentShadows(handler(resolveValue, shadows, get, options)),
66+
);
67+
}
68+
})
69+
.filter((v) => v !== undefined);
6070
};
6171

6272
function flattenShadowDescriptor(arg: StyleDescriptor): StyleDescriptor[] {

0 commit comments

Comments
 (0)