Skip to content

Commit 940a607

Browse files
authored
feat: unset (#90)
1 parent bcad4fc commit 940a607

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

src/compiler/stylesheet.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const staticDeclarations = new WeakMap<
3232

3333
const extraRules = new WeakMap<StyleRule, Partial<StyleRule>[]>();
3434

35+
const keywords = new Set(["unset"]);
36+
3537
export class StylesheetBuilder {
3638
animationFrames?: AnimationKeyframes_V2[];
3739
animationDeclarations: StyleDeclaration[] = [];
@@ -418,6 +420,8 @@ export class StylesheetBuilder {
418420
]);
419421
} else if (Array.isArray(value) && value.some(isStyleFunction)) {
420422
declarations.push([value, propPath]);
423+
} else if (typeof value === "string" && keywords.has(value)) {
424+
declarations.push([value, propPath]);
421425
} else {
422426
let staticDeclarationRecord = staticDeclarations.get(declarations);
423427
if (!staticDeclarationRecord) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { View } from "react-native";
2+
3+
import { renderHook } from "@testing-library/react-native";
4+
import { registerCSS } from "react-native-css/jest";
5+
6+
import { useNativeCss } from "../react/useNativeCss";
7+
8+
test("unset", () => {
9+
registerCSS(`.my-class { background-color: unset; }`);
10+
11+
const { result } = renderHook(() => {
12+
return useNativeCss(View, { className: "my-class" });
13+
});
14+
15+
expect(result.current.props).toStrictEqual({
16+
style: { backgroundColor: undefined },
17+
});
18+
});

src/runtime/native/styles/resolve.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,16 @@ export function resolveValue(
116116
case "number":
117117
case "boolean":
118118
return value;
119-
case "string":
120-
return value.endsWith("px") // Inline vars() might set a value with a px suffix
121-
? parseInt(value.slice(0, -2), 10)
122-
: value;
119+
case "string": {
120+
if (value === "unset") {
121+
return null;
122+
} else if (value.endsWith("px")) {
123+
// Inline vars() might set a value with a px suffix
124+
return parseInt(value.slice(0, -2), 10);
125+
} else {
126+
return value;
127+
}
128+
}
123129
case "object": {
124130
if (!Array.isArray(value)) {
125131
return value;

src/runtime/utils/objects.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,13 @@ export function applyValue(
4949
prop: string,
5050
value: any,
5151
) {
52+
// This is confusing.
53+
// An undefined value means "don't set anything" (something failed while parsing)
54+
// While a null value means "remove this value", which in React Native means "set to undefined"
5255
if (value === undefined) {
5356
return;
57+
} else if (value === null) {
58+
value = undefined;
5459
}
5560

5661
if (transformKeys.has(prop)) {

0 commit comments

Comments
 (0)