@stylexjs/shared 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -7,9 +7,19 @@
7
7
  *
8
8
  */
9
9
 
10
- type ValueWithDefault<T> =
11
- | T
12
- | Readonly<{ default: T; [$$Key$$: string]: ValueWithDefault<T> }>;
10
+ type NestedWithNumbers =
11
+ | number
12
+ | string
13
+ | Readonly<{
14
+ default: NestedWithNumbers;
15
+ [$$Key$$: string]: NestedWithNumbers;
16
+ }>;
17
+ type ValueWithDefault =
18
+ | string
19
+ | Readonly<{
20
+ default: ValueWithDefault;
21
+ [$$Key$$: string]: ValueWithDefault;
22
+ }>;
13
23
  type CSSSyntax =
14
24
  | '*'
15
25
  | '<length>'
@@ -26,180 +36,173 @@ type CSSSyntax =
26
36
  | '<transform-function>'
27
37
  | '<custom-ident>'
28
38
  | '<transform-list>';
29
- type CSSSyntaxType = CSSSyntax | ReadonlyArray<CSSSyntax>;
30
- export declare class CSSType {}
31
- export interface ICSSType<T extends string | number> {
32
- readonly value: ValueWithDefault<T>;
39
+ type CSSSyntaxType = CSSSyntax;
40
+ declare class BaseCSSType {
41
+ readonly value: ValueWithDefault;
33
42
  readonly syntax: CSSSyntaxType;
43
+ constructor(value: ValueWithDefault);
34
44
  }
45
+ export interface CSSType<_T extends string | number = string | number> {
46
+ readonly value: ValueWithDefault;
47
+ readonly syntax: CSSSyntaxType;
48
+ }
49
+ export declare const isCSSType: (
50
+ value: unknown,
51
+ ) => value is CSSType<string | number>;
35
52
  type AnguleValue = string;
36
53
  export declare class Angle<T extends AnguleValue>
37
- extends CSSType
38
- implements ICSSType<T>
54
+ extends BaseCSSType
55
+ implements CSSType<T>
39
56
  {
40
- readonly value: ValueWithDefault<T>;
57
+ readonly value: ValueWithDefault;
41
58
  readonly syntax: CSSSyntaxType;
42
59
  static readonly syntax: CSSSyntaxType;
43
- constructor(value: ValueWithDefault<T>);
44
60
  static create<T extends AnguleValue = AnguleValue>(
45
- value: ValueWithDefault<T>,
61
+ value: ValueWithDefault,
46
62
  ): Angle<T>;
47
63
  }
48
64
  export declare const angle: any;
49
65
  type ColorValue = string;
50
66
  export declare class Color<T extends ColorValue>
51
- extends CSSType
52
- implements ICSSType<T>
67
+ extends BaseCSSType
68
+ implements CSSType<T>
53
69
  {
54
- readonly value: ValueWithDefault<T>;
70
+ readonly value: ValueWithDefault;
55
71
  readonly syntax: CSSSyntaxType;
56
- constructor(value: ValueWithDefault<T>);
57
72
  static create<T extends ColorValue = ColorValue>(
58
- value: ValueWithDefault<T>,
73
+ value: ValueWithDefault,
59
74
  ): Color<T>;
60
75
  }
61
76
  export declare const color: any;
62
77
  type URLValue = string;
63
78
  export declare class Url<T extends URLValue>
64
- extends CSSType
65
- implements ICSSType<T>
79
+ extends BaseCSSType
80
+ implements CSSType<T>
66
81
  {
67
- readonly value: ValueWithDefault<T>;
82
+ readonly value: ValueWithDefault;
68
83
  readonly syntax: CSSSyntaxType;
69
- constructor(value: ValueWithDefault<T>);
70
- static create<T extends URLValue = URLValue>(
71
- value: ValueWithDefault<T>,
72
- ): Url<T>;
84
+ static create<T extends URLValue = URLValue>(value: ValueWithDefault): Url<T>;
73
85
  }
74
86
  export declare const url: any;
75
87
  type ImageValue = string;
76
88
  export declare class Image<T extends ImageValue>
77
89
  extends Url<T>
78
- implements ICSSType<T>
90
+ implements CSSType<T>
79
91
  {
80
- readonly value: ValueWithDefault<T>;
92
+ readonly value: ValueWithDefault;
81
93
  readonly syntax: CSSSyntaxType;
82
- constructor(value: ValueWithDefault<T>);
94
+ constructor(value: ValueWithDefault);
83
95
  static create<T extends ImageValue = ImageValue>(
84
- value: ValueWithDefault<T>,
96
+ value: ValueWithDefault,
85
97
  ): Image<T>;
86
98
  }
87
99
  export declare const image: any;
88
100
  type IntegerValue = number;
89
101
  export declare class Integer<T extends IntegerValue>
90
- extends CSSType
91
- implements ICSSType<T>
102
+ extends BaseCSSType
103
+ implements CSSType<T>
92
104
  {
93
- readonly value: ValueWithDefault<T>;
105
+ readonly value: ValueWithDefault;
94
106
  readonly syntax: CSSSyntaxType;
95
- constructor(value: ValueWithDefault<T>);
96
107
  static create<T extends IntegerValue = IntegerValue>(value: T): Integer<T>;
97
108
  }
98
109
  export declare const integer: any;
99
110
  type LengthPercentageValue = string;
100
- export declare class LengthPercentage<T extends LengthPercentageValue>
101
- extends CSSType
102
- implements ICSSType<string>
111
+ export declare class LengthPercentage<_T extends LengthPercentageValue>
112
+ extends BaseCSSType
113
+ implements CSSType<string>
103
114
  {
104
- readonly value: ValueWithDefault<T>;
115
+ readonly value: ValueWithDefault;
105
116
  readonly syntax: CSSSyntaxType;
106
- constructor(value: ValueWithDefault<T>);
107
- static createLength<T extends LengthPercentageValue | number>(
108
- value: ValueWithDefault<T>,
117
+ static createLength<_T extends LengthPercentageValue | number>(
118
+ value: ValueWithDefault,
109
119
  ): LengthPercentage<string>;
110
- static createPercentage<T extends LengthPercentageValue | number>(
111
- value: ValueWithDefault<T>,
120
+ static createPercentage<_T extends LengthPercentageValue | number>(
121
+ value: ValueWithDefault,
112
122
  ): LengthPercentage<string>;
113
123
  }
114
124
  export declare const lengthPercentage: any;
115
125
  type LengthValue = number | string;
116
- export declare class Length<T extends LengthValue>
126
+ export declare class Length<_T extends LengthValue>
117
127
  extends LengthPercentage<string>
118
- implements ICSSType<string>
128
+ implements CSSType<string>
119
129
  {
120
- readonly value: ValueWithDefault<string>;
130
+ readonly value: ValueWithDefault;
121
131
  readonly syntax: CSSSyntaxType;
122
- constructor(value: ValueWithDefault<T>);
123
132
  static create<T extends LengthValue = LengthValue>(
124
- value: ValueWithDefault<T>,
133
+ value: NestedWithNumbers,
125
134
  ): Length<T>;
126
135
  }
127
136
  export declare const length: any;
128
137
  type PercentageValue = string | number;
129
- export declare class Percentage<T extends PercentageValue>
138
+ export declare class Percentage<_T extends PercentageValue>
130
139
  extends LengthPercentage<string>
131
- implements ICSSType<string>
140
+ implements CSSType<string>
132
141
  {
133
- readonly value: ValueWithDefault<string>;
142
+ readonly value: ValueWithDefault;
134
143
  readonly syntax: CSSSyntaxType;
135
- constructor(value: ValueWithDefault<T>);
136
144
  static create<T extends PercentageValue = PercentageValue>(
137
- value: ValueWithDefault<T>,
145
+ value: NestedWithNumbers,
138
146
  ): Percentage<T>;
139
147
  }
140
148
  export declare const percentage: any;
141
149
  type NumberValue = number;
142
150
  export declare class Num<T extends NumberValue>
143
- extends CSSType
144
- implements ICSSType<T>
151
+ extends BaseCSSType
152
+ implements CSSType<T>
145
153
  {
146
- readonly value: ValueWithDefault<T>;
154
+ readonly value: ValueWithDefault;
147
155
  readonly syntax: CSSSyntaxType;
148
- constructor(value: ValueWithDefault<T>);
149
156
  static create<T extends NumberValue = NumberValue>(
150
- value: ValueWithDefault<T>,
157
+ value: NestedWithNumbers,
151
158
  ): Num<T>;
152
159
  }
153
160
  export declare const number: any;
154
161
  type ResolutionValue = string | 0;
155
162
  export declare class Resolution<T extends ResolutionValue>
156
- extends CSSType
157
- implements ICSSType<T>
163
+ extends BaseCSSType
164
+ implements CSSType<T>
158
165
  {
159
- readonly value: ValueWithDefault<T>;
166
+ readonly value: ValueWithDefault;
160
167
  readonly syntax: CSSSyntaxType;
161
- constructor(value: ValueWithDefault<T>);
162
168
  static create<T extends ResolutionValue = ResolutionValue>(
163
- value: ValueWithDefault<T>,
169
+ value: ValueWithDefault,
164
170
  ): Resolution<T>;
165
171
  }
166
172
  export declare const resolution: any;
167
173
  type TimeValue = string | 0;
168
174
  export declare class Time<T extends TimeValue>
169
- extends CSSType
170
- implements ICSSType<T>
175
+ extends BaseCSSType
176
+ implements CSSType<T>
171
177
  {
172
- readonly value: ValueWithDefault<T>;
178
+ readonly value: ValueWithDefault;
173
179
  readonly syntax: CSSSyntaxType;
174
- constructor(value: ValueWithDefault<T>);
175
180
  static create<T extends TimeValue = TimeValue>(
176
- value: ValueWithDefault<T>,
181
+ value: ValueWithDefault,
177
182
  ): Time<T>;
178
183
  }
179
184
  export declare const time: any;
180
185
  type TransformFunctionValue = string;
181
186
  export declare class TransformFunction<T extends TransformFunctionValue>
182
- extends CSSType
183
- implements ICSSType<T>
187
+ extends BaseCSSType
188
+ implements CSSType<T>
184
189
  {
185
- readonly value: ValueWithDefault<T>;
190
+ readonly value: ValueWithDefault;
186
191
  readonly syntax: CSSSyntaxType;
187
- constructor(value: ValueWithDefault<T>);
188
192
  static create<T extends TransformFunctionValue = TransformFunctionValue>(
189
- value: ValueWithDefault<T>,
193
+ value: ValueWithDefault,
190
194
  ): TransformFunction<T>;
191
195
  }
192
196
  export declare const transformFunction: any;
193
197
  type TransformListValue = string;
194
198
  export declare class TransformList<T extends TransformListValue>
195
- extends CSSType
196
- implements ICSSType<T>
199
+ extends BaseCSSType
200
+ implements CSSType<T>
197
201
  {
198
- readonly value: ValueWithDefault<T>;
202
+ readonly value: ValueWithDefault;
199
203
  readonly syntax: CSSSyntaxType;
200
- constructor(value: ValueWithDefault<T>);
201
204
  static create<T extends TransformListValue = TransformListValue>(
202
- value: ValueWithDefault<T>,
205
+ value: ValueWithDefault,
203
206
  ): TransformList<T>;
204
207
  }
205
208
  export declare const transformList: any;
@@ -3,40 +3,35 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.url = exports.transformList = exports.transformFunction = exports.time = exports.resolution = exports.percentage = exports.number = exports.lengthPercentage = exports.length = exports.integer = exports.image = exports.color = exports.angle = exports.Url = exports.TransformList = exports.TransformFunction = exports.Time = exports.Resolution = exports.Percentage = exports.Num = exports.LengthPercentage = exports.Length = exports.Integer = exports.Image = exports.Color = exports.CSSType = exports.Angle = void 0;
7
- class CSSType {}
8
- exports.CSSType = CSSType;
9
- class Angle extends CSSType {
10
- syntax = '<angle>';
11
- static syntax = '<angle>';
6
+ exports.url = exports.transformList = exports.transformFunction = exports.time = exports.resolution = exports.percentage = exports.number = exports.lengthPercentage = exports.length = exports.isCSSType = exports.integer = exports.image = exports.color = exports.angle = exports.Url = exports.TransformList = exports.TransformFunction = exports.Time = exports.Resolution = exports.Percentage = exports.Num = exports.LengthPercentage = exports.Length = exports.Integer = exports.Image = exports.Color = exports.Angle = void 0;
7
+ class BaseCSSType {
12
8
  constructor(value) {
13
- super();
14
9
  this.value = value;
15
10
  }
11
+ }
12
+ const isCSSType = value => {
13
+ return value instanceof BaseCSSType && value.value != null && typeof value.syntax === 'string';
14
+ };
15
+ exports.isCSSType = isCSSType;
16
+ class Angle extends BaseCSSType {
17
+ syntax = '<angle>';
18
+ static syntax = '<angle>';
16
19
  static create(value) {
17
20
  return new Angle(value);
18
21
  }
19
22
  }
20
23
  exports.Angle = Angle;
21
24
  const angle = exports.angle = Angle.create;
22
- class Color extends CSSType {
25
+ class Color extends BaseCSSType {
23
26
  syntax = '<color>';
24
- constructor(value) {
25
- super();
26
- this.value = value;
27
- }
28
27
  static create(value) {
29
28
  return new Color(value);
30
29
  }
31
30
  }
32
31
  exports.Color = Color;
33
32
  const color = exports.color = Color.create;
34
- class Url extends CSSType {
33
+ class Url extends BaseCSSType {
35
34
  syntax = '<url>';
36
- constructor(value) {
37
- super();
38
- this.value = value;
39
- }
40
35
  static create(value) {
41
36
  return new Url(value);
42
37
  }
@@ -55,24 +50,16 @@ class Image extends Url {
55
50
  }
56
51
  exports.Image = Image;
57
52
  const image = exports.image = Image.create;
58
- class Integer extends CSSType {
53
+ class Integer extends BaseCSSType {
59
54
  syntax = '<integer>';
60
- constructor(value) {
61
- super();
62
- this.value = value;
63
- }
64
55
  static create(value) {
65
- return new Integer(value);
56
+ return new Integer(convertNumberToStringUsing(String, '0')(value));
66
57
  }
67
58
  }
68
59
  exports.Integer = Integer;
69
60
  const integer = exports.integer = Integer.create;
70
- class LengthPercentage extends CSSType {
61
+ class LengthPercentage extends BaseCSSType {
71
62
  syntax = '<length-percentage>';
72
- constructor(value) {
73
- super();
74
- this.value = value;
75
- }
76
63
  static createLength(value) {
77
64
  return new LengthPercentage(convertNumberToLength(value));
78
65
  }
@@ -84,80 +71,54 @@ exports.LengthPercentage = LengthPercentage;
84
71
  const lengthPercentage = exports.lengthPercentage = LengthPercentage.createLength;
85
72
  class Length extends LengthPercentage {
86
73
  syntax = '<length>';
87
- constructor(value) {
88
- super(convertNumberToLength(value));
89
- }
90
74
  static create(value) {
91
- return new Length(value);
75
+ return new Length(convertNumberToLength(value));
92
76
  }
93
77
  }
94
78
  exports.Length = Length;
95
79
  const length = exports.length = Length.create;
96
80
  class Percentage extends LengthPercentage {
97
81
  syntax = '<percentage>';
98
- constructor(value) {
99
- super(convertNumberToPercentage(value));
100
- }
101
82
  static create(value) {
102
- return new Percentage(value);
83
+ return new Percentage(convertNumberToPercentage(value));
103
84
  }
104
85
  }
105
86
  exports.Percentage = Percentage;
106
87
  const percentage = exports.percentage = Percentage.create;
107
- class Num extends CSSType {
88
+ class Num extends BaseCSSType {
108
89
  syntax = '<number>';
109
- constructor(value) {
110
- super();
111
- this.value = value;
112
- }
113
90
  static create(value) {
114
- return new Num(value);
91
+ return new Num(convertNumberToBareString(value));
115
92
  }
116
93
  }
117
94
  exports.Num = Num;
118
95
  const number = exports.number = Num.create;
119
- class Resolution extends CSSType {
96
+ class Resolution extends BaseCSSType {
120
97
  syntax = '<resolution>';
121
- constructor(value) {
122
- super();
123
- this.value = value;
124
- }
125
98
  static create(value) {
126
99
  return new Resolution(value);
127
100
  }
128
101
  }
129
102
  exports.Resolution = Resolution;
130
103
  const resolution = exports.resolution = Resolution.create;
131
- class Time extends CSSType {
104
+ class Time extends BaseCSSType {
132
105
  syntax = '<time>';
133
- constructor(value) {
134
- super();
135
- this.value = value;
136
- }
137
106
  static create(value) {
138
107
  return new Time(value);
139
108
  }
140
109
  }
141
110
  exports.Time = Time;
142
111
  const time = exports.time = Time.create;
143
- class TransformFunction extends CSSType {
112
+ class TransformFunction extends BaseCSSType {
144
113
  syntax = '<transform-function>';
145
- constructor(value) {
146
- super();
147
- this.value = value;
148
- }
149
114
  static create(value) {
150
115
  return new TransformFunction(value);
151
116
  }
152
117
  }
153
118
  exports.TransformFunction = TransformFunction;
154
119
  const transformFunction = exports.transformFunction = TransformFunction.create;
155
- class TransformList extends CSSType {
120
+ class TransformList extends BaseCSSType {
156
121
  syntax = '<transform-list>';
157
- constructor(value) {
158
- super();
159
- this.value = value;
160
- }
161
122
  static create(value) {
162
123
  return new TransformList(value);
163
124
  }
@@ -172,20 +133,15 @@ const convertNumberToStringUsing = (transformNumber, defaultStr) => value => {
172
133
  return value;
173
134
  }
174
135
  if (typeof value === 'object') {
175
- const {
176
- default: defaultValue,
177
- ...rest
178
- } = value;
179
- const defaultResult = convertNumberToLength(defaultValue);
180
- const result = {
181
- default: typeof defaultResult === 'string' ? defaultResult : defaultStr
182
- };
183
- for (const [key, value] of Object.entries(rest)) {
184
- result[key] = convertNumberToLength(value);
136
+ const val = value;
137
+ const result = {};
138
+ for (const key of Object.keys(val)) {
139
+ result[key] = convertNumberToStringUsing(transformNumber, defaultStr)(val[key]);
185
140
  }
186
141
  return result;
187
142
  }
188
143
  return value;
189
144
  };
145
+ const convertNumberToBareString = convertNumberToStringUsing(value => String(value), '0');
190
146
  const convertNumberToLength = convertNumberToStringUsing(value => value === 0 ? '0' : `${value}px`, '0px');
191
147
  const convertNumberToPercentage = convertNumberToStringUsing(value => value === 0 ? '0' : `${value * 100}%`, '0');