@compiled/react 0.16.2 → 0.16.4

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.
Files changed (66) hide show
  1. package/dist/browser/class-names/index.d.ts +1 -1
  2. package/dist/browser/create-strict-api/index.d.ts +181 -0
  3. package/dist/browser/create-strict-api/index.js +65 -0
  4. package/dist/browser/create-strict-api/index.js.map +1 -0
  5. package/dist/browser/css/index.d.ts +1 -1
  6. package/dist/browser/css-map/index.d.ts +3 -5
  7. package/dist/browser/css-map/index.js +3 -5
  8. package/dist/browser/css-map/index.js.map +1 -1
  9. package/dist/browser/index.d.ts +1 -0
  10. package/dist/browser/index.js +1 -0
  11. package/dist/browser/index.js.map +1 -1
  12. package/dist/browser/types.d.ts +7 -2
  13. package/dist/browser/utils/error.d.ts +1 -0
  14. package/dist/browser/utils/error.js +19 -0
  15. package/dist/browser/utils/error.js.map +1 -1
  16. package/dist/browser/xcss-prop/index.d.ts +14 -10
  17. package/dist/browser/xcss-prop/index.js +1 -1
  18. package/dist/browser/xcss-prop/index.js.map +1 -1
  19. package/dist/cjs/class-names/index.d.ts +1 -1
  20. package/dist/cjs/create-strict-api/index.d.ts +181 -0
  21. package/dist/cjs/create-strict-api/index.js +69 -0
  22. package/dist/cjs/create-strict-api/index.js.map +1 -0
  23. package/dist/cjs/css/index.d.ts +1 -1
  24. package/dist/cjs/css-map/index.d.ts +3 -5
  25. package/dist/cjs/css-map/index.js +3 -5
  26. package/dist/cjs/css-map/index.js.map +1 -1
  27. package/dist/cjs/index.d.ts +1 -0
  28. package/dist/cjs/index.js +3 -1
  29. package/dist/cjs/index.js.map +1 -1
  30. package/dist/cjs/types.d.ts +7 -2
  31. package/dist/cjs/utils/error.d.ts +1 -0
  32. package/dist/cjs/utils/error.js +21 -1
  33. package/dist/cjs/utils/error.js.map +1 -1
  34. package/dist/cjs/xcss-prop/index.d.ts +14 -10
  35. package/dist/cjs/xcss-prop/index.js +1 -1
  36. package/dist/cjs/xcss-prop/index.js.map +1 -1
  37. package/dist/esm/class-names/index.d.ts +1 -1
  38. package/dist/esm/create-strict-api/index.d.ts +181 -0
  39. package/dist/esm/create-strict-api/index.js +65 -0
  40. package/dist/esm/create-strict-api/index.js.map +1 -0
  41. package/dist/esm/css/index.d.ts +1 -1
  42. package/dist/esm/css-map/index.d.ts +3 -5
  43. package/dist/esm/css-map/index.js +3 -5
  44. package/dist/esm/css-map/index.js.map +1 -1
  45. package/dist/esm/index.d.ts +1 -0
  46. package/dist/esm/index.js +1 -0
  47. package/dist/esm/index.js.map +1 -1
  48. package/dist/esm/types.d.ts +7 -2
  49. package/dist/esm/utils/error.d.ts +1 -0
  50. package/dist/esm/utils/error.js +19 -0
  51. package/dist/esm/utils/error.js.map +1 -1
  52. package/dist/esm/xcss-prop/index.d.ts +14 -10
  53. package/dist/esm/xcss-prop/index.js +1 -1
  54. package/dist/esm/xcss-prop/index.js.map +1 -1
  55. package/package.json +2 -1
  56. package/src/class-names/index.ts +1 -1
  57. package/src/create-strict-api/__tests__/__fixtures__/strict-api.ts +13 -0
  58. package/src/create-strict-api/__tests__/index.test.tsx +312 -0
  59. package/src/create-strict-api/__tests__/package.test.tsx +21 -0
  60. package/src/create-strict-api/index.ts +223 -0
  61. package/src/css/index.ts +1 -1
  62. package/src/css-map/index.ts +3 -5
  63. package/src/index.ts +1 -0
  64. package/src/types.ts +8 -2
  65. package/src/utils/error.ts +20 -0
  66. package/src/xcss-prop/index.ts +33 -10
@@ -0,0 +1,223 @@
1
+ import type { StrictCSSProperties, CSSPseudos } from '../types';
2
+ import { createStrictSetupError } from '../utils/error';
3
+ import { type CompiledStyles, cx, type Internal$XCSSProp } from '../xcss-prop';
4
+
5
+ type PseudosDeclarations = {
6
+ [Q in CSSPseudos]?: StrictCSSProperties;
7
+ };
8
+
9
+ type EnforceSchema<TObject> = {
10
+ [P in keyof TObject]?: P extends keyof CompiledSchema
11
+ ? TObject[P] extends Record<string, unknown>
12
+ ? EnforceSchema<TObject[P]>
13
+ : TObject[P]
14
+ : never;
15
+ };
16
+
17
+ type PickObjects<TObject> = {
18
+ [P in keyof TObject]: TObject[P] extends Record<string, unknown> ? TObject[P] : never;
19
+ };
20
+
21
+ interface CompiledAPI<TSchema> {
22
+ /**
23
+ * ## CSS
24
+ *
25
+ * Creates styles that are statically typed and useable with other Compiled APIs.
26
+ * For further details [read the documentation](https://compiledcssinjs.com/docs/api-css).
27
+ *
28
+ * @example
29
+ * ```
30
+ * const redText = css({
31
+ * color: 'red',
32
+ * });
33
+ *
34
+ * <div css={redText} />
35
+ * ```
36
+ */
37
+ css(
38
+ styles: StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>
39
+ ): StrictCSSProperties;
40
+ /**
41
+ * ## CSS Map
42
+ *
43
+ * Creates a collection of named styles that are statically typed and useable with other Compiled APIs.
44
+ * For further details [read the documentation](https://compiledcssinjs.com/docs/api-cssmap).
45
+ *
46
+ * @example
47
+ * ```
48
+ * const styles = cssMap({
49
+ * none: { borderStyle: 'none' },
50
+ * solid: { borderStyle: 'solid' },
51
+ * });
52
+ *
53
+ * <div css={styles.solid} />
54
+ * ```
55
+ */
56
+ cssMap<
57
+ TStyles extends Record<
58
+ string,
59
+ StrictCSSProperties & PseudosDeclarations & EnforceSchema<TSchema>
60
+ >
61
+ >(
62
+ styles: TStyles
63
+ ): {
64
+ readonly [P in keyof TStyles]: CompiledStyles<TStyles[P]>;
65
+ };
66
+ /**
67
+ * ## CX
68
+ *
69
+ * Use in conjunction with the {@link XCSSProp} to concatenate and conditionally apply
70
+ * declared styles. Can only be used with the {@link cssMap} and {@link XCSSProp} APIs.
71
+ *
72
+ * @example
73
+ * ```
74
+ * const styles = cssMap({
75
+ * text: { color: 'var(--ds-text)' },
76
+ * primary: { color: 'var(--ds-text-brand)' },
77
+ * });
78
+ *
79
+ * <Component xcss={cx(isPrimary && styles.text, !isPrimary && styles.primary)} />
80
+ * ```
81
+ */
82
+ cx: typeof cx;
83
+ /**
84
+ * ## XCSSProp
85
+ *
86
+ * Declare styles your component takes with all other styles marked as violations
87
+ * by the TypeScript compiler. There are two primary use cases for xcss prop:
88
+ *
89
+ * - safe style overrides
90
+ * - inverting style declarations
91
+ *
92
+ * Interverting style declarations is interesting for platform teams as
93
+ * it means products only pay for styles they use as they're now the ones who declare
94
+ * the styles!
95
+ *
96
+ * The {@link XCSSProp} type has generics two of which must be defined — use to explicitly
97
+ * set want you to maintain as API. Use {@link XCSSAllProperties} and {@link XCSSAllPseudos}
98
+ * to enable all properties and pseudos.
99
+ *
100
+ * The third generic is used to declare what properties and pseudos should be required.
101
+ *
102
+ * ```tsx
103
+ * interface MyComponentProps {
104
+ * // Color is accepted, all other properties / pseudos are considered violations.
105
+ * xcss?: ReturnType<typeof XCSSProp<'color', never>>;
106
+ *
107
+ * // Only backgrond color and hover pseudo is accepted.
108
+ * xcss?: ReturnType<typeof XCSSProp<'backgroundColor', '&:hover'>>;
109
+ *
110
+ * // All properties are accepted, all pseudos are considered violations.
111
+ * xcss?: ReturnType<typeof XCSSProp<XCSSAllProperties, never>>;
112
+ *
113
+ * // All properties are accepted, only the hover pseudo is accepted.
114
+ * xcss?: ReturnType<typeof XCSSProp<XCSSAllProperties, '&:hover'>>;
115
+ *
116
+ * // The xcss prop is required as well as the color property. No pseudos are required.
117
+ * xcss: ReturnType<
118
+ * typeof XCSSProp<
119
+ * XCSSAllProperties,
120
+ * '&:hover',
121
+ * { requiredProperties: 'color', requiredPseudos: never }
122
+ * >
123
+ * >;
124
+ * }
125
+ *
126
+ * function MyComponent({ xcss }: MyComponentProps) {
127
+ * return <div css={{ color: 'var(--ds-text-danger)' }} className={xcss} />
128
+ * }
129
+ * ```
130
+ *
131
+ * The xcss prop works with static inline objects and the [cssMap](https://compiledcssinjs.com/docs/api-cssmap) API.
132
+ *
133
+ * ```jsx
134
+ * // Declared as an inline object
135
+ * <Component xcss={{ color: 'var(--ds-text)' }} />
136
+ *
137
+ * // Declared with the cssMap API
138
+ * const styles = cssMap({ text: { color: 'var(--ds-text)' } });
139
+ * <Component xcss={styles.text} />
140
+ * ```
141
+ *
142
+ * To concatenate and conditonally apply styles use the {@link cssMap} and {@link cx} functions.
143
+ */
144
+ XCSSProp<
145
+ TAllowedProperties extends keyof StrictCSSProperties,
146
+ TAllowedPseudos extends CSSPseudos,
147
+ TRequiredProperties extends {
148
+ requiredProperties: TAllowedProperties;
149
+ requiredPseudos: TAllowedPseudos;
150
+ } = never
151
+ >(): Internal$XCSSProp<
152
+ TAllowedProperties,
153
+ TAllowedPseudos,
154
+ TSchema,
155
+ PickObjects<TSchema>,
156
+ TRequiredProperties
157
+ >;
158
+ }
159
+
160
+ type CompiledSchema = StrictCSSProperties & PseudosDeclarations;
161
+
162
+ /**
163
+ * ## Create Strict API
164
+ *
165
+ * Returns a strict subset of Compiled APIs augmented by a type definition.
166
+ * This API does not change Compileds build time behavior — merely augmenting
167
+ * the returned API types which enforce:
168
+ *
169
+ * - all APIs use object types
170
+ * - property values declared in the type definition must be used (else fallback to defaults)
171
+ * - a strict subset of pseudo states/selectors
172
+ * - unknown properties to be a type violation
173
+ *
174
+ * To set up:
175
+ *
176
+ * 1. Declare the API in a module (either local or in a package):
177
+ *
178
+ * @example
179
+ * ```tsx
180
+ * // ./foo.ts
181
+ * const { css } = createStrictAPI<{
182
+ * color: 'var(--ds-text)',
183
+ * '&:hover': { color: 'var(--ds-text-hover)' }
184
+ * }>();
185
+ *
186
+ * export { css };
187
+ * ```
188
+ *
189
+ * 2. Configure Compiled to pick up this module:
190
+ *
191
+ * @example
192
+ * ```diff
193
+ * // .compiledcssrc
194
+ * {
195
+ * + "importSources": ["./foo.ts"]
196
+ * }
197
+ * ```
198
+ *
199
+ * 3. Use the module in your application code:
200
+ *
201
+ * @example
202
+ * ```tsx
203
+ * import { css } from './foo';
204
+ *
205
+ * const styles = css({ color: 'var(--ds-text)' });
206
+ *
207
+ * <div css={styles} />
208
+ * ```
209
+ */
210
+ export function createStrictAPI<TSchema extends CompiledSchema>(): CompiledAPI<TSchema> {
211
+ return {
212
+ css() {
213
+ throw createStrictSetupError();
214
+ },
215
+ cssMap() {
216
+ throw createStrictSetupError();
217
+ },
218
+ cx,
219
+ XCSSProp() {
220
+ throw createStrictSetupError();
221
+ },
222
+ };
223
+ }
package/src/css/index.ts CHANGED
@@ -6,7 +6,7 @@ import { createSetupError } from '../utils/error';
6
6
  /**
7
7
  * ## CSS
8
8
  *
9
- * Define styles that are statically typed and useable with other Compiled APIs.
9
+ * Create styles that are statically typed and useable with other Compiled APIs.
10
10
  * For further details [read the documentation](https://compiledcssinjs.com/docs/api-css).
11
11
  *
12
12
  * ### Style with objects
@@ -84,9 +84,9 @@ type ExtendedSelectors = {
84
84
  };
85
85
 
86
86
  /**
87
- * ## cssMap
87
+ * ## CSS Map
88
88
  *
89
- * Creates a collection of named CSS rules that are statically typed and useable with other Compiled APIs.
89
+ * Creates a collection of named styles that are statically typed and useable with other Compiled APIs.
90
90
  * For further details [read the documentation](https://compiledcssinjs.com/docs/api-cssmap).
91
91
  *
92
92
  * @example
@@ -96,9 +96,7 @@ type ExtendedSelectors = {
96
96
  * solid: { borderStyle: 'solid' },
97
97
  * });
98
98
  *
99
- * const Component = ({ borderStyle }) => <div css={styles[borderStyle]} />
100
- *
101
- * <Component borderStyle="solid" />
99
+ * <div css={styles.solid} />
102
100
  * ```
103
101
  */
104
102
  export default function cssMap<
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export { styled } from './styled';
10
10
  export { ClassNames } from './class-names';
11
11
  export { default as css } from './css';
12
12
  export { default as cssMap } from './css-map';
13
+ export { createStrictAPI } from './create-strict-api';
13
14
  export { type XCSSAllProperties, type XCSSAllPseudos, type XCSSProp, cx } from './xcss-prop';
14
15
 
15
16
  // Pass through the (classic) jsx runtime.
package/src/types.ts CHANGED
@@ -95,9 +95,15 @@ export type CSSPseudos =
95
95
  | '&:visited';
96
96
 
97
97
  /**
98
- * The xcss prop must be given all known available properties even
99
- * if it takes a subset of them. This is ensure the (lack-of an)
98
+ * The XCSSProp must be given all known available properties even
99
+ * if it takes a subset of them. This ensures the (lack-of an)
100
100
  * excess property check doesn't enable makers to circumvent the
101
101
  * system and pass in values they shouldn't.
102
102
  */
103
103
  export type CSSProperties = Readonly<CSS.Properties<string | number>>;
104
+
105
+ /**
106
+ * A stricter subset of the {@link CSSProperties} type that excludes
107
+ * vendor and obsolete properties.
108
+ */
109
+ export type StrictCSSProperties = Readonly<CSS.StandardProperties & CSS.SvgProperties>;
@@ -17,3 +17,23 @@ export const createSetupError = (): Error => {
17
17
  Good luck!
18
18
  `);
19
19
  };
20
+
21
+ export const createStrictSetupError = (): Error => {
22
+ return new Error(`
23
+ ██████╗ ██████╗ ███╗ ███╗██████╗ ██╗██╗ ███████╗██████╗
24
+ ██╔════╝██╔═══██╗████╗ ████║██╔══██╗██║██║ ██╔════╝██╔══██╗
25
+ ██║ ██║ ██║██╔████╔██║██████╔╝██║██║ █████╗ ██║ ██║
26
+ ██║ ██║ ██║██║╚██╔╝██║██╔═══╝ ██║██║ ██╔══╝ ██║ ██║
27
+ ╚██████╗╚██████╔╝██║ ╚═╝ ██║██║ ██║███████╗███████╗██████╔╝
28
+ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝
29
+
30
+ @compiled/react
31
+
32
+ Code was executed when it shouldn't have. To resolve make sure to:
33
+
34
+ 1. Set up Compiled.
35
+ 2. Configure importSources in your Compiled config to point to the module that exports the output of createStrictAPI().
36
+
37
+ For more information visit https://compiledcssinjs.com/docs/installation and follow the instructions.
38
+ `);
39
+ };
@@ -3,19 +3,30 @@ import type * as CSS from 'csstype';
3
3
  import { ac } from '../runtime';
4
4
  import type { CSSPseudos, CSSProperties } from '../types';
5
5
 
6
- type XCSSItem<TStyleDecl extends keyof CSSProperties> = {
6
+ type MarkAsRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
7
+
8
+ type XCSSItem<TStyleDecl extends keyof CSSProperties, TCompiledTypedProperty> = {
7
9
  [Q in keyof CSSProperties]: Q extends TStyleDecl
8
- ? CompiledPropertyDeclarationReference | string | number
10
+ ?
11
+ | CompiledPropertyDeclarationReference
12
+ | (Q extends keyof TCompiledTypedProperty ? TCompiledTypedProperty[Q] : CSSProperties[Q])
9
13
  : never;
10
14
  };
11
15
 
12
16
  type XCSSPseudos<
13
17
  TAllowedProperties extends keyof CSSProperties,
14
18
  TAllowedPseudos extends CSSPseudos,
15
- TRequiredProperties extends { requiredProperties: TAllowedProperties }
19
+ TRequiredProperties extends { requiredProperties: TAllowedProperties },
20
+ TCompiledTypedPseudo
16
21
  > = {
17
22
  [Q in CSSPseudos]?: Q extends TAllowedPseudos
18
- ? MarkAsRequired<XCSSItem<TAllowedProperties>, TRequiredProperties['requiredProperties']>
23
+ ? MarkAsRequired<
24
+ XCSSItem<
25
+ TAllowedProperties,
26
+ Q extends keyof TCompiledTypedPseudo ? TCompiledTypedPseudo[Q] : object
27
+ >,
28
+ TRequiredProperties['requiredProperties']
29
+ >
19
30
  : never;
20
31
  };
21
32
 
@@ -69,7 +80,7 @@ export type XCSSAllProperties = keyof CSSProperties;
69
80
  export type XCSSAllPseudos = CSSPseudos;
70
81
 
71
82
  /**
72
- * ## xcss prop
83
+ * ## XCSSProp
73
84
  *
74
85
  * Declare styles your component takes with all other styles marked as violations
75
86
  * by the TypeScript compiler. There are two primary use cases for xcss prop:
@@ -132,10 +143,24 @@ export type XCSSProp<
132
143
  requiredProperties: TAllowedProperties;
133
144
  requiredPseudos: TAllowedPseudos;
134
145
  } = never
146
+ > = Internal$XCSSProp<TAllowedProperties, TAllowedPseudos, object, object, TRequiredProperties>;
147
+
148
+ export type Internal$XCSSProp<
149
+ TAllowedProperties extends keyof CSSProperties,
150
+ TAllowedPseudos extends CSSPseudos,
151
+ TCompiledTypedProperty,
152
+ TCompiledTypedPseudo,
153
+ TRequiredProperties extends {
154
+ requiredProperties: TAllowedProperties;
155
+ requiredPseudos: TAllowedPseudos;
156
+ }
135
157
  > =
136
- | (MarkAsRequired<XCSSItem<TAllowedProperties>, TRequiredProperties['requiredProperties']> &
158
+ | (MarkAsRequired<
159
+ XCSSItem<TAllowedProperties, TCompiledTypedProperty>,
160
+ TRequiredProperties['requiredProperties']
161
+ > &
137
162
  MarkAsRequired<
138
- XCSSPseudos<TAllowedProperties, TAllowedPseudos, TRequiredProperties>,
163
+ XCSSPseudos<TAllowedProperties, TAllowedPseudos, TRequiredProperties, TCompiledTypedPseudo>,
139
164
  TRequiredProperties['requiredPseudos']
140
165
  > &
141
166
  BlockedRules)
@@ -143,10 +168,8 @@ export type XCSSProp<
143
168
  | null
144
169
  | undefined;
145
170
 
146
- type MarkAsRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };
147
-
148
171
  /**
149
- * ## cx
172
+ * ## CX
150
173
  *
151
174
  * Use in conjunction with the {@link XCSSProp} to concatenate and conditionally apply
152
175
  * declared styles. Can only be used with the `cssMap()` and {@link XCSSProp} APIs.