@stylexjs/stylex 0.3.0 → 0.4.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 (45) hide show
  1. package/README.md +18 -43
  2. package/lib/StyleXCSSTypes.d.ts +3 -1
  3. package/lib/StyleXCSSTypes.js.flow +4 -2
  4. package/lib/StyleXSheet.js +17 -3
  5. package/lib/StyleXTypes.d.ts +10 -4
  6. package/lib/StyleXTypes.js.flow +9 -6
  7. package/lib/stylex.d.ts +0 -7
  8. package/lib/stylex.js +1 -5
  9. package/lib/stylex.js.flow +0 -4
  10. package/package.json +2 -2
  11. package/lib/native/CSSCustomPropertyValue.d.ts +0 -26
  12. package/lib/native/CSSCustomPropertyValue.js +0 -27
  13. package/lib/native/CSSCustomPropertyValue.js.flow +0 -27
  14. package/lib/native/CSSLengthUnitValue.d.ts +0 -18
  15. package/lib/native/CSSLengthUnitValue.js +0 -73
  16. package/lib/native/CSSLengthUnitValue.js.flow +0 -21
  17. package/lib/native/CSSMediaQuery.d.ts +0 -25
  18. package/lib/native/CSSMediaQuery.js +0 -55
  19. package/lib/native/CSSMediaQuery.js.flow +0 -26
  20. package/lib/native/SpreadOptions.d.ts +0 -19
  21. package/lib/native/SpreadOptions.js +0 -1
  22. package/lib/native/SpreadOptions.js.flow +0 -19
  23. package/lib/native/__tests__/__snapshots__/stylex-css-var-test.js.snap +0 -48
  24. package/lib/native/__tests__/__snapshots__/stylex-test.js.snap +0 -1046
  25. package/lib/native/__tests__/parseTimeValue-test.js +0 -11
  26. package/lib/native/__tests__/stylex-css-var-test.js +0 -148
  27. package/lib/native/__tests__/stylex-test.js +0 -924
  28. package/lib/native/errorMsg.d.ts +0 -11
  29. package/lib/native/errorMsg.js +0 -13
  30. package/lib/native/errorMsg.js.flow +0 -12
  31. package/lib/native/fixContentBox.d.ts +0 -11
  32. package/lib/native/fixContentBox.js +0 -59
  33. package/lib/native/fixContentBox.js.flow +0 -11
  34. package/lib/native/flattenStyle.d.ts +0 -15
  35. package/lib/native/flattenStyle.js +0 -20
  36. package/lib/native/flattenStyle.js.flow +0 -20
  37. package/lib/native/parseShadow.d.ts +0 -18
  38. package/lib/native/parseShadow.js +0 -36
  39. package/lib/native/parseShadow.js.flow +0 -19
  40. package/lib/native/parseTimeValue.d.ts +0 -11
  41. package/lib/native/parseTimeValue.js +0 -18
  42. package/lib/native/parseTimeValue.js.flow +0 -12
  43. package/lib/native/stylex.d.ts +0 -50
  44. package/lib/native/stylex.js +0 -393
  45. package/lib/native/stylex.js.flow +0 -60
package/README.md CHANGED
@@ -84,14 +84,14 @@ const styles = stylex.create({
84
84
  root: {
85
85
  width: '100%',
86
86
  color: 'rgb(60,60,60)',
87
- '@media (min-width: 800px)': {
88
- maxWidth: '800px',
87
+ maxWidth: {
88
+ '@media (min-width: 800px)': '800px',
89
89
  },
90
90
  },
91
91
  highlighted: {
92
92
  color: 'yellow',
93
- ':hover': {
94
- opacity: '0.9',
93
+ opacity: {
94
+ ':hover': '0.9',
95
95
  },
96
96
  },
97
97
  });
@@ -127,7 +127,7 @@ const styles = stylex.create({
127
127
 
128
128
  function InternalComponent(props) {
129
129
  return (
130
- <div {...props} {...stylex.props([styles.internalRoot, props.style])} />
130
+ <div {...props} {...stylex.props(styles.internalRoot, props.style)} />
131
131
  );
132
132
  }
133
133
 
@@ -150,12 +150,6 @@ style property values passed in via props.
150
150
  <div {...stylex.props(props.style, styles.root)} />
151
151
  ```
152
152
 
153
- You can even mix compiled styles with inline styles
154
-
155
- ```tsx
156
- <div {...stylex.props(styles.root, { opacity })} />
157
- ```
158
-
159
153
  ### stylex.firstThatWorks()
160
154
 
161
155
  Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful
@@ -185,15 +179,15 @@ This is equivalent to defining CSS as follows:
185
179
 
186
180
  StyleX comes with full support for Static Types.
187
181
 
188
- ### `XStyle<>`
182
+ ### `StyleXStyles<>`
189
183
 
190
- The most common type you might need to use is `XStyle<>`. This lets you accept
184
+ The most common type you might need to use is `StyleXStyles<>`. This lets you accept
191
185
  an object of arbitrary StyleX styles.
192
186
 
193
187
  ```tsx
194
188
  type Props = {
195
189
  ...
196
- style?: XStyle<>,
190
+ style?: StyleXStyles<>,
197
191
  };
198
192
 
199
193
  function MyComponent({style, ...}: Props) {
@@ -203,53 +197,34 @@ function MyComponent({style, ...}: Props) {
203
197
  }
204
198
  ```
205
199
 
206
- ### `XStyleWithout<>`
200
+ ### `StyleXStylesWithout<>`
207
201
 
208
- To disallow specific style properties, use the `XStyleWithout<>` type.
202
+ To disallow specific style properties, use the `StyleXStylesWithout<>` type.
209
203
 
210
204
  ```tsx
211
205
  type Props = {
212
206
  // ...
213
- style?: XStyleWithout<{
214
- postion: unknown;
207
+ style?: StyleXStylesWithout<{
208
+ position: unknown;
215
209
  display: unknown;
216
210
  }>;
217
211
  };
218
212
  ```
219
213
 
220
- ### `XStyleValue<>`
221
-
222
- To accept specific style properties only, use the `XStyle<{...}>` and
223
- `XStyleValue` types. For example, to allow only color-related style props:
224
214
 
225
- ```tsx
226
- type Props = {
227
- // ...
228
- style?: XStyle<{
229
- color?: StyleXValue;
230
- backgroundColor?: StyleXValue;
231
- borderColor?: StyleXValue;
232
- borderTopColor?: StyleXValue;
233
- borderEndColor?: StyleXValue;
234
- borderBottomColor?: StyleXValue;
235
- borderStartColor?: StyleXValue;
236
- }>;
237
- };
238
- ```
239
215
 
240
- ### `XStyleValueFor<>`
216
+ ### `StaticStyles<>`
241
217
 
242
- To limit the possible values for style properties, use the `XStyleValueFor<>`
243
- type. Pass in a type argument with a union of literal types that provide the set
244
- of possible values that the style property can have. For example, if a component
218
+ To constrain the styles to specific properties and values, use the `StaticStyles<>`
219
+ type. For example, if a component
245
220
  should accept `marginTop` but only accept one of `0`, `4`, or `8` pixels as
246
221
  values.
247
222
 
248
223
  ```tsx
249
224
  type Props = {
250
225
  ...
251
- style?: XStyle<{
252
- marginTop: XStyleValueFor<0 | 4 | 8>
226
+ style?: StaticStyles<{
227
+ marginTop?: 0 | 4 | 8;
253
228
  }>,
254
229
  };
255
230
  ```
@@ -278,7 +253,7 @@ the same CSS class_ rather than creating a new one.
278
253
  One of the benefits of this approach is that the generated CSS file grows
279
254
  _logarithmically_ as you add new styled components to your app. As more style
280
255
  declarations are added to components, they are more likely to already be in use
281
- elsehwere in the app. As a result of this CSS optimization, the generated CSS
256
+ elsewhere in the app. As a result of this CSS optimization, the generated CSS
282
257
  style sheet for an app is usually small enough to be contained in a single file
283
258
  and used across routes, avoiding style recalculation and layout thrashing as
284
259
  users navigate through your app.
@@ -333,7 +333,9 @@ type fontWeight =
333
333
  | 600
334
334
  | 700
335
335
  | 800
336
- | 900;
336
+ | 900
337
+ | string
338
+ | number;
337
339
  type gap = number | string;
338
340
  type grid = gridTemplate | string;
339
341
  type gridArea = gridLine | string;
@@ -335,7 +335,9 @@ type fontWeight =
335
335
  | 600
336
336
  | 700
337
337
  | 800
338
- | 900;
338
+ | 900
339
+ | string
340
+ | number;
339
341
  type gap = number | string;
340
342
  type grid = gridTemplate | string;
341
343
  type gridArea = gridLine | string;
@@ -866,7 +868,7 @@ export type CSSProperties = $ReadOnly<{
866
868
  // NOTE: adding a non-CSS property here for support themes in Stylex.
867
869
  theme?: all | string,
868
870
 
869
- // ...$Exact<SupportedVendorSpecificCSSProperties>, for Typescript compatibility
871
+ // ...$Exact<SupportedVendorSpecificCSSProperties>, for TypeScript compatibility
870
872
  MozOsxFontSmoothing?: all | 'grayscale',
871
873
  WebkitAppearance?: all | appearance,
872
874
  WebkitFontSmoothing?: all | 'antialiased',
@@ -138,7 +138,7 @@ class StyleXSheet {
138
138
  if (this.rules.includes(rawRule)) {
139
139
  return;
140
140
  }
141
- const rule = this.normalizeRule(rawRule);
141
+ const rule = this.normalizeRule(addSpecificityLevel(rawRule, Math.floor(priority / 1000)));
142
142
  const insertPos = this.getInsertPositionForPriority(priority);
143
143
  this.rules.splice(insertPos, 0, rule);
144
144
  this.ruleForPriority.set(priority, rule);
@@ -149,8 +149,10 @@ class StyleXSheet {
149
149
  const sheet = tag.sheet;
150
150
  if (sheet != null) {
151
151
  try {
152
- sheet.insertRule(rule, insertPos);
153
- } catch {}
152
+ sheet.insertRule(rule, Math.min(insertPos, sheet.cssRules.length));
153
+ } catch (err) {
154
+ console.error('insertRule error', err, rule, insertPos);
155
+ }
154
156
  }
155
157
  }
156
158
  }
@@ -164,6 +166,18 @@ function addAncestorSelector(selector, ancestorSelector) {
164
166
  const rest = selector.slice(firstBracketIndex + 1);
165
167
  return `${mediaQueryPart}${ancestorSelector} ${rest}`;
166
168
  }
169
+ function addSpecificityLevel(selector, index) {
170
+ if (selector.startsWith('@keyframes')) {
171
+ return selector;
172
+ }
173
+ const pseudo = Array.from({
174
+ length: index
175
+ }).map(() => ':not(#\\#)').join('');
176
+ const lastOpenCurly = selector.includes('::') ? selector.indexOf('::') : selector.lastIndexOf('{');
177
+ const beforeCurly = selector.slice(0, lastOpenCurly);
178
+ const afterCurly = selector.slice(lastOpenCurly);
179
+ return `${beforeCurly}${pseudo}${afterCurly}`;
180
+ }
167
181
  const styleSheet = exports.styleSheet = new StyleXSheet({
168
182
  supportsVariables: true,
169
183
  rootTheme: {},
@@ -73,7 +73,7 @@ export type NestedCSSPropTypes = Partial<
73
73
  }>
74
74
  >;
75
75
 
76
- type UserAuthoredStyles = { [key: string]: unknown };
76
+ type UserAuthoredStyles = CSSPropertiesWithExtras | { [key: string]: unknown };
77
77
  export type StyleXSingleStyle = false | (null | undefined | NestedCSSPropTypes);
78
78
  // NOTE: `XStyle` has been deprecated in favor of `StaticStyles` and `StyleXStyles`.
79
79
 
@@ -178,8 +178,11 @@ type TTokens = Readonly<{
178
178
  [key: string]: string | { [key: string]: string };
179
179
  }>;
180
180
 
181
+ type UnwrapVars<T> = T extends StyleXVar<infer U> ? U : T;
181
182
  export type FlattenTokens<T extends TTokens> = Readonly<{
182
- [Key in keyof T]: T[Key] extends { [key: string]: infer X } ? X : T[Key];
183
+ [Key in keyof T]: T[Key] extends { [key: string]: infer X }
184
+ ? UnwrapVars<X>
185
+ : UnwrapVars<T[Key]>;
183
186
  }>;
184
187
 
185
188
  export type StyleX$DefineVars = <
@@ -189,16 +192,19 @@ export type StyleX$DefineVars = <
189
192
  tokens: DefaultTokens,
190
193
  ) => VarGroup<FlattenTokens<DefaultTokens>, ID>;
191
194
 
195
+ declare class ThemeKey<out VG extends VarGroup> extends String {
196
+ private varGroup: VG;
197
+ }
192
198
  export type Theme<
193
199
  T extends VarGroup<unknown, symbol>,
194
200
  Tag extends symbol = symbol,
195
201
  > = Tag &
196
202
  Readonly<{
197
- theme: StyleXClassNameFor<string, IDFromVarGroup<T>>;
203
+ theme: StyleXClassNameFor<ThemeKey<T>, IDFromVarGroup<T>>;
198
204
  }>;
199
205
 
200
206
  type OverridesForTokenType<Config extends { [key: string]: unknown }> = {
201
- [Key in keyof Config]:
207
+ [Key in keyof Config]?:
202
208
  | Config[Key]
203
209
  | { default: Config[Key]; [atRule: AtRuleStr]: Config[Key] };
204
210
  };
@@ -148,29 +148,32 @@ type TTokens = $ReadOnly<{
148
148
  [string]:
149
149
  | number
150
150
  | string
151
- | $ReadOnly<{ default: number | string, [string]: number | string }>,
151
+ | $ReadOnly<{ default: number | string, [string]: number | string }>
152
+ | StyleXVar<string | number>,
152
153
  }>;
153
154
 
155
+ type UnwrapVars<T> = T extends StyleXVar<infer U> ? U : T;
154
156
  export type FlattenTokens<T: TTokens> = {
155
157
  +[Key in keyof T]: T[Key] extends { +default: infer X, +[string]: infer Y }
156
- ? X | Y
157
- : T[Key],
158
+ ? UnwrapVars<X | Y>
159
+ : UnwrapVars<T[Key]>,
158
160
  };
159
161
 
160
162
  export type StyleX$DefineVars = <DefaultTokens: TTokens, ID: string = string>(
161
163
  tokens: DefaultTokens,
162
164
  ) => VarGroup<FlattenTokens<DefaultTokens>, ID>;
163
165
 
164
- export type Theme<
166
+ // opaque type ThemeKey<+_VG: VarGroup<{ +[string]: mixed }>>: string = string;
167
+ declare export opaque type Theme<
165
168
  +T: VarGroup<{ +[string]: mixed }, string>,
166
169
  +_Tag: string = string,
167
- > = $ReadOnly<{
170
+ >: $ReadOnly<{
168
171
  $$css: true,
169
172
  [string]: StyleXClassNameFor<string, IDFromVarGroup<T>>,
170
173
  }>;
171
174
 
172
175
  export type OverridesForTokenType<Config: { +[string]: mixed }> = {
173
- [Key in keyof Config]:
176
+ [Key in keyof Config]?:
174
177
  | Config[Key]
175
178
  | { +default: Config[Key], +[string]: Config[Key] },
176
179
  };
package/lib/stylex.d.ts CHANGED
@@ -25,7 +25,6 @@ export type {
25
25
  StaticStyles,
26
26
  StaticStylesWithout,
27
27
  } from './StyleXTypes';
28
- import injectStyle from './stylex-inject';
29
28
  export declare function props(
30
29
  this: null | undefined | unknown,
31
30
  ...styles: ReadonlyArray<
@@ -113,7 +112,6 @@ export declare const keyframes: (keyframes: Keyframes) => string;
113
112
  export declare const firstThatWorks: <T extends string | number>(
114
113
  ...styles: ReadonlyArray<T>
115
114
  ) => ReadonlyArray<T>;
116
- export declare const inject: typeof injectStyle;
117
115
  type IStyleX = {
118
116
  (
119
117
  ...styles: ReadonlyArray<
@@ -141,11 +139,6 @@ type IStyleX = {
141
139
  firstThatWorks: <T extends string | number>(
142
140
  ...v: ReadonlyArray<T>
143
141
  ) => ReadonlyArray<T>;
144
- inject: (
145
- ltrRule: string,
146
- priority: number,
147
- rtlRule: null | undefined | string,
148
- ) => void;
149
142
  keyframes: (keyframes: Keyframes) => string;
150
143
  __customProperties?: { [$$Key$$: string]: unknown };
151
144
  };
package/lib/stylex.js CHANGED
@@ -4,12 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.__monkey_patch__ = __monkey_patch__;
7
- exports.keyframes = exports.inject = exports.include = exports.firstThatWorks = exports.defineVars = exports.default = exports.createTheme = exports.create = void 0;
7
+ exports.keyframes = exports.include = exports.firstThatWorks = exports.defineVars = exports.default = exports.createTheme = exports.create = void 0;
8
8
  exports.props = props;
9
9
  exports.types = exports.stylex = void 0;
10
- var _stylexInject = _interopRequireDefault(require("./stylex-inject"));
11
10
  var _styleq = require("styleq");
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
11
  function props() {
14
12
  const options = this;
15
13
  for (var _len = arguments.length, styles = new Array(_len), _key = 0; _key < _len; _key++) {
@@ -113,7 +111,6 @@ const firstThatWorks = function () {
113
111
  throw new Error('stylex.firstThatWorks should never be called.');
114
112
  };
115
113
  exports.firstThatWorks = firstThatWorks;
116
- const inject = exports.inject = _stylexInject.default;
117
114
  function _stylex() {
118
115
  for (var _len2 = arguments.length, styles = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
119
116
  styles[_key2] = arguments[_key2];
@@ -128,7 +125,6 @@ _stylex.createTheme = createTheme;
128
125
  _stylex.include = include;
129
126
  _stylex.keyframes = keyframes;
130
127
  _stylex.firstThatWorks = firstThatWorks;
131
- _stylex.inject = inject;
132
128
  _stylex.types = types;
133
129
  const __implementations = {};
134
130
  function __monkey_patch__(key, implementation) {
@@ -27,7 +27,6 @@ export type {
27
27
  StaticStylesWithout,
28
28
  } from './StyleXTypes';
29
29
 
30
- import injectStyle from './stylex-inject';
31
30
  declare export function props(
32
31
  this: ?mixed,
33
32
  ...styles: $ReadOnlyArray<
@@ -117,8 +116,6 @@ declare export const firstThatWorks: <T: string | number>(
117
116
  ...styles: $ReadOnlyArray<T>
118
117
  ) => $ReadOnlyArray<T>;
119
118
 
120
- declare export const inject: typeof injectStyle;
121
-
122
119
  type IStyleX = {
123
120
  (...styles: $ReadOnlyArray<StyleXArray<?CompiledStyles | boolean>>): string,
124
121
  props: (
@@ -140,7 +137,6 @@ type IStyleX = {
140
137
  firstThatWorks: <T: string | number>(
141
138
  ...v: $ReadOnlyArray<T>
142
139
  ) => $ReadOnlyArray<T>,
143
- inject: (ltrRule: string, priority: number, rtlRule: ?string) => void,
144
140
  keyframes: (keyframes: Keyframes) => string,
145
141
  __customProperties?: { [string]: mixed },
146
142
  ...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stylexjs/stylex",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "A library for defining styles for optimized user interfaces.",
5
5
  "main": "lib/stylex.js",
6
6
  "types": "lib/stylex.d.ts",
@@ -19,7 +19,7 @@
19
19
  "utility-types": "^3.10.0"
20
20
  },
21
21
  "devDependencies": {
22
- "@stylexjs/scripts": "0.3.0"
22
+ "@stylexjs/scripts": "0.4.0"
23
23
  },
24
24
  "jest": {},
25
25
  "files": [
@@ -1,26 +0,0 @@
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
- /**
11
- * Either create a custom property value or return null if the input is not a string
12
- * containing a 'var(--name)' or 'var(--name, default)' sequence.
13
- *
14
- * Made this a single function to test and create to avoid parsing the RegExp twice.
15
- */
16
- export declare function createCSSCustomPropertyValue(
17
- value: string,
18
- ): CSSCustomPropertyValue | null;
19
- /**
20
- * Class representing a custom property value with an optional fallback.
21
- */
22
- export declare class CSSCustomPropertyValue {
23
- name: string;
24
- defaultValue: unknown;
25
- constructor(kebabCasePropName: string, fallback: unknown);
26
- }
@@ -1,27 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.CSSCustomPropertyValue = void 0;
7
- exports.createCSSCustomPropertyValue = createCSSCustomPropertyValue;
8
- const CUSTOM_PROPERTY_REGEX = /^var\(--([\w-]+) *(?:\)|, *(.+)\))$/;
9
- function camelize(s) {
10
- return s.replace(/-./g, x => x.toUpperCase()[1]);
11
- }
12
- function createCSSCustomPropertyValue(value) {
13
- if (typeof value === 'string') {
14
- const match = CUSTOM_PROPERTY_REGEX.exec(value);
15
- if (match) {
16
- return new CSSCustomPropertyValue(match[1], match[2]);
17
- }
18
- }
19
- return null;
20
- }
21
- class CSSCustomPropertyValue {
22
- constructor(kebabCasePropName, fallback) {
23
- this.name = camelize(kebabCasePropName);
24
- this.defaultValue = fallback ?? null;
25
- }
26
- }
27
- exports.CSSCustomPropertyValue = CSSCustomPropertyValue;
@@ -1,27 +0,0 @@
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
- /**
11
- * Either create a custom property value or return null if the input is not a string
12
- * containing a 'var(--name)' or 'var(--name, default)' sequence.
13
- *
14
- * Made this a single function to test and create to avoid parsing the RegExp twice.
15
- */
16
- declare export function createCSSCustomPropertyValue(
17
- value: string,
18
- ): CSSCustomPropertyValue | null;
19
-
20
- /**
21
- * Class representing a custom property value with an optional fallback.
22
- */
23
- declare export class CSSCustomPropertyValue {
24
- name: string;
25
- defaultValue: mixed;
26
- constructor(kebabCasePropName: string, fallback: mixed): void;
27
- }
@@ -1,18 +0,0 @@
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 { SpreadOptions } from './SpreadOptions';
11
- type CSSLengthUnitType = 'em' | 'px' | 'rem' | 'vh' | 'vmax' | 'vmin' | 'vw';
12
- export declare class CSSLengthUnitValue {
13
- static parse(inp: string): [number, CSSLengthUnitType] | null;
14
- value: number;
15
- unit: CSSLengthUnitType;
16
- constructor(value: number, unit: CSSLengthUnitType);
17
- resolvePixelValue(options: SpreadOptions): number;
18
- }
@@ -1,73 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.CSSLengthUnitValue = void 0;
7
- const LENGTH_REGEX = /^([0-9]*[.]?[0-9]+)(em|px|rem|vh|vmax|vmin|vw)$/;
8
- class CSSLengthUnitValue {
9
- static parse(inp) {
10
- const match = inp.match(LENGTH_REGEX);
11
- if (match == null) {
12
- return null;
13
- }
14
- const [, value, unit] = match;
15
- const parsedValue = parseFloat(value);
16
- return [parsedValue, unit];
17
- }
18
- constructor(value, unit) {
19
- this.value = value;
20
- this.unit = unit;
21
- }
22
- resolvePixelValue(options) {
23
- const {
24
- viewportWidth,
25
- viewportHeight,
26
- fontScale = 1,
27
- inheritedFontSize
28
- } = options;
29
- const unit = this.unit;
30
- const value = this.value;
31
- const valuePercent = value / 100;
32
- switch (unit) {
33
- case 'em':
34
- {
35
- if (inheritedFontSize == null) {
36
- return fontScale * 16 * value;
37
- } else {
38
- return inheritedFontSize * value;
39
- }
40
- }
41
- case 'px':
42
- {
43
- return value;
44
- }
45
- case 'rem':
46
- {
47
- return fontScale * 16 * value;
48
- }
49
- case 'vh':
50
- {
51
- return viewportHeight * valuePercent;
52
- }
53
- case 'vmin':
54
- {
55
- return Math.min(viewportWidth, viewportHeight) * valuePercent;
56
- }
57
- case 'vmax':
58
- {
59
- return Math.max(viewportWidth, viewportHeight) * valuePercent;
60
- }
61
- case 'vw':
62
- {
63
- return viewportWidth * valuePercent;
64
- }
65
- default:
66
- {
67
- console.error(`[stylex]: Unsupported unit of "${unit}"`);
68
- return 0;
69
- }
70
- }
71
- }
72
- }
73
- exports.CSSLengthUnitValue = CSSLengthUnitValue;
@@ -1,21 +0,0 @@
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 { SpreadOptions } from './SpreadOptions';
11
-
12
- type CSSLengthUnitType = 'em' | 'px' | 'rem' | 'vh' | 'vmax' | 'vmin' | 'vw';
13
-
14
- // TODO: this only works on simple values
15
- declare export class CSSLengthUnitValue {
16
- static parse(inp: string): [number, CSSLengthUnitType] | null;
17
- value: number;
18
- unit: CSSLengthUnitType;
19
- constructor(value: number, unit: CSSLengthUnitType): void;
20
- resolvePixelValue(options: SpreadOptions): number;
21
- }
@@ -1,25 +0,0 @@
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 type MatchObject = {
11
- width: number;
12
- height: number;
13
- direction: 'ltr' | 'rtl';
14
- };
15
- export declare class CSSMediaQuery {
16
- static isMediaQueryString(inp: string): boolean;
17
- static resolveMediaQueries(
18
- styleObj: { readonly [$$Key$$: string]: unknown },
19
- matchObj: MatchObject,
20
- ): { [$$Key$$: string]: unknown };
21
- query: string;
22
- matchedStyle: { [$$Key$$: string]: unknown };
23
- constructor(query: string, matchedStyle: { [$$Key$$: string]: unknown });
24
- resolve(matchObject: MatchObject): { [$$Key$$: string]: unknown };
25
- }
@@ -1,55 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.CSSMediaQuery = void 0;
7
- var _cssMediaquery = _interopRequireDefault(require("css-mediaquery"));
8
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
- const MQ_PREFIX = '@media';
10
- class CSSMediaQuery {
11
- static isMediaQueryString(inp) {
12
- return inp.startsWith(MQ_PREFIX);
13
- }
14
- static resolveMediaQueries(styleObj, matchObj) {
15
- const mediaQueries = [];
16
- const result = {};
17
- for (const [key, value] of Object.entries(styleObj)) {
18
- if (value instanceof CSSMediaQuery) {
19
- mediaQueries.push(value);
20
- } else {
21
- result[key] = value;
22
- }
23
- }
24
- if (mediaQueries.length > 0) {
25
- for (const mqInst of mediaQueries) {
26
- const unresolvedMatchedStyle = mqInst.resolve(matchObj);
27
- const resolvedMatchedStyle = this.resolveMediaQueries(unresolvedMatchedStyle, matchObj);
28
- for (const propName in resolvedMatchedStyle) {
29
- result[propName] = resolvedMatchedStyle[propName];
30
- }
31
- }
32
- }
33
- return result;
34
- }
35
- constructor(query, matchedStyle) {
36
- this.query = query.replace(MQ_PREFIX, '');
37
- this.matchedStyle = matchedStyle;
38
- }
39
- resolve(matchObject) {
40
- const {
41
- width,
42
- height,
43
- direction
44
- } = matchObject;
45
- const matches = _cssMediaquery.default.match(this.query, {
46
- width,
47
- height,
48
- orientation: width > height ? 'landscape' : 'portrait',
49
- 'aspect-ratio': width / height,
50
- direction: direction
51
- });
52
- return matches ? this.matchedStyle : {};
53
- }
54
- }
55
- exports.CSSMediaQuery = CSSMediaQuery;