@react-native-styled-system/core 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/theme-gen.js +149 -0
- package/lib/commonjs/@types/SxProps.js +12 -0
- package/lib/commonjs/@types/SxProps.js.map +1 -0
- package/lib/commonjs/@types/ThemedDict.js +14 -0
- package/lib/commonjs/@types/ThemedDict.js.map +1 -0
- package/lib/commonjs/@types/ThemedTypings.js +6 -0
- package/lib/commonjs/@types/ThemedTypings.js.map +1 -0
- package/lib/commonjs/@types/Token.js +6 -0
- package/lib/commonjs/@types/Token.js.map +1 -0
- package/lib/commonjs/hook/useSx.js +82 -0
- package/lib/commonjs/hook/useSx.js.map +1 -0
- package/lib/commonjs/hook/useSxStyle.js +29 -0
- package/lib/commonjs/hook/useSxStyle.js.map +1 -0
- package/lib/commonjs/hook/useSxTokens.js +33 -0
- package/lib/commonjs/hook/useSxTokens.js.map +1 -0
- package/lib/commonjs/index.js +72 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/internal/TokenParser/ColorsParser.js +21 -0
- package/lib/commonjs/internal/TokenParser/ColorsParser.js.map +1 -0
- package/lib/commonjs/internal/TokenParser/RadiiParser.js +41 -0
- package/lib/commonjs/internal/TokenParser/RadiiParser.js.map +1 -0
- package/lib/commonjs/internal/TokenParser/SizesParser.js +41 -0
- package/lib/commonjs/internal/TokenParser/SizesParser.js.map +1 -0
- package/lib/commonjs/internal/TokenParser/SpaceParser.js +90 -0
- package/lib/commonjs/internal/TokenParser/SpaceParser.js.map +1 -0
- package/lib/commonjs/internal/TokenParser/TokenParser.js +23 -0
- package/lib/commonjs/internal/TokenParser/TokenParser.js.map +1 -0
- package/lib/commonjs/internal/TokenParser/TypographyParser.js +23 -0
- package/lib/commonjs/internal/TokenParser/TypographyParser.js.map +1 -0
- package/lib/commonjs/internal/useStableCallback.js +34 -0
- package/lib/commonjs/internal/useStableCallback.js.map +1 -0
- package/lib/commonjs/internal/util/fillNullishThemeKey.js +18 -0
- package/lib/commonjs/internal/util/fillNullishThemeKey.js.map +1 -0
- package/lib/commonjs/internal/util/fillStyleIfNotNullish.js +21 -0
- package/lib/commonjs/internal/util/fillStyleIfNotNullish.js.map +1 -0
- package/lib/commonjs/internal/util/parsePxSuffixNumber.js +16 -0
- package/lib/commonjs/internal/util/parsePxSuffixNumber.js.map +1 -0
- package/lib/commonjs/internal/util/printWarning.js +18 -0
- package/lib/commonjs/internal/util/printWarning.js.map +1 -0
- package/lib/commonjs/provider/StyledSystemProvider.js +25 -0
- package/lib/commonjs/provider/StyledSystemProvider.js.map +1 -0
- package/lib/commonjs/util/propsToThemedStyle.js +138 -0
- package/lib/commonjs/util/propsToThemedStyle.js.map +1 -0
- package/lib/module/@types/SxProps.js +6 -0
- package/lib/module/@types/SxProps.js.map +1 -0
- package/lib/module/@types/ThemedDict.js +8 -0
- package/lib/module/@types/ThemedDict.js.map +1 -0
- package/lib/module/@types/ThemedTypings.js +2 -0
- package/lib/module/@types/ThemedTypings.js.map +1 -0
- package/lib/module/@types/Token.js +2 -0
- package/lib/module/@types/Token.js.map +1 -0
- package/lib/module/hook/useSx.js +75 -0
- package/lib/module/hook/useSx.js.map +1 -0
- package/lib/module/hook/useSxStyle.js +22 -0
- package/lib/module/hook/useSxStyle.js.map +1 -0
- package/lib/module/hook/useSxTokens.js +26 -0
- package/lib/module/hook/useSxTokens.js.map +1 -0
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/internal/TokenParser/ColorsParser.js +14 -0
- package/lib/module/internal/TokenParser/ColorsParser.js.map +1 -0
- package/lib/module/internal/TokenParser/RadiiParser.js +34 -0
- package/lib/module/internal/TokenParser/RadiiParser.js.map +1 -0
- package/lib/module/internal/TokenParser/SizesParser.js +34 -0
- package/lib/module/internal/TokenParser/SizesParser.js.map +1 -0
- package/lib/module/internal/TokenParser/SpaceParser.js +82 -0
- package/lib/module/internal/TokenParser/SpaceParser.js.map +1 -0
- package/lib/module/internal/TokenParser/TokenParser.js +16 -0
- package/lib/module/internal/TokenParser/TokenParser.js.map +1 -0
- package/lib/module/internal/TokenParser/TypographyParser.js +15 -0
- package/lib/module/internal/TokenParser/TypographyParser.js.map +1 -0
- package/lib/module/internal/useStableCallback.js +27 -0
- package/lib/module/internal/useStableCallback.js.map +1 -0
- package/lib/module/internal/util/fillNullishThemeKey.js +11 -0
- package/lib/module/internal/util/fillNullishThemeKey.js.map +1 -0
- package/lib/module/internal/util/fillStyleIfNotNullish.js +14 -0
- package/lib/module/internal/util/fillStyleIfNotNullish.js.map +1 -0
- package/lib/module/internal/util/parsePxSuffixNumber.js +10 -0
- package/lib/module/internal/util/parsePxSuffixNumber.js.map +1 -0
- package/lib/module/internal/util/printWarning.js +11 -0
- package/lib/module/internal/util/printWarning.js.map +1 -0
- package/lib/module/provider/StyledSystemProvider.js +17 -0
- package/lib/module/provider/StyledSystemProvider.js.map +1 -0
- package/lib/module/util/propsToThemedStyle.js +131 -0
- package/lib/module/util/propsToThemedStyle.js.map +1 -0
- package/lib/typescript/@types/SxProps.d.ts +140 -0
- package/lib/typescript/@types/SxProps.d.ts.map +1 -0
- package/lib/typescript/@types/ThemedDict.d.ts +16 -0
- package/lib/typescript/@types/ThemedDict.d.ts.map +1 -0
- package/lib/typescript/@types/ThemedTypings.d.ts +9 -0
- package/lib/typescript/@types/ThemedTypings.d.ts.map +1 -0
- package/lib/typescript/@types/Token.d.ts +17 -0
- package/lib/typescript/@types/Token.d.ts.map +1 -0
- package/lib/typescript/hook/useSx.d.ts +307 -0
- package/lib/typescript/hook/useSx.d.ts.map +1 -0
- package/lib/typescript/hook/useSxStyle.d.ts +8 -0
- package/lib/typescript/hook/useSxStyle.d.ts.map +1 -0
- package/lib/typescript/hook/useSxTokens.d.ts +7 -0
- package/lib/typescript/hook/useSxTokens.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +7 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/internal/TokenParser/ColorsParser.d.ts +4 -0
- package/lib/typescript/internal/TokenParser/ColorsParser.d.ts.map +1 -0
- package/lib/typescript/internal/TokenParser/RadiiParser.d.ts +4 -0
- package/lib/typescript/internal/TokenParser/RadiiParser.d.ts.map +1 -0
- package/lib/typescript/internal/TokenParser/SizesParser.d.ts +4 -0
- package/lib/typescript/internal/TokenParser/SizesParser.d.ts.map +1 -0
- package/lib/typescript/internal/TokenParser/SpaceParser.d.ts +5 -0
- package/lib/typescript/internal/TokenParser/SpaceParser.d.ts.map +1 -0
- package/lib/typescript/internal/TokenParser/TokenParser.d.ts +10 -0
- package/lib/typescript/internal/TokenParser/TokenParser.d.ts.map +1 -0
- package/lib/typescript/internal/TokenParser/TypographyParser.d.ts +4 -0
- package/lib/typescript/internal/TokenParser/TypographyParser.d.ts.map +1 -0
- package/lib/typescript/internal/useStableCallback.d.ts +2 -0
- package/lib/typescript/internal/useStableCallback.d.ts.map +1 -0
- package/lib/typescript/internal/util/fillNullishThemeKey.d.ts +3 -0
- package/lib/typescript/internal/util/fillNullishThemeKey.d.ts.map +1 -0
- package/lib/typescript/internal/util/fillStyleIfNotNullish.d.ts +4 -0
- package/lib/typescript/internal/util/fillStyleIfNotNullish.d.ts.map +1 -0
- package/lib/typescript/internal/util/parsePxSuffixNumber.d.ts +2 -0
- package/lib/typescript/internal/util/parsePxSuffixNumber.d.ts.map +1 -0
- package/lib/typescript/internal/util/printWarning.d.ts +2 -0
- package/lib/typescript/internal/util/printWarning.d.ts.map +1 -0
- package/lib/typescript/provider/StyledSystemProvider.d.ts +13 -0
- package/lib/typescript/provider/StyledSystemProvider.d.ts.map +1 -0
- package/lib/typescript/util/propsToThemedStyle.d.ts +10 -0
- package/lib/typescript/util/propsToThemedStyle.d.ts.map +1 -0
- package/package.json +78 -0
- package/src/@types/SxProps.ts +255 -0
- package/src/@types/ThemedDict.ts +16 -0
- package/src/@types/ThemedTypings.ts +9 -0
- package/src/@types/Token.ts +21 -0
- package/src/hook/useSx.test.ts +348 -0
- package/src/hook/useSx.ts +94 -0
- package/src/hook/useSxStyle.test.ts +57 -0
- package/src/hook/useSxStyle.ts +32 -0
- package/src/hook/useSxTokens.test.ts +59 -0
- package/src/hook/useSxTokens.ts +42 -0
- package/src/index.ts +6 -0
- package/src/internal/TokenParser/ColorsParser.ts +18 -0
- package/src/internal/TokenParser/RadiiParser.ts +44 -0
- package/src/internal/TokenParser/SizesParser.ts +44 -0
- package/src/internal/TokenParser/SpaceParser.ts +95 -0
- package/src/internal/TokenParser/TokenParser.ts +18 -0
- package/src/internal/TokenParser/TypographyParser.ts +24 -0
- package/src/internal/useStableCallback.ts +29 -0
- package/src/internal/util/fillNullishThemeKey.ts +12 -0
- package/src/internal/util/fillStyleIfNotNullish.ts +26 -0
- package/src/internal/util/parsePxSuffixNumber.ts +10 -0
- package/src/internal/util/printWarning.ts +11 -0
- package/src/provider/StyledSystemProvider.tsx +23 -0
- package/src/util/propsToThemedStyle.ts +168 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { is } from '@mj-studio/js-util';
|
|
2
|
+
|
|
3
|
+
import type { ThemedDict } from '../../@types/ThemedDict';
|
|
4
|
+
import type { SizesValue, Token } from '../../@types/Token';
|
|
5
|
+
import { parsePxSuffixNumber } from '../util/parsePxSuffixNumber';
|
|
6
|
+
|
|
7
|
+
export const createSizesParser = (theme: ThemedDict) => {
|
|
8
|
+
return (token?: Token<'sizes'>) => parseSizes(theme, token);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const parseSizes = (theme: ThemedDict, token?: Token<'sizes'>): SizesValue | undefined => {
|
|
12
|
+
if (is.nullOrUndefined(token)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const px = parsePxSuffixNumber(token);
|
|
17
|
+
if (is.number(px)) {
|
|
18
|
+
return px;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// end with px but not number parsed
|
|
22
|
+
if (is.string(token) && token.endsWith('px')) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const sizes = theme.sizes;
|
|
27
|
+
|
|
28
|
+
if ((is.string(token) || is.number(token)) && token in sizes) {
|
|
29
|
+
return sizes[token];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (is.number(token)) {
|
|
33
|
+
const stringKey = `${token}`;
|
|
34
|
+
if (stringKey in sizes) {
|
|
35
|
+
return sizes[stringKey];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (is.numberString(token)) {
|
|
40
|
+
return Number(token);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return token;
|
|
44
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { is } from '@mj-studio/js-util';
|
|
2
|
+
|
|
3
|
+
import type { ThemedDict } from '../../@types/ThemedDict';
|
|
4
|
+
import type { SpaceValue, Token } from '../../@types/Token';
|
|
5
|
+
import { parsePxSuffixNumber } from '../util/parsePxSuffixNumber';
|
|
6
|
+
|
|
7
|
+
export const createSpaceParser = (theme: ThemedDict) => {
|
|
8
|
+
return (token?: Token<'space'>) => parseSpace(theme, token);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const createSpaceAsNumberOnlyParser = (theme: ThemedDict) => {
|
|
12
|
+
return (token?: Token<'space'>) => parseSpaceAsNumberOnly(theme, token);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Space should be handle negative string like '-1' as well
|
|
17
|
+
*/
|
|
18
|
+
const parseSpace = (theme: ThemedDict, token?: Token<'space'>): SpaceValue | undefined => {
|
|
19
|
+
if (is.nullOrUndefined(token)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const px = parsePxSuffixNumber(token);
|
|
24
|
+
if (is.number(px)) {
|
|
25
|
+
return px;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// end with px but not number parsed
|
|
29
|
+
if (is.string(token) && token.endsWith('px')) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const spaces = theme.space;
|
|
34
|
+
|
|
35
|
+
if ((is.string(token) || is.number(token)) && token in spaces) {
|
|
36
|
+
return spaces[token];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Parse is number
|
|
40
|
+
if (is.number(token)) {
|
|
41
|
+
const stringKey = `${token}`;
|
|
42
|
+
if (stringKey in spaces) {
|
|
43
|
+
return spaces[stringKey];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const negativeNumberKey = -token;
|
|
47
|
+
if (negativeNumberKey in spaces) {
|
|
48
|
+
const value = spaces[negativeNumberKey];
|
|
49
|
+
if (is.number(value)) {
|
|
50
|
+
return -value;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const negativeStringKey =
|
|
55
|
+
stringKey.charAt(0) === '-' ? stringKey.substring(1) : `-${stringKey}`;
|
|
56
|
+
|
|
57
|
+
if (negativeStringKey in spaces) {
|
|
58
|
+
const value = spaces[negativeStringKey];
|
|
59
|
+
if (is.number(value)) {
|
|
60
|
+
return -value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Parse prefix minus token string
|
|
66
|
+
if (is.string(token) && token.startsWith('-')) {
|
|
67
|
+
const originalToken = token.substring(1);
|
|
68
|
+
if (originalToken in spaces) {
|
|
69
|
+
const value = spaces[originalToken];
|
|
70
|
+
if (is.number(value)) {
|
|
71
|
+
return -value;
|
|
72
|
+
}
|
|
73
|
+
// not invert sign percent string yet(will be supported).
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// don't return malformed string. It is not acceptable as SpaceValue
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (is.numberString(token)) {
|
|
81
|
+
return Number(token);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return token;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Parse space and filter if it is number for number only prop like gap
|
|
89
|
+
*/
|
|
90
|
+
const parseSpaceAsNumberOnly = (theme: ThemedDict, token?: Token<'space'>): number | undefined => {
|
|
91
|
+
const ret = parseSpace(theme, token);
|
|
92
|
+
if (is.number(ret)) {
|
|
93
|
+
return ret;
|
|
94
|
+
}
|
|
95
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ThemedDict } from '../../@types/ThemedDict';
|
|
2
|
+
|
|
3
|
+
import { createColorsParser } from './ColorsParser';
|
|
4
|
+
import { createRadiiParser } from './RadiiParser';
|
|
5
|
+
import { createSizesParser } from './SizesParser';
|
|
6
|
+
import { createSpaceAsNumberOnlyParser, createSpaceParser } from './SpaceParser';
|
|
7
|
+
import { createTypographyParser } from './TypographyParser';
|
|
8
|
+
|
|
9
|
+
export const createTokenParsers = (theme: ThemedDict) => {
|
|
10
|
+
return {
|
|
11
|
+
colors: createColorsParser(theme),
|
|
12
|
+
space: createSpaceParser(theme),
|
|
13
|
+
spaceAsNumberOnly: createSpaceAsNumberOnlyParser(theme),
|
|
14
|
+
sizes: createSizesParser(theme),
|
|
15
|
+
radii: createRadiiParser(theme),
|
|
16
|
+
typography: createTypographyParser(theme),
|
|
17
|
+
};
|
|
18
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { is } from '@mj-studio/js-util';
|
|
2
|
+
import invariant from 'invariant';
|
|
3
|
+
|
|
4
|
+
import type { ThemedDict } from '../../@types/ThemedDict';
|
|
5
|
+
import type { Token, TypographyValue } from '../../@types/Token';
|
|
6
|
+
|
|
7
|
+
export const createTypographyParser = (theme: ThemedDict) => {
|
|
8
|
+
return (token?: Token<'typography'>) => parseTypography(theme, token);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const parseTypography = (
|
|
12
|
+
theme: ThemedDict,
|
|
13
|
+
token?: Token<'typography'>,
|
|
14
|
+
): TypographyValue | undefined => {
|
|
15
|
+
if (is.nullOrUndefined(token)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
invariant(is.string(token), 'typography token should be a string');
|
|
20
|
+
|
|
21
|
+
if (is.string(token) && token in theme.typography) {
|
|
22
|
+
return theme.typography[token];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a stabilized callback reference which delegates to the most recent unstable callback
|
|
3
|
+
*
|
|
4
|
+
* This is similar to useCallback, but allows unstableCallback to access the most recent values from the closure.
|
|
5
|
+
* This can be useful if the callback is being stored long term, such as in the Transition Hook registry.
|
|
6
|
+
*
|
|
7
|
+
* Example:
|
|
8
|
+
* ```jsx
|
|
9
|
+
* const latestValueFromProps = props.value
|
|
10
|
+
* const transitionHook = useStableCallback(() => console.log(latestValueFromProps));
|
|
11
|
+
* useEffect(() => {
|
|
12
|
+
* const deregister = transitionService.onBefore({ exiting: 'someState' }, transitionHook);
|
|
13
|
+
* return () => deregister();
|
|
14
|
+
* }, []);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
import { useCallback, useRef } from 'react';
|
|
18
|
+
|
|
19
|
+
export function useStableCallback<T extends Function>(unstableCallback: T): T {
|
|
20
|
+
const ref = useRef<T>(unstableCallback);
|
|
21
|
+
ref.current = unstableCallback;
|
|
22
|
+
|
|
23
|
+
const callback = useCallback(function () {
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
return ref.current && ref.current.apply(this, arguments);
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return callback as unknown as T;
|
|
29
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { TextStyle, ViewStyle } from 'react-native';
|
|
2
|
+
import { is } from '@mj-studio/js-util';
|
|
3
|
+
|
|
4
|
+
export function fillViewStyleIfNotNullish<K extends keyof ViewStyle>(
|
|
5
|
+
into: ViewStyle,
|
|
6
|
+
key: K,
|
|
7
|
+
value?: ViewStyle[K] | null,
|
|
8
|
+
) {
|
|
9
|
+
if (is.undefined(value) || is.null(value)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
into[key] = value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function fillTextStyleIfNotNullish<K extends Exclude<keyof TextStyle, keyof ViewStyle>>(
|
|
17
|
+
into: TextStyle,
|
|
18
|
+
key: K,
|
|
19
|
+
value?: TextStyle[K] | null,
|
|
20
|
+
) {
|
|
21
|
+
if (is.undefined(value) || is.null(value)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
into[key] = value;
|
|
26
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { is } from '@mj-studio/js-util';
|
|
2
|
+
|
|
3
|
+
export function parsePxSuffixNumber(value: any): number | undefined {
|
|
4
|
+
if (is.string(value) && value.endsWith('px')) {
|
|
5
|
+
const sub = value.substring(0, value.length - 2);
|
|
6
|
+
if (is.numberString(sub)) {
|
|
7
|
+
return Number(sub);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { PropsWithChildren } from 'react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
import type { ThemedDict } from '../@types/ThemedDict';
|
|
5
|
+
import { emptyThemedDict } from '../@types/ThemedDict';
|
|
6
|
+
import { fillNullishThemeKey } from '../internal/util/fillNullishThemeKey';
|
|
7
|
+
|
|
8
|
+
export type StyledSystemContextValue = {
|
|
9
|
+
theme: ThemedDict;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const StyledSystemContext = React.createContext<StyledSystemContextValue>({
|
|
13
|
+
theme: emptyThemedDict,
|
|
14
|
+
});
|
|
15
|
+
type Props = PropsWithChildren<{ theme: Partial<ThemedDict> }>;
|
|
16
|
+
|
|
17
|
+
export const StyledSystemProvider = ({ children, theme }: Props) => {
|
|
18
|
+
return (
|
|
19
|
+
<StyledSystemContext.Provider value={{ theme: fillNullishThemeKey(theme) }}>
|
|
20
|
+
{children}
|
|
21
|
+
</StyledSystemContext.Provider>
|
|
22
|
+
);
|
|
23
|
+
};
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/* eslint-disable padding-line-between-statements */
|
|
2
|
+
import type { TextStyle } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import type { TextSxProps } from '../@types/SxProps';
|
|
5
|
+
import type { ThemedDict } from '../@types/ThemedDict';
|
|
6
|
+
import { createTokenParsers } from '../internal/TokenParser/TokenParser';
|
|
7
|
+
import {
|
|
8
|
+
fillTextStyleIfNotNullish,
|
|
9
|
+
fillViewStyleIfNotNullish,
|
|
10
|
+
} from '../internal/util/fillStyleIfNotNullish';
|
|
11
|
+
import { printWarning } from '../internal/util/printWarning';
|
|
12
|
+
|
|
13
|
+
export type ThemedStyleType = 'ViewStyle' | 'TextStyle';
|
|
14
|
+
export const propsToThemedStyle = ({
|
|
15
|
+
theme,
|
|
16
|
+
sx,
|
|
17
|
+
styleType = 'ViewStyle',
|
|
18
|
+
}: {
|
|
19
|
+
theme?: ThemedDict;
|
|
20
|
+
sx?: TextSxProps;
|
|
21
|
+
styleType?: ThemedStyleType;
|
|
22
|
+
}): TextStyle | undefined => {
|
|
23
|
+
const ret: TextStyle = {};
|
|
24
|
+
|
|
25
|
+
if (!theme) {
|
|
26
|
+
printWarning('theme not found');
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (!sx) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const { colors, radii, sizes, space, spaceAsNumberOnly, typography } = createTokenParsers(theme);
|
|
35
|
+
|
|
36
|
+
// region colors
|
|
37
|
+
fillViewStyleIfNotNullish(ret, 'backgroundColor', colors(sx.backgroundColor ?? sx.bg));
|
|
38
|
+
fillViewStyleIfNotNullish(ret, 'borderColor', colors(sx.borderColor));
|
|
39
|
+
// endregion
|
|
40
|
+
|
|
41
|
+
// region space
|
|
42
|
+
fillViewStyleIfNotNullish(ret, 'margin', space(sx.margin ?? sx.m));
|
|
43
|
+
fillViewStyleIfNotNullish(ret, 'marginTop', space(sx.marginTop ?? sx.mt));
|
|
44
|
+
fillViewStyleIfNotNullish(ret, 'marginRight', space(sx.marginRight ?? sx.mr));
|
|
45
|
+
fillViewStyleIfNotNullish(ret, 'marginBottom', space(sx.marginBottom ?? sx.mb));
|
|
46
|
+
fillViewStyleIfNotNullish(ret, 'marginLeft', space(sx.marginLeft ?? sx.ml));
|
|
47
|
+
fillViewStyleIfNotNullish(ret, 'marginVertical', space(sx.marginY ?? sx.my));
|
|
48
|
+
fillViewStyleIfNotNullish(ret, 'marginHorizontal', space(sx.marginX ?? sx.mx));
|
|
49
|
+
|
|
50
|
+
fillViewStyleIfNotNullish(ret, 'padding', space(sx.padding ?? sx.p));
|
|
51
|
+
fillViewStyleIfNotNullish(ret, 'paddingTop', space(sx.paddingTop ?? sx.pt));
|
|
52
|
+
fillViewStyleIfNotNullish(ret, 'paddingRight', space(sx.paddingRight ?? sx.pr));
|
|
53
|
+
fillViewStyleIfNotNullish(ret, 'paddingBottom', space(sx.paddingBottom ?? sx.pb));
|
|
54
|
+
fillViewStyleIfNotNullish(ret, 'paddingLeft', space(sx.paddingLeft ?? sx.pl));
|
|
55
|
+
fillViewStyleIfNotNullish(ret, 'paddingVertical', space(sx.paddingY ?? sx.py));
|
|
56
|
+
fillViewStyleIfNotNullish(ret, 'paddingHorizontal', space(sx.paddingX ?? sx.px));
|
|
57
|
+
|
|
58
|
+
fillViewStyleIfNotNullish(ret, 'top', space(sx.top ?? (sx.absoluteFill ? 0 : undefined)));
|
|
59
|
+
fillViewStyleIfNotNullish(ret, 'right', space(sx.right ?? (sx.absoluteFill ? 0 : undefined)));
|
|
60
|
+
fillViewStyleIfNotNullish(ret, 'bottom', space(sx.bottom ?? (sx.absoluteFill ? 0 : undefined)));
|
|
61
|
+
fillViewStyleIfNotNullish(ret, 'left', space(sx.left ?? (sx.absoluteFill ? 0 : undefined)));
|
|
62
|
+
|
|
63
|
+
fillViewStyleIfNotNullish(ret, 'gap', spaceAsNumberOnly(sx.gap));
|
|
64
|
+
fillViewStyleIfNotNullish(ret, 'columnGap', spaceAsNumberOnly(sx.gapX));
|
|
65
|
+
fillViewStyleIfNotNullish(ret, 'rowGap', spaceAsNumberOnly(sx.gapY));
|
|
66
|
+
// endregion
|
|
67
|
+
|
|
68
|
+
// region sizes
|
|
69
|
+
fillViewStyleIfNotNullish(ret, 'width', sizes(sx.width ?? sx.w));
|
|
70
|
+
fillViewStyleIfNotNullish(ret, 'minWidth', sizes(sx.minWidth ?? sx.minW));
|
|
71
|
+
fillViewStyleIfNotNullish(ret, 'maxWidth', sizes(sx.maxWidth ?? sx.maxW));
|
|
72
|
+
|
|
73
|
+
fillViewStyleIfNotNullish(ret, 'height', sizes(sx.height ?? sx.h));
|
|
74
|
+
fillViewStyleIfNotNullish(ret, 'minHeight', sizes(sx.minHeight ?? sx.minH));
|
|
75
|
+
fillViewStyleIfNotNullish(ret, 'maxHeight', sizes(sx.maxHeight ?? sx.maxH));
|
|
76
|
+
// endregion
|
|
77
|
+
|
|
78
|
+
// region radii
|
|
79
|
+
fillViewStyleIfNotNullish(ret, 'borderRadius', radii(sx.borderRadius ?? sx.radius));
|
|
80
|
+
fillViewStyleIfNotNullish(
|
|
81
|
+
ret,
|
|
82
|
+
'borderTopLeftRadius',
|
|
83
|
+
radii(sx.borderTopLeftRadius ?? sx.topLeftRadius),
|
|
84
|
+
);
|
|
85
|
+
fillViewStyleIfNotNullish(
|
|
86
|
+
ret,
|
|
87
|
+
'borderTopRightRadius',
|
|
88
|
+
radii(sx.borderTopRightRadius ?? sx.topRightRadius),
|
|
89
|
+
);
|
|
90
|
+
fillViewStyleIfNotNullish(
|
|
91
|
+
ret,
|
|
92
|
+
'borderBottomLeftRadius',
|
|
93
|
+
radii(sx.borderBottomLeftRadius ?? sx.bottomLeftRadius),
|
|
94
|
+
);
|
|
95
|
+
fillViewStyleIfNotNullish(
|
|
96
|
+
ret,
|
|
97
|
+
'borderBottomRightRadius',
|
|
98
|
+
radii(sx.borderBottomRightRadius ?? sx.bottomRightRadius),
|
|
99
|
+
);
|
|
100
|
+
// endregion
|
|
101
|
+
|
|
102
|
+
// region styles
|
|
103
|
+
fillViewStyleIfNotNullish(ret, 'flex', sx.flex);
|
|
104
|
+
fillViewStyleIfNotNullish(ret, 'alignItems', sx.alignItems ?? (sx.center ? 'center' : undefined));
|
|
105
|
+
fillViewStyleIfNotNullish(ret, 'alignContent', sx.alignContent);
|
|
106
|
+
fillViewStyleIfNotNullish(
|
|
107
|
+
ret,
|
|
108
|
+
'justifyContent',
|
|
109
|
+
sx.justifyContent ?? (sx.center ? 'center' : undefined),
|
|
110
|
+
);
|
|
111
|
+
fillViewStyleIfNotNullish(ret, 'flexWrap', sx.flexWrap);
|
|
112
|
+
fillViewStyleIfNotNullish(ret, 'flexDirection', sx.flexDirection);
|
|
113
|
+
fillViewStyleIfNotNullish(ret, 'flexGrow', sx.flexGrow);
|
|
114
|
+
fillViewStyleIfNotNullish(ret, 'flexShrink', sx.flexShrink);
|
|
115
|
+
fillViewStyleIfNotNullish(ret, 'flexBasis', sx.flexBasis);
|
|
116
|
+
fillViewStyleIfNotNullish(ret, 'alignSelf', sx.alignSelf);
|
|
117
|
+
fillViewStyleIfNotNullish(
|
|
118
|
+
ret,
|
|
119
|
+
'position',
|
|
120
|
+
sx.position ?? sx.pos ?? (sx.absoluteFill ? 'absolute' : undefined),
|
|
121
|
+
);
|
|
122
|
+
fillViewStyleIfNotNullish(ret, 'borderWidth', sx.borderWidth);
|
|
123
|
+
fillViewStyleIfNotNullish(ret, 'borderTopWidth', sx.borderTopWidth);
|
|
124
|
+
fillViewStyleIfNotNullish(ret, 'borderRightWidth', sx.borderRightWidth);
|
|
125
|
+
fillViewStyleIfNotNullish(ret, 'borderBottomWidth', sx.borderBottomWidth);
|
|
126
|
+
fillViewStyleIfNotNullish(ret, 'borderLeftWidth', sx.borderLeftWidth);
|
|
127
|
+
|
|
128
|
+
fillViewStyleIfNotNullish(ret, 'opacity', sx.opacity);
|
|
129
|
+
fillViewStyleIfNotNullish(ret, 'overflow', sx.overflow);
|
|
130
|
+
fillViewStyleIfNotNullish(ret, 'transform', sx.transform);
|
|
131
|
+
fillViewStyleIfNotNullish(ret, 'aspectRatio', sx.aspectRatio);
|
|
132
|
+
fillViewStyleIfNotNullish(ret, 'display', sx.display);
|
|
133
|
+
fillViewStyleIfNotNullish(ret, 'elevation', sx.elevation);
|
|
134
|
+
fillViewStyleIfNotNullish(ret, 'zIndex', sx.zIndex);
|
|
135
|
+
// endregion
|
|
136
|
+
|
|
137
|
+
if (styleType === 'TextStyle') {
|
|
138
|
+
// region colors
|
|
139
|
+
fillTextStyleIfNotNullish(ret, 'color', colors(sx.color));
|
|
140
|
+
fillTextStyleIfNotNullish(ret, 'textDecorationColor', colors(sx.textDecorationColor));
|
|
141
|
+
fillTextStyleIfNotNullish(ret, 'textShadowColor', colors(sx.textShadowColor));
|
|
142
|
+
// endregion
|
|
143
|
+
|
|
144
|
+
// region styles
|
|
145
|
+
const t = typography(sx.typography ?? sx.t);
|
|
146
|
+
fillTextStyleIfNotNullish(ret, 'fontFamily', sx.fontFamily ?? t?.fontFamily);
|
|
147
|
+
fillTextStyleIfNotNullish(ret, 'fontSize', sx.fontSize ?? t?.fontSize);
|
|
148
|
+
fillTextStyleIfNotNullish(ret, 'lineHeight', sx.lineHeight ?? t?.lineHeight);
|
|
149
|
+
fillTextStyleIfNotNullish(ret, 'fontWeight', sx.fontWeight ?? sx.weight ?? t?.fontWeight);
|
|
150
|
+
fillTextStyleIfNotNullish(ret, 'letterSpacing', sx.letterSpacing ?? t?.letterSpacing);
|
|
151
|
+
fillTextStyleIfNotNullish(ret, 'textAlign', sx.textAlign ?? sx.align ?? t?.textAlign);
|
|
152
|
+
fillTextStyleIfNotNullish(ret, 'fontStyle', sx.fontStyle ?? t?.fontStyle);
|
|
153
|
+
fillTextStyleIfNotNullish(ret, 'textDecorationLine', sx.textDecorationLine);
|
|
154
|
+
fillTextStyleIfNotNullish(ret, 'textDecorationStyle', sx.textDecorationStyle);
|
|
155
|
+
fillTextStyleIfNotNullish(ret, 'textShadowOffset', sx.textShadowOffset);
|
|
156
|
+
fillTextStyleIfNotNullish(ret, 'textShadowRadius', sx.textShadowRadius);
|
|
157
|
+
fillTextStyleIfNotNullish(ret, 'textTransform', sx.textTransform);
|
|
158
|
+
fillTextStyleIfNotNullish(ret, 'userSelect', sx.userSelect);
|
|
159
|
+
// android
|
|
160
|
+
fillTextStyleIfNotNullish(ret, 'includeFontPadding', sx.includeFontPadding);
|
|
161
|
+
/* todo */
|
|
162
|
+
// ios
|
|
163
|
+
/* todo */
|
|
164
|
+
// endregion
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return ret;
|
|
168
|
+
};
|