@css-hooks/core 1.8.1 → 2.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/types/index.d.ts CHANGED
@@ -1,69 +1,351 @@
1
- type UnionToIntersection<T> = (T extends any ? (t: T) => any : never) extends (t: infer U) => any ? U : never;
2
- type HookSpec = `@${"media" | "container" | "supports"} ${string}` | `:${string}` | `${string}&${string}` | {
3
- or: Readonly<HookSpec[]>;
4
- } | {
5
- and: Readonly<HookSpec[]>;
6
- };
7
- export type WithHooks<HookProperties, Properties> = WithHooksImpl<Properties, HookProperties>;
8
- type WithHooksImpl<Properties, HookProperties, HookPropertiesSub extends HookProperties = HookProperties> = Properties & Partial<UnionToIntersection<HookPropertiesSub extends string ? {
9
- [K in HookPropertiesSub]: WithHooksImpl<Properties, Exclude<HookProperties, HookPropertiesSub>>;
10
- } : never>>;
11
- /** @internal */
12
- export declare function genericStringify(_: unknown, value: unknown): string | null;
13
- export declare function buildHooksSystem<Properties = Record<string, unknown>>(stringify?: (propertyName: keyof Properties, value: unknown) => string | null): <HookProperties extends string>(config: Record<HookProperties, HookSpec>, options?: (({
14
- debug?: boolean; /** @internal */
15
- hookNameToId?: undefined;
16
- } | {
17
- debug?: undefined;
18
- /** @internal */ hookNameToId?: (hookName: string) => string;
19
- }) & {
20
- fallback?: "unset" | "revert-layer";
21
- /** @experimental */
22
- sort?: boolean;
23
- }) | undefined) => [string, (styles_0: WithHooks<HookProperties, Properties>, ...styles_1: (WithHooks<HookProperties, Properties> | undefined)[]) => Properties];
24
1
  /**
25
- * A list of hooks offered as a "sensible default" to solve the most common use cases.
2
+ * CSS Hooks core library
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+
7
+ /**
8
+ * Represents the conditions under which a given hook or declaration applies.
9
+ *
10
+ * @typeParam S - The basic type of condition to enhance with boolean
11
+ * operations.
12
+ *
13
+ * @public
14
+ */
15
+ export type Condition<S> =
16
+ | S
17
+ | { and: Condition<S>[] }
18
+ | { or: Condition<S>[] }
19
+ | { not: Condition<S> };
20
+
21
+ /**
22
+ * Function to convert a value into a string.
23
+ *
24
+ * @param propertyName - The property name corresponding to the value being
25
+ * stringified
26
+ *
27
+ * @param value - The value to stringify
28
+ *
29
+ * @remarks
30
+ * Used for merging an override property value with the default/fallback value.
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).
49
+ *
50
+ * @param condition - The condition under which the styles apply.
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.
56
+ *
57
+ * @public
58
+ */
59
+ export type ConditionalStyleFn<HookName, CSSProperties> = (
60
+ condition: Condition<HookName>,
61
+ style: CSSProperties,
62
+ ) => [Condition<HookName>, CSSProperties];
63
+
64
+ /**
65
+ * Helper functions used to construct advanced conditions.
66
+ *
67
+ * @typeParam HookName - The name of the hooks available for use in style
68
+ * conditions.
69
+ *
70
+ * @public
71
+ */
72
+ export interface ConditionHelpers<HookName> {
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
+
91
+ /**
92
+ * Provides a way to declare conditional styles within a {@link Rule}.
93
+ *
94
+ * @typeParam HookName - The name of the hooks available for use in style
95
+ * conditions.
96
+ *
97
+ * @typeParam CSSProperties - The type of a standard (flat) style object,
98
+ * typically defined by an app framework (e.g., React's `CSSProperties` type).
99
+ *
100
+ * @returns A list of the conditional styles declared.
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.
120
+ *
121
+ * @typeParam CSSProperties - The type of a standard (flat) style object,
122
+ * typically defined by an app framework (e.g., React's `CSSProperties` type).
26
123
  *
27
- * @deprecated Use the `@css-hooks/recommended` package instead.
124
+ * @public
28
125
  */
29
- export declare const recommended: {
30
- readonly active: ":active";
31
- readonly autofill: {
32
- readonly or: readonly [":autofill", ":-webkit-autofill"];
33
- };
34
- readonly checked: ":checked";
35
- readonly default: ":default";
36
- readonly disabled: ":disabled";
37
- readonly empty: ":empty";
38
- readonly enabled: ":enabled";
39
- readonly evenChild: ":nth-child(even)";
40
- readonly firstChild: ":first-child";
41
- readonly firstOfType: ":first-of-type";
42
- readonly focus: ":focus";
43
- readonly focusVisible: ":focus-visible";
44
- readonly focusWithin: ":focus-within";
45
- readonly hover: ":hover";
46
- readonly inRange: ":in-range";
47
- readonly indeterminate: ":indeterminate";
48
- readonly invalid: ":invalid";
49
- readonly lastChild: ":last-child";
50
- readonly lastOfType: ":last-of-type";
51
- readonly oddChild: ":nth-child(odd)";
52
- readonly onlyChild: ":only-child";
53
- readonly onlyOfType: ":only-of-type";
54
- readonly outOfRange: ":out-of-range";
55
- readonly placeholderShown: {
56
- readonly or: readonly [":placeholder-shown", ":-moz-placeholder-shown"];
57
- };
58
- readonly readOnly: {
59
- readonly or: readonly [":read-only", ":-moz-read-only"];
60
- };
61
- readonly readWrite: {
62
- readonly or: readonly [":read-write", ":-moz-read-write"];
63
- };
64
- readonly required: ":required";
65
- readonly target: ":target";
66
- readonly valid: ":valid";
67
- readonly visited: ":visited";
126
+ export type Rule<HookName, CSSProperties> = CSSProperties & {
127
+ /**
128
+ * Conditional styles
129
+ */
130
+ on?:
131
+ | OnFn<HookName, CSSProperties>
132
+ | ReturnType<OnFn<HookName, CSSProperties>>;
68
133
  };
69
- export {};
134
+
135
+ /**
136
+ * Represents the type of the `css` function, used to transform a {@link Rule}
137
+ * into a flat style object.
138
+ *
139
+ * @typeParam HookName - The name of the hooks available for use in style
140
+ * conditions.
141
+ *
142
+ * @typeParam CSSProperties - The type of a standard (flat) style object,
143
+ * typically defined by an app framework (e.g., React's `CSSProperties` type).
144
+ *
145
+ * @returns A flat style object, with dynamic values derived from the
146
+ * conditional styles specified.
147
+ *
148
+ * @public
149
+ */
150
+ export type CssFn<HookName, CSSProperties> = (
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?: {
237
+ /**
238
+ * When enabled, the last property declared is sorted to the end, giving it
239
+ * the highest priority.
240
+ *
241
+ * @remarks
242
+ * Within a given rule, conditional styles are always treated as the last
243
+ * entry, giving the properties declared within the highest priority within
244
+ * that scope.
245
+ *
246
+ * @beta
247
+ *
248
+ * @defaultValue true
249
+ */
250
+ properties?: boolean;
251
+
252
+ /**
253
+ * When enabled, conditional styles are sorted to the end of the list of
254
+ * rules passed to the `css` function, always giving them the highest
255
+ * priority.
256
+ *
257
+ * @beta
258
+ *
259
+ * @defaultValue true
260
+ */
261
+ conditionalStyles?: boolean;
262
+ };
263
+
264
+ /**
265
+ * This option allows you to customize how hook names are transformed into
266
+ * valid CSS identifiers. Useful for testing.
267
+ *
268
+ * @internal
269
+ */
270
+ hookNameToId?: (hookName: GetHookNames<HooksConfig>) => string;
271
+ }
272
+
273
+ /**
274
+ * Contains the {@link CssFn | css} function used to define enhanced styles,
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.
280
+ *
281
+ * @typeParam CSSProperties - The type of a standard (flat) style object,
282
+ * typically defined by an app framework (e.g., React's `CSSProperties` type).
283
+ *
284
+ * @public
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
300
+ *
301
+ * @public
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.
315
+ *
316
+ * @typeParam CSSProperties - The type of a standard (flat) style object,
317
+ * typically defined by an app framework (e.g., React's `CSSProperties` type).
318
+ *
319
+ * @param config - The configuration used to define hooks and adjust the related
320
+ * functionality as needed depending on the use case.
321
+ *
322
+ * @public
323
+ */
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
+
334
+ /**
335
+ * Creates a flavor of CSS Hooks tailored to a specific app framework.
336
+ *
337
+ * @param stringify - The function used to stringify values when merging
338
+ * conditional styles.
339
+ *
340
+ * @returns The `createHooks` function used to bootstrap CSS Hooks within an app
341
+ * or component library.
342
+ *
343
+ * @remarks
344
+ * Primarily for internal use, advanced use cases, or when an appropriate
345
+ * framework integration is not provided.
346
+ *
347
+ * @public
348
+ */
349
+ declare function buildHooksSystem<CSSProperties = Record<string, unknown>>(
350
+ stringify?: StringifyFn,
351
+ ): CreateHooksFn<CSSProperties>;