@stylexjs/stylex 0.2.0-beta.19 → 0.2.0-beta.21

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.
@@ -11,8 +11,8 @@ import type { CSSProperties } from './StyleXCSSTypes';
11
11
  declare const StyleXClassNameTag: unique symbol;
12
12
  export type StyleXClassNameFor<K, V> = string & {
13
13
  _opaque: typeof StyleXClassNameTag;
14
- key: K;
15
- value: V;
14
+ _key: K;
15
+ _value: V;
16
16
  };
17
17
 
18
18
  export type StyleXClassNameForValue<V> = StyleXClassNameFor<unknown, V>;
@@ -21,119 +21,174 @@ export type StyleXClassName = StyleXClassNameFor<unknown, unknown>;
21
21
  // Type for arbitrarily nested Array.
22
22
  export type StyleXArray<T> = T | ReadonlyArray<StyleXArray<T>>;
23
23
 
24
- type CSSPropTypes = {
25
- [Key in keyof CSSProperties]: StyleXClassNameFor<Key, CSSProperties[Key]>;
26
- };
24
+ declare const StyleXVarTag: unique symbol;
25
+ export type StyleXVar<_Val> = string & typeof StyleXVarTag;
26
+
27
+ // prettier-ignore
28
+ type NonDollarChars =
29
+ | 'a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'd' | 'D'
30
+ | 'e' | 'E' | 'f' | 'F' | 'g' | 'G'
31
+ | 'h' | 'H' | 'i' | 'I' | 'j' | 'J' | 'k' | 'K'
32
+ | 'l' | 'L' | 'm' | 'M' | 'n' | 'N' | 'o' | 'O' | 'p' | 'P'
33
+ | 'q' | 'Q' | 'r' | 'R' | 's' | 'S' | 't' | 'T'
34
+ | 'u' | 'U' | 'v' | 'V' | 'w' | 'W'
35
+ | 'x' | 'X' | 'y' | 'Y' | 'z' | 'Z'
36
+ | '-' | '_' | '@' | ':';
37
+
38
+ // Strings that don't start with a dollar sign.
39
+ // So that we can `&` with {$$css: true} without type errors.
40
+ type NonDollarStr = `${NonDollarChars}${string}`;
41
+ type PseudoClassStr = `:${string}`;
42
+ type AtRuleStr = `@${string}`;
43
+
44
+ type CondStr = PseudoClassStr | AtRuleStr;
45
+
46
+ type CSSPropertiesWithExtras = Partial<
47
+ Readonly<
48
+ CSSProperties & {
49
+ '::after': CSSProperties;
50
+ '::backdrop': CSSProperties;
51
+ '::before': CSSProperties;
52
+ '::cue': CSSProperties;
53
+ '::cue-region': CSSProperties;
54
+ '::first-letter': CSSProperties;
55
+ '::first-line': CSSProperties;
56
+ '::file-selector-button': CSSProperties;
57
+ '::grammar-error': CSSProperties;
58
+ '::marker': CSSProperties;
59
+ // This is a pattern and not a static key so it cannot be typed correctly.
60
+ // '::part()': CSSProperties;
61
+ '::placeholder': CSSProperties;
62
+ '::selection': CSSProperties;
63
+ // This is a pattern and not a static key so it cannot be typed correctly.
64
+ // '::slotted()': CSSProperties;
65
+ '::spelling-error': CSSProperties;
66
+ '::target-text': CSSProperties;
67
+ }
68
+ >
69
+ >;
27
70
 
28
- export type NestedCSSPropTypes = CSSPropTypes &
71
+ export type NestedCSSPropTypes = Partial<
29
72
  Readonly<{
30
- // NOTE: the actual type should be nested objects.
31
- // fix after the types in stylex.js are fixed.
32
- ':active': StyleXClassName;
33
- ':focus': StyleXClassName;
34
- ':focus-visible': StyleXClassName;
35
- ':hover': StyleXClassName;
36
- ':disabled': StyleXClassName;
37
- ':empty': StyleXClassName;
38
- ':first-child': StyleXClassName;
39
- ':last-child': StyleXClassName;
40
- '::before': StyleXClassName;
41
- '::after': StyleXClassName;
42
- '::placeholder': StyleXClassName;
43
- '::-webkit-scrollbar': StyleXClassName;
44
-
45
- [key: `@media (max-width: ${number}px)`]: StyleXClassName;
46
- [key: `@media (min-width: ${number}px)`]: StyleXClassName;
47
- [
48
- key: `@media (min-width: ${number}px) and (max-width: ${number}px)`
49
- ]: StyleXClassName;
50
-
51
- [key: `@media (max-height: ${number}px)`]: StyleXClassName;
52
- [key: `@media (min-height: ${number}px)`]: StyleXClassName;
53
- [
54
- key: `@media (min-height: ${number}px) and (max-height: ${number}px)`
55
- ]: StyleXClassName;
56
-
57
- [
58
- key: `@media (-webkit-min-device-pixel-ratio: ${number})`
59
- ]: StyleXClassName;
60
- '@media print': StyleXClassName;
61
-
62
- // webkit styles used for Search in Safari
63
- '::-webkit-search-decoration': StyleXClassName;
64
- '::-webkit-search-cancel-button': StyleXClassName;
65
- '::-webkit-search-results-button': StyleXClassName;
66
- '::-webkit-search-results-decoration': StyleXClassName;
67
- }>;
73
+ [Key in keyof CSSPropertiesWithExtras]: StyleXClassNameFor<
74
+ Key,
75
+ CSSPropertiesWithExtras[Key]
76
+ >;
77
+ }>
78
+ >;
68
79
 
80
+ type UserAuthoredStyles = { [key: NonDollarStr]: unknown };
69
81
  export type StyleXSingleStyle = false | (null | undefined | NestedCSSPropTypes);
70
- export type XStyle<T = NestedCSSPropTypes> = StyleXArray<
71
- false | (null | undefined | T)
72
- >;
73
- export type XStyleWithout<T extends { [$$Key$$: string]: void }> = XStyle<
82
+ export type XStyle<T extends UserAuthoredStyles = NestedCSSPropTypes> =
83
+ StyleXArray<false | (null | undefined | Readonly<T & { $$css: true }>)>;
84
+ export type XStyleWithout<T extends UserAuthoredStyles> = XStyle<
74
85
  Readonly<Pick<NestedCSSPropTypes, Exclude<keyof NestedCSSPropTypes, keyof T>>>
75
86
  >;
76
87
 
77
- export type Keyframes = Readonly<{ [name: string]: CSSProperties }>;
78
- export type LegacyTheme = Readonly<{ [constantName: string]: string }>;
88
+ export type Keyframes = Readonly<{ [name: NonDollarStr]: CSSProperties }>;
89
+ export type LegacyTheme = Readonly<{ [constantName: NonDollarStr]: string }>;
90
+
91
+ type ComplexStyleValueType<T> = T extends string | number | null
92
+ ? T
93
+ : T extends StyleXVar<infer U>
94
+ ? U
95
+ : T extends ReadonlyArray<infer U>
96
+ ? U
97
+ : T extends Readonly<{ default: infer A; [cond: CondStr]: infer B }>
98
+ ? ComplexStyleValueType<A> | ComplexStyleValueType<B>
99
+ : T;
100
+
101
+ type _MapNamespace<CSS> = Readonly<{
102
+ [Key in keyof CSS]: StyleXClassNameFor<Key, ComplexStyleValueType<CSS[Key]>>;
103
+ }>;
79
104
 
80
- type RawStyles = {
81
- [key: string]: null | string | number | Array<string | number> | RawStyles;
82
- };
105
+ export type MapNamespace<CSS> = Readonly<
106
+ _MapNamespace<CSS> & Readonly<{ $$css: true }>
107
+ >;
83
108
 
84
- type CompiledNamespace<N extends RawStyles> = {
85
- [K in keyof N]: N[K] extends string | number | null
86
- ? StyleXClassNameFor<K, N[K]>
87
- : N[K] extends ReadonlyArray<infer T>
88
- ? StyleXClassNameFor<K, T>
89
- : K extends `:${string}` | `@${string}`
90
- ? N[K] extends RawStyles
91
- ? CompiledNamespace<N[K]>
92
- : StyleXClassNameFor<K, N[K]>
93
- : N[K] extends { [key: string]: infer T }
94
- ? StyleXClassNameFor<K, T> // TODO: Handle nested objects
95
- : never;
96
- };
109
+ export type MapNamespaces<
110
+ S extends {
111
+ [key: string]: UserAuthoredStyles | ((...args: any) => UserAuthoredStyles);
112
+ },
113
+ > = Readonly<{
114
+ [Key in keyof S]: S[Key] extends (...args: infer Args) => infer Obj
115
+ ? (...args: Args) => Readonly<[MapNamespace<Obj>, InlineStyles]>
116
+ : MapNamespace<S[Key]>;
117
+ }>;
97
118
 
98
- export type Stylex$Create = <const S extends { [n: string]: RawStyles }>(
119
+ export type Stylex$Create = <
120
+ const S extends {
121
+ [key: string]: UserAuthoredStyles | ((...args: any) => UserAuthoredStyles);
122
+ },
123
+ >(
99
124
  styles: S,
100
- ) => Readonly<{
101
- [N in keyof S]: CompiledNamespace<S[N]>;
102
- }>;
125
+ ) => MapNamespaces<S>;
126
+
127
+ export type CompiledStyles = Readonly<
128
+ {
129
+ [key: NonDollarStr]:
130
+ | StyleXClassName
131
+ | Readonly<{ [key: NonDollarStr]: StyleXClassName }>;
132
+ } & { $$css: true }
133
+ >;
134
+
135
+ export type InlineStyles = Readonly<
136
+ { [key: NonDollarStr]: string } & { $$css?: void }
137
+ >;
103
138
 
104
- export type CompiledStyles = Readonly<{
105
- [key: string]: StyleXClassName | Readonly<{ [key: string]: StyleXClassName }>;
139
+ type _GenStylePropType<CSS extends UserAuthoredStyles> = Readonly<{
140
+ [Key in keyof CSS]: StyleXClassNameFor<Key, Readonly<CSS[Key]>>;
106
141
  }>;
142
+ type GenStylePropType<CSS extends UserAuthoredStyles> = Readonly<
143
+ _GenStylePropType<CSS> & { $$css: true } & Partial<{
144
+ [Key in Exclude<
145
+ keyof CSSPropertiesWithExtras,
146
+ keyof CSS | '$$css'
147
+ >]: never;
148
+ }>
149
+ >;
107
150
 
108
- type TTokens = {
109
- [key: string]: string | { default: string; [key: string]: string };
110
- };
151
+ // Replace `XStyle` with this.
152
+ export type StaticStyles<
153
+ CSS extends UserAuthoredStyles = CSSPropertiesWithExtras,
154
+ > = StyleXArray<false | null | void | GenStylePropType<CSS>>;
155
+ export type StaticStylesWithout<CSS extends UserAuthoredStyles> = StaticStyles<
156
+ Omit<CSSPropertiesWithExtras, keyof CSS>
157
+ >;
111
158
 
112
- export type FlattenTokens<
113
- T extends {
114
- [key: string]: string | { default: string; [key: string]: string };
115
- },
116
- > = {
117
- [Key in keyof T]: T[Key] extends { default: infer X } & {
118
- [key: Exclude<string, 'default'>]: infer Y;
119
- }
120
- ? X | Y
121
- : T[Key];
122
- };
159
+ export type StyleXStyles<
160
+ CSS extends UserAuthoredStyles = CSSPropertiesWithExtras,
161
+ > = StyleXArray<
162
+ | null
163
+ | void
164
+ | false
165
+ | GenStylePropType<CSS>
166
+ | Readonly<[GenStylePropType<CSS>, InlineStyles]>
167
+ >;
168
+ export type StyleXStylesWithout<CSS extends UserAuthoredStyles> = StyleXStyles<
169
+ Omit<CSSPropertiesWithExtras, keyof CSS>
170
+ >;
123
171
 
172
+ declare const StyleXThemeTag: unique symbol;
124
173
  export type Theme<
125
- Tokens extends { [key: string]: unknown },
174
+ Tokens extends { [key: NonDollarStr]: unknown },
126
175
  ID extends symbol = symbol,
127
- > = Readonly<{
128
- [_Key in Exclude<keyof Tokens, '_opaque' | '_tokens'>]: string;
129
- }> & {
130
- _opaque: ID;
131
- _tokens: Tokens;
132
- };
176
+ > = Readonly<{ [_Key in keyof Tokens]: string }> & {
177
+ $opaqueId: ID;
178
+ $tokens: Tokens;
179
+ } & typeof StyleXThemeTag;
133
180
 
134
- export type TokensFromTheme<T extends Theme<TTokens>> = T['_tokens'];
181
+ export type TokensFromTheme<T extends Theme<TTokens>> = T['$tokens'];
135
182
 
136
- export type IDFromTheme<T extends Theme<TTokens>> = T['_opaque'];
183
+ export type IDFromTheme<T extends Theme<TTokens>> = T['$opaqueId'];
184
+
185
+ type TTokens = {
186
+ [key: NonDollarStr]: string | { default: string; [key: AtRuleStr]: string };
187
+ };
188
+
189
+ export type FlattenTokens<T extends TTokens> = Readonly<{
190
+ [Key in keyof T]: T[Key] extends { [key: string]: infer X } ? X : T[Key];
191
+ }>;
137
192
 
138
193
  export type StyleX$CreateVars = <
139
194
  DefaultTokens extends TTokens,
@@ -146,14 +201,15 @@ export type Variant<
146
201
  T extends Theme<TTokens, symbol>,
147
202
  // eslint-disable-next-line no-unused-vars
148
203
  Tag extends symbol = symbol,
149
- > = Readonly<{
150
- [Key: symbol]: StyleXClassNameFor<string, IDFromTheme<T>>;
151
- }> & { _opaque: Tag };
204
+ > = Tag &
205
+ Readonly<{
206
+ theme: StyleXClassNameFor<string, IDFromTheme<T>>;
207
+ }>;
152
208
 
153
- type OverridesForTokenType<Config extends { [key: string]: unknown }> = {
209
+ type OverridesForTokenType<Config extends { [key: string]: any }> = {
154
210
  [Key in keyof Config]:
155
211
  | Config[Key]
156
- | { default: Config[Key]; [atRule: string]: Config[Key] };
212
+ | { default: Config[Key]; [atRule: AtRuleStr]: Config[Key] };
157
213
  };
158
214
 
159
215
  export type StyleX$OverrideVars = <
@@ -18,75 +18,78 @@ export type StyleXClassName = StyleXClassNameFor<mixed, mixed>;
18
18
  // Type for arbitrarily nested Array.
19
19
  export type StyleXArray<+T> = T | $ReadOnlyArray<StyleXArray<T>>;
20
20
 
21
- type CSSPropTypes = $ReadOnly<$ObjMap<CSSProperties, () => StyleXClassName>>;
22
- export type NestedCSSPropTypes = $ReadOnly<{
23
- ...$Exact<CSSPropTypes>,
24
- // NOTE: the actual type should be nested objects.
25
- // fix after the types in stylex.js are fixed.
26
- ':active'?: StyleXClassName,
27
- ':focus'?: StyleXClassName,
28
- ':focus-visible'?: StyleXClassName,
29
- ':hover'?: StyleXClassName,
30
- ':disabled'?: StyleXClassName,
31
- ':empty'?: StyleXClassName,
32
- ':first-child'?: StyleXClassName,
33
- ':last-child'?: StyleXClassName,
34
- '::before'?: StyleXClassName,
35
- '::after'?: StyleXClassName,
36
- '::placeholder'?: StyleXClassName,
37
- '::-webkit-scrollbar'?: StyleXClassName,
21
+ type CSSPropertiesWithExtras = $ReadOnly<{
22
+ ...CSSProperties,
23
+ ':active'?: CSSProperties,
24
+ ':focus'?: CSSProperties,
25
+ ':focus-visible'?: CSSProperties,
26
+ ':hover'?: CSSProperties,
27
+ ':disabled'?: CSSProperties,
28
+ ':empty'?: CSSProperties,
29
+ ':first-child'?: CSSProperties,
30
+ ':last-child'?: CSSProperties,
31
+ '::before'?: CSSProperties,
32
+ '::after'?: CSSProperties,
33
+ '::placeholder'?: CSSProperties,
34
+ '::-webkit-scrollbar'?: CSSProperties,
38
35
  // Find a better way to do this. Being forced to add every media query.
39
- '@media (max-width: 564px)'?: StyleXClassName,
40
- '@media (min-height: 700px)'?: StyleXClassName,
41
- '@media (min-height: 700px) and (max-height: 789px)'?: StyleXClassName,
42
- '@media (min-height: 753px) and (max-height: 789px)'?: StyleXClassName,
43
- '@media (min-height: 790px)'?: StyleXClassName,
44
- '@media (max-width: 648px)'?: StyleXClassName,
45
- '@media (max-width: 899px)'?: StyleXClassName,
46
- '@media (max-width: 900px)'?: StyleXClassName,
47
- '@media (min-width: 900px)'?: StyleXClassName,
48
- '@media (min-width: 900px) and (max-width: 1259px)'?: StyleXClassName,
49
- '@media (max-width: 1099px)'?: StyleXClassName,
50
- '@media (max-width: 1199px)'?: StyleXClassName,
51
- '@media (max-width: 1259px)'?: StyleXClassName,
52
- '@media (min-width: 1290px)'?: StyleXClassName,
53
- '@media (max-width: 420px)'?: StyleXClassName,
54
- '@media (max-width: 500px)'?: StyleXClassName,
55
- '@media (pointer: coarse)'?: StyleXClassName,
56
- '@media (-webkit-min-device-pixel-ratio: 0)'?: StyleXClassName,
57
- '@media print'?: StyleXClassName,
36
+ '@media (max-width: 564px)'?: CSSProperties,
37
+ '@media (min-height: 700px)'?: CSSProperties,
38
+ '@media (min-height: 700px) and (max-height: 789px)'?: CSSProperties,
39
+ '@media (min-height: 753px) and (max-height: 789px)'?: CSSProperties,
40
+ '@media (min-height: 790px)'?: CSSProperties,
41
+ '@media (max-width: 648px)'?: CSSProperties,
42
+ '@media (max-width: 899px)'?: CSSProperties,
43
+ '@media (max-width: 900px)'?: CSSProperties,
44
+ '@media (min-width: 900px)'?: CSSProperties,
45
+ '@media (min-width: 900px) and (max-width: 1259px)'?: CSSProperties,
46
+ '@media (max-width: 1099px)'?: CSSProperties,
47
+ '@media (max-width: 1199px)'?: CSSProperties,
48
+ '@media (max-width: 1259px)'?: CSSProperties,
49
+ '@media (min-width: 1290px)'?: CSSProperties,
50
+ '@media (max-width: 420px)'?: CSSProperties,
51
+ '@media (max-width: 500px)'?: CSSProperties,
52
+ '@media (pointer: coarse)'?: CSSProperties,
53
+ '@media (-webkit-min-device-pixel-ratio: 0)'?: CSSProperties,
54
+ '@media print'?: CSSProperties,
58
55
  // Media queries used for Oculus Web Design Systems (OCDS components).
59
- '@media (max-width: 767px)'?: StyleXClassName,
60
- '@media (min-width: 768px)'?: StyleXClassName,
61
- '@media (min-width: 768px) and (max-width: 1024px)'?: StyleXClassName,
62
- '@media (max-width: 1024px)'?: StyleXClassName,
63
- '@media (min-width: 1025px)'?: StyleXClassName,
64
- '@media (min-width: 1025px) and (max-width: 1920px)'?: StyleXClassName,
65
- '@media (max-width: 1920px)'?: StyleXClassName,
66
- '@media (min-width: 1921px)'?: StyleXClassName,
56
+ '@media (max-width: 767px)'?: CSSProperties,
57
+ '@media (min-width: 768px)'?: CSSProperties,
58
+ '@media (min-width: 768px) and (max-width: 1024px)'?: CSSProperties,
59
+ '@media (max-width: 1024px)'?: CSSProperties,
60
+ '@media (min-width: 1025px)'?: CSSProperties,
61
+ '@media (min-width: 1025px) and (max-width: 1920px)'?: CSSProperties,
62
+ '@media (max-width: 1920px)'?: CSSProperties,
63
+ '@media (min-width: 1921px)'?: CSSProperties,
67
64
  // Media queries used for Intern Data Products
68
- '@media (min-width: 1500px)'?: StyleXClassName,
69
- '@media (min-width: 1800px)'?: StyleXClassName,
70
- '@media (min-width: 2250px)'?: StyleXClassName,
65
+ '@media (min-width: 1500px)'?: CSSProperties,
66
+ '@media (min-width: 1800px)'?: CSSProperties,
67
+ '@media (min-width: 2250px)'?: CSSProperties,
71
68
  // webkit styles used for Search in Safari
72
- '::-webkit-search-decoration'?: StyleXClassName,
73
- '::-webkit-search-cancel-button'?: StyleXClassName,
74
- '::-webkit-search-results-button'?: StyleXClassName,
75
- '::-webkit-search-results-decoration'?: StyleXClassName,
69
+ '::-webkit-search-decoration'?: CSSProperties,
70
+ '::-webkit-search-cancel-button'?: CSSProperties,
71
+ '::-webkit-search-results-button'?: CSSProperties,
72
+ '::-webkit-search-results-decoration'?: CSSProperties,
76
73
  // Media queries used for the logged out header
77
- '@media (min-width: 950px)'?: StyleXClassName,
74
+ '@media (min-width: 950px)'?: CSSProperties,
78
75
  // Media queries used for bizweb
79
- '@media (min-width: 1440px)'?: StyleXClassName,
80
- '@media (min-width: 1920px)'?: StyleXClassName,
76
+ '@media (min-width: 1440px)'?: CSSProperties,
77
+ '@media (min-width: 1920px)'?: CSSProperties,
81
78
  // Media queries used for fbai
82
- '@media (min-width: 800px)'?: StyleXClassName,
79
+ '@media (min-width: 800px)'?: CSSProperties,
83
80
  // Media queries used for messengerkidsdotcom
84
- '@media (max-width: 1024px) and (min-width: 501px)'?: StyleXClassName,
81
+ '@media (max-width: 1024px) and (min-width: 501px)'?: CSSProperties,
82
+ }>;
83
+
84
+ export type NestedCSSPropTypes = $ReadOnly<{
85
+ [Key in keyof CSSPropertiesWithExtras]?: StyleXClassNameForKey<Key>,
85
86
  }>;
86
87
 
87
88
  export type StyleXSingleStyle = false | ?NestedCSSPropTypes;
88
- export type XStyle<+T = NestedCSSPropTypes> = StyleXArray<false | ?T>;
89
- export type XStyleWithout<+T: { [string]: void, ... }> = XStyle<
89
+ export type XStyle<+T = NestedCSSPropTypes> = StyleXArray<
90
+ false | ?$ReadOnly<{ ...T, $$css: true }>,
91
+ >;
92
+ export type XStyleWithout<+T: { +[string]: mixed }> = XStyle<
90
93
  $ReadOnly<$Rest<NestedCSSPropTypes, $Exact<T>>>,
91
94
  >;
92
95
 
@@ -94,24 +97,31 @@ export type Keyframes = $ReadOnly<{ [name: string]: CSSProperties, ... }>;
94
97
 
95
98
  export type LegacyTheme = $ReadOnly<{ [constantName: string]: string, ... }>;
96
99
 
97
- // type CSSValue = string | number | $ReadOnlyArray<mixed>;
98
- type MapCSSValueToClassName = <K, V>(K, V) => StyleXClassNameFor<K, V>;
99
-
100
- export type MapNamespace<CSS: { ... }> = $ReadOnly<{
101
- ...$ObjMapi<CSS, MapCSSValueToClassName>,
100
+ type ComplexStyleValueType<+T> = T extends string | number | null
101
+ ? T
102
+ : T extends StyleXVar<infer U>
103
+ ? U
104
+ : T extends $ReadOnlyArray<infer U>
105
+ ? U
106
+ : T extends $ReadOnly<{ default: infer A, +[string]: infer B }>
107
+ ? ComplexStyleValueType<A> | ComplexStyleValueType<B>
108
+ : T;
109
+
110
+ type _MapNamespace<+CSS: { +[string]: mixed }> = $ReadOnly<{
111
+ [Key in keyof CSS]: StyleXClassNameFor<Key, ComplexStyleValueType<CSS[Key]>>,
112
+ }>;
113
+ export type MapNamespace<+CSS: { +[string]: mixed }> = $ReadOnly<{
114
+ ..._MapNamespace<CSS>,
102
115
  $$css: true,
103
116
  }>;
104
- // NOTE: Flow was confused by nested ObjMap so for now, nested styles
105
- // are typed incorrectly to be a string. This won't matter for the time being.
106
- // type MapStyleToClassName = (<Rule: {}>(
107
- // Rule,
108
- // ) => $ObjMap<Rule, MapCSSValueToClassName>) &
109
- // MapCSSValueToClassName;
110
- export type MapNamespaces = <CSS: { ... }>(CSS) => MapNamespace<CSS>;
111
-
112
- export type Stylex$Create = <S: { ... }>(
117
+ export type MapNamespaces<+S: { +[string]: mixed }> = $ReadOnly<{
118
+ [Key in keyof S]: S[Key] extends (...args: infer Args) => infer Obj
119
+ ? (...args: Args) => $ReadOnly<[MapNamespace<Obj>, InlineStyles]>
120
+ : MapNamespace<S[Key]>,
121
+ }>;
122
+ export type Stylex$Create = <S: { +[string]: mixed }>(
113
123
  styles: S,
114
- ) => $ReadOnly<$ObjMap<S, MapNamespaces>>;
124
+ ) => MapNamespaces<S>;
115
125
 
116
126
  export type CompiledStyles = $ReadOnly<{
117
127
  $$css: true,
@@ -120,15 +130,47 @@ export type CompiledStyles = $ReadOnly<{
120
130
  | $ReadOnly<{ [key: string]: StyleXClassName, ... }>,
121
131
  ...
122
132
  }>;
133
+ export type InlineStyles = $ReadOnly<{
134
+ $$css?: void,
135
+ [key: string]: string,
136
+ }>;
123
137
 
138
+ type _GenStylePropType<+CSS: { +[string]: mixed }> = $ReadOnly<{
139
+ [Key in keyof CSS]: CSS[Key] extends { +[string]: mixed }
140
+ ? StyleXClassNameFor<Key, $ReadOnly<CSS[Key]>>
141
+ : StyleXClassNameFor<Key, CSS[Key]>,
142
+ }>;
143
+ type GenStylePropType<+CSS: { +[string]: mixed }> = $ReadOnly<{
144
+ ..._GenStylePropType<CSS>,
145
+ $$css: true,
146
+ }>;
147
+
148
+ // Replace `XStyle` with this.
149
+ export type StaticStyles<+CSS: { +[string]: mixed } = CSSPropertiesWithExtras> =
150
+ StyleXArray<false | ?GenStylePropType<$ReadOnly<CSS>>>;
151
+
152
+ export type StaticStylesWithout<+CSS: { +[string]: mixed }> = StaticStyles<
153
+ $Rest<CSSPropertiesWithExtras, $ReadOnly<CSS>>,
154
+ >;
155
+
156
+ export type StyleXStyles<+CSS: { +[string]: mixed } = CSSPropertiesWithExtras> =
157
+ StyleXArray<
158
+ | ?false
159
+ | GenStylePropType<$ReadOnly<CSS>>
160
+ | $ReadOnly<[GenStylePropType<$ReadOnly<CSS>>, InlineStyles]>,
161
+ >;
162
+
163
+ export type StyleXStylesWithout<+CSS: { +[string]: mixed }> = StyleXStyles<
164
+ $Rest<CSSPropertiesWithExtras, $ReadOnly<CSS>>,
165
+ >;
124
166
  // This is the type for the variables object
125
- declare export opaque type StyleXVarsTheme<+Vars: { +[string]: string }>: Vars;
167
+ declare export opaque type StyleXVar<+_Val: mixed>;
126
168
 
127
169
  declare export opaque type Theme<
128
170
  +Tokens: { +[string]: mixed },
129
171
  // eslint-disable-next-line no-unused-vars
130
172
  +ID: string = string,
131
- >: $ReadOnly<{ [$Keys<Tokens>]: string }>;
173
+ >: $ReadOnly<{ [Key in keyof Tokens]: StyleXVar<Tokens[Key]> }>;
132
174
 
133
175
  export type TokensFromTheme<T: Theme<{ +[string]: mixed }>> = T extends Theme<
134
176
  infer Tokens,
@@ -142,22 +184,17 @@ type IDFromTheme<T: Theme<{ +[string]: mixed }>> = T extends Theme<
142
184
  ? ID
143
185
  : empty;
144
186
 
145
- export type FlattenTokens<
146
- T: {
147
- +[string]: string | { +default: string, +[string]: string },
148
- },
149
- > = {
187
+ type TTokens = $ReadOnly<{
188
+ [string]: string | $ReadOnly<{ default: string, [string]: string }>,
189
+ }>;
190
+
191
+ export type FlattenTokens<T: TTokens> = {
150
192
  +[Key in keyof T]: T[Key] extends { +default: infer X, +[string]: infer Y }
151
193
  ? X | Y
152
194
  : T[Key],
153
195
  };
154
196
 
155
- export type StyleX$CreateVars = <
156
- +DefaultTokens: {
157
- +[string]: string | { default: string, +[string]: string },
158
- },
159
- +ID: string = string,
160
- >(
197
+ export type StyleX$CreateVars = <DefaultTokens: TTokens, ID: string = string>(
161
198
  tokens: DefaultTokens,
162
199
  ) => Theme<FlattenTokens<DefaultTokens>, ID>;
163
200
 
@@ -176,8 +213,8 @@ export type OverridesForTokenType<Config: { +[string]: mixed }> = {
176
213
  };
177
214
 
178
215
  export type StyleX$OverrideVars = <
179
- +BaseTokens: Theme<{ +[string]: mixed }>,
180
- +ID: string = string,
216
+ BaseTokens: Theme<{ +[string]: mixed }>,
217
+ ID: string = string,
181
218
  >(
182
219
  baseTokens: BaseTokens,
183
220
  overrides: OverridesForTokenType<TokensFromTheme<BaseTokens>>,
@@ -7,8 +7,20 @@
7
7
  *
8
8
  */
9
9
 
10
- export declare function isCustomPropertyValue(value: unknown): boolean;
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
+ */
11
22
  export declare class CSSCustomPropertyValue {
12
23
  name: string;
13
- constructor(value: string);
24
+ defaultValue: unknown;
25
+ constructor(kebabCasePropName: string, fallback: unknown);
14
26
  }
@@ -4,22 +4,24 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.CSSCustomPropertyValue = void 0;
7
- exports.isCustomPropertyValue = isCustomPropertyValue;
8
- const CUSTOM_PROPERTY_REGEX = /^var\(--(.+)\)$/;
7
+ exports.createCSSCustomPropertyValue = createCSSCustomPropertyValue;
8
+ const CUSTOM_PROPERTY_REGEX = /^var\(--([\w-]+) *(?:\)|, *(.+)\))$/;
9
9
  function camelize(s) {
10
10
  return s.replace(/-./g, x => x.toUpperCase()[1]);
11
11
  }
12
- function isCustomPropertyValue(value) {
13
- return typeof value === "string" && CUSTOM_PROPERTY_REGEX.test(value);
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;
14
20
  }
15
21
  class CSSCustomPropertyValue {
16
- constructor(value) {
17
- const found = value.match(CUSTOM_PROPERTY_REGEX);
18
- if (found == null) {
19
- throw new Error("[stylex]: Unable to find custom property reference in input string");
20
- }
21
- const [, kebabCasePropName] = found;
22
+ constructor(kebabCasePropName, fallback) {
22
23
  this.name = camelize(kebabCasePropName);
24
+ this.defaultValue = fallback ?? null;
23
25
  }
24
26
  }
25
27
  exports.CSSCustomPropertyValue = CSSCustomPropertyValue;
@@ -7,9 +7,21 @@
7
7
  * @flow strict
8
8
  */
9
9
 
10
- declare export function isCustomPropertyValue(value: mixed): boolean;
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;
11
19
 
20
+ /**
21
+ * Class representing a custom property value with an optional fallback.
22
+ */
12
23
  declare export class CSSCustomPropertyValue {
13
24
  name: string;
14
- constructor(value: string): void;
25
+ defaultValue: mixed;
26
+ constructor(kebabCasePropName: string, fallback: mixed): void;
15
27
  }