@compiled/react 0.16.4 → 0.16.6
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.
- package/dist/browser/create-strict-api/index.d.ts +6 -4
- package/dist/browser/create-strict-api/index.js.map +1 -1
- package/dist/cjs/create-strict-api/index.d.ts +6 -4
- package/dist/cjs/create-strict-api/index.js.map +1 -1
- package/dist/esm/create-strict-api/index.d.ts +6 -4
- package/dist/esm/create-strict-api/index.js.map +1 -1
- package/package.json +1 -1
- package/src/create-strict-api/__tests__/__fixtures__/strict-api.ts +1 -1
- package/src/create-strict-api/__tests__/index.test.tsx +256 -0
- package/src/create-strict-api/index.ts +14 -12
|
@@ -9,7 +9,9 @@ type EnforceSchema<TObject> = {
|
|
|
9
9
|
type PickObjects<TObject> = {
|
|
10
10
|
[P in keyof TObject]: TObject[P] extends Record<string, unknown> ? TObject[P] : never;
|
|
11
11
|
};
|
|
12
|
-
|
|
12
|
+
type CSSStyles<TSchema extends CompiledSchema> = StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>;
|
|
13
|
+
type CSSMapStyles<TSchema extends CompiledSchema> = Record<string, CSSStyles<TSchema>>;
|
|
14
|
+
interface CompiledAPI<TSchema extends CompiledSchema> {
|
|
13
15
|
/**
|
|
14
16
|
* ## CSS
|
|
15
17
|
*
|
|
@@ -25,7 +27,7 @@ interface CompiledAPI<TSchema> {
|
|
|
25
27
|
* <div css={redText} />
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
css(styles:
|
|
30
|
+
css(styles: CSSStyles<TSchema>): StrictCSSProperties;
|
|
29
31
|
/**
|
|
30
32
|
* ## CSS Map
|
|
31
33
|
*
|
|
@@ -42,8 +44,8 @@ interface CompiledAPI<TSchema> {
|
|
|
42
44
|
* <div css={styles.solid} />
|
|
43
45
|
* ```
|
|
44
46
|
*/
|
|
45
|
-
cssMap<
|
|
46
|
-
readonly [P in keyof
|
|
47
|
+
cssMap<TStylesMap extends CSSMapStyles<TSchema>>(styles: CSSMapStyles<TSchema> & TStylesMap): {
|
|
48
|
+
readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
|
|
47
49
|
};
|
|
48
50
|
/**
|
|
49
51
|
* ## CX
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create-strict-api/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAuB,EAAE,EAA0B,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create-strict-api/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAuB,EAAE,EAA0B,MAAM,cAAc,CAAC;AAiK/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO;QACL,GAAG;YACD,MAAM,sBAAsB,EAAE,CAAC;QACjC,CAAC;QACD,MAAM;YACJ,MAAM,sBAAsB,EAAE,CAAC;QACjC,CAAC;QACD,EAAE;QACF,QAAQ;YACN,MAAM,sBAAsB,EAAE,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -9,7 +9,9 @@ type EnforceSchema<TObject> = {
|
|
|
9
9
|
type PickObjects<TObject> = {
|
|
10
10
|
[P in keyof TObject]: TObject[P] extends Record<string, unknown> ? TObject[P] : never;
|
|
11
11
|
};
|
|
12
|
-
|
|
12
|
+
type CSSStyles<TSchema extends CompiledSchema> = StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>;
|
|
13
|
+
type CSSMapStyles<TSchema extends CompiledSchema> = Record<string, CSSStyles<TSchema>>;
|
|
14
|
+
interface CompiledAPI<TSchema extends CompiledSchema> {
|
|
13
15
|
/**
|
|
14
16
|
* ## CSS
|
|
15
17
|
*
|
|
@@ -25,7 +27,7 @@ interface CompiledAPI<TSchema> {
|
|
|
25
27
|
* <div css={redText} />
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
css(styles:
|
|
30
|
+
css(styles: CSSStyles<TSchema>): StrictCSSProperties;
|
|
29
31
|
/**
|
|
30
32
|
* ## CSS Map
|
|
31
33
|
*
|
|
@@ -42,8 +44,8 @@ interface CompiledAPI<TSchema> {
|
|
|
42
44
|
* <div css={styles.solid} />
|
|
43
45
|
* ```
|
|
44
46
|
*/
|
|
45
|
-
cssMap<
|
|
46
|
-
readonly [P in keyof
|
|
47
|
+
cssMap<TStylesMap extends CSSMapStyles<TSchema>>(styles: CSSMapStyles<TSchema> & TStylesMap): {
|
|
48
|
+
readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
|
|
47
49
|
};
|
|
48
50
|
/**
|
|
49
51
|
* ## CX
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create-strict-api/index.ts"],"names":[],"mappings":";;;AACA,0CAAwD;AACxD,4CAA+E;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create-strict-api/index.ts"],"names":[],"mappings":";;;AACA,0CAAwD;AACxD,4CAA+E;AAiK/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,SAAgB,eAAe;IAC7B,OAAO;QACL,GAAG;YACD,MAAM,IAAA,8BAAsB,GAAE,CAAC;QACjC,CAAC;QACD,MAAM;YACJ,MAAM,IAAA,8BAAsB,GAAE,CAAC;QACjC,CAAC;QACD,EAAE,EAAF,cAAE;QACF,QAAQ;YACN,MAAM,IAAA,8BAAsB,GAAE,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC;AAbD,0CAaC"}
|
|
@@ -9,7 +9,9 @@ type EnforceSchema<TObject> = {
|
|
|
9
9
|
type PickObjects<TObject> = {
|
|
10
10
|
[P in keyof TObject]: TObject[P] extends Record<string, unknown> ? TObject[P] : never;
|
|
11
11
|
};
|
|
12
|
-
|
|
12
|
+
type CSSStyles<TSchema extends CompiledSchema> = StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>;
|
|
13
|
+
type CSSMapStyles<TSchema extends CompiledSchema> = Record<string, CSSStyles<TSchema>>;
|
|
14
|
+
interface CompiledAPI<TSchema extends CompiledSchema> {
|
|
13
15
|
/**
|
|
14
16
|
* ## CSS
|
|
15
17
|
*
|
|
@@ -25,7 +27,7 @@ interface CompiledAPI<TSchema> {
|
|
|
25
27
|
* <div css={redText} />
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
css(styles:
|
|
30
|
+
css(styles: CSSStyles<TSchema>): StrictCSSProperties;
|
|
29
31
|
/**
|
|
30
32
|
* ## CSS Map
|
|
31
33
|
*
|
|
@@ -42,8 +44,8 @@ interface CompiledAPI<TSchema> {
|
|
|
42
44
|
* <div css={styles.solid} />
|
|
43
45
|
* ```
|
|
44
46
|
*/
|
|
45
|
-
cssMap<
|
|
46
|
-
readonly [P in keyof
|
|
47
|
+
cssMap<TStylesMap extends CSSMapStyles<TSchema>>(styles: CSSMapStyles<TSchema> & TStylesMap): {
|
|
48
|
+
readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
|
|
47
49
|
};
|
|
48
50
|
/**
|
|
49
51
|
* ## CX
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create-strict-api/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAuB,EAAE,EAA0B,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/create-strict-api/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAuB,EAAE,EAA0B,MAAM,cAAc,CAAC;AAiK/E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+CG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO;QACL,GAAG;YACD,MAAM,sBAAsB,EAAE,CAAC;QACjC,CAAC;QACD,MAAM;YACJ,MAAM,sBAAsB,EAAE,CAAC;QACjC,CAAC;QACD,EAAE;QACF,QAAQ;YACN,MAAM,sBAAsB,EAAE,CAAC;QACjC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -86,6 +86,56 @@ describe('createStrictAPI()', () => {
|
|
|
86
86
|
|
|
87
87
|
expect(getByTestId('div')).toHaveCompiledCss('all', 'inherit');
|
|
88
88
|
});
|
|
89
|
+
|
|
90
|
+
it('should type error with css properties not in the style scope', () => {
|
|
91
|
+
// NOTE: These are split into mutliple `css()` calls to ensure the type errors are not hidden
|
|
92
|
+
// as only one will error at a time when combined into one query
|
|
93
|
+
|
|
94
|
+
const bgStyles = css({
|
|
95
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
96
|
+
// @ts-expect-error - Object literal may only specify known properties, and 'bg' does not exist in type …
|
|
97
|
+
bg: 'red',
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
const colourStyles = css({
|
|
101
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
102
|
+
// @ts-expect-error - Object literal may only specify known properties, and 'colour' does not exist in type …
|
|
103
|
+
colour: 'var(--ds-text)',
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const hoverStyles = css({
|
|
107
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
108
|
+
'&:hover': {
|
|
109
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
110
|
+
// @ts-expect-error - Object literal may only specify known properties, and 'colour' does not exist in type …
|
|
111
|
+
colour: 'var(--ds-text-hover)',
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const invalidPsuedoStyles = css({
|
|
116
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
117
|
+
// @ts-expect-error - bject literal may only specify known properties, and ''&:invalid-pseudo'' does not exist in type …
|
|
118
|
+
'&:invalid-pseudo': {
|
|
119
|
+
color: 'var(--ds-text)',
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const { getByTestId } = render(
|
|
124
|
+
<div css={[bgStyles, colourStyles, hoverStyles, invalidPsuedoStyles]} data-testid="div" />
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
expect(getByTestId('div')).toHaveCompiledCss('font-weight', 'bold');
|
|
128
|
+
expect(getByTestId('div')).toHaveCompiledCss('font-weight', 'bold', {
|
|
129
|
+
target: ':hover',
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// These still get compiled to css, even if they're not valid
|
|
133
|
+
expect(getByTestId('div')).toHaveCompiledCss('bg', 'red');
|
|
134
|
+
expect(getByTestId('div')).toHaveCompiledCss('colour', 'var(--ds-text)');
|
|
135
|
+
expect(getByTestId('div')).toHaveCompiledCss('colour', 'var(--ds-text-hover)', {
|
|
136
|
+
target: ':hover',
|
|
137
|
+
});
|
|
138
|
+
});
|
|
89
139
|
});
|
|
90
140
|
|
|
91
141
|
describe('cssMap()', () => {
|
|
@@ -152,9 +202,215 @@ describe('createStrictAPI()', () => {
|
|
|
152
202
|
|
|
153
203
|
expect(getByTestId('div')).toHaveCompiledCss('val', 'ok', { target: ':hover' });
|
|
154
204
|
});
|
|
205
|
+
|
|
206
|
+
it('should type error with css properties not in the style scope', () => {
|
|
207
|
+
// NOTE: These are split into mutliple `css()` calls to ensure the type errors are not hidden
|
|
208
|
+
// as only one will error at a time when combined into one query
|
|
209
|
+
|
|
210
|
+
const bgStyles = cssMap({
|
|
211
|
+
primary: {
|
|
212
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
213
|
+
// @ts-expect-error - Object literal may only specify known properties, and 'bg' does not exist in type …
|
|
214
|
+
bg: 'red',
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const colourStyles = cssMap({
|
|
219
|
+
primary: {
|
|
220
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
221
|
+
// @ts-expect-error - Object literal may only specify known properties, and 'colour' does not exist in type …
|
|
222
|
+
colour: 'var(--ds-text)',
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const hoverStyles = cssMap({
|
|
227
|
+
primary: {
|
|
228
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
229
|
+
'&:hover': {
|
|
230
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
231
|
+
// @ts-expect-error - Object literal may only specify known properties, and 'colour' does not exist in type …
|
|
232
|
+
colour: 'var(--ds-text-hover)',
|
|
233
|
+
},
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
const invalidPsuedoStyles = cssMap({
|
|
238
|
+
primary: {
|
|
239
|
+
fontWeight: 'bold', // just a valid property to ensure the `extends` keyword isn't working as intended
|
|
240
|
+
// @ts-expect-error - bject literal may only specify known properties, and ''&:invalid-pseudo'' does not exist in type …
|
|
241
|
+
'&:invalid-pseudo': {
|
|
242
|
+
color: 'var(--ds-text)',
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const { getByTestId } = render(
|
|
248
|
+
<div
|
|
249
|
+
css={[
|
|
250
|
+
bgStyles.primary,
|
|
251
|
+
colourStyles.primary,
|
|
252
|
+
hoverStyles.primary,
|
|
253
|
+
invalidPsuedoStyles.primary,
|
|
254
|
+
]}
|
|
255
|
+
data-testid="div"
|
|
256
|
+
/>
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
expect(getByTestId('div')).toHaveCompiledCss('font-weight', 'bold');
|
|
260
|
+
expect(getByTestId('div')).toHaveCompiledCss('font-weight', 'bold', {
|
|
261
|
+
target: ':hover',
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
// These still get compiled to css, even if they're not valid
|
|
265
|
+
expect(getByTestId('div')).toHaveCompiledCss('bg', 'red');
|
|
266
|
+
expect(getByTestId('div')).toHaveCompiledCss('colour', 'var(--ds-text)');
|
|
267
|
+
expect(getByTestId('div')).toHaveCompiledCss('colour', 'var(--ds-text-hover)', {
|
|
268
|
+
target: ':hover',
|
|
269
|
+
});
|
|
270
|
+
});
|
|
155
271
|
});
|
|
156
272
|
|
|
157
273
|
describe('XCSSProp', () => {
|
|
274
|
+
it('should allow valid values from cssMap', () => {
|
|
275
|
+
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) {
|
|
276
|
+
return <button data-testid="button" className={xcss} />;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const styles = cssMap({ bg: { background: 'var(--ds-surface)' } });
|
|
280
|
+
const { getByTestId } = render(<Button xcss={styles.bg} />);
|
|
281
|
+
|
|
282
|
+
expect(getByTestId('button')).toHaveCompiledCss('background', 'var(--ds-surface)');
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('should disallow invalid values from cssMap', () => {
|
|
286
|
+
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) {
|
|
287
|
+
return <button data-testid="button" className={xcss} />;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const styles = cssMap({ bg: { accentColor: 'red' } });
|
|
291
|
+
const { getByTestId } = render(
|
|
292
|
+
<Button
|
|
293
|
+
// @ts-expect-error — Type 'CompiledStyles<{ accentColor: "red"; }>' is not assignable to type ...
|
|
294
|
+
xcss={styles.bg}
|
|
295
|
+
/>
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
expect(getByTestId('button')).toHaveCompiledCss('accent-color', 'red');
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('should allow constrained background and pseudo', () => {
|
|
302
|
+
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', '&:hover'>> }) {
|
|
303
|
+
return <button data-testid="button" className={xcss} />;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const styles = cssMap({
|
|
307
|
+
primary: {
|
|
308
|
+
background: 'var(--ds-surface)',
|
|
309
|
+
'&:hover': { background: 'var(--ds-surface-hover)' },
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
const { getByTestId } = render(<Button xcss={styles.primary} />);
|
|
314
|
+
|
|
315
|
+
expect(getByTestId('button')).toHaveCompiledCss('background', 'var(--ds-surface)');
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('should type error on a partially invalid declaration', () => {
|
|
319
|
+
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', '&:hover'>> }) {
|
|
320
|
+
return <button data-testid="button" className={xcss} />;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const styles = cssMap({
|
|
324
|
+
bad: {
|
|
325
|
+
// @ts-expect-error — Property 'bad' is incompatible with index signature.
|
|
326
|
+
foo: 'bar',
|
|
327
|
+
color: 'var(--ds-text)',
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const { getByTestId } = render(
|
|
332
|
+
<Button
|
|
333
|
+
// @ts-expect-error — Type 'CompiledStyles<{ foo: string; color: "var(--ds-text)"; }>' is not assignable to type
|
|
334
|
+
xcss={styles.bad}
|
|
335
|
+
/>
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
expect(getByTestId('button')).toHaveCompiledCss('color', 'var(--ds-text)');
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
it('should error with values not in the strict `CompiledAPI`', () => {
|
|
342
|
+
function Button({
|
|
343
|
+
xcss,
|
|
344
|
+
}: {
|
|
345
|
+
xcss: ReturnType<typeof XCSSProp<'background' | 'color', '&:hover'>>;
|
|
346
|
+
}) {
|
|
347
|
+
return <button data-testid="button" className={xcss} />;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const styles = cssMap({
|
|
351
|
+
primary: {
|
|
352
|
+
// @ts-expect-error -- This is not in the `createStrictAPI` schema—this should be a css variable.
|
|
353
|
+
color: 'red',
|
|
354
|
+
background: 'var(--ds-surface)',
|
|
355
|
+
'&:hover': { background: 'var(--ds-surface-hover)' },
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
const { getByTestId } = render(
|
|
360
|
+
<Button
|
|
361
|
+
// @ts-expect-error -- Errors because `color` conflicts with the `XCSSProp` schema–`color` should be a css variable.
|
|
362
|
+
xcss={styles.primary}
|
|
363
|
+
/>
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
expect(getByTestId('button')).toHaveCompiledCss('background', 'var(--ds-surface)');
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
it('should error with properties not in the `XCSSProp`', () => {
|
|
370
|
+
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'color', '&:focus'>> }) {
|
|
371
|
+
return <button data-testid="button" className={xcss} />;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const styles = cssMap({
|
|
375
|
+
primary: {
|
|
376
|
+
background: 'var(--ds-surface)',
|
|
377
|
+
'&:hover': { background: 'var(--ds-surface-hover)' },
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
const { getByTestId } = render(
|
|
382
|
+
<Button
|
|
383
|
+
// @ts-expect-error -- Errors because `background` + `&:hover` are not in the `XCSSProp` schema.
|
|
384
|
+
xcss={styles.primary}
|
|
385
|
+
/>
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
expect(getByTestId('button')).toHaveCompiledCss('background', 'var(--ds-surface)');
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('should error with invalid values', () => {
|
|
392
|
+
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', '&:hover'>> }) {
|
|
393
|
+
return <button data-testid="button" className={xcss} />;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const styles = cssMap({
|
|
397
|
+
primary: {
|
|
398
|
+
// @ts-expect-error -- Fails because `foo` is not assignable to our CSSProperties whatsoever.
|
|
399
|
+
foo: 'bar',
|
|
400
|
+
background: 'var(--ds-surface)',
|
|
401
|
+
'&:hover': {
|
|
402
|
+
// This does not fail, but would if the above was removed; this should be tested in raw `cssMap` fully.
|
|
403
|
+
foo: 'bar',
|
|
404
|
+
background: 'var(--ds-surface-hover)',
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
const { getByTestId } = render(<Button xcss={styles.primary} />);
|
|
410
|
+
|
|
411
|
+
expect(getByTestId('button')).toHaveCompiledCss('background', 'var(--ds-surface)');
|
|
412
|
+
});
|
|
413
|
+
|
|
158
414
|
it('should allow valid values', () => {
|
|
159
415
|
function Button({ xcss }: { xcss: ReturnType<typeof XCSSProp<'background', never>> }) {
|
|
160
416
|
return <button data-testid="button" className={xcss} />;
|
|
@@ -18,7 +18,13 @@ type PickObjects<TObject> = {
|
|
|
18
18
|
[P in keyof TObject]: TObject[P] extends Record<string, unknown> ? TObject[P] : never;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
type CSSStyles<TSchema extends CompiledSchema> = StrictCSSProperties &
|
|
22
|
+
PseudosDeclarations &
|
|
23
|
+
EnforceSchema<TSchema>;
|
|
24
|
+
|
|
25
|
+
type CSSMapStyles<TSchema extends CompiledSchema> = Record<string, CSSStyles<TSchema>>;
|
|
26
|
+
|
|
27
|
+
interface CompiledAPI<TSchema extends CompiledSchema> {
|
|
22
28
|
/**
|
|
23
29
|
* ## CSS
|
|
24
30
|
*
|
|
@@ -34,9 +40,7 @@ interface CompiledAPI<TSchema> {
|
|
|
34
40
|
* <div css={redText} />
|
|
35
41
|
* ```
|
|
36
42
|
*/
|
|
37
|
-
css(
|
|
38
|
-
styles: StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>
|
|
39
|
-
): StrictCSSProperties;
|
|
43
|
+
css(styles: CSSStyles<TSchema>): StrictCSSProperties;
|
|
40
44
|
/**
|
|
41
45
|
* ## CSS Map
|
|
42
46
|
*
|
|
@@ -53,15 +57,13 @@ interface CompiledAPI<TSchema> {
|
|
|
53
57
|
* <div css={styles.solid} />
|
|
54
58
|
* ```
|
|
55
59
|
*/
|
|
56
|
-
cssMap<
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
>
|
|
61
|
-
>(
|
|
62
|
-
styles: TStyles
|
|
60
|
+
cssMap<TStylesMap extends CSSMapStyles<TSchema>>(
|
|
61
|
+
// We intersection type the generic both with the concrete type and the generic to ensure the output has the generic applied.
|
|
62
|
+
// Without both it would either have the input arg not have excess property check kick in allowing unexpected values or
|
|
63
|
+
// have all values set as the output making usage with XCSSProp have type violations unexpectedly.
|
|
64
|
+
styles: CSSMapStyles<TSchema> & TStylesMap
|
|
63
65
|
): {
|
|
64
|
-
readonly [P in keyof
|
|
66
|
+
readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
|
|
65
67
|
};
|
|
66
68
|
/**
|
|
67
69
|
* ## CX
|