@stylexjs/shared 0.5.1 → 0.6.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.
@@ -9,8 +9,12 @@
9
9
 
10
10
  import type { TRawValue, StyleRule, StyleXOptions } from './common-types';
11
11
  export declare function convertStyleToClassName(
12
- objEntry: [string, TRawValue],
12
+ objEntry: Readonly<[string, TRawValue]>,
13
13
  pseudos: ReadonlyArray<string>,
14
14
  atRules: ReadonlyArray<string>,
15
15
  options: StyleXOptions,
16
16
  ): StyleRule;
17
+ declare function variableFallbacks(
18
+ values: ReadonlyArray<string>,
19
+ ): Array<string>;
20
+ export default variableFallbacks;
@@ -4,12 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.convertStyleToClassName = convertStyleToClassName;
7
+ exports.default = variableFallbacks;
7
8
  var _hash = _interopRequireDefault(require("./hash"));
8
9
  var _dashify = _interopRequireDefault(require("./utils/dashify"));
9
10
  var _transformValue = _interopRequireDefault(require("./transform-value"));
10
11
  var _generateCssRule = require("./generate-css-rule");
11
12
  var _defaultOptions = require("./utils/default-options");
12
13
  var _objectUtils = require("./utils/object-utils");
14
+ var messages = _interopRequireWildcard(require("./messages"));
15
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
16
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
13
17
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
18
  function convertStyleToClassName(objEntry, pseudos, atRules) {
15
19
  let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _defaultOptions.defaultOptions;
@@ -18,7 +22,10 @@ function convertStyleToClassName(objEntry, pseudos, atRules) {
18
22
  } = options;
19
23
  const [key, rawValue] = objEntry;
20
24
  const dashedKey = (0, _dashify.default)(key);
21
- const value = Array.isArray(rawValue) ? rawValue.map(eachValue => (0, _transformValue.default)(key, eachValue, options)) : (0, _transformValue.default)(key, rawValue, options);
25
+ let value = Array.isArray(rawValue) ? rawValue.map(eachValue => (0, _transformValue.default)(key, eachValue, options)) : (0, _transformValue.default)(key, rawValue, options);
26
+ if (Array.isArray(value) && value.find(val => val.startsWith('var(') && val.endsWith(')'))) {
27
+ value = variableFallbacks(value);
28
+ }
22
29
  const sortedPseudos = (0, _objectUtils.arraySort)(pseudos ?? []);
23
30
  const sortedAtRules = (0, _objectUtils.arraySort)(atRules ?? []);
24
31
  const atRuleHashString = sortedPseudos.join('');
@@ -28,4 +35,29 @@ function convertStyleToClassName(objEntry, pseudos, atRules) {
28
35
  const className = classNamePrefix + (0, _hash.default)('<>' + stringToHash);
29
36
  const cssRules = (0, _generateCssRule.generateRule)(className, dashedKey, value, pseudos, atRules);
30
37
  return [key, className, cssRules];
38
+ }
39
+ function variableFallbacks(values) {
40
+ const firstVar = values.findIndex(val => val.startsWith('var(') && val.endsWith(')'));
41
+ const lastVar = values.findLastIndex(val => val.startsWith('var(') && val.endsWith(')'));
42
+ const valuesBeforeFirstVar = values.slice(0, firstVar);
43
+ let varValues = values.slice(firstVar, lastVar + 1).reverse();
44
+ const valuesAfterLastVar = values.slice(lastVar + 1);
45
+ if (varValues.find(val => !val.startsWith('var(') || !val.endsWith(')'))) {
46
+ throw new Error(messages.NON_CONTIGUOUS_VARS);
47
+ }
48
+ varValues = varValues.map(val => val.slice(4, -1));
49
+ return [...(valuesBeforeFirstVar.length > 0 ? valuesBeforeFirstVar.map(val => composeVars(...varValues, val)) : composeVars(...varValues)), ...valuesAfterLastVar];
50
+ }
51
+ function composeVars() {
52
+ for (var _len = arguments.length, vars = new Array(_len), _key = 0; _key < _len; _key++) {
53
+ vars[_key] = arguments[_key];
54
+ }
55
+ const [first, ...rest] = vars;
56
+ if (rest.length > 0) {
57
+ return `var(${first},${composeVars(...rest)})`;
58
+ } else if (first.startsWith('--')) {
59
+ return `var(${first})`;
60
+ } else {
61
+ return first;
62
+ }
31
63
  }
@@ -16,8 +16,12 @@ import type { TRawValue, StyleRule, StyleXOptions } from './common-types';
16
16
  // Hashes to get a className
17
17
  // Returns the final key, className a CSS Rule
18
18
  declare export function convertStyleToClassName(
19
- objEntry: [string, TRawValue],
19
+ objEntry: $ReadOnly<[string, TRawValue]>,
20
20
  pseudos: $ReadOnlyArray<string>,
21
21
  atRules: $ReadOnlyArray<string>,
22
22
  options: StyleXOptions,
23
23
  ): StyleRule;
24
+
25
+ declare export default function variableFallbacks(
26
+ values: $ReadOnlyArray<string>,
27
+ ): Array<string>;
package/lib/messages.d.ts CHANGED
@@ -39,3 +39,4 @@ export declare const NO_PROJECT_ROOT_DIRECTORY: 'The project root directory `roo
39
39
  export declare const NON_EXPORT_NAMED_DECLARATION: 'The return value of stylex.defineVars() must be bound to a named export.';
40
40
  export declare const ANONYMOUS_THEME: 'stylex.createTheme() must be bound to a named constant.';
41
41
  export declare const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS: 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
42
+ export declare const NON_CONTIGUOUS_VARS: 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
package/lib/messages.js CHANGED
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.UNKNOWN_PROP_KEY = exports.UNKNOWN_NAMESPACE = exports.UNEXPECTED_ARGUMENT = exports.UNBOUND_STYLEX_CALL_VALUE = exports.ONLY_TOP_LEVEL_INCLUDES = exports.ONLY_TOP_LEVEL = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.NO_PROJECT_ROOT_DIRECTORY = exports.NO_PARENT_PATH = exports.NO_CONDITIONAL_SHORTHAND = exports.NON_STATIC_VALUE = exports.NON_STATIC_KEYFRAME_VALUE = exports.NON_OBJECT_KEYFRAME = exports.NON_OBJECT_FOR_STYLEX_KEYFRAMES_CALL = exports.NON_OBJECT_FOR_STYLEX_CALL = exports.NON_EXPORT_NAMED_DECLARATION = exports.LOCAL_ONLY = exports.LINT_UNCLOSED_FUNCTION = exports.INVALID_SPREAD = exports.INVALID_PSEUDO_OR_AT_RULE = exports.INVALID_PSEUDO = exports.ILLEGAL_PROP_VALUE = exports.ILLEGAL_PROP_ARRAY_VALUE = exports.ILLEGAL_NESTED_PSEUDO = exports.ILLEGAL_NAMESPACE_VALUE = exports.ILLEGAL_NAMESPACE_TYPE = exports.ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = exports.ILLEGAL_ARGUMENT_LENGTH = exports.EXPECTED_FUNCTION_CALL = exports.ESCAPED_STYLEX_VALUE = exports.DUPLICATE_CONDITIONAL = exports.ANONYMOUS_THEME = void 0;
6
+ exports.UNKNOWN_PROP_KEY = exports.UNKNOWN_NAMESPACE = exports.UNEXPECTED_ARGUMENT = exports.UNBOUND_STYLEX_CALL_VALUE = exports.ONLY_TOP_LEVEL_INCLUDES = exports.ONLY_TOP_LEVEL = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.NO_PROJECT_ROOT_DIRECTORY = exports.NO_PARENT_PATH = exports.NO_CONDITIONAL_SHORTHAND = exports.NON_STATIC_VALUE = exports.NON_STATIC_KEYFRAME_VALUE = exports.NON_OBJECT_KEYFRAME = exports.NON_OBJECT_FOR_STYLEX_KEYFRAMES_CALL = exports.NON_OBJECT_FOR_STYLEX_CALL = exports.NON_EXPORT_NAMED_DECLARATION = exports.NON_CONTIGUOUS_VARS = exports.LOCAL_ONLY = exports.LINT_UNCLOSED_FUNCTION = exports.INVALID_SPREAD = exports.INVALID_PSEUDO_OR_AT_RULE = exports.INVALID_PSEUDO = exports.ILLEGAL_PROP_VALUE = exports.ILLEGAL_PROP_ARRAY_VALUE = exports.ILLEGAL_NESTED_PSEUDO = exports.ILLEGAL_NAMESPACE_VALUE = exports.ILLEGAL_NAMESPACE_TYPE = exports.ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = exports.ILLEGAL_ARGUMENT_LENGTH = exports.EXPECTED_FUNCTION_CALL = exports.ESCAPED_STYLEX_VALUE = exports.DUPLICATE_CONDITIONAL = exports.ANONYMOUS_THEME = void 0;
7
7
  const ILLEGAL_ARGUMENT_LENGTH = exports.ILLEGAL_ARGUMENT_LENGTH = 'stylex.create() should have 1 argument.';
8
8
  const ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = exports.ILLEGAL_ARG_LENGTH_FOR_KEYFRAMES = 'stylex.keyframes() should have 1 argument.';
9
9
  const NON_STATIC_VALUE = exports.NON_STATIC_VALUE = 'Only static values are allowed inside of a stylex.create() call.';
@@ -35,4 +35,5 @@ const DUPLICATE_CONDITIONAL = exports.DUPLICATE_CONDITIONAL = 'The same pseudo s
35
35
  const NO_PROJECT_ROOT_DIRECTORY = exports.NO_PROJECT_ROOT_DIRECTORY = 'The project root directory `rootDir` is not configured.';
36
36
  const NON_EXPORT_NAMED_DECLARATION = exports.NON_EXPORT_NAMED_DECLARATION = 'The return value of stylex.defineVars() must be bound to a named export.';
37
37
  const ANONYMOUS_THEME = exports.ANONYMOUS_THEME = 'stylex.createTheme() must be bound to a named constant.';
38
- const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
38
+ const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = exports.ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS = 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
39
+ const NON_CONTIGUOUS_VARS = exports.NON_CONTIGUOUS_VARS = 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
@@ -42,3 +42,4 @@ declare export const NO_PROJECT_ROOT_DIRECTORY: 'The project root directory `roo
42
42
  declare export const NON_EXPORT_NAMED_DECLARATION: 'The return value of stylex.defineVars() must be bound to a named export.';
43
43
  declare export const ANONYMOUS_THEME: 'stylex.createTheme() must be bound to a named constant.';
44
44
  declare export const ONLY_NAMED_PARAMETERS_IN_DYNAMIC_STYLE_FUNCTIONS: 'Only named parameters are allowed in Dynamic Style functions. Destructuring, spreading or default values are not allowed.';
45
+ declare export const NON_CONTIGUOUS_VARS: 'All variables passed to `stylex.firstThatWorks` must be contiguous.';
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = styleXCreateTheme;
7
7
  var _hash = _interopRequireDefault(require("./hash"));
8
+ var _stylexVarsUtils = require("./stylex-vars-utils");
9
+ var _types = require("./types");
8
10
  var _defaultOptions = require("./utils/default-options");
9
11
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
12
  function styleXCreateTheme(themeVars, variables, options) {
@@ -17,48 +19,36 @@ function styleXCreateTheme(themeVars, variables, options) {
17
19
  ..._defaultOptions.defaultOptions,
18
20
  ...options
19
21
  };
22
+ const rulesByAtRule = {};
20
23
  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 stylesToInject = {
50
- [overrideClassName]: {
51
- ltr: `.${overrideClassName}{${cssVariablesOverrideString}}`,
52
- priority: 0.8,
53
- rtl: undefined
54
- }
55
- };
24
+ for (const key of sortedKeys) {
25
+ const value = (0, _types.isCSSType)(variables[key]) ? variables[key].value : variables[key];
26
+ const nameHash = themeVars[key].slice(6, -1);
27
+ (0, _stylexVarsUtils.collectVarsByAtRule)(key, {
28
+ nameHash,
29
+ value
30
+ }, rulesByAtRule);
31
+ }
32
+ const sortedAtRules = Object.keys(rulesByAtRule).sort((a, b) => a === 'default' ? -1 : b === 'default' ? 1 : a.localeCompare(b));
33
+ const atRulesStringForHash = sortedAtRules.map(atRule => (0, _stylexVarsUtils.wrapWithAtRules)(rulesByAtRule[atRule].join(''), atRule)).join('');
34
+ const overrideClassName = classNamePrefix + (0, _hash.default)(atRulesStringForHash);
35
+ const stylesToInject = {};
56
36
  for (const atRule of sortedAtRules) {
57
- stylesToInject[overrideClassName + '-' + (0, _hash.default)(atRule)] = {
58
- ltr: `${atRule}{.${overrideClassName}{${atRules[atRule].join('')}}}`,
59
- priority: 0.9,
60
- rtl: null
61
- };
37
+ const decls = rulesByAtRule[atRule].join('');
38
+ const rule = `.${overrideClassName}{${decls}}`;
39
+ if (atRule === 'default') {
40
+ stylesToInject[overrideClassName] = {
41
+ ltr: rule,
42
+ priority: 0.5,
43
+ rtl: null
44
+ };
45
+ } else {
46
+ stylesToInject[overrideClassName + '-' + (0, _hash.default)(atRule)] = {
47
+ ltr: (0, _stylexVarsUtils.wrapWithAtRules)(rule, atRule),
48
+ priority: 0.5 + 0.1 * (0, _stylexVarsUtils.priorityForAtRule)(atRule),
49
+ rtl: null
50
+ };
51
+ }
62
52
  }
63
53
  return [{
64
54
  $$css: true,
@@ -8,13 +8,9 @@
8
8
  */
9
9
 
10
10
  import type { InjectableStyle, StyleXOptions } from './common-types';
11
- type VarsConfig = Readonly<{
12
- [$$Key$$: string]:
13
- | string
14
- | Readonly<{ default: string; [$$Key$$: string]: string }>;
15
- }>;
11
+ import type { VarsConfig } from './stylex-vars-utils';
16
12
  type VarsObject<Vars extends VarsConfig> = Readonly</**
17
- * > 17 | ...$ObjMapConst<Vars, string>,
13
+ * > 14 | ...$ObjMapConst<Vars, string>,
18
14
  * | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Unsupported feature: Translating "object types with complex spreads" is currently not supported.
19
15
  **/
20
16
  any>;
@@ -7,6 +7,8 @@ exports.default = styleXDefineVars;
7
7
  var _hash = _interopRequireDefault(require("./hash"));
8
8
  var _objectUtils = require("./utils/object-utils");
9
9
  var _defaultOptions = require("./utils/default-options");
10
+ var _stylexVarsUtils = require("./stylex-vars-utils");
11
+ var _types = require("./types");
10
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
13
  function styleXDefineVars(variables, options) {
12
14
  const {
@@ -17,8 +19,20 @@ function styleXDefineVars(variables, options) {
17
19
  ...options
18
20
  };
19
21
  const themeNameHash = classNamePrefix + (0, _hash.default)(themeName);
22
+ const typedVariables = {};
20
23
  const variablesMap = (0, _objectUtils.objMap)(variables, (value, key) => {
21
24
  const nameHash = classNamePrefix + (0, _hash.default)(`${themeName}.${key}`);
25
+ if ((0, _types.isCSSType)(value)) {
26
+ const v = value;
27
+ typedVariables[nameHash] = {
28
+ initialValue: (0, _stylexVarsUtils.getDefaultValue)(v.value),
29
+ syntax: v.syntax
30
+ };
31
+ return {
32
+ nameHash,
33
+ value: v.value
34
+ };
35
+ }
22
36
  return {
23
37
  nameHash,
24
38
  value
@@ -31,42 +45,47 @@ function styleXDefineVars(variables, options) {
31
45
  return `var(--${nameHash})`;
32
46
  });
33
47
  const injectableStyles = constructCssVariablesString(variablesMap, themeNameHash);
48
+ const injectableTypes = (0, _objectUtils.objMap)(typedVariables, (_ref2, nameHash) => {
49
+ let {
50
+ initialValue: iv,
51
+ syntax
52
+ } = _ref2;
53
+ return {
54
+ ltr: `@property --${nameHash} { syntax: "${syntax}"; inherits: true;${iv != null ? ` initial-value: ${iv}` : ''} }`,
55
+ rtl: null,
56
+ priority: 0
57
+ };
58
+ });
34
59
  return [{
35
60
  ...themeVariablesObject,
36
61
  __themeName__: themeNameHash
37
- }, injectableStyles];
62
+ }, {
63
+ ...injectableTypes,
64
+ ...injectableStyles
65
+ }];
38
66
  }
39
67
  function constructCssVariablesString(variables, themeNameHash) {
40
- const ruleByAtRule = {};
68
+ const rulesByAtRule = {};
41
69
  for (const [key, {
42
70
  nameHash,
43
71
  value
44
- }] of (0, _objectUtils.objEntries)(variables)) {
45
- if (value !== null && typeof value === 'object') {
46
- if (value.default === undefined) {
47
- throw new Error('Default value is not defined for ' + key + ' variable.');
48
- }
49
- const v = value;
50
- for (const [key, value] of (0, _objectUtils.objEntries)(v)) {
51
- ruleByAtRule[key] ??= [];
52
- ruleByAtRule[key].push(`--${nameHash}:${value};`);
53
- }
54
- } else {
55
- ruleByAtRule.default ??= [];
56
- ruleByAtRule.default.push(`--${nameHash}:${value};`);
57
- }
72
+ }] of Object.entries(variables)) {
73
+ (0, _stylexVarsUtils.collectVarsByAtRule)(key, {
74
+ nameHash,
75
+ value
76
+ }, rulesByAtRule);
58
77
  }
59
78
  const result = {};
60
- for (const [key, value] of (0, _objectUtils.objEntries)(ruleByAtRule)) {
61
- const suffix = key === 'default' ? '' : `-${(0, _hash.default)(key)}`;
79
+ for (const [atRule, value] of Object.entries(rulesByAtRule)) {
80
+ const suffix = atRule === 'default' ? '' : `-${(0, _hash.default)(atRule)}`;
62
81
  let ltr = `:root{${value.join('')}}`;
63
- if (key !== 'default') {
64
- ltr = `${key}{${ltr}}`;
82
+ if (atRule !== 'default') {
83
+ ltr = (0, _stylexVarsUtils.wrapWithAtRules)(ltr, atRule);
65
84
  }
66
85
  result[themeNameHash + suffix] = {
67
86
  ltr,
68
87
  rtl: null,
69
- priority: key === 'default' ? 0 : 0.1
88
+ priority: (0, _stylexVarsUtils.priorityForAtRule)(atRule) * 0.1
70
89
  };
71
90
  }
72
91
  return result;
@@ -8,10 +8,7 @@
8
8
  */
9
9
 
10
10
  import type { InjectableStyle, StyleXOptions } from './common-types';
11
-
12
- type VarsConfig = $ReadOnly<{
13
- [string]: string | $ReadOnly<{ default: string, [string]: string }>,
14
- }>;
11
+ import type { VarsConfig } from './stylex-vars-utils';
15
12
 
16
13
  type VarsObject<Vars: VarsConfig> = $ReadOnly<{
17
14
  ...$ObjMapConst<Vars, string>,
@@ -7,7 +7,7 @@
7
7
  *
8
8
  */
9
9
 
10
- declare function stylexFirstThatWorks<T>(
10
+ declare function stylexFirstThatWorks<T extends string>(
11
11
  ...args: ReadonlyArray<T>
12
12
  ): ReadonlyArray<T>;
13
13
  export default stylexFirstThatWorks;
@@ -7,6 +7,6 @@
7
7
  * @flow strict
8
8
  */
9
9
 
10
- declare export default function stylexFirstThatWorks<T>(
10
+ declare export default function stylexFirstThatWorks<T: string>(
11
11
  ...args: $ReadOnlyArray<T>
12
12
  ): $ReadOnlyArray<T>;
@@ -0,0 +1,27 @@
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 { CSSType } from './types';
11
+ export type VarsConfigValue =
12
+ | string
13
+ | Readonly<{ default: VarsConfigValue; [$$Key$$: string]: VarsConfigValue }>;
14
+ export type VarsConfig = Readonly<{
15
+ [$$Key$$: string]: VarsConfigValue | CSSType;
16
+ }>;
17
+ export declare function collectVarsByAtRule(
18
+ key: string,
19
+ $$PARAM_1$$: { readonly nameHash: string; readonly value: VarsConfigValue },
20
+ collection: { [$$Key$$: string]: Array<string> },
21
+ atRules: Array<string>,
22
+ ): void;
23
+ export declare function wrapWithAtRules(ltr: string, atRule: string): string;
24
+ export declare function priorityForAtRule(atRule: string): number;
25
+ export declare function getDefaultValue(
26
+ value: VarsConfigValue,
27
+ ): null | undefined | string;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.collectVarsByAtRule = collectVarsByAtRule;
7
+ exports.getDefaultValue = getDefaultValue;
8
+ exports.priorityForAtRule = priorityForAtRule;
9
+ exports.wrapWithAtRules = wrapWithAtRules;
10
+ const SPLIT_TOKEN = '__$$__';
11
+ function collectVarsByAtRule(key, _ref) {
12
+ let {
13
+ nameHash,
14
+ value
15
+ } = _ref;
16
+ let collection = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
17
+ let atRules = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
18
+ if (typeof value === 'string' || typeof value === 'number') {
19
+ const val = typeof value === 'number' ? value.toString() : value;
20
+ const key = atRules.length === 0 ? 'default' : [...atRules].sort().join(SPLIT_TOKEN);
21
+ collection[key] ??= [];
22
+ collection[key].push(`--${nameHash}:${val};`);
23
+ return;
24
+ }
25
+ if (value === null) {
26
+ return;
27
+ }
28
+ if (Array.isArray(value)) {
29
+ throw new Error('Array is not supported in stylex.defineVars');
30
+ }
31
+ if (typeof value === 'object') {
32
+ if (value.default === undefined) {
33
+ throw new Error('Default value is not defined for ' + key + ' variable.');
34
+ }
35
+ for (const atRule of Object.keys(value)) {
36
+ collectVarsByAtRule(key, {
37
+ nameHash,
38
+ value: value[atRule]
39
+ }, collection, atRule === 'default' ? atRules : [...atRules, atRule]);
40
+ }
41
+ }
42
+ }
43
+ function wrapWithAtRules(ltr, atRule) {
44
+ return atRule.split(SPLIT_TOKEN).reduce((acc, atRule) => `${atRule}{${acc}}`, ltr);
45
+ }
46
+ function priorityForAtRule(atRule) {
47
+ if (atRule === 'default') {
48
+ return 0;
49
+ }
50
+ return atRule.split(SPLIT_TOKEN).length;
51
+ }
52
+ function getDefaultValue(value) {
53
+ if (typeof value === 'string' || typeof value === 'number') {
54
+ return value.toString();
55
+ }
56
+ if (value == null) {
57
+ return null;
58
+ }
59
+ if (Array.isArray(value)) {
60
+ throw new Error('Array is not supported in stylex.defineVars');
61
+ }
62
+ if (typeof value === 'object') {
63
+ if (value.default === undefined) {
64
+ throw new Error('Default value is not defined for variable.');
65
+ }
66
+ return getDefaultValue(value.default);
67
+ }
68
+ throw new Error('Invalid value in stylex.defineVars');
69
+ }
@@ -0,0 +1,31 @@
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 { CSSType } from './types';
11
+
12
+ export type VarsConfigValue =
13
+ | string
14
+ | $ReadOnly<{ default: VarsConfigValue, [string]: VarsConfigValue }>;
15
+
16
+ export type VarsConfig = $ReadOnly<{
17
+ [string]: VarsConfigValue | CSSType<>,
18
+ }>;
19
+
20
+ declare export function collectVarsByAtRule(
21
+ key: string,
22
+ { +nameHash: string, +value: VarsConfigValue },
23
+ collection: { [string]: Array<string> },
24
+ atRules: Array<string>,
25
+ ): void;
26
+
27
+ declare export function wrapWithAtRules(ltr: string, atRule: string): string;
28
+
29
+ declare export function priorityForAtRule(atRule: string): number;
30
+
31
+ declare export function getDefaultValue(value: VarsConfigValue): ?string;