@stylexjs/stylex 0.18.1 → 0.18.3

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.
@@ -12,6 +12,9 @@ import type {
12
12
  InlineStyles,
13
13
  Keyframes,
14
14
  MapNamespaces,
15
+ NestedConstsValue,
16
+ NestedStringValue,
17
+ NestedVarsValue,
15
18
  StaticStyles,
16
19
  StaticStylesWithout,
17
20
  StyleX$Create,
@@ -30,6 +33,7 @@ import type {
30
33
  StyleX$When,
31
34
  MapNamespace,
32
35
  StyleX$DefineMarker,
36
+ StyleX$Env,
33
37
  } from './types/StyleXTypes';
34
38
  import type { ValueWithDefault } from './types/StyleXUtils';
35
39
  import * as Types from './types/VarTypes';
@@ -58,6 +62,32 @@ export declare const defineConsts: StyleX$DefineConsts;
58
62
  export declare type defineConsts = typeof defineConsts;
59
63
  export declare const defineVars: StyleX$DefineVars;
60
64
  export declare type defineVars = typeof defineVars;
65
+ export declare const unstable_conditional: <
66
+ T extends { readonly default: unknown; readonly [$$Key$$: string]: unknown },
67
+ >(
68
+ _value: T,
69
+ ) => T;
70
+ export declare type unstable_conditional = typeof unstable_conditional;
71
+ export declare const unstable_defineVarsNested: <
72
+ T extends { readonly [$$Key$$: string]: NestedVarsValue },
73
+ >(
74
+ _styles: T,
75
+ ) => T;
76
+ export declare type unstable_defineVarsNested =
77
+ typeof unstable_defineVarsNested;
78
+ export declare const unstable_defineConstsNested: <
79
+ T extends { readonly [$$Key$$: string]: NestedConstsValue },
80
+ >(
81
+ _styles: T,
82
+ ) => T;
83
+ export declare type unstable_defineConstsNested =
84
+ typeof unstable_defineConstsNested;
85
+ export declare const unstable_createThemeNested: (
86
+ _baseTokens: { readonly [$$Key$$: string]: NestedStringValue },
87
+ _overrides: { readonly [$$Key$$: string]: NestedVarsValue },
88
+ ) => CompiledStyles;
89
+ export declare type unstable_createThemeNested =
90
+ typeof unstable_createThemeNested;
61
91
  export declare const defineMarker: StyleX$DefineMarker;
62
92
  export declare type defineMarker = typeof defineMarker;
63
93
  export declare const firstThatWorks: <T extends string | number>(
@@ -82,6 +112,16 @@ export declare function props(
82
112
  'data-style-src'?: string;
83
113
  style?: Readonly<{ [$$Key$$: string]: string | number }>;
84
114
  }>;
115
+ export declare function attrs(
116
+ this: null | undefined | unknown,
117
+ ...styles: ReadonlyArray<
118
+ StyleXArray<
119
+ | (null | undefined | CompiledStyles)
120
+ | boolean
121
+ | Readonly<[CompiledStyles, InlineStyles]>
122
+ >
123
+ >
124
+ ): Readonly<{ class?: string; 'data-style-src'?: string; style?: string }>;
85
125
  export declare const viewTransitionClass: (
86
126
  _viewTransitionClass: ViewTransitionClass,
87
127
  ) => string;
@@ -92,7 +132,7 @@ export declare const defaultMarker: () => MapNamespace<
92
132
  export declare type defaultMarker = typeof defaultMarker;
93
133
  export declare const when: StyleX$When;
94
134
  export declare type when = typeof when;
95
- export declare const env: Readonly<{ [$$Key$$: string]: unknown }>;
135
+ export declare const env: StyleX$Env;
96
136
  export declare type env = typeof env;
97
137
  export declare const types: {
98
138
  angle: <T extends string | 0 = string | 0>(
@@ -146,7 +186,7 @@ type IStyleX = {
146
186
  createTheme: StyleX$CreateTheme;
147
187
  defineConsts: StyleX$DefineConsts;
148
188
  defineVars: StyleX$DefineVars;
149
- env: Readonly<{ [$$Key$$: string]: unknown }>;
189
+ env: StyleX$Env;
150
190
  defaultMarker: () => MapNamespace<Readonly<{ marker: 'default-marker' }>>;
151
191
  defineMarker: StyleX$DefineMarker;
152
192
  firstThatWorks: <T extends string | number>(
@@ -168,9 +208,41 @@ type IStyleX = {
168
208
  'data-style-src'?: string;
169
209
  style?: Readonly<{ [$$Key$$: string]: string | number }>;
170
210
  }>;
211
+ attrs: (
212
+ this: null | undefined | unknown,
213
+ ...styles: ReadonlyArray<
214
+ StyleXArray<
215
+ | (null | undefined | CompiledStyles)
216
+ | boolean
217
+ | Readonly<[CompiledStyles, InlineStyles]>
218
+ >
219
+ >
220
+ ) => Readonly<{ class?: string; 'data-style-src'?: string; style?: string }>;
171
221
  viewTransitionClass: (viewTransitionClass: ViewTransitionClass) => string;
172
222
  types: typeof types;
173
223
  when: typeof when;
224
+ unstable_conditional: <
225
+ T extends {
226
+ readonly default: unknown;
227
+ readonly [$$Key$$: string]: unknown;
228
+ },
229
+ >(
230
+ value: T,
231
+ ) => T;
232
+ unstable_defineVarsNested: <
233
+ T extends { readonly [$$Key$$: string]: NestedVarsValue },
234
+ >(
235
+ tokens: T,
236
+ ) => T;
237
+ unstable_defineConstsNested: <
238
+ T extends { readonly [$$Key$$: string]: NestedConstsValue },
239
+ >(
240
+ tokens: T,
241
+ ) => T;
242
+ unstable_createThemeNested: (
243
+ baseTokens: { readonly [$$Key$$: string]: NestedStringValue },
244
+ overrides: { readonly [$$Key$$: string]: NestedVarsValue },
245
+ ) => CompiledStyles;
174
246
  __customProperties?: { [$$Key$$: string]: unknown };
175
247
  };
176
248
  export declare const legacyMerge: IStyleX;
package/lib/cjs/stylex.js CHANGED
@@ -146,6 +146,18 @@ const defineConsts = function stylexDefineConsts(_styles) {
146
146
  const defineVars = function stylexDefineVars(_styles) {
147
147
  throw errorForFn('defineVars');
148
148
  };
149
+ const unstable_conditional = function stylexConditional(_value) {
150
+ throw errorForFn('unstable_conditional');
151
+ };
152
+ const unstable_defineVarsNested = function stylexDefineVarsNested(_styles) {
153
+ throw errorForFn('unstable_defineVarsNested');
154
+ };
155
+ const unstable_defineConstsNested = function stylexDefineConstsNested(_styles) {
156
+ throw errorForFn('unstable_defineConstsNested');
157
+ };
158
+ const unstable_createThemeNested = (_baseTokens, _overrides) => {
159
+ throw errorForFn('unstable_createThemeNested');
160
+ };
149
161
  const defineMarker = () => {
150
162
  throw errorForFn('defineMarker');
151
163
  };
@@ -172,6 +184,25 @@ function props(...styles) {
172
184
  }
173
185
  return result;
174
186
  }
187
+ const toKebabCase = str => str.replace(/([A-Z])/g, '-$1').toLowerCase();
188
+ function attrs(...styles) {
189
+ const {
190
+ className,
191
+ style,
192
+ 'data-style-src': dataStyleSrc
193
+ } = props.apply(this, styles);
194
+ const result = {};
195
+ if (className != null) {
196
+ result.class = className;
197
+ }
198
+ if (style != null) {
199
+ result.style = Object.entries(style).map(([key, value]) => `${toKebabCase(key)}:${value}`).join(';');
200
+ }
201
+ if (dataStyleSrc != null) {
202
+ result['data-style-src'] = dataStyleSrc;
203
+ }
204
+ return result;
205
+ }
175
206
  const viewTransitionClass = _viewTransitionClass => {
176
207
  throw errorForFn('viewTransitionClass');
177
208
  };
@@ -237,26 +268,34 @@ const types = {
237
268
  throw errorForType('transformList');
238
269
  }
239
270
  };
240
- function _legacyMerge(...styles) {
241
- const [className] = styleqExports.styleq(styles);
242
- return className;
243
- }
244
- _legacyMerge.create = create;
245
- _legacyMerge.createTheme = createTheme;
246
- _legacyMerge.defineConsts = defineConsts;
247
- _legacyMerge.defineMarker = defineMarker;
248
- _legacyMerge.defineVars = defineVars;
249
- _legacyMerge.defaultMarker = defaultMarker;
250
- _legacyMerge.firstThatWorks = firstThatWorks;
251
- _legacyMerge.keyframes = keyframes;
252
- _legacyMerge.positionTry = positionTry;
253
- _legacyMerge.props = props;
254
- _legacyMerge.types = types;
255
- _legacyMerge.when = when;
256
- _legacyMerge.viewTransitionClass = viewTransitionClass;
257
- _legacyMerge.env = env;
258
- const legacyMerge = _legacyMerge;
271
+ const legacyMerge = /*@__PURE__*/ function () {
272
+ function _legacyMerge(...styles) {
273
+ const [className] = styleqExports.styleq(styles);
274
+ return className;
275
+ }
276
+ _legacyMerge.create = create;
277
+ _legacyMerge.createTheme = createTheme;
278
+ _legacyMerge.defineConsts = defineConsts;
279
+ _legacyMerge.defineMarker = defineMarker;
280
+ _legacyMerge.defineVars = defineVars;
281
+ _legacyMerge.defaultMarker = defaultMarker;
282
+ _legacyMerge.firstThatWorks = firstThatWorks;
283
+ _legacyMerge.keyframes = keyframes;
284
+ _legacyMerge.positionTry = positionTry;
285
+ _legacyMerge.props = props;
286
+ _legacyMerge.attrs = attrs;
287
+ _legacyMerge.types = types;
288
+ _legacyMerge.when = when;
289
+ _legacyMerge.viewTransitionClass = viewTransitionClass;
290
+ _legacyMerge.env = env;
291
+ _legacyMerge.unstable_conditional = unstable_conditional;
292
+ _legacyMerge.unstable_defineVarsNested = unstable_defineVarsNested;
293
+ _legacyMerge.unstable_defineConstsNested = unstable_defineConstsNested;
294
+ _legacyMerge.unstable_createThemeNested = unstable_createThemeNested;
295
+ return _legacyMerge;
296
+ }();
259
297
 
298
+ exports.attrs = attrs;
260
299
  exports.create = create;
261
300
  exports.createTheme = createTheme;
262
301
  exports.defaultMarker = defaultMarker;
@@ -270,5 +309,9 @@ exports.legacyMerge = legacyMerge;
270
309
  exports.positionTry = positionTry;
271
310
  exports.props = props;
272
311
  exports.types = types;
312
+ exports.unstable_conditional = unstable_conditional;
313
+ exports.unstable_createThemeNested = unstable_createThemeNested;
314
+ exports.unstable_defineConstsNested = unstable_defineConstsNested;
315
+ exports.unstable_defineVarsNested = unstable_defineVarsNested;
273
316
  exports.viewTransitionClass = viewTransitionClass;
274
317
  exports.when = when;
@@ -12,6 +12,9 @@ import type {
12
12
  InlineStyles,
13
13
  Keyframes,
14
14
  MapNamespaces,
15
+ NestedConstsValue,
16
+ NestedStringValue,
17
+ NestedVarsValue,
15
18
  StaticStyles,
16
19
  StaticStylesWithout,
17
20
  StyleX$Create,
@@ -30,6 +33,7 @@ import type {
30
33
  StyleX$When,
31
34
  MapNamespace,
32
35
  StyleX$DefineMarker,
36
+ StyleX$Env,
33
37
  } from './types/StyleXTypes';
34
38
  import type { ValueWithDefault } from './types/StyleXUtils';
35
39
  import * as Types from './types/VarTypes';
@@ -60,9 +64,32 @@ declare export const defineConsts: StyleX$DefineConsts;
60
64
 
61
65
  declare export const defineVars: StyleX$DefineVars;
62
66
 
67
+ declare export const unstable_conditional: <
68
+ const T extends { +default: unknown, +[string]: unknown },
69
+ >(
70
+ _value: T,
71
+ ) => T;
72
+
73
+ declare export const unstable_defineVarsNested: <
74
+ const T extends { +[string]: NestedVarsValue },
75
+ >(
76
+ _styles: T,
77
+ ) => T;
78
+
79
+ declare export const unstable_defineConstsNested: <
80
+ const T extends { +[string]: NestedConstsValue },
81
+ >(
82
+ _styles: T,
83
+ ) => T;
84
+
85
+ declare export const unstable_createThemeNested: (
86
+ _baseTokens: { +[string]: NestedStringValue },
87
+ _overrides: { +[string]: NestedVarsValue },
88
+ ) => CompiledStyles;
89
+
63
90
  declare export const defineMarker: StyleX$DefineMarker;
64
91
 
65
- declare export const firstThatWorks: <T: string | number>(
92
+ declare export const firstThatWorks: <T extends string | number>(
66
93
  ..._styles: ReadonlyArray<T>
67
94
  ) => ReadonlyArray<T>;
68
95
 
@@ -83,6 +110,19 @@ declare export function props(
83
110
  style?: Readonly<{ [string]: string | number }>,
84
111
  }>;
85
112
 
113
+ declare export function attrs(
114
+ this: ?unknown,
115
+ ...styles: ReadonlyArray<
116
+ StyleXArray<
117
+ ?CompiledStyles | boolean | Readonly<[CompiledStyles, InlineStyles]>,
118
+ >,
119
+ >
120
+ ): Readonly<{
121
+ class?: string,
122
+ 'data-style-src'?: string,
123
+ style?: string,
124
+ }>;
125
+
86
126
  declare export const viewTransitionClass: (
87
127
  _viewTransitionClass: ViewTransitionClass,
88
128
  ) => string;
@@ -95,34 +135,38 @@ declare export const defaultMarker: () => MapNamespace<
95
135
 
96
136
  declare export const when: StyleX$When;
97
137
 
98
- declare export const env: $ReadOnly<{ [string]: mixed }>;
138
+ declare export const env: StyleX$Env;
99
139
 
100
140
  declare export const types: {
101
- angle: <T: string | 0 = string | 0>(
141
+ angle: <T extends string | 0 = string | 0>(
102
142
  _v: ValueWithDefault<T>,
103
143
  ) => Types.Angle<T>,
104
- color: <T: string = string>(_v: ValueWithDefault<T>) => Types.Color<T>,
105
- url: <T: string = string>(_v: ValueWithDefault<T>) => Types.Url<T>,
106
- image: <T: string = string>(_v: ValueWithDefault<T>) => Types.Image<T>,
107
- integer: <T: number = number>(_v: ValueWithDefault<T>) => Types.Integer<T>,
108
- lengthPercentage: <T: number | string = number | string>(
144
+ color: <T extends string = string>(_v: ValueWithDefault<T>) => Types.Color<T>,
145
+ url: <T extends string = string>(_v: ValueWithDefault<T>) => Types.Url<T>,
146
+ image: <T extends string = string>(_v: ValueWithDefault<T>) => Types.Image<T>,
147
+ integer: <T extends number = number>(
148
+ _v: ValueWithDefault<T>,
149
+ ) => Types.Integer<T>,
150
+ lengthPercentage: <T extends number | string = number | string>(
109
151
  _v: ValueWithDefault<T>,
110
152
  ) => Types.LengthPercentage<T>,
111
- length: <T: number | string = number | string>(
153
+ length: <T extends number | string = number | string>(
112
154
  _v: ValueWithDefault<T>,
113
155
  ) => Types.Length<T>,
114
- percentage: <T: number | string = number | string>(
156
+ percentage: <T extends number | string = number | string>(
115
157
  _v: ValueWithDefault<T>,
116
158
  ) => Types.Percentage<T>,
117
- number: <T: number = number>(_v: ValueWithDefault<T>) => Types.Num<T>,
118
- resolution: <T: string = string>(
159
+ number: <T extends number = number>(_v: ValueWithDefault<T>) => Types.Num<T>,
160
+ resolution: <T extends string = string>(
119
161
  _v: ValueWithDefault<T>,
120
162
  ) => Types.Resolution<T>,
121
- time: <T: string | 0 = string | 0>(_v: ValueWithDefault<T>) => Types.Time<T>,
122
- transformFunction: <T: string = string>(
163
+ time: <T extends string | 0 = string | 0>(
164
+ _v: ValueWithDefault<T>,
165
+ ) => Types.Time<T>,
166
+ transformFunction: <T extends string = string>(
123
167
  _v: ValueWithDefault<T>,
124
168
  ) => Types.TransformFunction<T>,
125
- transformList: <T: string = string>(
169
+ transformList: <T extends string = string>(
126
170
  _v: ValueWithDefault<T>,
127
171
  ) => Types.TransformList<T>,
128
172
  };
@@ -137,14 +181,14 @@ type IStyleX = {
137
181
  createTheme: StyleX$CreateTheme,
138
182
  defineConsts: StyleX$DefineConsts,
139
183
  defineVars: StyleX$DefineVars,
140
- env: $ReadOnly<{ [string]: mixed }>,
184
+ env: StyleX$Env,
141
185
  defaultMarker: () => MapNamespace<
142
186
  Readonly<{
143
187
  marker: 'default-marker',
144
188
  }>,
145
189
  >,
146
190
  defineMarker: StyleX$DefineMarker,
147
- firstThatWorks: <T: string | number>(
191
+ firstThatWorks: <T extends string | number>(
148
192
  ...v: ReadonlyArray<T>
149
193
  ) => ReadonlyArray<T>,
150
194
  keyframes: (keyframes: Keyframes) => string,
@@ -161,9 +205,38 @@ type IStyleX = {
161
205
  'data-style-src'?: string,
162
206
  style?: Readonly<{ [string]: string | number }>,
163
207
  }>,
208
+ attrs: (
209
+ this: ?unknown,
210
+ ...styles: ReadonlyArray<
211
+ StyleXArray<
212
+ ?CompiledStyles | boolean | Readonly<[CompiledStyles, InlineStyles]>,
213
+ >,
214
+ >
215
+ ) => Readonly<{
216
+ class?: string,
217
+ 'data-style-src'?: string,
218
+ style?: string,
219
+ }>,
164
220
  viewTransitionClass: (viewTransitionClass: ViewTransitionClass) => string,
165
221
  types: typeof types,
166
222
  when: typeof when,
223
+ unstable_conditional: <
224
+ const T extends { +default: unknown, +[string]: unknown },
225
+ >(
226
+ value: T,
227
+ ) => T,
228
+ unstable_defineVarsNested: <const T extends { +[string]: NestedVarsValue }>(
229
+ tokens: T,
230
+ ) => T,
231
+ unstable_defineConstsNested: <
232
+ const T extends { +[string]: NestedConstsValue },
233
+ >(
234
+ tokens: T,
235
+ ) => T,
236
+ unstable_createThemeNested: (
237
+ baseTokens: { +[string]: NestedStringValue },
238
+ overrides: { +[string]: NestedVarsValue },
239
+ ) => CompiledStyles,
167
240
  __customProperties?: { [string]: unknown },
168
241
  ...
169
242
  };
@@ -11,6 +11,6 @@
11
11
  // value: T;
12
12
  // }
13
13
  // This is the type for the variables object
14
- declare export opaque type StyleXVar<+_Val: unknown>: string;
14
+ declare export opaque type StyleXVar<+_Val extends unknown>: string;
15
15
 
16
16
  declare export opaque type StyleXClassNameFor<+_K, +_V>: string;
@@ -252,12 +252,17 @@ export type IDFromVarGroup<T extends VarGroup<{}>> = T['__opaqueId'];
252
252
 
253
253
  type TTokens = Readonly<{
254
254
  [key: string]:
255
- | NestedVarObject<null | string | number>
256
- | StyleXVar<null | string | number>
255
+ | NestedVarObject<
256
+ null | string | number | StyleXVar<null | string | number>
257
+ >
257
258
  | CSSType<null | string | number>;
258
259
  }>;
259
260
 
260
- type UnwrapVars<T> = T extends StyleXVar<infer U> ? U : T;
261
+ type UnwrapVars<T> = T extends () => infer U
262
+ ? UnwrapVars<U>
263
+ : T extends StyleXVar<infer U>
264
+ ? U
265
+ : T;
261
266
  export type FlattenTokens<T extends TTokens> = Readonly<{
262
267
  [Key in keyof T]: T[Key] extends { [key: string]: infer X }
263
268
  ? UnwrapVars<X>
@@ -266,6 +271,7 @@ export type FlattenTokens<T extends TTokens> = Readonly<{
266
271
 
267
272
  type NestedVarObject<T> =
268
273
  | T
274
+ | (() => T)
269
275
  | Readonly<{
270
276
  default: NestedVarObject<T>;
271
277
  [key: AtRuleStr]: NestedVarObject<T>;
@@ -354,3 +360,56 @@ export type StyleX$When = {
354
360
  // @ts-expect-error - Trying to use a symbol in a string is not normally allowed
355
361
  ) => `:where-any-sibling(${Pseudo}, ${MarkerSymbol})`;
356
362
  };
363
+
364
+ export interface Register {}
365
+
366
+ export type StyleX$Env = Register extends { env: infer TEnv }
367
+ ? TEnv
368
+ : Readonly<{ [key: string]: unknown }>;
369
+
370
+ // === Nested API Types ===
371
+
372
+ export type NestedVarsValue =
373
+ | string
374
+ | CSSType<string | number>
375
+ | { readonly [key: string]: NestedVarsValue };
376
+
377
+ export type NestedConstsValue =
378
+ | string
379
+ | number
380
+ | { readonly [key: string]: NestedConstsValue };
381
+
382
+ export type NestedStringValue =
383
+ | string
384
+ | { readonly [key: string]: NestedStringValue };
385
+
386
+ // unstable_defineVarsNested: preserves nested key structure in output.
387
+ // Uses generic <T> to give consumers key-level autocomplete.
388
+ // Leaf values are replaced with var(--hash) strings at compile time.
389
+ export type StyleX$DefineVarsNested = <
390
+ const T extends { [key: string]: NestedVarsValue },
391
+ >(
392
+ tokens: T,
393
+ ) => T;
394
+
395
+ // unstable_defineConstsNested: same as input — values inlined at compile time.
396
+ export type StyleX$DefineConstsNested = <
397
+ const T extends { [key: string]: NestedConstsValue },
398
+ >(
399
+ tokens: T,
400
+ ) => T;
401
+
402
+ // unstable_createThemeNested: returns a flat theme object like createTheme.
403
+ export type StyleX$CreateThemeNested = (
404
+ baseTokens: { readonly [key: string]: NestedStringValue },
405
+ overrides: { readonly [key: string]: NestedVarsValue },
406
+ ) => CompiledStyles;
407
+
408
+ // unstable_conditional: identity function for type disambiguation.
409
+ // Marks a conditional @media/@supports value so the type system can
410
+ // distinguish it from namespace objects in nested token definitions.
411
+ export type StyleX$Conditional = <
412
+ const T extends { default: unknown; [key: `@${string}`]: unknown },
413
+ >(
414
+ value: T,
415
+ ) => T;