Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,15 @@ export function parseLength(

if ("unit" in length) {
switch (length.unit) {
case "px":
return length.value;
case "px": {
if (length.value === Infinity) {
return 9999;
} else if (length.value === -Infinity) {
return -9999;
} else {
return length.value;
}
}
case "rem":
if (typeof inlineRem === "number") {
return length.value * inlineRem;
Expand Down Expand Up @@ -2222,7 +2229,11 @@ export function parseDimension(
): StyleDescriptor {
switch (unit) {
case "px":
return value;
if (value === Infinity) {
return 9999;
} else {
return value;
}
case "%":
return `${value}%`;
case "rnh":
Expand Down
252 changes: 132 additions & 120 deletions src/runtime/native/__tests__/calc.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,167 +2,165 @@ import { render, screen } from "@testing-library/react-native";
import { View } from "react-native-css/components/View";
import { registerCSS, testID } from "react-native-css/jest";

describe("css", () => {
test("calc(10px + 100px)", () => {
registerCSS(
`.my-class {
test("calc(10px + 100px)", () => {
registerCSS(
`.my-class {
width: calc(10px + 100px);
}`,
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 110,
},
testID,
});
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 110,
},
testID,
});
});

test("calc(100% - 30px)", () => {
registerCSS(
`.my-class {
test("calc(100% - 30px)", () => {
registerCSS(
`.my-class {
width: calc(100% - 30px);
}`,
);
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);
render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

// React Native does not support calc() with a percentage value, so there should be no style
expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
testID,
});
// React Native does not support calc() with a percentage value, so there should be no style
expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
testID,
});
});

test("calc(2em * 3)", () => {
registerCSS(
`.my-class {
test("calc(2em * 3)", () => {
registerCSS(
`.my-class {
width: calc(2em * 2);
font-size: 5px
}`,
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 20,
fontSize: 5,
},
testID,
});
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 20,
fontSize: 5,
},
testID,
});
});

test("calc(2rem * 5)", () => {
registerCSS(
`.my-class {
test("calc(2rem * 5)", () => {
registerCSS(
`.my-class {
width: calc(2rem * 5)
}`,
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 140,
},
testID,
});
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 140,
},
testID,
});
});

test("calc(var(--variable) + 20px)", () => {
registerCSS(
`.my-class {
test("calc(var(--variable) + 20px)", () => {
registerCSS(
`.my-class {
--my-var: 100px;
width: calc(var(--my-var) + 20px)
}`,
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 120,
},
testID,
});
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: 120,
},
testID,
});
});

test("calc(var(--percent) + 20%)", () => {
registerCSS(
`.my-class {
test("calc(var(--percent) + 20%)", () => {
registerCSS(
`.my-class {
--percent: 10%;
width: calc(var(--percent) + 20%)
}`,
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: "30%",
},
testID,
});
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {
width: "30%",
},
testID,
});
});

test("calc(var(--variable) + 20%)", () => {
// React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined`
registerCSS(
`.my-class {
test("calc(var(--variable) + 20%)", () => {
// React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined`
registerCSS(
`.my-class {
--variable: 100px;
width: calc(var(--variable) + 20%)
}`,
);
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);
render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {},
testID,
});
expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {},
testID,
});
});

test("calc(var(--percent) + 20px)", () => {
// React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined`
registerCSS(
`.my-class {
test("calc(var(--percent) + 20px)", () => {
// React Native does not support calc() with a percentage value and a non-percentage unit, so this should be `undefined`
registerCSS(
`.my-class {
--percent: 10%;
width: calc(var(--percent) + 20px)
}`,
);
);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);
render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {},
testID,
});
expect(component.type).toBe("View");
expect(component.props).toStrictEqual({
children: undefined,
style: {},
testID,
});
});

Expand Down Expand Up @@ -192,3 +190,17 @@ test("calc & colors", () => {
testID,
});
});

test("infinity", () => {
registerCSS(`.my-class {
border-radius: calc(infinity * 1px);
}`);

render(<View testID={testID} className="my-class" />);
const component = screen.getByTestId(testID);

expect(component.type).toBe("View");
expect(component.props.style).toStrictEqual({
borderRadius: 9999,
});
});