@compiled/react 0.16.4 → 0.16.5

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.
@@ -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
- interface CompiledAPI<TSchema> {
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: StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>): StrictCSSProperties;
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<TStyles extends Record<string, StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>>>(styles: TStyles): {
46
- readonly [P in keyof TStyles]: CompiledStyles<TStyles[P]>;
47
+ cssMap<TStylesMap extends CSSMapStyles<TSchema>>(styles: CSSMapStyles<TSchema>): {
48
+ readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
47
49
  };
48
50
  /**
49
51
  * ## CX
@@ -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
- interface CompiledAPI<TSchema> {
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: StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>): StrictCSSProperties;
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<TStyles extends Record<string, StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>>>(styles: TStyles): {
46
- readonly [P in keyof TStyles]: CompiledStyles<TStyles[P]>;
47
+ cssMap<TStylesMap extends CSSMapStyles<TSchema>>(styles: CSSMapStyles<TSchema>): {
48
+ readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
47
49
  };
48
50
  /**
49
51
  * ## CX
@@ -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
- interface CompiledAPI<TSchema> {
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: StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>): StrictCSSProperties;
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<TStyles extends Record<string, StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>>>(styles: TStyles): {
46
- readonly [P in keyof TStyles]: CompiledStyles<TStyles[P]>;
47
+ cssMap<TStylesMap extends CSSMapStyles<TSchema>>(styles: CSSMapStyles<TSchema>): {
48
+ readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
47
49
  };
48
50
  /**
49
51
  * ## CX
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@compiled/react",
3
- "version": "0.16.4",
3
+ "version": "0.16.5",
4
4
  "description": "A familiar and performant compile time CSS-in-JS library for React.",
5
5
  "keywords": [
6
6
  "compiled",
@@ -1,4 +1,4 @@
1
- import { createStrictAPI } from '@compiled/react';
1
+ import { createStrictAPI } from '../../index';
2
2
 
3
3
  const { css, XCSSProp, cssMap, cx } = createStrictAPI<{
4
4
  '&:hover': {
@@ -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,6 +202,72 @@ 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', () => {
@@ -18,7 +18,12 @@ type PickObjects<TObject> = {
18
18
  [P in keyof TObject]: TObject[P] extends Record<string, unknown> ? TObject[P] : never;
19
19
  };
20
20
 
21
- interface CompiledAPI<TSchema> {
21
+ type CSSStyles<TSchema extends CompiledSchema> = StrictCSSProperties &
22
+ PseudosDeclarations &
23
+ EnforceSchema<TSchema>;
24
+ type CSSMapStyles<TSchema extends CompiledSchema> = Record<string, CSSStyles<TSchema>>;
25
+
26
+ interface CompiledAPI<TSchema extends CompiledSchema> {
22
27
  /**
23
28
  * ## CSS
24
29
  *
@@ -34,9 +39,7 @@ interface CompiledAPI<TSchema> {
34
39
  * <div css={redText} />
35
40
  * ```
36
41
  */
37
- css(
38
- styles: StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>
39
- ): StrictCSSProperties;
42
+ css(styles: CSSStyles<TSchema>): StrictCSSProperties;
40
43
  /**
41
44
  * ## CSS Map
42
45
  *
@@ -53,15 +56,12 @@ interface CompiledAPI<TSchema> {
53
56
  * <div css={styles.solid} />
54
57
  * ```
55
58
  */
56
- cssMap<
57
- TStyles extends Record<
58
- string,
59
- StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>
60
- >
61
- >(
62
- styles: TStyles
59
+ cssMap<TStylesMap extends CSSMapStyles<TSchema>>(
60
+ // NOTE: This should match the generic `TStylesMap extends …` as we want this arg to strictly satisfy this type, not just extend it.
61
+ // The "extends" functionality is to infer and build the return type, this is to enforce the input type.
62
+ styles: CSSMapStyles<TSchema>
63
63
  ): {
64
- readonly [P in keyof TStyles]: CompiledStyles<TStyles[P]>;
64
+ readonly [P in keyof TStylesMap]: CompiledStyles<TStylesMap[P]>;
65
65
  };
66
66
  /**
67
67
  * ## CX