@mochi-inc-japan/react-native-stylex-sheet 1.0.0

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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja.md +608 -0
  3. package/README.md +599 -0
  4. package/lib/commonjs/index.js +65 -0
  5. package/lib/commonjs/index.js.map +1 -0
  6. package/lib/commonjs/tests/index.test.js +224 -0
  7. package/lib/commonjs/tests/index.test.js.map +1 -0
  8. package/lib/commonjs/tests/media.test.js +226 -0
  9. package/lib/commonjs/tests/media.test.js.map +1 -0
  10. package/lib/commonjs/tests/mock.test.js +54 -0
  11. package/lib/commonjs/tests/mock.test.js.map +1 -0
  12. package/lib/commonjs/tests/test-utils.js +60 -0
  13. package/lib/commonjs/tests/test-utils.js.map +1 -0
  14. package/lib/commonjs/utils/base.js +72 -0
  15. package/lib/commonjs/utils/base.js.map +1 -0
  16. package/lib/commonjs/utils/hooks.js +60 -0
  17. package/lib/commonjs/utils/hooks.js.map +1 -0
  18. package/lib/commonjs/utils/media.js +69 -0
  19. package/lib/commonjs/utils/media.js.map +1 -0
  20. package/lib/commonjs/utils/theme.js +25 -0
  21. package/lib/commonjs/utils/theme.js.map +1 -0
  22. package/lib/commonjs/utils/tokens.js +18 -0
  23. package/lib/commonjs/utils/tokens.js.map +1 -0
  24. package/lib/commonjs/utils/types.js +6 -0
  25. package/lib/commonjs/utils/types.js.map +1 -0
  26. package/lib/commonjs/utils/variant.js +75 -0
  27. package/lib/commonjs/utils/variant.js.map +1 -0
  28. package/lib/module/index.js +6 -0
  29. package/lib/module/index.js.map +1 -0
  30. package/lib/module/tests/index.test.js +220 -0
  31. package/lib/module/tests/index.test.js.map +1 -0
  32. package/lib/module/tests/media.test.js +222 -0
  33. package/lib/module/tests/media.test.js.map +1 -0
  34. package/lib/module/tests/mock.test.js +49 -0
  35. package/lib/module/tests/mock.test.js.map +1 -0
  36. package/lib/module/tests/test-utils.js +52 -0
  37. package/lib/module/tests/test-utils.js.map +1 -0
  38. package/lib/module/utils/base.js +63 -0
  39. package/lib/module/utils/base.js.map +1 -0
  40. package/lib/module/utils/hooks.js +52 -0
  41. package/lib/module/utils/hooks.js.map +1 -0
  42. package/lib/module/utils/media.js +59 -0
  43. package/lib/module/utils/media.js.map +1 -0
  44. package/lib/module/utils/theme.js +16 -0
  45. package/lib/module/utils/theme.js.map +1 -0
  46. package/lib/module/utils/tokens.js +11 -0
  47. package/lib/module/utils/tokens.js.map +1 -0
  48. package/lib/module/utils/types.js +2 -0
  49. package/lib/module/utils/types.js.map +1 -0
  50. package/lib/module/utils/variant.js +65 -0
  51. package/lib/module/utils/variant.js.map +1 -0
  52. package/lib/types/index.d.ts +6 -0
  53. package/lib/types/tests/check-descriptor.test.d.ts +0 -0
  54. package/lib/types/tests/index.test.d.ts +1 -0
  55. package/lib/types/tests/media.test.d.ts +1 -0
  56. package/lib/types/tests/mock.test.d.ts +1 -0
  57. package/lib/types/tests/test-utils.d.ts +7 -0
  58. package/lib/types/tests/utils.d.ts +6 -0
  59. package/lib/types/utils/base.d.ts +13 -0
  60. package/lib/types/utils/hooks.d.ts +16 -0
  61. package/lib/types/utils/index.d.ts +9 -0
  62. package/lib/types/utils/media.d.ts +5 -0
  63. package/lib/types/utils/theme.d.ts +14 -0
  64. package/lib/types/utils/tokens.d.ts +3 -0
  65. package/lib/types/utils/types.d.ts +28 -0
  66. package/lib/types/utils/utils.d.ts +4 -0
  67. package/lib/types/utils/variant.d.ts +50 -0
  68. package/package.json +132 -0
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.props = exports.mix = exports.create = void 0;
7
+ var _reactNative = require("react-native");
8
+ var _media = require("./media");
9
+ var _variant = require("./variant");
10
+ var _theme = require("./theme");
11
+ function bundleStyleSheet(styleObject) {
12
+ const result = Object.entries(styleObject).reduce((current, _ref) => {
13
+ let [key, value] = _ref;
14
+ if (typeof value === 'object') {
15
+ Object.entries(value).forEach(_ref2 => {
16
+ let [variantKey, variantValue] = _ref2;
17
+ if (!current[variantKey]) {
18
+ current[variantKey] = {};
19
+ }
20
+ current[variantKey][key] = variantValue;
21
+ });
22
+ } else {
23
+ current.default || (current.default = {});
24
+ current.default[key] = value;
25
+ }
26
+ return current;
27
+ }, {});
28
+ return _reactNative.StyleSheet.create(result);
29
+ }
30
+ const create = args => {
31
+ return Object.entries(args).reduce((current, _ref3) => {
32
+ let [key, value] = _ref3;
33
+ current[key] = bundleStyleSheet(value);
34
+ return current;
35
+ }, {});
36
+ };
37
+ exports.create = create;
38
+ const mix = (target, variantArgs) => {
39
+ if (!target) return [];
40
+ const [_target, config] = Array.isArray(target) ? target : [target, {}];
41
+ let results = _target.default ? [_target.default] : [];
42
+ if (config.theme) {
43
+ const themeSheet = (0, _theme.resolveTheme)(_target, config.theme);
44
+ themeSheet && results.push(themeSheet);
45
+ }
46
+ if (variantArgs) {
47
+ results = [...results, ...(0, _variant.variants)(_target, variantArgs)];
48
+ }
49
+ if (config.media) {
50
+ results = [...results, ...(0, _media.media)(_target, config.media)];
51
+ }
52
+ return results;
53
+ };
54
+ exports.mix = mix;
55
+ const props = function () {
56
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
57
+ args[_key] = arguments[_key];
58
+ }
59
+ return {
60
+ style: args.reduce((acc, arg) => {
61
+ var _default;
62
+ if (!arg) return acc;
63
+ if (Array.isArray(arg)) {
64
+ return [...acc, ...arg];
65
+ }
66
+ const xStyleBase = (_default = arg.default) !== null && _default !== void 0 ? _default : arg;
67
+ return [...acc, xStyleBase];
68
+ }, [])
69
+ };
70
+ };
71
+ exports.props = props;
72
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_reactNative","require","_media","_variant","_theme","bundleStyleSheet","styleObject","result","Object","entries","reduce","current","_ref","key","value","forEach","_ref2","variantKey","variantValue","default","StyleSheet","create","args","_ref3","exports","mix","target","variantArgs","_target","config","Array","isArray","results","theme","themeSheet","resolveTheme","push","variants","media","props","_len","arguments","length","_key","style","acc","arg","_default","xStyleBase"],"sources":["base.ts"],"sourcesContent":["import { StyleSheet } from 'react-native';\nimport {\n XRNStyleSheets,\n XRNStyle,\n NamedStyles,\n RNStyle,\n VariantStyleSheet,\n} from './types';\nimport { media } from './media';\nimport { variants } from './variant';\nimport { resolveTheme } from './theme';\n\nfunction bundleStyleSheet<S extends RNStyle>(styleObject: XRNStyle<S>) {\n const result = Object.entries(styleObject).reduce((current, [key, value]) => {\n if (typeof value === 'object') {\n Object.entries(value).forEach(([variantKey, variantValue]) => {\n if (!current[variantKey]) {\n current[variantKey] = {};\n }\n (current[variantKey] as any)[key] = variantValue;\n });\n } else {\n current.default ||= {};\n (current.default as any)[key] = value;\n }\n return current;\n }, {} as Record<string, RNStyle>);\n return StyleSheet.create(result);\n}\n\nexport const create = <T extends NamedStyles<any, R>, R extends RNStyle>(\n args: T\n): XRNStyleSheets<T, R> => {\n return Object.entries(args).reduce((current, [key, value]) => {\n (current as any)[key] = bundleStyleSheet(value);\n return current;\n }, {} as XRNStyleSheets<T, R>);\n};\n\nexport const mix = <\n Variants extends { [key: string]: string | number | boolean | undefined },\n Theme extends string = string,\n T extends VariantStyleSheet<string, RNStyle> = VariantStyleSheet<\n string,\n RNStyle\n >\n>(\n target?: T | [T, { theme?: Theme; media?: number | string }],\n variantArgs?: Variants\n): RNStyle[] => {\n if (!target) return [];\n const [_target, config] = Array.isArray(target) ? target : [target as T, {}];\n let results = _target.default ? [_target.default] : ([] as RNStyle[]);\n if (config.theme) {\n const themeSheet = resolveTheme(_target, config.theme);\n themeSheet && results.push(themeSheet);\n }\n if (variantArgs) {\n results = [...results, ...variants(_target, variantArgs)];\n }\n if (config.media) {\n results = [...results, ...media(_target, config.media)];\n }\n return results;\n};\n\ntype PropValue = VariantStyleSheet<string, RNStyle> | RNStyle;\nexport const props = <T extends RNStyle>(\n ...args: (PropValue | RNStyle[] | false | null | undefined)[]\n): { style: T[] } => {\n return {\n style: args.reduce((acc: T[], arg) => {\n if (!arg) return acc;\n if (Array.isArray(arg)) {\n return [...acc, ...arg] as T[];\n }\n const xStyleBase =\n (arg as VariantStyleSheet<string, RNStyle>).default ?? arg;\n return [...acc, xStyleBase] as T[];\n }, []) as T[],\n };\n};\n"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAQA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,QAAA,GAAAF,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AAEA,SAASI,gBAAgBA,CAAoBC,WAAwB,EAAE;EACrE,MAAMC,MAAM,GAAGC,MAAM,CAACC,OAAO,CAACH,WAAW,CAAC,CAACI,MAAM,CAAC,CAACC,OAAO,EAAAC,IAAA,KAAmB;IAAA,IAAjB,CAACC,GAAG,EAAEC,KAAK,CAAC,GAAAF,IAAA;IACtE,IAAI,OAAOE,KAAK,KAAK,QAAQ,EAAE;MAC7BN,MAAM,CAACC,OAAO,CAACK,KAAK,CAAC,CAACC,OAAO,CAACC,KAAA,IAAgC;QAAA,IAA/B,CAACC,UAAU,EAAEC,YAAY,CAAC,GAAAF,KAAA;QACvD,IAAI,CAACL,OAAO,CAACM,UAAU,CAAC,EAAE;UACxBN,OAAO,CAACM,UAAU,CAAC,GAAG,CAAC,CAAC;QAC1B;QACCN,OAAO,CAACM,UAAU,CAAC,CAASJ,GAAG,CAAC,GAAGK,YAAY;MAClD,CAAC,CAAC;IACJ,CAAC,MAAM;MACLP,OAAO,CAACQ,OAAO,KAAfR,OAAO,CAACQ,OAAO,GAAK,CAAC,CAAC;MACrBR,OAAO,CAACQ,OAAO,CAASN,GAAG,CAAC,GAAGC,KAAK;IACvC;IACA,OAAOH,OAAO;EAChB,CAAC,EAAE,CAAC,CAA4B,CAAC;EACjC,OAAOS,uBAAU,CAACC,MAAM,CAACd,MAAM,CAAC;AAClC;AAEO,MAAMc,MAAM,GACjBC,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AAMc,KAAK,GAAG,SAAAA,CAAA,EAEA;EAAA,SAAAC,IAAA,GAAAC,SAAA,CAAAC,MAAA,EADhBpB,IAAI,OAAAQ,KAAA,CAAAU,IAAA,GAAAG,IAAA,MAAAA,IAAA,GAAAH,IAAA,EAAAG,IAAA;IAAJrB,IAAI,CAAAqB,IAAA,IAAAF,SAAA,CAAAE,IAAA;EAAA;EAEP,OAAO;IACLC,KAAK,EAAEtB,IAAI,CAACZ,MAAM,CAAC,CAACmC,GAAQ,EAAEC,GAAG,KAAK;MAAA,IAAAC,QAAA;MACpC,IAAI,CAACD,GAAG,EAAE,OAAOD,GAAG;MACpB,IAAIf,KAAK,CAACC,OAAO,CAACe,GAAG,CAAC,EAAE;QACtB,OAAO,CAAC,GAAGD,GAAG,EAAE,GAAGC,GAAG,CAAC;MACzB;MACA,MAAME,UAAU,IAAAD,QAAA,GACbD,GAAG,CAAwC3B,OAAO,cAAA4B,QAAA,cAAAA,QAAA,GAAID,GAAG;MAC5D,OAAO,CAAC,GAAGD,GAAG,EAAEG,UAAU,CAAC;IAC7B,CAAC,EAAE,EAAE;EACP,CAAC;AACH,CAAC;AAACxB,OAAA,CAAAe,KAAA,GAAAA,KAAA","ignoreList":[]}
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useStylex = exports.RNStyleXProvider = void 0;
7
+ var _react = require("react");
8
+ var _reactNative = require("react-native");
9
+ var _base = require("./base");
10
+ var _media = require("./media");
11
+ const RNStyleXContext = /*#__PURE__*/(0, _react.createContext)(undefined);
12
+ const RNStyleXProvider = componentProps => {
13
+ var _componentProps$windo, _componentProps$theme;
14
+ const {
15
+ width
16
+ } = (0, _reactNative.useWindowDimensions)();
17
+ const correctedWidth = (_componentProps$windo = componentProps.windowWidth) !== null && _componentProps$windo !== void 0 ? _componentProps$windo : _reactNative.PixelRatio.getPixelSizeForLayoutSize(width);
18
+ const theme = (_componentProps$theme = componentProps.theme) !== null && _componentProps$theme !== void 0 ? _componentProps$theme : 'default';
19
+ const value = (0, _react.useMemo)(() => {
20
+ const media = componentProps.media && (0, _media.detectMedia)(componentProps.media, correctedWidth);
21
+ return {
22
+ windowWidth: correctedWidth,
23
+ theme,
24
+ props() {
25
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
26
+ args[_key] = arguments[_key];
27
+ }
28
+ const sheets = args.map(v => Array.isArray(v) ? v : v && this.mix(v));
29
+ return (0, _base.props)(...sheets);
30
+ },
31
+ mix(arg, variants) {
32
+ if (!arg) return [];
33
+ const config = {
34
+ theme,
35
+ media: media !== null && media !== void 0 ? media : this.windowWidth
36
+ };
37
+ if (Array.isArray(arg)) {
38
+ return (0, _base.mix)([arg[0], {
39
+ ...config,
40
+ ...arg[1]
41
+ }], variants);
42
+ }
43
+ return (0, _base.mix)([arg, config], variants);
44
+ }
45
+ };
46
+ }, [correctedWidth, theme]);
47
+ return /*#__PURE__*/(0, _react.createElement)(RNStyleXContext.Provider, {
48
+ value
49
+ }, _react.Children.only(componentProps.children));
50
+ };
51
+ exports.RNStyleXProvider = RNStyleXProvider;
52
+ const useStylex = () => {
53
+ const context = (0, _react.useContext)(RNStyleXContext);
54
+ if (!context) {
55
+ throw new Error('useStylex must be used within a RNStyleXProvider');
56
+ }
57
+ return context;
58
+ };
59
+ exports.useStylex = useStylex;
60
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_react","require","_reactNative","_base","_media","RNStyleXContext","createContext","undefined","RNStyleXProvider","componentProps","_componentProps$windo","_componentProps$theme","width","useWindowDimensions","correctedWidth","windowWidth","PixelRatio","getPixelSizeForLayoutSize","theme","value","useMemo","media","detectMedia","props","_len","arguments","length","args","Array","_key","sheets","map","v","isArray","mix","arg","variants","config","createElement","Provider","Children","only","children","exports","useStylex","context","useContext","Error"],"sources":["hooks.ts"],"sourcesContent":["import {\n Children,\n createContext,\n createElement,\n useContext,\n useMemo,\n} from 'react';\nimport { PixelRatio, useWindowDimensions } from 'react-native';\nimport { mix, props } from './base';\nimport { RNStyle, VariantStyleSheet } from './types';\nimport { detectMedia } from './media';\n\ntype IApi = {\n props: typeof props;\n mix: typeof mix;\n};\n\ntype UserConfig = {\n windowWidth: number;\n theme: string;\n};\n\ntype ValueType = UserConfig & IApi;\n\nconst RNStyleXContext = createContext<ValueType | undefined>(undefined);\n\nexport const RNStyleXProvider = (\n componentProps: Partial<Pick<ValueType, 'windowWidth' | 'theme'>> & {\n media?: Record<string, string>;\n children: React.ReactNode;\n }\n) => {\n const { width } = useWindowDimensions();\n const correctedWidth =\n componentProps.windowWidth ?? PixelRatio.getPixelSizeForLayoutSize(width);\n const theme = componentProps.theme ?? 'default';\n\n const value = useMemo<ValueType>(() => {\n const media = componentProps.media && detectMedia(componentProps.media, correctedWidth);\n return {\n windowWidth: correctedWidth,\n theme,\n props(...args) {\n const sheets = args.map((v) =>\n Array.isArray(v)\n ? v\n : v && this.mix(v as VariantStyleSheet<string, RNStyle>)\n );\n return props(...sheets);\n },\n mix(arg, variants) {\n if (!arg) return [];\n const config = {\n theme,\n media: media ?? this.windowWidth,\n };\n if (Array.isArray(arg)) {\n return mix([arg[0], { ...config, ...arg[1] }], variants);\n }\n return mix([arg, config], variants);\n },\n };\n }, [correctedWidth, theme]);\n return createElement(\n RNStyleXContext.Provider,\n { value },\n Children.only(componentProps.children)\n );\n};\n\nexport const useStylex = () => {\n const context = useContext(RNStyleXContext);\n if (!context) {\n throw new Error('useStylex must be used within a RNStyleXProvider');\n }\n return context;\n};\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAOA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AAEA,IAAAG,MAAA,GAAAH,OAAA;AAcA,MAAMI,eAAe,gBAAG,IAAAC,oBAAa,EAAwBC,SAAS,CAAC;AAEhE,MAAMC,gBAAgB,GAC3BC,cAGC,IACE;E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AACK,OAAO,CAACE,GAAG,CAAC,EAAE;UACtB,OAAO,IAAAD,SAAG,EAAC,CAACC,GAAG,CAAC,CAAC,CAAC,EAAE;YAAE,GAAGE,MAAM;YAAE,GAAGF,GAAG,CAAC,CAAC;UAAE,CAAC,CAAC,EAAEC,QAAQ,CAAC;QAC1D;QACA,OAAO,IAAAF,SAAG,EAAC,CAACC,GAAG,EAAEE,MAAM,CAAC,EAAED,QAAQ,CAAC;MACrC;IACF,CAAC;EACH,CAAC,EAAE,CAACtB,cAAc,EAAEI,KAAK,CAAC,CAAC;EAC3B,oBAAO,IAAAoB,oBAAa,EAClBjC,eAAe,CAACkC,QAAQ,EACxB;IAAEpB;EAAM,CAAC,EACTqB,eAAQ,CAACC,IAAI,CAAChC,cAAc,CAACiC,QAAQ,CACvC,CAAC;AACH,CAAC;AAACC,OAAA,CAAAnC,gBAAA,GAAAA,gBAAA;AAEK,MAAMoC,SAAS,GAAGA,CAAA,KAAM;EAC7B,MAAMC,OAAO,GAAG,IAAAC,iBAAU,EAACzC,eAAe,CAAC;EAC3C,IAAI,CAACwC,OAAO,EAAE;IACZ,MAAM,IAAIE,KAAK,CAAC,kDAAkD,CAAC;EACrE;EACA,OAAOF,OAAO;AAChB,CAAC;AAACF,OAAA,CAAAC,SAAA,GAAAA,SAAA","ignoreList":[]}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.detectMedia = void 0;
7
+ exports.matchMediaRangeQuery = matchMediaRangeQuery;
8
+ exports.media = void 0;
9
+ const media = (target, mediaParam) => {
10
+ const result = [];
11
+ if (typeof mediaParam === 'string') {
12
+ target[mediaParam] && result.push(target[mediaParam]);
13
+ } else if (typeof mediaParam === 'number') {
14
+ const keys = Object.keys(target);
15
+ for (let i = keys.length - 1; i >= 0; i--) {
16
+ const key = keys[i];
17
+ // NOTE: Assuming only one media query will match, we can break after the first match.
18
+ // Because later media query will be more specific and override the previous one.
19
+ if (matchMediaRangeQuery(key, mediaParam)) {
20
+ result.push(target[key]);
21
+ break;
22
+ }
23
+ }
24
+ }
25
+ return result;
26
+ };
27
+
28
+ // NOTE: input mediaKeys
29
+ // - ['(750px <= width < 1080px)', '(width > 750px)']
30
+ exports.media = media;
31
+ const detectMedia = (mediaKeys, width) => {
32
+ const targets = Array.isArray(mediaKeys) ? mediaKeys : Object.values(mediaKeys);
33
+ for (const val of targets) {
34
+ if (matchMediaRangeQuery(val, width)) {
35
+ return val;
36
+ }
37
+ }
38
+ return;
39
+ };
40
+ exports.detectMedia = detectMedia;
41
+ const VALID_SIGNS = ['<=', '<', '>=', '>'];
42
+ function matchMediaRangeQuery(query, windowWidth) {
43
+ const singleRangeRegex = /^\(width\s+([><=]+)\s+([0-9]+)px\)$/;
44
+ const multiRangeRegex = /^\(([0-9]+)px\s([><=]+)\swidth\s+([><=]+)\s+([0-9]+)px\)$/; // prettier-ignore
45
+ const singleRangeMatches = query.match(singleRangeRegex);
46
+ const multiRangeMatches = query.match(multiRangeRegex);
47
+ if (!singleRangeMatches && !multiRangeMatches) return false;
48
+ let result;
49
+ if (multiRangeMatches && multiRangeMatches.length === 5) {
50
+ const [, _width1, sign1, sign2, _width2] = multiRangeMatches;
51
+ const width1 = parseInt(_width1, 10);
52
+ const width2 = parseInt(_width2, 10);
53
+ if (VALID_SIGNS.includes(sign1) && VALID_SIGNS.includes(sign2)) {
54
+ result = eval(`${width1} ${sign1} ${windowWidth} && ${windowWidth} ${sign2} ${width2}`);
55
+ }
56
+ } else if (singleRangeMatches && singleRangeMatches.length === 3) {
57
+ const [, sign, _width] = singleRangeMatches;
58
+ const width = parseInt(_width, 10);
59
+ if (VALID_SIGNS.includes(sign)) {
60
+ result = eval(`${windowWidth} ${sign} ${width}`);
61
+ }
62
+ }
63
+ if (result === undefined) return false;
64
+ if (typeof result !== 'boolean') {
65
+ console.warn(`Unexpected media query result. Expected a boolean but got ${result}. Please make sure your media query syntax is correct.`);
66
+ }
67
+ return result;
68
+ }
69
+ //# sourceMappingURL=media.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["media","target","mediaParam","result","push","keys","Object","i","length","key","matchMediaRangeQuery","exports","detectMedia","mediaKeys","width","targets","Array","isArray","values","val","VALID_SIGNS","query","windowWidth","singleRangeRegex","multiRangeRegex","singleRangeMatches","match","multiRangeMatches","_width1","sign1","sign2","_width2","width1","parseInt","width2","includes","eval","sign","_width","undefined","console","warn"],"sources":["media.ts"],"sourcesContent":["import { RNStyle, VariantStyleSheet } from './types';\nexport type Media = Record<string, string | boolean>;\n\nexport const media = <T extends VariantStyleSheet<string, RNStyle>>(\n target: T,\n mediaParam: number | string\n): T[keyof T][] => {\n const result: T[keyof T][] = [];\n if (typeof mediaParam === 'string') {\n target[mediaParam] && result.push(target[mediaParam] as T[keyof T]);\n } else if (typeof mediaParam === 'number') {\n const keys = Object.keys(target);\n for (let i = keys.length - 1; i >= 0; i--) {\n const key = keys[i];\n // NOTE: Assuming only one media query will match, we can break after the first match.\n // Because later media query will be more specific and override the previous one.\n if (matchMediaRangeQuery(key, mediaParam)) {\n result.push(target[key] as T[keyof T]);\n break;\n }\n }\n }\n return result;\n};\n\n// NOTE: input mediaKeys\n// - ['(750px <= width < 1080px)', '(width > 750px)']\nexport const detectMedia = (\n mediaKeys: string[] | Record<string, string>,\n width: number\n): string | undefined => {\n const targets = Array.isArray(mediaKeys)\n ? mediaKeys\n : Object.values(mediaKeys);\n for (const val of targets) {\n if (matchMediaRangeQuery(val, width)) {\n return val;\n }\n }\n return;\n};\n\nconst VALID_SIGNS = ['<=', '<', '>=', '>'];\n\nexport function matchMediaRangeQuery(\n query: string,\n windowWidth: number\n): boolean {\n const singleRangeRegex = /^\\(width\\s+([><=]+)\\s+([0-9]+)px\\)$/;\n const multiRangeRegex = /^\\(([0-9]+)px\\s([><=]+)\\swidth\\s+([><=]+)\\s+([0-9]+)px\\)$/; // prettier-ignore\n const singleRangeMatches = query.match(singleRangeRegex);\n const multiRangeMatches = query.match(multiRangeRegex);\n\n if (!singleRangeMatches && !multiRangeMatches) return false;\n\n let result;\n\n if (multiRangeMatches && multiRangeMatches.length === 5) {\n const [, _width1, sign1, sign2, _width2] = multiRangeMatches;\n const width1 = parseInt(_width1, 10);\n const width2 = parseInt(_width2, 10);\n\n if (VALID_SIGNS.includes(sign1) && VALID_SIGNS.includes(sign2)) {\n result = eval(\n `${width1} ${sign1} ${windowWidth} && ${windowWidth} ${sign2} ${width2}`\n );\n }\n } else if (singleRangeMatches && singleRangeMatches.length === 3) {\n const [, sign, _width] = singleRangeMatches;\n const width = parseInt(_width, 10);\n\n if (VALID_SIGNS.includes(sign)) {\n result = eval(`${windowWidth} ${sign} ${width}`);\n }\n }\n\n if (result === undefined) return false;\n\n if (typeof result !== 'boolean') {\n console.warn(\n `Unexpected media query result. Expected a boolean but got ${result}. Please make sure your media query syntax is correct.`\n );\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;AAGO,MAAMA,KAAK,GAAGA,CACnBC,MAAS,EACTC,UAA2B,KACV;EACjB,MAAMC,MAAoB,GAAG,EAAE;EAC/B,IAAI,OAAOD,UAAU,KAAK,QAAQ,EAAE;IAClCD,MAAM,CAACC,UAAU,CAAC,IAAIC,MAAM,CAACC,IAAI,CAACH,MAAM,CAACC,UAAU,CAAe,CAAC;EACrE,CAAC,MAAM,IAAI,OAAOA,UAAU,KAAK,QAAQ,EAAE;IACzC,MAAMG,IAAI,GAAGC,MAAM,CAACD,IAAI,CAACJ,MAAM,CAAC;IAChC,KAAK,IAAIM,CAAC,GAAGF,IAAI,CAACG,MAAM,GAAG,CAAC,EAAED,CAAC,IAAI,CAAC,EAAEA,CAAC,EAAE,EAAE;MACzC,MAAME,GAAG,GAAGJ,IAAI,CAACE,CAAC,CAAC;MACnB;MACA;MACA,IAAIG,oBAAoB,CAACD,GAAG,EAAEP,UAAU,CAAC,EAAE;QACzCC,MAAM,CAACC,IAAI,CAACH,MAAM,CAACQ,GAAG,CAAe,CAAC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N,KAAK,CAAC,IAAIT,WAAW,CAACe,QAAQ,CAACL,KAAK,CAAC,EAAE;MAC9D3B,MAAM,GAAGiC,IAAI,CACX,GAAGJ,MAAM,IAAIH,KAAK,IAAIP,WAAW,OAAOA,WAAW,IAAIQ,KAAK,IAAII,MAAM,EACxE,CAAC;IACH;EACF,CAAC,MAAM,IAAIT,kBAAkB,IAAIA,kBAAkB,CAACjB,MAAM,KAAK,CAAC,EAAE;IAChE,MAAM,GAAG6B,IAAI,EAAEC,MAAM,CAAC,GAAGb,kBAAkB;IAC3C,MAAMX,KAAK,GAAGmB,QAAQ,CAACK,MAAM,EAAE,EAAE,CAAC;IAElC,IAAIlB,WAAW,CAACe,QAAQ,CAACE,IAAI,CAAC,EAAE;MAC9BlC,MAAM,GAAGiC,IAAI,CAAC,GAAGd,WAAW,IAAIe,IAAI,IAAIvB,KAAK,EAAE,CAAC;IAClD;EACF;EAEA,IAAIX,MAAM,KAAKoC,SAAS,EAAE,OAAO,KAAK;EAEtC,IAAI,OAAOpC,MAAM,KAAK,SAAS,EAAE;IAC/BqC,OAAO,CAACC,IAAI,CACV,6DAA6DtC,MAAM,wDACrE,CAAC;EACH;EAEA,OAAOA,MAAM;AACf","ignoreList":[]}
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createThemes = createThemes;
7
+ exports.resolveTheme = exports.getThemeKey = void 0;
8
+ var _variant = require("./variant");
9
+ const getThemeKey = theme => (0, _variant.createVariantKey)('@theme', theme);
10
+ exports.getThemeKey = getThemeKey;
11
+ const resolveTheme = (target, themeKey) => target[themeKey];
12
+ exports.resolveTheme = resolveTheme;
13
+ function createThemes(themes) {
14
+ if (new Set(themes).size !== themes.length) {
15
+ throw new Error('Themes must be unique');
16
+ }
17
+ const themesObj = themes.reduce((current, theme) => {
18
+ current[theme] = getThemeKey(theme);
19
+ return current;
20
+ }, {});
21
+ return {
22
+ themes: themesObj
23
+ };
24
+ }
25
+ //# sourceMappingURL=theme.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["_variant","require","getThemeKey","theme","createVariantKey","exports","resolveTheme","target","themeKey","createThemes","themes","Set","size","length","Error","themesObj","reduce","current"],"sources":["theme.ts"],"sourcesContent":["import { RNStyle, VariantStyleSheet } from './types';\nimport { createVariantKey } from './variant';\n\nexport const getThemeKey = (theme: string) => createVariantKey('@theme', theme);\n\nexport const resolveTheme = <\n T extends VariantStyleSheet<any, RNStyle> = VariantStyleSheet<any, any>\n>(\n target: T,\n themeKey: string\n) => target[themeKey];\n\ntype AllowNoKeyAccessObject<R extends Record<string, any>> = {\n [K in keyof R]: R[K];\n} & { [key: string]: undefined };\n\nexport function createThemes<\n const T extends string[]\n>(\n themes: T\n): {\n themes: AllowNoKeyAccessObject<{\n [K in T[number]]: string\n }>\n} {\n if (new Set(themes).size !== themes.length) {\n throw new Error('Themes must be unique');\n }\n const themesObj = themes.reduce((current, theme) => {\n current[theme as T[number]] = getThemeKey(theme);\n return current;\n }, {} as { [K in T[number]]: string });\n return {\n themes: themesObj as AllowNoKeyAccessObject<{\n [K in T[number]]: string\n }>,\n };\n}\n"],"mappings":";;;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AAEO,MAAMC,WAAW,GAAIC,KAAa,IAAK,IAAAC,yBAAgB,EAAC,QAAQ,EAAED,KAAK,CAAC;AAACE,OAAA,CAAAH,WAAA,GAAAA,WAAA;AAEzE,MAAMI,YAAY,GAAGA,CAG1BC,MAAS,EACTC,QAAgB,KACbD,MAAM,CAACC,QAAQ,CAAC;AAACH,OAAA,CAAAC,YAAA,GAAAA,YAAA;AAMf,SAASG,YAAYA,CAG1BC,MAAS,EAKT;EACA,IAAI,IAAIC,GAAG,CAACD,MAAM,CAAC,CAACE,IAAI,KAAKF,MAAM,CAACG,MAAM,EAAE;IAC1C,MAAM,IAAIC,KAAK,CAAC,uBAAuB,CAAC;EAC1C;EACA,MAAMC,SAAS,GAAGL,MAAM,CAACM,MAAM,CAAC,CAACC,OAAO,EAAEd,KAAK,KAAK;IAClDc,OAAO,CAACd,KAAK,CAAc,GAAGD,WAAW,CAACC,KAAK,CAAC;IAChD,OAAOc,OAAO;EAChB,CAAC,EAAE,CAAC,CAAiC,CAAC;EACtC,OAAO;IACLP,MAAM,EAAEK;EAGV,CAAC;AACH","ignoreList":[]}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.defineConsts = defineConsts;
7
+ exports.defineVars = defineVars;
8
+ function defineVars(defaults) {
9
+ return Object.freeze({
10
+ ...defaults
11
+ });
12
+ }
13
+
14
+ // NOTE: RNでは、build前compileがないので、defineConstsはdefineVarsのAliasとして扱う
15
+ function defineConsts(consts) {
16
+ return defineVars(consts);
17
+ }
18
+ //# sourceMappingURL=tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["defineVars","defaults","Object","freeze","defineConsts","consts"],"sources":["tokens.ts"],"sourcesContent":["import { VariantStyle } from './types';\n\nexport function defineVars<\n T extends Record<string, string | number | VariantStyle<string | number>>\n>(defaults: T): Readonly<T> {\n return Object.freeze({ ...defaults });\n}\n\n// NOTE: RNでは、build前compileがないので、defineConstsはdefineVarsのAliasとして扱う\nexport function defineConsts<\n T extends Record<string, string | number | VariantStyle<string | number>>\n>(consts: T): Readonly<T> {\n return defineVars(consts);\n}\n"],"mappings":";;;;;;;AAEO,SAASA,UAAUA,CAExBC,QAAW,EAAe;EAC1B,OAAOC,MAAM,CAACC,MAAM,CAAC;IAAE,GAAGF;EAAS,CAAC,CAAC;AACvC;;AAEA;AACO,SAASG,YAAYA,CAE1BC,MAAS,EAAe;EACxB,OAAOL,UAAU,CAACK,MAAM,CAAC;AAC3B","ignoreList":[]}
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":[],"sources":["types.ts"],"sourcesContent":["import type { StyleSheet } from 'react-native';\n\nexport type StyleObject = StyleSheet.NamedStyles<any>;\nexport type RNStyle = StyleObject[string];\n\nexport type VariantStyle<S> = { [key: string]: S };\nexport type VariantStyleSheet<Key extends string, S extends RNStyle> = {\n [key in Key]: S;\n};\n\nexport type XRNStyle<S extends RNStyle = RNStyle> = {\n [key in keyof S]: VariantStyle<S[key]> | S[key];\n};\n\ntype ExtractVariantKeys<\n X extends XRNStyle<S>,\n S extends RNStyle = RNStyle\n> = Extract<keyof Extract<X[keyof X], VariantStyle<any>>, string>;\n\nexport type NamedStyles<T = string, R extends RNStyle = RNStyle> = {\n [P in keyof T]: XRNStyle<R>;\n};\n\nexport type XRNStyleSheets<\n X extends NamedStyles<any, S>,\n S extends RNStyle = RNStyle\n> = {\n [key in keyof X]: VariantStyleSheet<ExtractVariantKeys<X[key]>, S>;\n};\n\ntype ExtractPostFix<T extends string> = T extends `@${string}_${infer PostFix}`\n ? PostFix\n : 'default';\n\n// NOTE: This map for keyof (X | Y) = (keyof X & keyof Y) != (keyof X | keyof Y).\n// different object value is composed to union type and keyof is applied later\n// without explicit individual object evaluation by this utility type.\ntype KeysOfUnion<T> = T extends T ? keyof T : never;\n\nexport type Variants<\n A extends Record<string, Record<string, Record<string, any>>>\n> = {\n [key in keyof A]?:\n | number\n | boolean\n | ExtractPostFix<Extract<KeysOfUnion<A[key][keyof A[key]]>, string>>;\n};\n\nexport type VariantValue<S extends RNStyle = RNStyle> = {\n [key in keyof S]: VariantStyle<S[key]>;\n}; // Style Fields\n"],"mappings":"","ignoreList":[]}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.variants = exports.createVariants = exports.createVariantValue = exports.createVariantKey = void 0;
7
+ const createVariantKey = (variantKey, key) => `@${variantKey}_${key}`;
8
+ exports.createVariantKey = createVariantKey;
9
+ const createVariantValue = (variantKey, args) => {
10
+ return Object.entries(args).reduce((current, _ref) => {
11
+ let [key, value] = _ref;
12
+ if (key === 'default') {
13
+ current.default = value;
14
+ } else {
15
+ current[createVariantKey(variantKey, key)] = value;
16
+ }
17
+ return current;
18
+ }, {});
19
+ };
20
+ exports.createVariantValue = createVariantValue;
21
+ /**
22
+ * INPUT;
23
+ * {
24
+ * shape: {
25
+ * borderRadius: {
26
+ * default: 4,
27
+ * round: 8,
28
+ * square: 0,
29
+ * },
30
+ * fontSize: {
31
+ * default: 14,
32
+ * round: 16,
33
+ * square: 18,
34
+ * }
35
+ * }
36
+ * }
37
+ * OUTPUT;
38
+ * {
39
+ * shape: {
40
+ * borderRadius: {
41
+ * default: 4,
42
+ * @shape_round: 8,
43
+ * @shape_square: 0,
44
+ * },
45
+ * fontSize: {
46
+ * default: 14,
47
+ * @shape_round: 16,
48
+ * @shape_square: 18,
49
+ * }
50
+ * }
51
+ * }
52
+ **/
53
+ const createVariants = args => {
54
+ return Object.entries(args).reduce((current, _ref2) => {
55
+ let [variantKey, value] = _ref2;
56
+ current[variantKey] || (current[variantKey] = {});
57
+ Object.entries(value).forEach(_ref3 => {
58
+ let [styleField, variantValue] = _ref3;
59
+ current[variantKey][styleField] = createVariantValue(variantKey, variantValue);
60
+ });
61
+ return current;
62
+ }, {});
63
+ };
64
+ exports.createVariants = createVariants;
65
+ const variants = (target, variants) => {
66
+ const results = [];
67
+ for (const [variantKey, variantValue] of Object.entries(variants)) {
68
+ if (variantValue === 'default') continue;
69
+ const nextStyle = target[`@${variantKey}_${variantValue}`];
70
+ nextStyle && results.push(nextStyle);
71
+ }
72
+ return results;
73
+ };
74
+ exports.variants = variants;
75
+ //# sourceMappingURL=variant.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["createVariantKey","variantKey","key","exports","createVariantValue","args","Object","entries","reduce","current","_ref","value","default","createVariants","_ref2","forEach","_ref3","styleField","variantValue","variants","target","results","nextStyle","push"],"sources":["variant.ts"],"sourcesContent":["import type {\n RNStyle,\n VariantStyle,\n VariantStyleSheet,\n VariantValue,\n} from './types';\n\ntype CreateVariantValueOutput<\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n K extends string,\n A extends VariantStyle<any>\n> = {\n [key in keyof A as key extends 'default'\n ? 'default'\n : `@${K}_${key & string}`]: A[key];\n};\n\nexport const createVariantKey = (variantKey: string, key: string) =>\n `@${variantKey}_${key}`;\n\nexport const createVariantValue = <\n K extends string,\n A extends VariantStyle<any>\n>(\n variantKey: K,\n args: A\n): CreateVariantValueOutput<K, A> => {\n return Object.entries(args).reduce((current, [key, value]) => {\n if (key === 'default') {\n (current as any).default = value;\n } else {\n (current as any)[createVariantKey(variantKey, key)] = value;\n }\n return current;\n }, {} as CreateVariantValueOutput<K, A>);\n};\n\ntype VariantArg<S extends RNStyle = RNStyle> = Record<\n string, // variantKey\n VariantValue<S>\n>;\n\ntype VariantsOutput<\n A extends Record<string, Record<string, Record<string, any>>>\n> = {\n [key in Extract<keyof A, string>]: {\n [subKey in keyof A[key]]: CreateVariantValueOutput<key, A[key][subKey]>;\n };\n};\n\n/**\n * INPUT;\n * {\n * shape: {\n * borderRadius: {\n * default: 4,\n * round: 8,\n * square: 0,\n * },\n * fontSize: {\n * default: 14,\n * round: 16,\n * square: 18,\n * }\n * }\n * }\n * OUTPUT;\n * {\n * shape: {\n * borderRadius: {\n * default: 4,\n * @shape_round: 8,\n * @shape_square: 0,\n * },\n * fontSize: {\n * default: 14,\n * @shape_round: 16,\n * @shape_square: 18,\n * }\n * }\n * }\n **/\nexport const createVariants = <A extends VariantArg>(\n args: A\n): VariantsOutput<A> => {\n return Object.entries(args).reduce((current, [variantKey, value]) => {\n current[variantKey] ||= {};\n Object.entries(value).forEach(([styleField, variantValue]) => {\n current[variantKey][styleField] = createVariantValue(\n variantKey,\n variantValue\n );\n });\n return current;\n }, {} as Record<string, any>) as VariantsOutput<A>;\n};\n\nexport const variants = <\n V extends { [key: string]: string | number | boolean | undefined },\n T extends VariantStyleSheet<any, RNStyle> = VariantStyleSheet<any, any>\n>(\n target: T,\n variants: V\n): T[keyof T][] => {\n const results = [] as T[keyof T][];\n for (const [variantKey, variantValue] of Object.entries(variants)) {\n if (variantValue === 'default') continue;\n const nextStyle = target[`@${variantKey}_${variantValue}`];\n nextStyle && results.push(nextStyle as T[keyof T]);\n }\n return results;\n};\n"],"mappings":";;;;;;AAiBO,MAAMA,gBAAgB,GAAGA,CAACC,UAAkB,EAAEC,GAAW,KAC9D,IAAID,UAAU,IAAIC,GAAG,EAAE;AAACC,OAAA,CAAAH,gBAAA,GAAAA,gBAAA;AAEnB,MAAMI,kBAAkB,GAAGA,CAIhCH,UAAa,EACbI,IAAO,KAC4B;EACnC,OAAOC,MAAM,CAACC,OAAO,CAACF,IAAI,CAAC,CAACG,MAAM,CAAC,CAACC,OAAO,EAAAC,IAAA,KAAmB;IAAA,IAAjB,CAACR,GAAG,EAAES,KAAK,CAAC,GAAAD,IAAA;IACvD,IAAIR,GAAG,KAAK,SAAS,EAAE;MACpBO,OAAO,CAASG,OAAO,GAAGD,KAAK;IAClC,CAAC,MAAM;MACJF,OAAO,CAAST,gBAAgB,CAACC,UAAU,EAAEC,GAAG,CAAC,CAAC,GAAGS,KAAK;IAC7D;IACA,OAAOF,OAAO;EAChB,CAAC,EAAE,CAAC,CAAmC,CAAC;AAC1C,CAAC;AAACN,OAAA,CAAAC,kBAAA,GAAAA,kBAAA;AAeF;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMS,cAAc,GACzBR,IAAO,IACe;EACtB,OAAOC,MAAM,CAACC,OAAO,CAACF,IAAI,CAAC,CAACG,MAAM,CAAC,CAACC,OAAO,EAAAK,KAAA,KAA0B;IAAA,IAAxB,CAACb,UAAU,EAAEU,KAAK,CAAC,GAAAG,KAAA;IAC9DL,OAAO,CAACR,UAAU,CAAC,KAAnBQ,OAAO,CAACR,UAAU,CAAC,GAAK,CAAC,CAAC;IAC1BK,MAAM,CAACC,OAAO,CAACI,KAAK,CAAC,CAACI,OAAO,CAACC,KAAA,IAAgC;MAAA,IAA/B,CAACC,UAAU,EAAEC,YAAY,CAAC,GAAAF,KAAA;MACvDP,OAAO,CAACR,UAAU,CAAC,CAACgB,UAAU,CAAC,GAAGb,kBAAkB,CAClDH,UAAU,EACViB,YACF,CAAC;IACH,CAAC,CAAC;IACF,OAAOT,OAAO;EAChB,CAAC,EAAE,CAAC,CAAwB,CAAC;AAC/B,CAAC;AAACN,OAAA,CAAAU,cAAA,GAAAA,cAAA;AAEK,MAAMM,QAAQ,GAAGA,CAItBC,MAAS,EACTD,QAAW,KACM;EACjB,MAAME,OAAO,GAAG,EAAkB;EAClC,KAAK,MAAM,CAACpB,UAAU,EAAEiB,YAAY,CAAC,IAAIZ,MAAM,CAACC,OAAO,CAACY,QAAQ,CAAC,EAAE;IACjE,IAAID,YAAY,KAAK,SAAS,EAAE;IAChC,MAAMI,SAAS,GAAGF,MAAM,CAAC,IAAInB,UAAU,IAAIiB,YAAY,EAAE,CAAC;IAC1DI,SAAS,IAAID,OAAO,CAACE,IAAI,CAACD,SAAuB,CAAC;EACpD;EACA,OAAOD,OAAO;AAChB,CAAC;AAAClB,OAAA,CAAAgB,QAAA,GAAAA,QAAA","ignoreList":[]}
@@ -0,0 +1,6 @@
1
+ export { create, props, mix } from './utils/base';
2
+ export { createVariants } from './utils/variant';
3
+ export { defineConsts, defineVars } from './utils/tokens';
4
+ export { useStylex, RNStyleXProvider } from './utils/hooks';
5
+ export { createThemes } from './utils/theme';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["create","props","mix","createVariants","defineConsts","defineVars","useStylex","RNStyleXProvider","createThemes"],"sources":["index.ts"],"sourcesContent":["export { create, props, mix } from './utils/base';\nexport { createVariants } from './utils/variant';\nexport { defineConsts, defineVars } from './utils/tokens';\nexport { useStylex, RNStyleXProvider } from './utils/hooks';\nexport { createThemes } from './utils/theme';\nexport type {\n Variants,\n XRNStyle,\n RNStyle,\n XRNStyleSheets,\n} from './utils/types';\n"],"mappings":"AAAA,SAASA,MAAM,EAAEC,KAAK,EAAEC,GAAG,QAAQ,cAAc;AACjD,SAASC,cAAc,QAAQ,iBAAiB;AAChD,SAASC,YAAY,EAAEC,UAAU,QAAQ,gBAAgB;AACzD,SAASC,SAAS,EAAEC,gBAAgB,QAAQ,eAAe;AAC3D,SAASC,YAAY,QAAQ,eAAe","ignoreList":[]}
@@ -0,0 +1,220 @@
1
+ import { render } from '@testing-library/react-native';
2
+ import React from 'react';
3
+ import { StyleSheet, View } from 'react-native';
4
+ import * as stylex from '../';
5
+ import { finalStyle, reduceStyles } from './test-utils';
6
+
7
+ // ---------------------------------------------------------------------------
8
+ // Basic
9
+ // ---------------------------------------------------------------------------
10
+
11
+ describe('Basic', () => {
12
+ it('create() + props() returns base styles', () => {
13
+ const styles = stylex.create({
14
+ view: {
15
+ backgroundColor: 'red',
16
+ height: 100,
17
+ width: 100
18
+ }
19
+ });
20
+ const {
21
+ style
22
+ } = stylex.props(styles.view);
23
+ expect(style[0]).toMatchObject({
24
+ backgroundColor: 'red',
25
+ height: 100,
26
+ width: 100
27
+ });
28
+ });
29
+ it('stylex.props() renders base styles', () => {
30
+ var _toJSON;
31
+ const styles = stylex.create({
32
+ view: {
33
+ backgroundColor: 'red',
34
+ height: 100,
35
+ width: 100
36
+ },
37
+ view2: {
38
+ height: 200,
39
+ width: 200
40
+ }
41
+ });
42
+ function Comp() {
43
+ return /*#__PURE__*/React.createElement(View, stylex.props(styles.view, styles.view2));
44
+ }
45
+ const {
46
+ toJSON
47
+ } = render(/*#__PURE__*/React.createElement(Comp, null));
48
+ expect((_toJSON = toJSON()) === null || _toJSON === void 0 ? void 0 : _toJSON.props.style[1]).toMatchObject({
49
+ height: 200,
50
+ width: 200
51
+ });
52
+ });
53
+ it('stylex.props() has backward compatibility on StyleSheet', () => {
54
+ const sameArgs = {
55
+ view: {
56
+ backgroundColor: 'red',
57
+ height: 100,
58
+ width: 100
59
+ },
60
+ view2: {
61
+ height: 200,
62
+ width: 200
63
+ }
64
+ };
65
+ const styles = stylex.create(sameArgs);
66
+ const orgStyles = StyleSheet.create(sameArgs);
67
+ expect(stylex.props(styles.view, styles.view2).style).toMatchObject([orgStyles.view, orgStyles.view2]);
68
+ });
69
+ });
70
+
71
+ // ---------------------------------------------------------------------------
72
+ // Variants
73
+ // ---------------------------------------------------------------------------
74
+
75
+ describe('Variants', () => {
76
+ it('variants are applied correctly', () => {
77
+ const vars = stylex.defineVars({
78
+ colors: {
79
+ default: 'white',
80
+ primary: 'red',
81
+ secondary: 'blue'
82
+ },
83
+ sizes: {
84
+ default: 10,
85
+ small: 5,
86
+ medium: 15
87
+ }
88
+ });
89
+ const variants = stylex.createVariants({
90
+ var1: {
91
+ backgroundColor: vars.colors
92
+ },
93
+ var2: {
94
+ width: vars.sizes
95
+ }
96
+ });
97
+ const styles = stylex.create({
98
+ view: {
99
+ backgroundColor: variants.var1.backgroundColor,
100
+ width: variants.var2.width
101
+ }
102
+ });
103
+ expect(stylex.props(styles.view).style[0]).toMatchObject({
104
+ backgroundColor: 'white',
105
+ width: 10
106
+ });
107
+ expect(stylex.props(styles.view).style.length).toBe(1);
108
+ expect(stylex.props(stylex.mix(styles.view, {
109
+ var1: 'primary',
110
+ var2: 'medium'
111
+ })).style).toMatchObject([{
112
+ backgroundColor: 'white',
113
+ width: 10
114
+ }, {
115
+ backgroundColor: 'red'
116
+ }, {
117
+ width: 15
118
+ }]);
119
+ });
120
+ it('multiple style have same variant value is applied correctly', () => {
121
+ const variants = stylex.createVariants({
122
+ shape: {
123
+ borderRadius: {
124
+ default: 4,
125
+ round: 8,
126
+ square: 0,
127
+ test: 1
128
+ },
129
+ fontSize: {
130
+ default: 14,
131
+ round: 16,
132
+ square: 18
133
+ }
134
+ }
135
+ });
136
+ const styles = stylex.create({
137
+ view: {
138
+ ...variants.shape
139
+ }
140
+ });
141
+ expect(reduceStyles(stylex.props(stylex.mix(styles.view, {
142
+ shape: 'round'
143
+ })).style)).toMatchObject({
144
+ borderRadius: 8,
145
+ fontSize: 16
146
+ });
147
+ expect(reduceStyles(stylex.props(stylex.mix(styles.view, {
148
+ shape: 'test'
149
+ })).style)).toMatchObject({
150
+ borderRadius: 1,
151
+ fontSize: 14
152
+ });
153
+ });
154
+ });
155
+
156
+ // ---------------------------------------------------------------------------
157
+ // Theme queries
158
+ // ---------------------------------------------------------------------------
159
+
160
+ describe('Theme', () => {
161
+ it('theme usage implicitly', () => {
162
+ const {
163
+ themes
164
+ } = stylex.createThemes(['light', 'dark']);
165
+ const styles = stylex.create({
166
+ view: {
167
+ borderBlockColor: {
168
+ default: 'black',
169
+ [themes.light]: 'white',
170
+ [themes.dark]: 'gray'
171
+ }
172
+ }
173
+ });
174
+ function Comp() {
175
+ const sx = stylex.useStylex();
176
+ return /*#__PURE__*/React.createElement(View, sx.props(styles.view));
177
+ }
178
+ const outputStyle = theme => {
179
+ return finalStyle(/*#__PURE__*/React.createElement(stylex.RNStyleXProvider, {
180
+ theme: themes[theme]
181
+ }, /*#__PURE__*/React.createElement(Comp, null)));
182
+ };
183
+ expect(outputStyle('light')).toMatchObject({
184
+ borderBlockColor: 'white'
185
+ });
186
+ expect(outputStyle('dark')).toMatchObject({
187
+ borderBlockColor: 'gray'
188
+ });
189
+ });
190
+ it('theme usage implicitly', () => {
191
+ const {
192
+ themes
193
+ } = stylex.createThemes(['light', 'dark']);
194
+ const styles = stylex.create({
195
+ view: {
196
+ borderBlockColor: {
197
+ default: 'black',
198
+ [themes.light]: 'white',
199
+ [themes.dark]: 'gray'
200
+ }
201
+ }
202
+ });
203
+ function Comp() {
204
+ const sx = stylex.useStylex();
205
+ return /*#__PURE__*/React.createElement(View, sx.props(sx.mix(styles.view)));
206
+ }
207
+ const outputStyle = theme => {
208
+ return finalStyle(/*#__PURE__*/React.createElement(stylex.RNStyleXProvider, {
209
+ theme: themes[theme]
210
+ }, /*#__PURE__*/React.createElement(Comp, null)));
211
+ };
212
+ expect(outputStyle('light')).toMatchObject({
213
+ borderBlockColor: 'white'
214
+ });
215
+ expect(outputStyle('dark')).toMatchObject({
216
+ borderBlockColor: 'gray'
217
+ });
218
+ });
219
+ });
220
+ //# sourceMappingURL=index.test.js.map