Skip to content

Commit d213bd1

Browse files
committed
feat: box-shadow
1 parent 52f4410 commit d213bd1

File tree

14 files changed

+159
-225
lines changed

14 files changed

+159
-225
lines changed

example/app.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "example",
3-
"plugins": ["expo-dev-client"],
42
"expo": {
3+
"name": "example",
4+
"plugins": ["expo-dev-client"],
55
"userInterfaceStyle": "automatic",
66
"android": {
77
"package": "dev.reactnativecss"

example/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { View, Text } from "react-native";
1+
import { Text, View } from "react-native";
2+
23
import "../global.css";
34

45
export default function App() {

src/compiler/__tests__/compiler.test.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,15 @@ test.skip("animations", () => {
231231
],
232232
});
233233
});
234+
235+
test("breaks apart comma separated variables", () => {
236+
const compiled = compile(`
237+
:root {
238+
--test: blue, green;
239+
}
240+
`);
241+
242+
expect(compiled).toStrictEqual({
243+
vr: [["test", [[["blue"], ["green"]]]]],
244+
});
245+
});

src/compiler/declarations.ts

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,32 @@ import type { StyleDescriptor, StyleFunction } from "./compiler.types";
3737
import { parseEasingFunction, parseIterationCount } from "./keyframes";
3838
import { toRNProperty } from "./selectors";
3939
import type { StylesheetBuilder } from "./stylesheet";
40-
import { isValid, validProperties, validPropertiesLoose } from "./valid";
40+
41+
const CommaSeparator = Symbol("CommaSeparator");
4142

4243
type DeclarationType<P extends Declaration["property"]> = Extract<
4344
Declaration,
4445
{ property: P }
4546
>;
4647

47-
type Parser<K extends (typeof validProperties)[number]> = (
48-
declaration: Extract<Declaration, { property: K }>,
48+
type Parser<T extends Declaration["property"]> = (
49+
declaration: Extract<Declaration, { property: T }>,
4950
builder: StylesheetBuilder,
5051
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
5152
) => StyleDescriptor | void;
5253

5354
const propertyRename: Record<string, string> = {};
5455

55-
const runtimeShorthands = new Set([
56+
const needsRuntimeParsing = new Set([
5657
"animation",
5758
"text-shadow",
5859
"transform",
60+
"box-shadow",
5961
"border",
6062
]);
6163

6264
const parsers: {
63-
[K in (typeof validProperties)[number]]: Parser<K>;
65+
[K in Declaration["property"]]?: Parser<K>;
6466
} = {
6567
"align-content": parseAlignContent,
6668
"align-items": parseAlignItems,
@@ -189,7 +191,6 @@ const parsers: {
189191
"padding-left": parseSizeDeclaration,
190192
"padding-right": parseSizeDeclaration,
191193
"padding-top": parseSizeDeclaration,
192-
"pointer-events": parsePointerEvents,
193194
"position": parsePosition,
194195
"right": parseSizeDeclaration,
195196
"rotate": parseRotate,
@@ -218,6 +219,12 @@ const parsers: {
218219
"z-index": parseZIndex,
219220
};
220221

222+
// This is missing LightningCSS types
223+
(parsers as Record<string, Parser<Declaration["property"]>>)["pointer-events"] =
224+
parsePointerEvents as Parser<Declaration["property"]>;
225+
226+
const validProperties = new Set(Object.keys(parsers));
227+
221228
export function parseDeclaration(
222229
declaration: Declaration,
223230
builder: StylesheetBuilder,
@@ -783,7 +790,7 @@ export function parseDeclarationUnparsed(
783790
) {
784791
let property = declaration.value.propertyId.property;
785792

786-
if (!isValid(declaration.value.propertyId)) {
793+
if (!(property in parsers)) {
787794
builder.addWarning("property", property);
788795
return;
789796
}
@@ -799,7 +806,7 @@ export function parseDeclarationUnparsed(
799806
/**
800807
* Unparsed shorthand properties need to be parsed at runtime
801808
*/
802-
if (runtimeShorthands.has(property)) {
809+
if (needsRuntimeParsing.has(property)) {
803810
let args = parseUnparsed(declaration.value.value, builder);
804811
if (!isStyleDescriptorArray(args)) {
805812
args = [args];
@@ -833,7 +840,7 @@ export function parseDeclarationCustom(
833840
) {
834841
const property = declaration.value.name;
835842
if (
836-
validPropertiesLoose.has(property) ||
843+
validProperties.has(property) ||
837844
property.startsWith("--") ||
838845
property.startsWith("-rn-")
839846
) {
@@ -856,9 +863,21 @@ export function reduceParseUnparsed(
856863

857864
if (result.length === 0) {
858865
return undefined;
859-
} else {
860-
return result;
861866
}
867+
868+
let currentGroup: StyleDescriptor[] = [];
869+
const groups: StyleDescriptor[][] = [currentGroup];
870+
871+
for (const value of result) {
872+
if ((value as unknown) === CommaSeparator) {
873+
currentGroup = [];
874+
groups.push(currentGroup);
875+
} else {
876+
currentGroup.push(value);
877+
}
878+
}
879+
880+
return groups.length === 1 ? groups[0] : groups;
862881
}
863882

864883
export function unparsedFunction(
@@ -1004,6 +1023,8 @@ export function parseUnparsed(
10041023
return `${round(tokenOrValue.value.value * 100)}%`;
10051024
case "dimension":
10061025
return parseDimension(tokenOrValue.value, builder);
1026+
case "comma":
1027+
return CommaSeparator as unknown as StyleDescriptor;
10071028
case "at-keyword":
10081029
case "hash":
10091030
case "id-hash":
@@ -1013,7 +1034,6 @@ export function parseUnparsed(
10131034
case "comment":
10141035
case "colon":
10151036
case "semicolon":
1016-
case "comma":
10171037
case "include-match":
10181038
case "dash-match":
10191039
case "prefix-match":
@@ -1997,29 +2017,20 @@ export function parseTextAlign(
19972017
}
19982018

19992019
export function parseBoxShadow(
2000-
{ value }: DeclarationType<"box-shadow">,
2001-
builder: StylesheetBuilder,
2020+
_: DeclarationType<"box-shadow">,
2021+
_builder: StylesheetBuilder,
20022022
) {
2003-
if (value.length > 1) {
2004-
builder.addWarning("value", "multiple box shadows");
2005-
return;
2006-
}
2007-
2008-
const boxShadow = value[0];
2009-
2010-
if (!boxShadow) {
2011-
return;
2012-
}
2023+
return undefined;
20132024

2014-
builder.addDescriptor("shadowColor", parseColor(boxShadow.color, builder));
2015-
builder.addDescriptor("shadowRadius", parseLength(boxShadow.spread, builder));
2016-
// builder.addDescriptor("style",
2017-
// ["shadowOffsetWidth"],
2018-
// parseLength(boxShadow.xOffset, options, ["", ""),
2019-
// );
2020-
// builder.addDescriptor("style",
2021-
// ["shadowOffset", "height"],
2022-
// parseLength(boxShadow.yOffset, builder),
2025+
// return value.map(
2026+
// (shadow): BoxShadowValue => ({
2027+
// color: parseColor(shadow.color, builder),
2028+
// offsetX: parseLength(shadow.xOffset, builder) as number,
2029+
// offsetY: parseLength(shadow.yOffset, builder) as number,
2030+
// blurRadius: parseLength(shadow.blur, builder) as number,
2031+
// spreadDistance: parseLength(shadow.spread, builder) as number,
2032+
// inset: shadow.inset,
2033+
// }),
20232034
// );
20242035
}
20252036

src/compiler/valid.ts

Lines changed: 0 additions & 166 deletions
This file was deleted.

src/metro/resolver.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { basename, resolve, sep } from "node:path";
2+
13
import type {
2-
Resolution,
34
CustomResolutionContext,
45
CustomResolver,
6+
Resolution,
57
} from "metro-resolver";
6-
import { basename, resolve, sep } from "node:path";
8+
79
import { allowedModules } from "../babel/allowedModules";
810

911
const thisModuleDist = resolve(__dirname, "../../../dist");

0 commit comments

Comments
 (0)