@stylexjs/shared 0.2.0-beta.8 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. package/README.md +81 -0
  2. package/lib/common-types.d.ts +50 -0
  3. package/lib/common-types.js.flow +65 -0
  4. package/lib/convert-to-className.d.ts +16 -0
  5. package/lib/convert-to-className.js +17 -26
  6. package/lib/convert-to-className.js.flow +23 -0
  7. package/lib/generate-css-rule.d.ts +17 -0
  8. package/lib/generate-css-rule.js +8 -47
  9. package/lib/generate-css-rule.js.flow +17 -0
  10. package/lib/hash.d.ts +11 -0
  11. package/lib/hash.js +3 -27
  12. package/lib/hash.js.flow +10 -0
  13. package/lib/index.d.ts +42 -85
  14. package/lib/index.js +19 -27
  15. package/lib/index.js.flow +59 -0
  16. package/lib/messages.d.ts +36 -0
  17. package/lib/messages.js +28 -55
  18. package/lib/messages.js.flow +39 -0
  19. package/lib/physical-rtl/generate-ltr.d.ts +11 -0
  20. package/lib/physical-rtl/generate-ltr.js +18 -46
  21. package/lib/physical-rtl/generate-ltr.js.flow +12 -0
  22. package/lib/physical-rtl/generate-rtl.d.ts +13 -0
  23. package/lib/physical-rtl/generate-rtl.js +18 -50
  24. package/lib/physical-rtl/generate-rtl.js.flow +12 -0
  25. package/lib/preprocess-rules/PreRule.d.ts +52 -0
  26. package/lib/preprocess-rules/PreRule.js +87 -0
  27. package/lib/preprocess-rules/PreRule.js.flow +64 -0
  28. package/lib/preprocess-rules/application-order.d.ts +290 -0
  29. package/lib/preprocess-rules/application-order.js +193 -0
  30. package/lib/preprocess-rules/application-order.js.flow +241 -0
  31. package/lib/preprocess-rules/basic-validation.d.ts +13 -0
  32. package/lib/preprocess-rules/basic-validation.js +83 -0
  33. package/lib/preprocess-rules/basic-validation.js.flow +13 -0
  34. package/lib/preprocess-rules/flatten-raw-style-obj.d.ts +25 -0
  35. package/lib/preprocess-rules/flatten-raw-style-obj.js +120 -0
  36. package/lib/preprocess-rules/flatten-raw-style-obj.js.flow +27 -0
  37. package/lib/preprocess-rules/index.d.ts +18 -0
  38. package/lib/preprocess-rules/index.js +30 -0
  39. package/lib/preprocess-rules/index.js.flow +19 -0
  40. package/lib/preprocess-rules/legacy-expand-shorthands.d.ts +200 -0
  41. package/lib/preprocess-rules/legacy-expand-shorthands.js +136 -0
  42. package/lib/preprocess-rules/legacy-expand-shorthands.js.flow +179 -0
  43. package/lib/preprocess-rules/property-specificity.d.ts +89 -0
  44. package/lib/preprocess-rules/property-specificity.js +107 -0
  45. package/lib/preprocess-rules/property-specificity.js.flow +98 -0
  46. package/lib/properties/CSS Animations.json +445 -0
  47. package/lib/properties/CSS Backgrounds and Borders.json +1085 -0
  48. package/lib/properties/CSS Basic User Interface.json +365 -0
  49. package/lib/properties/CSS Box Alignment.json +245 -0
  50. package/lib/properties/CSS Box Model.json +501 -0
  51. package/lib/properties/CSS Color.json +100 -0
  52. package/lib/properties/CSS Columns.json +185 -0
  53. package/lib/properties/CSS Containment.json +203 -0
  54. package/lib/properties/CSS Counter Styles.json +56 -0
  55. package/lib/properties/CSS Display.json +20 -0
  56. package/lib/properties/CSS Flexible Box Layout.json +167 -0
  57. package/lib/properties/CSS Fonts.json +684 -0
  58. package/lib/properties/CSS Fragmentation.json +110 -0
  59. package/lib/properties/CSS Generated Content.json +38 -0
  60. package/lib/properties/CSS Grid Layout.json +500 -0
  61. package/lib/properties/CSS Images.json +91 -0
  62. package/lib/properties/CSS Inline.json +38 -0
  63. package/lib/properties/CSS Lists and Counters.json +86 -0
  64. package/lib/properties/CSS Logical Properties.json +1086 -0
  65. package/lib/properties/CSS Masking.json +399 -0
  66. package/lib/properties/CSS Miscellaneous.json +38 -0
  67. package/lib/properties/CSS Motion Path.json +132 -0
  68. package/lib/properties/CSS Overflow.json +216 -0
  69. package/lib/properties/CSS Pages.json +83 -0
  70. package/lib/properties/CSS Positioning.json +166 -0
  71. package/lib/properties/CSS Ruby.json +55 -0
  72. package/lib/properties/CSS Scroll Anchoring.json +19 -0
  73. package/lib/properties/CSS Scroll Snap.json +604 -0
  74. package/lib/properties/CSS Scrollbars.json +38 -0
  75. package/lib/properties/CSS Shapes.json +56 -0
  76. package/lib/properties/CSS Speech.json +20 -0
  77. package/lib/properties/CSS Table.json +115 -0
  78. package/lib/properties/CSS Text Decoration.json +312 -0
  79. package/lib/properties/CSS Text.json +415 -0
  80. package/lib/properties/CSS Transforms.json +188 -0
  81. package/lib/properties/CSS Transitions.json +122 -0
  82. package/lib/properties/CSS Variables.json +20 -0
  83. package/lib/properties/CSS View Transitions.json +20 -0
  84. package/lib/properties/CSS Will Change.json +20 -0
  85. package/lib/properties/CSS Writing Modes.json +92 -0
  86. package/lib/properties/Compositing and Blending.json +62 -0
  87. package/lib/properties/Filter Effects.json +38 -0
  88. package/lib/properties/MathML.json +56 -0
  89. package/lib/properties/Microsoft Extensions.json +885 -0
  90. package/lib/properties/Mozilla Extensions.json +607 -0
  91. package/lib/properties/Pointer Events.json +20 -0
  92. package/lib/properties/WebKit Extensions.json +707 -0
  93. package/lib/properties.json +10122 -0
  94. package/lib/stylex-create-theme.d.ts +26 -0
  95. package/lib/stylex-create-theme.js +62 -0
  96. package/lib/stylex-create-theme.js.flow +19 -0
  97. package/lib/stylex-create.d.ts +23 -0
  98. package/lib/stylex-create.js +30 -142
  99. package/lib/stylex-create.js.flow +30 -0
  100. package/lib/stylex-define-vars.d.ts +36 -0
  101. package/lib/stylex-define-vars.js +72 -0
  102. package/lib/stylex-define-vars.js.flow +28 -0
  103. package/lib/stylex-first-that-works.d.ts +13 -0
  104. package/lib/stylex-first-that-works.js +0 -10
  105. package/lib/stylex-first-that-works.js.flow +12 -0
  106. package/lib/stylex-include.d.ts +18 -0
  107. package/lib/stylex-include.js +0 -10
  108. package/lib/stylex-include.js.flow +20 -0
  109. package/lib/stylex-keyframes.d.ts +17 -0
  110. package/lib/stylex-keyframes.js +22 -30
  111. package/lib/stylex-keyframes.js.flow +20 -0
  112. package/lib/transform-value.d.ts +22 -0
  113. package/lib/transform-value.js +9 -23
  114. package/lib/transform-value.js.flow +25 -0
  115. package/lib/types/index.d.ts +205 -0
  116. package/lib/types/index.js +191 -0
  117. package/lib/types/index.js.flow +241 -0
  118. package/lib/utils/Rule.d.ts +58 -0
  119. package/lib/utils/Rule.js +50 -0
  120. package/lib/utils/Rule.js.flow +64 -0
  121. package/lib/utils/dashify.d.ts +11 -0
  122. package/lib/utils/dashify.js +0 -8
  123. package/lib/utils/dashify.js.flow +10 -0
  124. package/lib/utils/default-options.d.ts +11 -0
  125. package/lib/utils/default-options.js +14 -0
  126. package/lib/utils/default-options.js.flow +25 -0
  127. package/lib/utils/file-based-identifier.d.ts +15 -0
  128. package/lib/utils/file-based-identifier.js +14 -0
  129. package/lib/utils/file-based-identifier.js.flow +14 -0
  130. package/lib/utils/genCSSRule.d.ts +15 -0
  131. package/lib/utils/genCSSRule.js +9 -18
  132. package/lib/utils/genCSSRule.js.flow +15 -0
  133. package/lib/utils/normalize-value.d.ts +16 -0
  134. package/lib/utils/normalize-value.js +11 -15
  135. package/lib/utils/normalize-value.js.flow +16 -0
  136. package/lib/utils/normalizers/convert-camel-case-transition-props.d.ts +14 -0
  137. package/lib/utils/normalizers/convert-camel-case-transition-props.js +23 -0
  138. package/lib/utils/normalizers/convert-camel-case-transition-props.js.flow +13 -0
  139. package/lib/utils/normalizers/convert-camel-case-values.d.ts +14 -0
  140. package/lib/utils/normalizers/convert-camel-case-values.js +23 -0
  141. package/lib/utils/normalizers/convert-camel-case-values.js.flow +13 -0
  142. package/lib/utils/normalizers/detect-unclosed-fns.d.ts +17 -0
  143. package/lib/utils/normalizers/detect-unclosed-fns.js +0 -12
  144. package/lib/utils/normalizers/detect-unclosed-fns.js.flow +16 -0
  145. package/lib/utils/normalizers/font-size-px-to-rem.d.ts +19 -0
  146. package/lib/utils/normalizers/font-size-px-to-rem.js +9 -19
  147. package/lib/utils/normalizers/font-size-px-to-rem.js.flow +18 -0
  148. package/lib/utils/normalizers/leading-zero.d.ts +17 -0
  149. package/lib/utils/normalizers/leading-zero.js +0 -12
  150. package/lib/utils/normalizers/leading-zero.js.flow +16 -0
  151. package/lib/utils/normalizers/quotes.d.ts +18 -0
  152. package/lib/utils/normalizers/quotes.js +0 -13
  153. package/lib/utils/normalizers/quotes.js.flow +17 -0
  154. package/lib/utils/normalizers/timings.d.ts +18 -0
  155. package/lib/utils/normalizers/timings.js +0 -14
  156. package/lib/utils/normalizers/timings.js.flow +17 -0
  157. package/lib/utils/normalizers/whitespace.d.ts +19 -0
  158. package/lib/utils/normalizers/whitespace.js +0 -15
  159. package/lib/utils/normalizers/whitespace.js.flow +18 -0
  160. package/lib/utils/normalizers/zero-dimensions.d.ts +19 -0
  161. package/lib/utils/normalizers/zero-dimensions.js +0 -15
  162. package/lib/utils/normalizers/zero-dimensions.js.flow +18 -0
  163. package/lib/utils/object-utils.d.ts +66 -0
  164. package/lib/utils/object-utils.js +21 -13
  165. package/lib/utils/object-utils.js.flow +77 -0
  166. package/lib/utils/property-priorities.d.ts +11 -0
  167. package/lib/utils/property-priorities.js +531 -0
  168. package/lib/utils/property-priorities.js.flow +10 -0
  169. package/lib/utils/split-css-value.d.ts +14 -0
  170. package/lib/utils/split-css-value.js +33 -0
  171. package/lib/utils/split-css-value.js.flow +15 -0
  172. package/lib/validate.d.ts +12 -0
  173. package/lib/validate.js +1 -10
  174. package/lib/validate.js.flow +12 -0
  175. package/package.json +4 -5
  176. package/lib/expand-shorthands.d.ts +0 -5
  177. package/lib/expand-shorthands.js +0 -387
  178. package/lib/stylex-defaultValue.js +0 -397
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import type { InjectableStyle, StyleXOptions } from './common-types';
11
+ declare function styleXCreateTheme(
12
+ themeVars: {
13
+ readonly __themeName__: string;
14
+ readonly [$$Key$$: string]: string;
15
+ },
16
+ variables: {
17
+ readonly [$$Key$$: string]:
18
+ | string
19
+ | { default: string; readonly [$$Key$$: string]: string };
20
+ },
21
+ options?: StyleXOptions,
22
+ ): [
23
+ { $$css: true; readonly [$$Key$$: string]: string },
24
+ { [$$Key$$: string]: InjectableStyle },
25
+ ];
26
+ export default styleXCreateTheme;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = styleXCreateTheme;
7
+ var _hash = _interopRequireDefault(require("./hash"));
8
+ var _defaultOptions = require("./utils/default-options");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ function styleXCreateTheme(themeVars, variables, options) {
11
+ if (typeof themeVars.__themeName__ !== 'string') {
12
+ throw new Error('Can only override variables theme created with stylex.defineVars().');
13
+ }
14
+ const {
15
+ classNamePrefix
16
+ } = {
17
+ ..._defaultOptions.defaultOptions,
18
+ ...options
19
+ };
20
+ const sortedKeys = Object.keys(variables).sort();
21
+ const atRules = {};
22
+ const cssVariablesOverrideString = sortedKeys.map(key => {
23
+ const varNameHash = themeVars[key].slice(4, -1);
24
+ const value = variables[key];
25
+ if (varNameHash != null && value !== null && typeof value === 'object') {
26
+ if (value.default === undefined) {
27
+ throw new Error('Default value is not defined for ' + key + ' variable.');
28
+ }
29
+ const definedVarString = `${varNameHash}:${value.default};`;
30
+ Object.keys(value).forEach(key => {
31
+ if (key.startsWith('@')) {
32
+ const definedVarStringForAtRule = `${varNameHash}:${value[key]};`;
33
+ if (atRules[key] == null) {
34
+ atRules[key] = [definedVarStringForAtRule];
35
+ } else {
36
+ atRules[key].push(definedVarStringForAtRule);
37
+ }
38
+ }
39
+ });
40
+ return definedVarString;
41
+ }
42
+ return varNameHash != null && typeof value !== 'object' ? `${varNameHash}:${value};` : '';
43
+ }).join('');
44
+ const sortedAtRules = Object.keys(atRules).sort();
45
+ const atRulesStringForHash = sortedAtRules.map(atRule => {
46
+ return `${atRule}{${atRules[atRule].sort().join('')}}`;
47
+ }).join('');
48
+ const overrideClassName = classNamePrefix + (0, _hash.default)(cssVariablesOverrideString + atRulesStringForHash);
49
+ const atRulesCss = sortedAtRules.map(atRule => {
50
+ return `${atRule}{.${overrideClassName}{${atRules[atRule].join('')}}}`;
51
+ }).join('');
52
+ return [{
53
+ $$css: true,
54
+ [themeVars.__themeName__]: overrideClassName
55
+ }, {
56
+ [overrideClassName]: {
57
+ ltr: `.${overrideClassName}{${cssVariablesOverrideString}}${atRulesCss}`,
58
+ priority: 0.99,
59
+ rtl: undefined
60
+ }
61
+ }];
62
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import type { InjectableStyle, StyleXOptions } from './common-types';
11
+
12
+ // It takes an object of variables with their values and the original set of variables to override
13
+ // and returns a hashed className with variables overrides.
14
+ //
15
+ declare export default function styleXCreateTheme(
16
+ themeVars: { +__themeName__: string, +[string]: string },
17
+ variables: { +[string]: string | { default: string, +[string]: string } },
18
+ options?: StyleXOptions,
19
+ ): [{ $$css: true, +[string]: string }, { [string]: InjectableStyle }];
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import type {
11
+ RawStyles,
12
+ InjectableStyle,
13
+ StyleXOptions,
14
+ FlatCompiledStyles,
15
+ } from './common-types';
16
+ declare function styleXCreateSet(
17
+ namespaces: { readonly [$$Key$$: string]: RawStyles },
18
+ options?: StyleXOptions,
19
+ ): [
20
+ { [$$Key$$: string]: FlatCompiledStyles },
21
+ { [$$Key$$: string]: InjectableStyle },
22
+ ];
23
+ export default styleXCreateSet;
@@ -4,159 +4,47 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = styleXCreateSet;
7
- var _convertToClassName = _interopRequireDefault(require("./convert-to-className"));
8
- var _expandShorthands = _interopRequireWildcard(require("./expand-shorthands"));
9
7
  var _objectUtils = require("./utils/object-utils");
10
- var messages = _interopRequireWildcard(require("./messages"));
11
8
  var _stylexInclude = require("./stylex-include");
12
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
13
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
- /**
16
- * Copyright (c) Meta Platforms, Inc. and affiliates.
17
- *
18
- * This source code is licensed under the MIT license found in the
19
- * LICENSE file in the root directory of this source tree.
20
- *
21
- *
22
- */
23
-
24
- // This takes the object of styles passed to `stylex.create` and transforms it.
25
- // The transformation replaces style values with classNames.
26
- //
27
- // It also collects all injected styles along the way.
28
- // It then returns a tuple of the transformed style Object and an object of injected styles.
29
- //
30
- // This function does some basic validation, and then uses `styleXCreateNamespace` to transform
31
- // each namespace within,
32
- //
33
- // Before returning, it ensures that there are no duplicate styles being injected.
9
+ var _defaultOptions = require("./utils/default-options");
10
+ var _flattenRawStyleObj = require("./preprocess-rules/flatten-raw-style-obj");
11
+ var _basicValidation = require("./preprocess-rules/basic-validation");
34
12
  function styleXCreateSet(namespaces) {
35
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
13
+ let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _defaultOptions.defaultOptions;
36
14
  const resolvedNamespaces = {};
37
15
  const injectedStyles = {};
38
16
  for (const namespaceName of Object.keys(namespaces)) {
39
17
  const namespace = namespaces[namespaceName];
40
- if (typeof namespace !== 'object' || Array.isArray(namespace)) {
41
- throw new Error(messages.ILLEGAL_NAMESPACE_VALUE);
42
- }
43
- const [resolvedNamespace, injected] = styleXCreateNamespace(namespace, options);
44
- const compiledNamespace = (0, _objectUtils.flattenObject)(resolvedNamespace);
45
- resolvedNamespaces[namespaceName] = {
46
- ...compiledNamespace,
47
- $$css: true
48
- };
49
- for (const cn of Object.keys(injected)) {
50
- if (injectedStyles[cn] == null) {
51
- injectedStyles[cn] = injected[cn];
52
- }
53
- }
54
- }
55
- return [resolvedNamespaces, injectedStyles];
56
- }
57
-
58
- // Transforms a single style namespace.
59
- // e.g. Something along the lines of:
60
- // {color: 'red', margin: '10px'} =>
61
- // {
62
- // color: 'color-red',
63
- // marginTop: 'margin-top-10px',
64
- // marginBottom: 'margin-bottom-10px',
65
- // marginStart: 'margin-start-10px',
66
- // marginEnd: 'margin-end-10px'
67
- // }
68
- //
69
- // First, it expands shorthand properties. (margin => marginTop, marginBottom, marginStart, marginEnd)
70
- // Then, it converts each style value to a className.
71
- // Then, it returns the transformed style Object and an object of injected styles.
72
- function styleXCreateNamespace(style, options) {
73
- const namespaceEntries = (0, _objectUtils.objEntries)(style);
74
-
75
- // First the shorthand properties are expanded.
76
- // e.g. `margin` gets expanded to `marginTop`, `marginBottom`, `marginStart`, `marginEnd`.
77
- // `entries` is an array of [key, value] pairs.
78
- const entries = namespaceEntries.flatMap(_ref => {
79
- let [key, value] = _ref;
80
- if (value instanceof _stylexInclude.IncludedStyles) {
81
- return [[key, value]];
82
- }
83
- if (value != null && typeof value === 'object' && !Array.isArray(value)) {
84
- if (!key.startsWith(':') && !key.startsWith('@') && _expandShorthands.expandedKeys.includes(key)) {
85
- throw new Error(messages.INVALID_PSEUDO);
86
- }
87
- return [[key, (0, _objectUtils.objFromEntries)((0, _objectUtils.objEntries)(value).flatMap(_ref2 => {
88
- let [innerKey, innerValue] = _ref2;
89
- if (innerValue != null && typeof innerValue === 'object' && !Array.isArray(innerValue)) {
90
- throw new Error(messages.ILLEGAL_NESTED_PSEUDO);
91
- }
92
- if (!options.allowUnsafeProperties && _expandShorthands.BANNED_PROPERTIES[key]) {
93
- throw new Error(_expandShorthands.BANNED_PROPERTIES[key]);
94
- }
95
- return (0, _expandShorthands.default)([innerKey, innerValue]);
96
- }))]];
97
- } else {
98
- if (value !== null && typeof value !== 'string' && typeof value !== 'number' && !Array.isArray(value)) {
99
- throw new Error(messages.ILLEGAL_PROP_VALUE);
100
- }
101
- if (Array.isArray(value) && value.some(val => typeof val === 'object')) {
102
- throw new Error(messages.ILLEGAL_PROP_ARRAY_VALUE);
103
- }
104
- if (!options.allowUnsafeProperties && _expandShorthands.BANNED_PROPERTIES[key]) {
105
- throw new Error(_expandShorthands.BANNED_PROPERTIES[key]);
106
- }
107
- return (0, _expandShorthands.default)([key, value]);
108
- }
109
- });
110
-
111
- // Now each [key, value] pair is considered a single atomic style.
112
- // This atomic style is converted to a className by hashing
113
- //
114
- // The [key, className] pair is then added to the output Object: `resolvedNamespace`.
115
- // While hashing, the CSS rule that the className is generated from is also added to the output Object: `injectedStyles`.
116
- const resolvedNamespace = {};
117
- const injectedStyles = {};
118
- for (const [key, val] of entries) {
119
- if (val instanceof _stylexInclude.IncludedStyles) {
120
- resolvedNamespace[key] = val;
121
- } else if (val != null && typeof val === 'object' && !Array.isArray(val)) {
122
- if (key.startsWith(':') || key.startsWith('@')) {
123
- const pseudo = key;
124
- const innerObj = {};
125
- for (const [innerKey, innerVal] of (0, _objectUtils.objEntries)(val)) {
126
- if (innerVal === null) {
127
- innerObj[innerKey] = null;
128
- } else {
129
- const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([innerKey, innerVal], pseudo, options);
130
- innerObj[updatedKey] = className;
131
- injectedStyles[updatedKey + pseudo] = [className, cssRule];
132
- }
133
- }
134
- resolvedNamespace[key] = innerObj;
18
+ (0, _basicValidation.validateNamespace)(namespace);
19
+ const flattenedNamespace = (0, _flattenRawStyleObj.flattenRawStyleObject)(namespace, options);
20
+ const compiledNamespaceTuples = flattenedNamespace.map(_ref => {
21
+ let [key, value] = _ref;
22
+ return [key, value.compiled(options)];
23
+ });
24
+ const compiledNamespace = (0, _objectUtils.objFromEntries)(compiledNamespaceTuples);
25
+ const namespaceObj = {};
26
+ for (const key of Object.keys(compiledNamespace)) {
27
+ const value = compiledNamespace[key];
28
+ if (value instanceof _stylexInclude.IncludedStyles) {
29
+ namespaceObj[key] = value;
135
30
  } else {
136
- const propKey = key;
137
- const classNames = [];
138
- for (const [pseudo, innerVal] of (0, _objectUtils.objEntries)(val)) {
139
- if (pseudo !== 'default' && !pseudo.startsWith(':') && !pseudo.startsWith('@')) {
140
- throw new Error(messages.INVALID_PSEUDO);
141
- }
142
- if (innerVal !== null) {
143
- const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([propKey, innerVal], pseudo === 'default' ? undefined : pseudo, options);
144
- injectedStyles[updatedKey + pseudo] = [className, cssRule];
145
- classNames.push(className);
31
+ const classNameTuples = value.map(v => Array.isArray(v) ? v : null).filter(Boolean);
32
+ const className = classNameTuples.map(_ref2 => {
33
+ let [className] = _ref2;
34
+ return className;
35
+ }).join(' ') || null;
36
+ namespaceObj[key] = className;
37
+ for (const [className, injectable] of classNameTuples) {
38
+ if (injectedStyles[className] == null) {
39
+ injectedStyles[className] = injectable;
146
40
  }
147
41
  }
148
- resolvedNamespace[key] = classNames.join(' ');
149
- }
150
- } else {
151
- if (val === null) {
152
- resolvedNamespace[key] = null;
153
- } else {
154
- const [updatedKey, className, cssRule] = (0, _convertToClassName.default)([key, val], undefined, options);
155
- resolvedNamespace[updatedKey] = className;
156
- injectedStyles[updatedKey] = [className, cssRule];
157
42
  }
158
43
  }
44
+ resolvedNamespaces[namespaceName] = {
45
+ ...namespaceObj,
46
+ $$css: true
47
+ };
159
48
  }
160
- const finalInjectedStyles = (0, _objectUtils.objFromEntries)((0, _objectUtils.objValues)(injectedStyles));
161
- return [resolvedNamespace, finalInjectedStyles];
49
+ return [resolvedNamespaces, injectedStyles];
162
50
  }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import type {
11
+ RawStyles,
12
+ InjectableStyle,
13
+ StyleXOptions,
14
+ FlatCompiledStyles,
15
+ } from './common-types';
16
+
17
+ // This takes the object of styles passed to `stylex.create` and transforms it.
18
+ // The transformation replaces style values with classNames.
19
+ //
20
+ // It also collects all injected styles along the way.
21
+ // It then returns a tuple of the transformed style Object and an object of injected styles.
22
+ //
23
+ // This function does some basic validation, and then uses `styleXCreateNamespace` to transform
24
+ // each namespace within,
25
+ //
26
+ // Before returning, it ensures that there are no duplicate styles being injected.
27
+ declare export default function styleXCreateSet(
28
+ namespaces: { +[string]: RawStyles },
29
+ options?: StyleXOptions,
30
+ ): [{ [string]: FlatCompiledStyles }, { [string]: InjectableStyle }];
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import type { StyleXOptions } from './common-types';
11
+ type VarsObject<
12
+ Vars extends {
13
+ readonly [$$Key$$: string]:
14
+ | string
15
+ | { readonly default: string; readonly [$$Key$$: string]: string };
16
+ },
17
+ > = Readonly</**
18
+ * > 15 | ...$ObjMapConst<Vars, string>,
19
+ * | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unsupported feature: Translating "object types with complex spreads" is currently not supported.
20
+ **/
21
+ any>;
22
+ declare function styleXDefineVars<
23
+ Vars extends {
24
+ readonly [$$Key$$: string]:
25
+ | string
26
+ | { readonly default: string; readonly [$$Key$$: string]: string };
27
+ },
28
+ >(
29
+ variables: Vars,
30
+ options: Readonly<
31
+ Omit<Partial<StyleXOptions>, keyof ({ themeName: string })> & {
32
+ themeName: string;
33
+ }
34
+ >,
35
+ ): [VarsObject<Vars>, { css: string }];
36
+ export default styleXDefineVars;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = styleXDefineVars;
7
+ var _hash = _interopRequireDefault(require("./hash"));
8
+ var _objectUtils = require("./utils/object-utils");
9
+ var _defaultOptions = require("./utils/default-options");
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ function styleXDefineVars(variables, options) {
12
+ const {
13
+ classNamePrefix,
14
+ themeName
15
+ } = {
16
+ ..._defaultOptions.defaultOptions,
17
+ ...options
18
+ };
19
+ const themeNameHash = classNamePrefix + (0, _hash.default)(themeName);
20
+ const variablesMap = (0, _objectUtils.objMap)(variables, (value, key) => {
21
+ const nameHash = classNamePrefix + (0, _hash.default)(`${themeName}.${key}`);
22
+ return {
23
+ nameHash,
24
+ value
25
+ };
26
+ });
27
+ const themeVariablesObject = (0, _objectUtils.objMap)(variablesMap, _ref => {
28
+ let {
29
+ nameHash
30
+ } = _ref;
31
+ return `var(--${nameHash})`;
32
+ });
33
+ const cssVariablesString = constructCssVariablesString(variablesMap);
34
+ return [{
35
+ ...themeVariablesObject,
36
+ __themeName__: themeNameHash
37
+ }, {
38
+ css: cssVariablesString
39
+ }];
40
+ }
41
+ function constructCssVariablesString(variables) {
42
+ const atRules = {};
43
+ const varsString = (0, _objectUtils.objEntries)(variables).map(_ref2 => {
44
+ let [key, {
45
+ nameHash,
46
+ value
47
+ }] = _ref2;
48
+ if (value !== null && typeof value === 'object') {
49
+ if (value.default === undefined) {
50
+ throw new Error('Default value is not defined for ' + key + ' variable.');
51
+ }
52
+ const definedVarString = `--${nameHash}:${value.default};`;
53
+ Object.keys(value).forEach(key => {
54
+ if (key.startsWith('@')) {
55
+ const definedVarStringForAtRule = `--${nameHash}:${value[key]};`;
56
+ if (atRules[key] == null) {
57
+ atRules[key] = [definedVarStringForAtRule];
58
+ } else {
59
+ atRules[key].push(definedVarStringForAtRule);
60
+ }
61
+ }
62
+ });
63
+ return definedVarString;
64
+ }
65
+ return `--${nameHash}:${value};`;
66
+ }).join('');
67
+ const atRulesString = (0, _objectUtils.objEntries)(atRules).map(_ref3 => {
68
+ let [atRule, varsArr] = _ref3;
69
+ return `${atRule}{:root{${varsArr.join('')}}}`;
70
+ }).join('');
71
+ return `:root{${varsString}}${atRulesString || ''}`;
72
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ import type { StyleXOptions } from './common-types';
11
+
12
+ type VarsObject<
13
+ Vars: { +[string]: string | { +default: string, +[string]: string } },
14
+ > = $ReadOnly<{
15
+ ...$ObjMapConst<Vars, string>,
16
+ __themeName__: string,
17
+ }>;
18
+
19
+ // Similar to `stylex.create` it takes an object of variables with their values
20
+ // and returns a string after hashing it.
21
+ declare export default function styleXDefineVars<
22
+ Vars: {
23
+ +[string]: string | { +default: string, +[string]: string },
24
+ },
25
+ >(
26
+ variables: Vars,
27
+ options: $ReadOnly<{ ...Partial<StyleXOptions>, themeName: string, ... }>,
28
+ ): [VarsObject<Vars>, { css: string }];
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ declare function stylexFirstThatWorks<T>(
11
+ ...args: ReadonlyArray<T>
12
+ ): ReadonlyArray<T>;
13
+ export default stylexFirstThatWorks;
@@ -4,16 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = stylexFirstThatWorks;
7
- /**
8
- * Copyright (c) Meta Platforms, Inc. and affiliates.
9
- *
10
- * This source code is licensed under the MIT license found in the
11
- * LICENSE file in the root directory of this source tree.
12
- *
13
- *
14
- *
15
- */
16
-
17
7
  function stylexFirstThatWorks() {
18
8
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
19
9
  args[_key] = arguments[_key];
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ declare export default function stylexFirstThatWorks<T>(
11
+ ...args: $ReadOnlyArray<T>
12
+ ): $ReadOnlyArray<T>;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ export declare class IncludedStyles {
11
+ astNode: any;
12
+ constructor(astNode: any);
13
+ }
14
+ declare function stylexInclude(
15
+ firstArg: any,
16
+ ...styles: any
17
+ ): { [key: string]: IncludedStyles };
18
+ export default stylexInclude;
@@ -8,16 +8,6 @@ exports.default = stylexInclude;
8
8
  var messages = _interopRequireWildcard(require("./messages"));
9
9
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
10
10
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
- /**
12
- * Copyright (c) Meta Platforms, Inc. and affiliates.
13
- *
14
- * This source code is licensed under the MIT license found in the
15
- * LICENSE file in the root directory of this source tree.
16
- *
17
- *
18
- *
19
- */
20
-
21
11
  let number = 0;
22
12
  function uuid() {
23
13
  return `__included_${++number}__`;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict
8
+ */
9
+
10
+ declare export class IncludedStyles {
11
+ astNode: any;
12
+ constructor(astNode: any): void;
13
+ }
14
+
15
+ declare export default function stylexInclude(
16
+ firstArg: any,
17
+ ...styles: any
18
+ ): {
19
+ [key: string]: IncludedStyles,
20
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ *
8
+ */
9
+
10
+ import type { InjectableStyle, StyleXOptions } from './common-types';
11
+ declare function styleXKeyframes(
12
+ frames: {
13
+ readonly [$$Key$$: string]: { readonly [$$Key$$: string]: string | number };
14
+ },
15
+ options: StyleXOptions,
16
+ ): [string, InjectableStyle];
17
+ export default styleXKeyframes;