@css-hooks/core 2.0.4 → 3.0.0
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/README.md +30 -33
- package/cjs/index.js +133 -307
- package/esm/index.js +129 -305
- package/package.json +9 -18
- package/tsdoc-metadata.json +11 -0
- package/types/index.d.ts +84 -270
package/types/index.d.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*
|
|
4
4
|
* @packageDocumentation
|
|
5
5
|
*/
|
|
6
|
-
|
|
7
6
|
/**
|
|
8
7
|
* Represents the conditions under which a given hook or declaration applies.
|
|
9
8
|
*
|
|
@@ -12,325 +11,139 @@
|
|
|
12
11
|
*
|
|
13
12
|
* @public
|
|
14
13
|
*/
|
|
15
|
-
export type Condition<S> =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
export type Condition<S> = S | {
|
|
15
|
+
and: Condition<S>[];
|
|
16
|
+
} | {
|
|
17
|
+
or: Condition<S>[];
|
|
18
|
+
} | {
|
|
19
|
+
not: Condition<S>;
|
|
20
|
+
};
|
|
21
21
|
/**
|
|
22
22
|
* Function to convert a value into a string.
|
|
23
23
|
*
|
|
24
|
+
* @param value - The value to stringify
|
|
25
|
+
*
|
|
24
26
|
* @param propertyName - The property name corresponding to the value being
|
|
25
27
|
* stringified
|
|
26
28
|
*
|
|
27
|
-
* @param value - The value to stringify
|
|
28
|
-
*
|
|
29
29
|
* @remarks
|
|
30
|
-
* Used for merging
|
|
31
|
-
*
|
|
32
|
-
* @returns The stringified value or `null` if the value can't be stringified.
|
|
33
|
-
*
|
|
34
|
-
* @public
|
|
35
|
-
*/
|
|
36
|
-
export type StringifyFn = (
|
|
37
|
-
propertyName: string,
|
|
38
|
-
value: unknown,
|
|
39
|
-
) => string | null;
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Callback to construct a conditional style group.
|
|
43
|
-
*
|
|
44
|
-
* @typeParam HookName - The name of the hooks available for use in style
|
|
45
|
-
* conditions.
|
|
46
|
-
*
|
|
47
|
-
* @typeParam CSSProperties - The type of a standard (flat) style object,
|
|
48
|
-
* typically defined by an app framework (e.g., React's `CSSProperties` type).
|
|
30
|
+
* Used for merging a conditional property value with the fallback value.
|
|
49
31
|
*
|
|
50
|
-
* @
|
|
51
|
-
*
|
|
52
|
-
* @param styles - The styles that apply when the specified condition is met.
|
|
53
|
-
*
|
|
54
|
-
* @returns A list of style objects and the condition under which each one
|
|
55
|
-
* applies.
|
|
32
|
+
* @returns The stringified value, or `undefined` if the value cannot be
|
|
33
|
+
* stringified
|
|
56
34
|
*
|
|
57
35
|
* @public
|
|
58
36
|
*/
|
|
59
|
-
export type
|
|
60
|
-
condition: Condition<HookName>,
|
|
61
|
-
style: CSSProperties,
|
|
62
|
-
) => [Condition<HookName>, CSSProperties];
|
|
63
|
-
|
|
37
|
+
export type StringifyFn = (value: unknown, propertyName: string) => string | null;
|
|
64
38
|
/**
|
|
65
|
-
*
|
|
39
|
+
* Represents the selector logic used to create a hook.
|
|
66
40
|
*
|
|
67
|
-
* @
|
|
68
|
-
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* Two types are supported:
|
|
43
|
+
* 1. A basic selector, where `&` is used as a placeholder for the element to
|
|
44
|
+
* which the condition applies. The `&` character must appear somewhere.
|
|
45
|
+
* 2. A `@media`, `@container`, or `@supports` at-rule. The value must begin
|
|
46
|
+
* with one of these keywords, followed by a space.
|
|
69
47
|
*
|
|
70
48
|
* @public
|
|
71
49
|
*/
|
|
72
|
-
export
|
|
73
|
-
/**
|
|
74
|
-
* Creates a condition that is true when all of the conditions passed as
|
|
75
|
-
* arguments are true.
|
|
76
|
-
*/
|
|
77
|
-
and(...conditions: Condition<HookName>[]): Condition<HookName>;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Creates a condition that is true when any of the conditions passed as
|
|
81
|
-
* arguments is true.
|
|
82
|
-
*/
|
|
83
|
-
or(...conditions: Condition<HookName>[]): Condition<HookName>;
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Creates a condition that is true when the specified condition is false.
|
|
87
|
-
*/
|
|
88
|
-
not(condition: Condition<HookName>): Condition<HookName>;
|
|
89
|
-
}
|
|
90
|
-
|
|
50
|
+
export type Selector = `${string}&${string}` | `@${"media" | "container" | "supports"} ${string}`;
|
|
91
51
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @typeParam HookName - The name of the hooks available for use in style
|
|
95
|
-
* conditions.
|
|
52
|
+
* Enhances a style object by merging in conditional declarations.
|
|
96
53
|
*
|
|
97
54
|
* @typeParam CSSProperties - The type of a standard (flat) style object,
|
|
98
55
|
* typically defined by an app framework (e.g., React's `CSSProperties` type).
|
|
99
56
|
*
|
|
100
|
-
* @
|
|
101
|
-
*
|
|
102
|
-
* @public
|
|
103
|
-
*/
|
|
104
|
-
export type OnFn<HookName, CSSProperties> = (
|
|
105
|
-
/**
|
|
106
|
-
* The callback used to construct a conditional style group.
|
|
107
|
-
*/
|
|
108
|
-
$: ConditionalStyleFn<HookName, CSSProperties>,
|
|
109
|
-
/**
|
|
110
|
-
* Helper functions used to construct advanced conditions.
|
|
111
|
-
*/
|
|
112
|
-
helpers: ConditionHelpers<HookName>,
|
|
113
|
-
) => [condition: Condition<HookName>, style: CSSProperties][];
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Represents a style object, optionally enhanced with inline styles.
|
|
117
|
-
*
|
|
118
|
-
* @typeParam HookName - The name of the hooks available for use in style
|
|
119
|
-
* conditions.
|
|
57
|
+
* @param style - The original style object containing default/fallback values
|
|
120
58
|
*
|
|
121
|
-
* @
|
|
122
|
-
* typically defined by an app framework (e.g., React's `CSSProperties` type).
|
|
59
|
+
* @returns An enhanced style object with conditional styles applied
|
|
123
60
|
*
|
|
124
61
|
* @public
|
|
125
62
|
*/
|
|
126
|
-
export type
|
|
127
|
-
/**
|
|
128
|
-
* Conditional styles
|
|
129
|
-
*/
|
|
130
|
-
on?:
|
|
131
|
-
| OnFn<HookName, CSSProperties>
|
|
132
|
-
| ReturnType<OnFn<HookName, CSSProperties>>;
|
|
133
|
-
};
|
|
134
|
-
|
|
63
|
+
export type EnhanceStyleFn<CSSProperties> = (style: CSSProperties) => CSSProperties;
|
|
135
64
|
/**
|
|
136
|
-
*
|
|
137
|
-
*
|
|
65
|
+
* An object containing the functions needed to support and use the configured
|
|
66
|
+
* hooks.
|
|
138
67
|
*
|
|
139
|
-
* @typeParam
|
|
140
|
-
* conditions.
|
|
68
|
+
* @typeParam S - The type of the selector logic for which to generate hooks
|
|
141
69
|
*
|
|
142
70
|
* @typeParam CSSProperties - The type of a standard (flat) style object,
|
|
143
71
|
* typically defined by an app framework (e.g., React's `CSSProperties` type).
|
|
144
72
|
*
|
|
145
|
-
* @returns A flat style object, with dynamic values derived from the
|
|
146
|
-
* conditional styles specified.
|
|
147
|
-
*
|
|
148
73
|
* @public
|
|
149
74
|
*/
|
|
150
|
-
export
|
|
151
|
-
/**
|
|
152
|
-
* A style object, optionally enhanced with conditional styles.
|
|
153
|
-
*/
|
|
154
|
-
style: Rule<HookName, CSSProperties>,
|
|
155
|
-
/**
|
|
156
|
-
* A list of style objects, each optionally enhanced with conditional styles.
|
|
157
|
-
*
|
|
158
|
-
* @beta
|
|
159
|
-
*/
|
|
160
|
-
...styles: (Rule<HookName, CSSProperties> | undefined)[]
|
|
161
|
-
) => CSSProperties;
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* Represents a basic hook implementation, using CSS syntax to define a selector or
|
|
165
|
-
* at-rule.
|
|
166
|
-
*
|
|
167
|
-
* @remarks
|
|
168
|
-
* Two types are supported:
|
|
169
|
-
* 1. A selector, where `&` is used as a placeholder for the element to which
|
|
170
|
-
* the condition applies. The `&` character must appear somewhere.
|
|
171
|
-
* 2. A `@media`, `@container`, or `@supports` at-rule. The value must begin
|
|
172
|
-
* with one of these keywords, followed by a space.
|
|
173
|
-
*
|
|
174
|
-
* These can be combined using the {@link Condition} structure to form complex logic.
|
|
175
|
-
*
|
|
176
|
-
* @public
|
|
177
|
-
*/
|
|
178
|
-
export type HookImpl =
|
|
179
|
-
| `${string}&${string}`
|
|
180
|
-
| `@${"media" | "container" | "supports"} ${string}`;
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Represents the configuration used to set up hooks.
|
|
184
|
-
*
|
|
185
|
-
* @typeParam HooksConfig - the hooks configured for use in conditional styles.
|
|
186
|
-
* See {@link CreateHooksFn} for additional type information.
|
|
187
|
-
*
|
|
188
|
-
* @public
|
|
189
|
-
*/
|
|
190
|
-
export interface Config<HooksConfig> {
|
|
191
|
-
/**
|
|
192
|
-
* The hooks available for use in conditional styles.
|
|
193
|
-
*/
|
|
194
|
-
hooks: HooksConfig;
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* The fallback keyword to use when no other value is available. The
|
|
198
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/revert-layer | revert-layer}
|
|
199
|
-
* keyword is functionally the best option, but
|
|
200
|
-
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/unset | unset}
|
|
201
|
-
* has better compatibility.
|
|
202
|
-
*
|
|
203
|
-
* @defaultValue "revert-layer"
|
|
204
|
-
*
|
|
205
|
-
* @remarks
|
|
206
|
-
* Compatibility data are available from
|
|
207
|
-
* {@link https://wpt.fyi/results/css/css-cascade?label=master&label=stable&product=chrome-99.0.4844.84&product=edge-99.0.1150.55&product=firefox-97.0.2&product=safari-16.4%20%2818615.1.26.110.1%29&q=revert-layer | Web Platform Tests}.
|
|
208
|
-
*/
|
|
209
|
-
fallback?: "revert-layer" | "unset";
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Whether to enable debug mode.
|
|
213
|
-
*
|
|
214
|
-
* @defaultValue false
|
|
215
|
-
*
|
|
216
|
-
* @remarks
|
|
217
|
-
*
|
|
218
|
-
* When debug mode is enabled:
|
|
219
|
-
*
|
|
220
|
-
* 1. The style sheet returned by the `styleSheet` function is pretty-printed.
|
|
221
|
-
*
|
|
222
|
-
* 2. Extra white space is included in inline style declarations for improved
|
|
223
|
-
* readability.
|
|
224
|
-
*
|
|
225
|
-
* 3. Hook identifiers (underlying CSS variables) are tagged with user-defined
|
|
226
|
-
* hook names.
|
|
227
|
-
*/
|
|
228
|
-
debug?: boolean;
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Options for sorting declarations when multiple rules are passed to the
|
|
232
|
-
* {@link CssFn | css} function.
|
|
233
|
-
*
|
|
234
|
-
* @beta
|
|
235
|
-
*/
|
|
236
|
-
sort?: {
|
|
75
|
+
export interface CreateHooksResult<S, CSSProperties> {
|
|
237
76
|
/**
|
|
238
|
-
*
|
|
239
|
-
|
|
77
|
+
* Enhances a style object with conditional styles.
|
|
78
|
+
*/
|
|
79
|
+
on: (condition: Condition<S>, style: CSSProperties) => EnhanceStyleFn<CSSProperties>;
|
|
80
|
+
/**
|
|
81
|
+
* Combines a list of conditions into a single condition which is true when
|
|
82
|
+
* all of the specified conditions are true.
|
|
240
83
|
*
|
|
241
|
-
* @
|
|
242
|
-
*
|
|
243
|
-
* entry, giving the properties declared within the highest priority within
|
|
244
|
-
* that scope.
|
|
84
|
+
* @typeParam C - The type of the conditions which must all be true in order
|
|
85
|
+
* for the condition to be true.
|
|
245
86
|
*
|
|
246
|
-
* @
|
|
87
|
+
* @param conditions - The conditions which must all be true in order for the
|
|
88
|
+
* condition to be true.
|
|
247
89
|
*
|
|
248
|
-
* @
|
|
90
|
+
* @returns A condition that is true when all of the specified conditions are
|
|
91
|
+
* true.
|
|
249
92
|
*/
|
|
250
|
-
|
|
251
|
-
|
|
93
|
+
and: <C extends Condition<S>[]>(...conditions: C) => {
|
|
94
|
+
and: C;
|
|
95
|
+
};
|
|
252
96
|
/**
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
97
|
+
* Combines a list of conditions into a single condition which is true when
|
|
98
|
+
* any of the specified conditions are true.
|
|
99
|
+
*
|
|
100
|
+
* @typeParam C - The type of the conditions any one of which must be true in
|
|
101
|
+
* order for the condition to be true.
|
|
256
102
|
*
|
|
257
|
-
* @
|
|
103
|
+
* @param conditions - The conditions any one of which must be true in order
|
|
104
|
+
* for the condition to be true.
|
|
258
105
|
*
|
|
259
|
-
* @
|
|
106
|
+
* @returns A condition that is true when any of the specified conditions are
|
|
107
|
+
* true.
|
|
260
108
|
*/
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
109
|
+
or: <C extends Condition<S>[]>(...conditions: C) => {
|
|
110
|
+
or: C;
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Negates a condition.
|
|
114
|
+
*
|
|
115
|
+
* @typeParam condition - The type of the condition which must be false in
|
|
116
|
+
* order for the resulting condition to be true.
|
|
117
|
+
*
|
|
118
|
+
* @param condition - The condition which must be false in order for the
|
|
119
|
+
* resulting condition to be true.
|
|
120
|
+
*
|
|
121
|
+
* @returns A condition that is true when the specified condition is false.
|
|
122
|
+
*/
|
|
123
|
+
not: <C extends Condition<S>>(condition: C) => {
|
|
124
|
+
not: C;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* The style sheet required to support the configured hooks.
|
|
128
|
+
*/
|
|
129
|
+
styleSheet: () => string;
|
|
271
130
|
}
|
|
272
|
-
|
|
273
131
|
/**
|
|
274
|
-
*
|
|
275
|
-
* along with the
|
|
276
|
-
* style sheet required to support it.
|
|
277
|
-
*
|
|
278
|
-
* @typeParam HookName - The name of the hooks available for use in style
|
|
279
|
-
* conditions.
|
|
132
|
+
* Represents the function used to define hooks and related configuration.
|
|
280
133
|
*
|
|
281
134
|
* @typeParam CSSProperties - The type of a standard (flat) style object,
|
|
282
135
|
* typically defined by an app framework (e.g., React's `CSSProperties` type).
|
|
283
136
|
*
|
|
284
|
-
* @
|
|
285
|
-
*/
|
|
286
|
-
export interface Hooks<HookName, CSSProperties> {
|
|
287
|
-
/**
|
|
288
|
-
* The `css` function used to define enhanced styles.
|
|
289
|
-
*/
|
|
290
|
-
css: CssFn<HookName, CSSProperties>;
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* The style sheet required to support the configured hooks.
|
|
294
|
-
*/
|
|
295
|
-
styleSheet: () => string;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* A utility type used to extract hook names from configuration
|
|
137
|
+
* @typeParam S - The type of selectors for which to create hooks.
|
|
300
138
|
*
|
|
301
|
-
* @
|
|
302
|
-
*/
|
|
303
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
304
|
-
export type GetHookNames<HooksConfig> = HooksConfig extends (
|
|
305
|
-
_: any,
|
|
306
|
-
) => Record<infer HookName, any>
|
|
307
|
-
? HookName
|
|
308
|
-
: HooksConfig extends Record<infer HookName, unknown>
|
|
309
|
-
? HookName
|
|
310
|
-
: never;
|
|
311
|
-
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Represents the function used to define hooks and related configuration.
|
|
139
|
+
* @param selectors - The selectors for which to create hooks.
|
|
315
140
|
*
|
|
316
|
-
* @
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
* @param config - The configuration used to define hooks and adjust the related
|
|
320
|
-
* functionality as needed depending on the use case.
|
|
141
|
+
* @returns An object containing the functions needed to support and use the
|
|
142
|
+
* configured hooks.
|
|
321
143
|
*
|
|
322
144
|
* @public
|
|
323
145
|
*/
|
|
324
|
-
export type CreateHooksFn<CSSProperties> = <
|
|
325
|
-
HooksConfig extends
|
|
326
|
-
| Record<string, Condition<HookImpl>>
|
|
327
|
-
| ((
|
|
328
|
-
helpers: ConditionHelpers<HookImpl>,
|
|
329
|
-
) => Record<string, Condition<HookImpl>>),
|
|
330
|
-
>(
|
|
331
|
-
config: Config<HooksConfig>,
|
|
332
|
-
) => Hooks<GetHookNames<HooksConfig>, CSSProperties>;
|
|
333
|
-
|
|
146
|
+
export type CreateHooksFn<CSSProperties> = <S extends Selector>(...selectors: S[]) => CreateHooksResult<S, CSSProperties>;
|
|
334
147
|
/**
|
|
335
148
|
* Creates a flavor of CSS Hooks tailored to a specific app framework.
|
|
336
149
|
*
|
|
@@ -345,7 +158,8 @@ export type CreateHooksFn<CSSProperties> = <
|
|
|
345
158
|
* framework integration is not provided.
|
|
346
159
|
*
|
|
347
160
|
* @public
|
|
161
|
+
*
|
|
348
162
|
*/
|
|
349
|
-
declare function buildHooksSystem<CSSProperties
|
|
350
|
-
|
|
351
|
-
): CreateHooksFn<CSSProperties>;
|
|
163
|
+
export declare function buildHooksSystem<CSSProperties extends {
|
|
164
|
+
[P: string]: any;
|
|
165
|
+
} = Record<string, unknown>>(stringify?: StringifyFn): CreateHooksFn<CSSProperties>;
|