@stylexjs/stylex 0.2.0-beta.16 → 0.2.0-beta.17

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.
@@ -1,10 +1 @@
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
- 'use strict';
1
+ "use strict";
@@ -87,27 +87,20 @@ export type NestedCSSPropTypes = $ReadOnly<{
87
87
  export type StyleXSingleStyle = false | ?NestedCSSPropTypes;
88
88
  export type XStyle<+T = NestedCSSPropTypes> = StyleXArray<false | ?T>;
89
89
  export type XStyleWithout<+T: { [string]: void, ... }> = XStyle<
90
- $ReadOnly<$Rest<NestedCSSPropTypes, $Exact<T>>>
90
+ $ReadOnly<$Rest<NestedCSSPropTypes, $Exact<T>>>,
91
91
  >;
92
92
 
93
- export type Styles = $ReadOnly<{ [namespace: string]: Style, ... }>;
94
-
95
- export type Style = $ReadOnly<{
96
- ...CSSProperties,
97
- [pseudo: string]: CSSProperties,
98
- ...
99
- }>;
100
-
101
- export type Rules = Style | CSSProperties;
102
-
103
93
  export type Keyframes = $ReadOnly<{ [name: string]: CSSProperties, ... }>;
104
94
 
105
- export type Theme = $ReadOnly<{ [constantName: string]: string, ... }>;
95
+ export type LegacyTheme = $ReadOnly<{ [constantName: string]: string, ... }>;
106
96
 
107
97
  // type CSSValue = string | number | $ReadOnlyArray<mixed>;
108
98
  type MapCSSValueToClassName = <K, V>(K, V) => StyleXClassNameFor<K, V>;
109
99
 
110
- export type MapNamespace<CSS: { ... }> = $ObjMapi<CSS, MapCSSValueToClassName>;
100
+ export type MapNamespace<CSS: { ... }> = $ReadOnly<{
101
+ ...$ObjMapi<CSS, MapCSSValueToClassName>,
102
+ $$css: true,
103
+ }>;
111
104
  // NOTE: Flow was confused by nested ObjMap so for now, nested styles
112
105
  // are typed incorrectly to be a string. This won't matter for the time being.
113
106
  // type MapStyleToClassName = (<Rule: {}>(
@@ -117,19 +110,75 @@ export type MapNamespace<CSS: { ... }> = $ObjMapi<CSS, MapCSSValueToClassName>;
117
110
  export type MapNamespaces = <CSS: { ... }>(CSS) => MapNamespace<CSS>;
118
111
 
119
112
  export type Stylex$Create = <S: { ... }>(
120
- styles: S
113
+ styles: S,
121
114
  ) => $ReadOnly<$ObjMap<S, MapNamespaces>>;
122
115
 
116
+ export type CompiledStyles = $ReadOnly<{
117
+ $$css: true,
118
+ [key: string]:
119
+ | StyleXClassName
120
+ | $ReadOnly<{ [key: string]: StyleXClassName, ... }>,
121
+ ...
122
+ }>;
123
+
123
124
  // This is the type for the variables object
124
125
  declare export opaque type StyleXVarsTheme<+Vars: { +[string]: string }>: Vars;
125
126
 
126
- export type Stylex$CreateVars = <+Vars: { +[string]: string }>(
127
- styles: Vars,
128
- config?: { themeName: string }
129
- ) => StyleXVarsTheme<$ReadOnly<$ObjMapConst<Vars, string>>>;
127
+ declare export opaque type Theme<
128
+ +Tokens: { +[string]: mixed },
129
+ // eslint-disable-next-line no-unused-vars
130
+ +ID: string = string,
131
+ >: $ReadOnly<{ [$Keys<Tokens>]: string }>;
132
+
133
+ export type TokensFromTheme<T: Theme<{ +[string]: mixed }>> = T extends Theme<
134
+ infer Tokens,
135
+ >
136
+ ? Tokens
137
+ : empty;
138
+ type IDFromTheme<T: Theme<{ +[string]: mixed }>> = T extends Theme<
139
+ { +[string]: mixed },
140
+ infer ID,
141
+ >
142
+ ? ID
143
+ : empty;
144
+
145
+ export type FlattenTokens<
146
+ T: {
147
+ +[string]: string | { +default: string, +[string]: string },
148
+ },
149
+ > = {
150
+ +[Key in keyof T]: T[Key] extends { +default: infer X, +[string]: infer Y }
151
+ ? X | Y
152
+ : T[Key],
153
+ };
154
+
155
+ export type StyleX$CreateVars = <
156
+ +DefaultTokens: {
157
+ +[string]: string | { default: string, +[string]: string },
158
+ },
159
+ +ID: string = string,
160
+ >(
161
+ tokens: DefaultTokens,
162
+ ) => Theme<FlattenTokens<DefaultTokens>, ID>;
163
+
164
+ export type Variant<
165
+ +T: Theme<{ +[string]: mixed }, string>,
166
+ +_Tag: string = string,
167
+ > = $ReadOnly<{
168
+ $$css: true,
169
+ [string]: StyleXClassNameFor<string, IDFromTheme<T>>,
170
+ }>;
130
171
 
131
- export type Stylex$OverrideVars = <+Vars: { +[string]: string }>(
132
- styles: Vars & { __themeName__: string },
133
- stylesOverride: Vars,
134
- config?: { themeName: string }
135
- ) => StyleXVarsTheme<$ReadOnly<$ObjMapConst<Vars, string>>>;
172
+ export type OverridesForTokenType<Config: { +[string]: mixed }> = {
173
+ [Key in keyof Config]:
174
+ | Config[Key]
175
+ | { +default: Config[Key], +[string]: Config[Key] },
176
+ };
177
+
178
+ export type StyleX$OverrideVars = <
179
+ +BaseTokens: Theme<{ +[string]: mixed }>,
180
+ +ID: string = string,
181
+ >(
182
+ baseTokens: BaseTokens,
183
+ overrides: OverridesForTokenType<TokensFromTheme<BaseTokens>>,
184
+ ) => Variant<BaseTokens, ID>;
@@ -5,27 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.CSSCustomPropertyValue = void 0;
7
7
  exports.isCustomPropertyValue = isCustomPropertyValue;
8
- /**
9
- * Copyright (c) Meta Platforms, Inc. and affiliates.
10
- *
11
- * This source code is licensed under the MIT license found in the
12
- * LICENSE file in the root directory of this source tree.
13
- *
14
- *
15
- */
16
-
17
8
  const CUSTOM_PROPERTY_REGEX = /^var\(--(.+)\)$/;
18
9
  function camelize(s) {
19
10
  return s.replace(/-./g, x => x.toUpperCase()[1]);
20
11
  }
21
12
  function isCustomPropertyValue(value) {
22
- return typeof value === 'string' && CUSTOM_PROPERTY_REGEX.test(value);
13
+ return typeof value === "string" && CUSTOM_PROPERTY_REGEX.test(value);
23
14
  }
24
15
  class CSSCustomPropertyValue {
25
16
  constructor(value) {
26
17
  const found = value.match(CUSTOM_PROPERTY_REGEX);
27
18
  if (found == null) {
28
- throw new Error('[stylex]: Unable to find custom property reference in input string');
19
+ throw new Error("[stylex]: Unable to find custom property reference in input string");
29
20
  }
30
21
  const [, kebabCasePropName] = found;
31
22
  this.name = camelize(kebabCasePropName);
@@ -17,6 +17,6 @@ export declare class CSSLengthUnitValue {
17
17
  viewportWidth: number,
18
18
  viewportHeight: number,
19
19
  fontScale: number,
20
- inheritedFontSize: null | undefined | number
20
+ inheritedFontSize: null | undefined | number,
21
21
  ): number;
22
22
  }
@@ -4,17 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.CSSLengthUnitValue = void 0;
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
7
  const LENGTH_REGEX = /^([0-9]*[.]?[0-9]+)(em|px|rem|vh|vmax|vmin|vw)$/;
17
- // TODO: this only works on simple values
18
8
  class CSSLengthUnitValue {
19
9
  static parse(inp) {
20
10
  const match = inp.match(LENGTH_REGEX);
@@ -23,7 +13,6 @@ class CSSLengthUnitValue {
23
13
  }
24
14
  const [, value, unit] = match;
25
15
  const parsedValue = parseFloat(value);
26
- // $FlowFixMe
27
16
  return new CSSLengthUnitValue(parsedValue, unit);
28
17
  }
29
18
  constructor(value, unit) {
@@ -35,7 +24,7 @@ class CSSLengthUnitValue {
35
24
  const value = this.value;
36
25
  const valuePercent = value / 100;
37
26
  switch (unit) {
38
- case 'em':
27
+ case "em":
39
28
  {
40
29
  if (inheritedFontSize == null) {
41
30
  return fontScale * 16 * value;
@@ -43,27 +32,27 @@ class CSSLengthUnitValue {
43
32
  return inheritedFontSize * value;
44
33
  }
45
34
  }
46
- case 'px':
35
+ case "px":
47
36
  {
48
37
  return value;
49
38
  }
50
- case 'rem':
39
+ case "rem":
51
40
  {
52
41
  return fontScale * 16 * value;
53
42
  }
54
- case 'vh':
43
+ case "vh":
55
44
  {
56
45
  return viewportHeight * valuePercent;
57
46
  }
58
- case 'vmin':
47
+ case "vmin":
59
48
  {
60
49
  return Math.min(viewportWidth, viewportHeight) * valuePercent;
61
50
  }
62
- case 'vmax':
51
+ case "vmax":
63
52
  {
64
53
  return Math.max(viewportWidth, viewportHeight) * valuePercent;
65
54
  }
66
- case 'vw':
55
+ case "vw":
67
56
  {
68
57
  return viewportWidth * valuePercent;
69
58
  }
@@ -19,6 +19,6 @@ declare export class CSSLengthUnitValue {
19
19
  viewportWidth: number,
20
20
  viewportHeight: number,
21
21
  fontScale: number,
22
- inheritedFontSize: ?number
22
+ inheritedFontSize: ?number,
23
23
  ): number;
24
24
  }
@@ -16,7 +16,7 @@ export declare class CSSMediaQuery {
16
16
  static isMediaQueryString(inp: string): boolean;
17
17
  static resolveMediaQueries(
18
18
  styleObj: { readonly [$$Key$$: string]: unknown },
19
- matchObj: MatchObject
19
+ matchObj: MatchObject,
20
20
  ): { [$$Key$$: string]: unknown };
21
21
  query: string;
22
22
  matchedStyle: { [$$Key$$: string]: unknown };
@@ -6,16 +6,7 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.CSSMediaQuery = void 0;
7
7
  var _cssMediaquery = _interopRequireDefault(require("css-mediaquery"));
8
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- /**
10
- * Copyright (c) Meta Platforms, Inc. and affiliates.
11
- *
12
- * This source code is licensed under the MIT license found in the
13
- * LICENSE file in the root directory of this source tree.
14
- *
15
- *
16
- */
17
-
18
- const MQ_PREFIX = '@media';
9
+ const MQ_PREFIX = "@media";
19
10
  class CSSMediaQuery {
20
11
  static isMediaQueryString(inp) {
21
12
  return inp.startsWith(MQ_PREFIX);
@@ -23,8 +14,6 @@ class CSSMediaQuery {
23
14
  static resolveMediaQueries(styleObj, matchObj) {
24
15
  const mediaQueries = [];
25
16
  const result = {};
26
-
27
- // collect all the media queries
28
17
  for (const [key, value] of Object.entries(styleObj)) {
29
18
  if (value instanceof CSSMediaQuery) {
30
19
  mediaQueries.push(value);
@@ -32,9 +21,6 @@ class CSSMediaQuery {
32
21
  result[key] = value;
33
22
  }
34
23
  }
35
-
36
- // check the media queries to see if any apply and if they do,
37
- // merge their styles into the result
38
24
  if (mediaQueries.length > 0) {
39
25
  for (const mqInst of mediaQueries) {
40
26
  const unresolvedMatchedStyle = mqInst.resolve(matchObj);
@@ -47,7 +33,7 @@ class CSSMediaQuery {
47
33
  return result;
48
34
  }
49
35
  constructor(query, matchedStyle) {
50
- this.query = query.replace(MQ_PREFIX, '');
36
+ this.query = query.replace(MQ_PREFIX, "");
51
37
  this.matchedStyle = matchedStyle;
52
38
  }
53
39
  resolve(matchObject) {
@@ -59,8 +45,8 @@ class CSSMediaQuery {
59
45
  const matches = _cssMediaquery.default.match(this.query, {
60
46
  width,
61
47
  height,
62
- orientation: width > height ? 'landscape' : 'portrait',
63
- 'aspect-ratio': width / height,
48
+ orientation: width > height ? "landscape" : "portrait",
49
+ "aspect-ratio": width / height,
64
50
  direction: direction
65
51
  });
66
52
  return matches ? this.matchedStyle : {};
@@ -17,7 +17,7 @@ declare export class CSSMediaQuery {
17
17
  static isMediaQueryString(inp: string): boolean;
18
18
  static resolveMediaQueries(
19
19
  styleObj: { +[string]: mixed },
20
- matchObj: MatchObject
20
+ matchObj: MatchObject,
21
21
  ): { [string]: mixed };
22
22
  query: string;
23
23
  matchedStyle: { [string]: mixed };
@@ -75,6 +75,39 @@ exports[`length units 10 "vw" units are resolved to pixels 1`] = `
75
75
  }
76
76
  `;
77
77
 
78
+ exports[`styles animation-delay 1`] = `
79
+ {
80
+ "style": {
81
+ "animationDelay": 300,
82
+ },
83
+ }
84
+ `;
85
+
86
+ exports[`styles animation-duration 1`] = `
87
+ {
88
+ "style": {
89
+ "animationDuration": 500,
90
+ },
91
+ }
92
+ `;
93
+
94
+ exports[`styles border-style 1`] = `
95
+ {
96
+ "style": {
97
+ "borderWidth": 0,
98
+ },
99
+ }
100
+ `;
101
+
102
+ exports[`styles border-style 2`] = `
103
+ {
104
+ "style": {
105
+ "borderStyle": "solid",
106
+ "borderWidth": 10,
107
+ },
108
+ }
109
+ `;
110
+
78
111
  exports[`styles box-shadow 1`] = `
79
112
  {
80
113
  "style": {
@@ -151,6 +184,38 @@ exports[`styles line-clamp 1`] = `
151
184
  }
152
185
  `;
153
186
 
187
+ exports[`styles logical border radius: endend 1`] = `
188
+ {
189
+ "style": {
190
+ "borderBottomEndRadius": 10,
191
+ },
192
+ }
193
+ `;
194
+
195
+ exports[`styles logical border radius: endstart 1`] = `
196
+ {
197
+ "style": {
198
+ "borderBottomStartRadius": 10,
199
+ },
200
+ }
201
+ `;
202
+
203
+ exports[`styles logical border radius: startend 1`] = `
204
+ {
205
+ "style": {
206
+ "borderTopEndRadius": 10,
207
+ },
208
+ }
209
+ `;
210
+
211
+ exports[`styles logical border radius: startstart 1`] = `
212
+ {
213
+ "style": {
214
+ "borderTopStartRadius": 10,
215
+ },
216
+ }
217
+ `;
218
+
154
219
  exports[`styles object-fit: contain 1`] = `
155
220
  {
156
221
  "style": {
@@ -341,6 +406,22 @@ exports[`styles transform: translate 1`] = `
341
406
  }
342
407
  `;
343
408
 
409
+ exports[`styles transition-delay 1`] = `
410
+ {
411
+ "style": {
412
+ "transitionDelay": 300,
413
+ },
414
+ }
415
+ `;
416
+
417
+ exports[`styles transition-duration 1`] = `
418
+ {
419
+ "style": {
420
+ "transitionDuration": 500,
421
+ },
422
+ }
423
+ `;
424
+
344
425
  exports[`styles user-select 1`] = `
345
426
  {
346
427
  "style": {
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+
3
+ var _parseTimeValue = require("../parseTimeValue");
4
+ const WPT_TRANSITION_DELAY_TEST_CASES = [["10.2s", 10200], ["1s", 1000], ["0.1s", 100], ["0.01s", 10], ["0.001s", 1], ["0.009s", 9], ["0s", 0], [".0s", 0], [".3s", 300], ["-5s", -5000], ["10200ms", 10200], ["1000ms", 1000], ["100ms", 100], ["10ms", 10], ["9ms", 9], ["1ms", 1], ["0ms", 0], ["-500ms", -500], ["foobar", 0]];
5
+ describe("parseTimeValue", () => {
6
+ it("parses time values to milliseconds", () => {
7
+ for (const [timeValue, expectedMilliseconds] of WPT_TRANSITION_DELAY_TEST_CASES) {
8
+ expect((0, _parseTimeValue.parseTimeValue)(timeValue)).toEqual(expectedMilliseconds);
9
+ }
10
+ });
11
+ });