Skip to content

Commit d3fc70a

Browse files
committed
Add native support for css.types.* functions
These functions pass the value straight through on native. Fix #331
1 parent ecacaec commit d3fc70a

4 files changed

Lines changed: 237 additions & 1 deletion

File tree

apps/examples/src/components/tokens.stylex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const tokens: StyleVars<
1919
inputPlaceholderColor: string
2020
}>
2121
> = css.defineVars({
22-
squareColor: 'red',
22+
squareColor: css.types.color('red'),
2323
textColor: {
2424
default: 'darkred',
2525
'@media (prefers-color-scheme: dark)': 'lightred'

packages/react-strict-dom/src/native/stylex/index.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,78 @@ export function props(
641641

642642
return nativeProps;
643643
}
644+
645+
type ValueWithDefault<+T> =
646+
| T
647+
| $ReadOnly<{
648+
default: ValueWithDefault<T>,
649+
[string]: ValueWithDefault<T>
650+
}>;
651+
652+
export const types = {
653+
angle: <T: string | 0 = string | 0>(
654+
value: ValueWithDefault<T>
655+
): ValueWithDefault<T> => {
656+
return value;
657+
},
658+
color: <T: string = string>(
659+
value: ValueWithDefault<T>
660+
): ValueWithDefault<T> => {
661+
return value;
662+
},
663+
image: <T: string = string>(
664+
value: ValueWithDefault<T>
665+
): ValueWithDefault<T> => {
666+
return value;
667+
},
668+
integer: <T: number | string = number | string>(
669+
value: ValueWithDefault<T>
670+
): ValueWithDefault<T> => {
671+
return value;
672+
},
673+
length: <T: number | string = number | string>(
674+
value: ValueWithDefault<T>
675+
): ValueWithDefault<T> => {
676+
return value;
677+
},
678+
lengthPercentage: <T: number | string = number | string>(
679+
value: ValueWithDefault<T>
680+
): ValueWithDefault<T> => {
681+
return value;
682+
},
683+
number: <T: number | string = number | string>(
684+
value: ValueWithDefault<T>
685+
): ValueWithDefault<T> => {
686+
return value;
687+
},
688+
percentage: <T: number | string = number | string>(
689+
value: ValueWithDefault<T>
690+
): ValueWithDefault<T> => {
691+
return value;
692+
},
693+
resolution: <T: string = string>(
694+
value: ValueWithDefault<T>
695+
): ValueWithDefault<T> => {
696+
return value;
697+
},
698+
time: <T: string | 0 = string | 0>(
699+
value: ValueWithDefault<T>
700+
): ValueWithDefault<T> => {
701+
return value;
702+
},
703+
transformFunction: <T: string = string>(
704+
value: ValueWithDefault<T>
705+
): ValueWithDefault<T> => {
706+
return value;
707+
},
708+
transformList: <T: string = string>(
709+
value: ValueWithDefault<T>
710+
): ValueWithDefault<T> => {
711+
return value;
712+
},
713+
url: <T: string = string>(
714+
value: ValueWithDefault<T>
715+
): ValueWithDefault<T> => {
716+
return value;
717+
}
718+
};

packages/react-strict-dom/tests/__snapshots__/css-test.native.js.snap-native

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,77 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`css.types.* types.angle: tokens 1`] = `
4+
{
5+
"angle": "var(--angle__id__6)",
6+
}
7+
`;
8+
9+
exports[`css.types.* types.color: tokens 1`] = `
10+
{
11+
"color": "var(--color__id__7)",
12+
}
13+
`;
14+
15+
exports[`css.types.* types.image: tokens 1`] = `
16+
{
17+
"image": "var(--image__id__8)",
18+
}
19+
`;
20+
21+
exports[`css.types.* types.integer: tokens 1`] = `
22+
{
23+
"integer": "var(--integer__id__9)",
24+
}
25+
`;
26+
27+
exports[`css.types.* types.length: tokens 1`] = `
28+
{
29+
"length": "var(--length__id__10)",
30+
}
31+
`;
32+
33+
exports[`css.types.* types.lengthPercentage: tokens 1`] = `
34+
{
35+
"lengthPercentage": "var(--lengthPercentage__id__11)",
36+
}
37+
`;
38+
39+
exports[`css.types.* types.number: tokens 1`] = `
40+
{
41+
"number": "var(--number__id__12)",
42+
}
43+
`;
44+
45+
exports[`css.types.* types.percentage: tokens 1`] = `
46+
{
47+
"percentage": "var(--percentage__id__13)",
48+
}
49+
`;
50+
51+
exports[`css.types.* types.resolution: tokens 1`] = `
52+
{
53+
"resolution": "var(--resolution__id__14)",
54+
}
55+
`;
56+
57+
exports[`css.types.* types.time: tokens 1`] = `
58+
{
59+
"time": "var(--time__id__15)",
60+
}
61+
`;
62+
63+
exports[`css.types.* types.transformFunction: tokens 1`] = `
64+
{
65+
"transformFunction": "var(--transformFunction__id__16)",
66+
}
67+
`;
68+
69+
exports[`css.types.* types.url: tokens 1`] = `
70+
{
71+
"url": "var(--url__id__17)",
72+
}
73+
`;
74+
375
exports[`properties: custom property css.createTheme: css.__customProperties 1`] = `
476
{
577
"rootColor__id__1": "red",

packages/react-strict-dom/tests/css-test.native.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,3 +1822,92 @@ describe('queries: @media', () => {
18221822
expect(props.style.color).toBe('white');
18231823
});
18241824
});
1825+
1826+
describe('css.types.*', () => {
1827+
test('types.angle', () => {
1828+
const tokens = css.defineVars({
1829+
angle: css.types.angle('40deg')
1830+
});
1831+
expect(tokens).toMatchSnapshot('tokens');
1832+
});
1833+
1834+
test('types.color', () => {
1835+
const tokens = css.defineVars({
1836+
color: css.types.color({
1837+
default: 'blue',
1838+
'@media (prefers-color-scheme: dark)': 'lightblue'
1839+
})
1840+
});
1841+
expect(tokens).toMatchSnapshot('tokens');
1842+
});
1843+
1844+
test('types.image', () => {
1845+
const tokens = css.defineVars({
1846+
image: css.types.image('./jpg')
1847+
});
1848+
expect(tokens).toMatchSnapshot('tokens');
1849+
});
1850+
1851+
test('types.integer', () => {
1852+
const tokens = css.defineVars({
1853+
integer: css.types.integer(4)
1854+
});
1855+
expect(tokens).toMatchSnapshot('tokens');
1856+
});
1857+
1858+
test('types.length', () => {
1859+
const tokens = css.defineVars({
1860+
length: css.types.length('4px')
1861+
});
1862+
expect(tokens).toMatchSnapshot('tokens');
1863+
});
1864+
1865+
test('types.lengthPercentage', () => {
1866+
const tokens = css.defineVars({
1867+
lengthPercentage: css.types.lengthPercentage('100%')
1868+
});
1869+
expect(tokens).toMatchSnapshot('tokens');
1870+
});
1871+
1872+
test('types.number', () => {
1873+
const tokens = css.defineVars({
1874+
number: css.types.number(4.4)
1875+
});
1876+
expect(tokens).toMatchSnapshot('tokens');
1877+
});
1878+
1879+
test('types.percentage', () => {
1880+
const tokens = css.defineVars({
1881+
percentage: css.types.percentage('100%')
1882+
});
1883+
expect(tokens).toMatchSnapshot('tokens');
1884+
});
1885+
1886+
test('types.resolution', () => {
1887+
const tokens = css.defineVars({
1888+
resolution: css.types.resolution('96dpi')
1889+
});
1890+
expect(tokens).toMatchSnapshot('tokens');
1891+
});
1892+
1893+
test('types.time', () => {
1894+
const tokens = css.defineVars({
1895+
time: css.types.time('1s')
1896+
});
1897+
expect(tokens).toMatchSnapshot('tokens');
1898+
});
1899+
1900+
test('types.transformFunction', () => {
1901+
const tokens = css.defineVars({
1902+
transformFunction: css.types.transformFunction('translateX(10px)')
1903+
});
1904+
expect(tokens).toMatchSnapshot('tokens');
1905+
});
1906+
1907+
test('types.url', () => {
1908+
const tokens = css.defineVars({
1909+
url: css.types.url('https://foobar.com')
1910+
});
1911+
expect(tokens).toMatchSnapshot('tokens');
1912+
});
1913+
});

0 commit comments

Comments
 (0)