@pikacss/core 0.0.1

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.
@@ -0,0 +1,410 @@
1
+ import * as CSS from 'csstype';
2
+
3
+ type ExtractFn = (styleDefinition: StyleDefinition$1) => Promise<ExtractedAtomicStyleContent[]>;
4
+
5
+ declare function createEngine(config?: EngineConfig): Promise<Engine>;
6
+ declare class Engine {
7
+ config: ResolvedEngineConfig;
8
+ extract: ExtractFn;
9
+ store: {
10
+ atomicNames: Map<string, string>;
11
+ atomicRules: Map<string, AtomicStyle>;
12
+ };
13
+ constructor(config: ResolvedEngineConfig);
14
+ notifyPreflightUpdated(): void;
15
+ notifyAtomicRuleAdded(): void;
16
+ notifyAutocompleteConfigUpdated(): void;
17
+ appendAutocompleteSelectors(...selectors: string[]): void;
18
+ appendAutocompleteStyleItemStrings(...styleItemStrings: string[]): void;
19
+ appendAutocompleteExtraProperties(...properties: string[]): void;
20
+ appendAutocompleteExtraCssProperties(...properties: string[]): void;
21
+ appendAutocompletePropertyValues(property: string, ...tsTypes: string[]): void;
22
+ appendAutocompleteCssPropertyValues(property: string, ...values: (string | number)[]): void;
23
+ use(...itemList: StyleItem$1[]): Promise<string[]>;
24
+ renderPreviewStyles(...itemList: StyleItem$1[]): Promise<string>;
25
+ renderStyles(): string;
26
+ renderPreflights(): string;
27
+ renderAtomicStyles(): string;
28
+ }
29
+
30
+ type DefineHooks<Hooks extends Record<string, [type: 'sync' | 'async', payload: any, returnValue?: any]>> = Hooks;
31
+ type EngineHooksDefinition<_CustomConfig, _Selector extends string, _CSSProperty extends string, _Properties, _StyleDefinition, _StyleItem> = DefineHooks<{
32
+ config: ['async', config: EngineConfig<EnginePlugin[], _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem> & _CustomConfig];
33
+ beforeConfigResolving: ['sync', config: EngineConfig<EnginePlugin[], _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem> & _CustomConfig, void];
34
+ configResolved: ['async', resolvedConfig: ResolvedEngineConfig];
35
+ engineInitialized: ['sync', engine: Engine];
36
+ transformSelectors: ['async', selectors: string[]];
37
+ transformStyleItems: ['async', styleItems: _StyleItem[]];
38
+ transformStyleDefinitions: ['async', styleDefinitions: _StyleDefinition[]];
39
+ preflightUpdated: ['sync', void];
40
+ atomicRuleAdded: ['sync', void];
41
+ autocompleteConfigUpdated: ['sync', void];
42
+ }>;
43
+ type EnginePluginHooksOptions<_CustomConfig, _Selector extends string, _CSSProperty extends string, _Properties, _StyleDefinition, _StyleItem> = {
44
+ [K in keyof EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>]?: EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][0] extends 'async' ? (...params: EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][1] extends void ? [] : [payload: EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][1]]) => Awaitable<EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][1] | void> : (...params: EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][1] extends void ? [] : [payload: EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][1]]) => EngineHooksDefinition<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem>[K][1] | void;
45
+ };
46
+ interface EnginePlugin<_CustomConfig = any, _Selector extends string = string, _CSSProperty extends string = string, _Properties = Properties$1, _StyleDefinition = StyleDefinition$1, _StyleItem = StyleItem$1> extends EnginePluginHooksOptions<_CustomConfig, _Selector, _CSSProperty, _Properties, _StyleDefinition, _StyleItem> {
47
+ name: string;
48
+ order?: 'pre' | 'post';
49
+ }
50
+
51
+ type UnionString = string & {};
52
+ type UnionNumber = number & {};
53
+ type Arrayable<T> = T | T[];
54
+ type Awaitable<T> = T | Promise<T>;
55
+ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
56
+ type IsEqual<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false;
57
+ type Simplify<T> = {
58
+ [K in keyof T]: T[K];
59
+ } & {};
60
+ type ToKebab<T extends string> = T extends `${infer A}${infer B}` ? [A extends Uppercase<A> ? 1 : 0, A extends Lowercase<A> ? 1 : 0] extends [1, 0] ? `-${Lowercase<A>}${ToKebab<`${B}`>}` : `${A}${ToKebab<`${B}`>}` : T;
61
+ type FromKebab<T extends string> = T extends `--${string}` ? T : T extends `-${infer A}${infer B}` ? `${Uppercase<A>}${FromKebab<`${B}`>}` : T extends `${infer A}${infer B}` ? `${A}${FromKebab<`${B}`>}` : T;
62
+ type GetValue<Obj extends Record<string, any>, K extends string> = (IsEqual<Obj, object> | IsEqual<Obj, {}> | IsEqual<Obj[K], unknown>) extends false ? Obj[K] : never;
63
+ type PluginsCustomConfig<Plugins extends EnginePlugin[], Result extends Record<string, any> = {}> = Plugins extends [infer Plugin extends EnginePlugin, ...infer Rest extends EnginePlugin[]] ? PluginsCustomConfig<Rest, Plugin extends EnginePlugin<infer PluginConfig> ? Simplify<Result & PluginConfig> : Result> : Result;
64
+
65
+ interface AutocompleteConfig {
66
+ selectors?: string[];
67
+ styleItemStrings?: string[];
68
+ extraProperties?: string[];
69
+ extraCssProperties?: string[];
70
+ properties?: [property: string, tsType: Arrayable<string>][];
71
+ cssProperties?: [property: string, value: Arrayable<string | number>][];
72
+ }
73
+ interface ResolvedAutocompleteConfig {
74
+ selectors: Set<string>;
75
+ styleItemStrings: Set<string>;
76
+ extraProperties: Set<string>;
77
+ extraCssProperties: Set<string>;
78
+ properties: Map<string, string[]>;
79
+ cssProperties: Map<string, (string | number)[]>;
80
+ }
81
+
82
+ interface ImportantConfig {
83
+ default?: boolean;
84
+ }
85
+
86
+ type PropertyValue = string | number | [value: string | number, fallback: (string | number)[]] | null | undefined;
87
+ type Properties$1 = Record<string, PropertyValue>;
88
+ interface StyleDefinition$1 {
89
+ [K: string]: PropertyValue | StyleDefinition$1 | StyleItem$1[];
90
+ }
91
+ type StyleItem$1 = string | StyleDefinition$1;
92
+ interface ExtractedAtomicStyleContent {
93
+ selector: string[];
94
+ property: string;
95
+ value: string[] | null | undefined;
96
+ }
97
+ interface AtomicStyleContent {
98
+ selector: string[];
99
+ property: string;
100
+ value: string[];
101
+ }
102
+ interface AtomicStyle {
103
+ name: string;
104
+ content: AtomicStyleContent;
105
+ }
106
+
107
+ interface Frames<_Properties = Properties$1> {
108
+ from: _Properties;
109
+ to: _Properties;
110
+ [K: `${number}%`]: _Properties;
111
+ }
112
+ type KeyframesConfig<_Properties = Properties$1> = string | [name: string, frames?: Frames<_Properties>, autocomplete?: string[]] | {
113
+ name: string;
114
+ frames?: Frames<_Properties>;
115
+ autocomplete?: string[];
116
+ };
117
+
118
+ type PreflightFn = (engine: Engine) => string;
119
+ /**
120
+ * PreflightConfig can be a string or a function that returns a string.
121
+ *
122
+ * 1. A string is a static preflight style.
123
+ * 2. A function is a dynamic preflight style that can use the engine instance to generate styles.
124
+ */
125
+ type PreflightConfig = string | PreflightFn;
126
+
127
+ type SelectorConfig<_Selector extends string = string> = string | [selector: RegExp, value: (matched: RegExpMatchArray) => Awaitable<Arrayable<UnionString | _Selector>>, autocomplete?: Arrayable<string>] | [selector: string, value: Arrayable<UnionString | _Selector>] | {
128
+ selector: RegExp;
129
+ value: (matched: RegExpMatchArray) => Awaitable<Arrayable<UnionString | _Selector>>;
130
+ autocomplete?: Arrayable<string>;
131
+ } | {
132
+ selector: string;
133
+ value: Arrayable<UnionString | _Selector>;
134
+ };
135
+
136
+ type ShortcutConfig<_StyleItem = StyleItem$1> = string | [shortcut: RegExp, value: (matched: RegExpMatchArray) => Awaitable<Arrayable<_StyleItem>>, autocomplete?: Arrayable<string>] | {
137
+ shortcut: RegExp;
138
+ value: (matched: RegExpMatchArray) => Awaitable<Arrayable<_StyleItem>>;
139
+ autocomplete?: Arrayable<string>;
140
+ } | [shortcut: string, value: Arrayable<_StyleItem>] | {
141
+ shortcut: string;
142
+ value: Arrayable<_StyleItem>;
143
+ };
144
+
145
+ interface VariableAutocomplete<_CSSProperty extends string = string> {
146
+ /**
147
+ * Specify the properties that the variable can be used as a value of.
148
+ *
149
+ * @default ['*']
150
+ */
151
+ asValueOf?: Arrayable<UnionString | '*' | _CSSProperty>;
152
+ /**
153
+ * Whether to add the variable as a CSS property.
154
+ *
155
+ * @default true
156
+ */
157
+ asProperty?: boolean;
158
+ }
159
+ type VariableConfig<_CSSProperty extends string = string> = string | [name: string, value?: string, autocomplete?: VariableAutocomplete<_CSSProperty>] | {
160
+ name: string;
161
+ value?: string;
162
+ autocomplete?: VariableAutocomplete<_CSSProperty>;
163
+ };
164
+
165
+ interface EngineConfig<_Plugins extends EnginePlugin[] = EnginePlugin[], _Selector extends string = string, _CSSProperty extends string = string, _Properties = Properties$1, _StyleDefinition = StyleDefinition$1, _StyleItem = StyleItem$1> {
166
+ /**
167
+ * Register plugins to extend PikaCSS functionality.
168
+ *
169
+ * @example
170
+ * ```ts
171
+ * {
172
+ * plugins: [
173
+ * icons(), // Add icon support
174
+ * customPlugin() // Custom plugin
175
+ * ]
176
+ * }
177
+ * ```
178
+ */
179
+ plugins?: [..._Plugins];
180
+ /**
181
+ * Set the prefix for generated atomic class names.
182
+ *
183
+ * @default ''
184
+ * @example
185
+ * ```ts
186
+ * {
187
+ * prefix: 'pika-' // Generated class names will be like 'pika-xxx'
188
+ * }
189
+ * ```
190
+ */
191
+ prefix?: string;
192
+ /**
193
+ * Set the default selector format. '&&' will be replaced with the atomic style name.
194
+ *
195
+ * @default '.&&'
196
+ * @example
197
+ * ```ts
198
+ * {
199
+ * defaultSelector: '.&&' // Use class attribute: <div class="a b c">
200
+ * // or
201
+ * defaultSelector: '[data-pika~="&&"]' // Use attribute selector: <div data-pika="a b c">
202
+ * }
203
+ * ```
204
+ */
205
+ defaultSelector?: string;
206
+ /**
207
+ * Define global CSS styles that will be injected before atomic styles.
208
+ * Can be used for CSS variables, global animations, base styles, etc.
209
+ *
210
+ * @default []
211
+ * @example
212
+ * ```ts
213
+ * {
214
+ * preflights: [
215
+ * // Use CSS string directly
216
+ * ':root { --color: blue }',
217
+ * // Or use function to generate dynamically
218
+ * (engine) => ':root { --theme: dark }'
219
+ * ]
220
+ * }
221
+ * ```
222
+ */
223
+ preflights?: PreflightConfig[];
224
+ /**
225
+ * Configure autocomplete functionality, including suggestions for selectors,
226
+ * style item strings, extra properties, etc.
227
+ *
228
+ * @example
229
+ * ```ts
230
+ * {
231
+ * autocomplete: {
232
+ * selectors: ['hover', 'focus'],
233
+ * styleItemStrings: ['flex-center'],
234
+ * extraProperties: ['customProp'],
235
+ * properties: [['color', 'string']]
236
+ * }
237
+ * }
238
+ * ```
239
+ */
240
+ autocomplete?: AutocompleteConfig;
241
+ /**
242
+ * Configure !important usage.
243
+ *
244
+ * @default { default: false }
245
+ * @example
246
+ * ```ts
247
+ * {
248
+ * important: {
249
+ * default: true // All styles will be marked as !important
250
+ * }
251
+ * }
252
+ * ```
253
+ */
254
+ important?: ImportantConfig;
255
+ /**
256
+ * Set the prefix for CSS variables.
257
+ *
258
+ * @example
259
+ * ```ts
260
+ * {
261
+ * variablesPrefix: 'theme',
262
+ * variables: [['color', 'blue']] // Generates: --theme-color: blue
263
+ * }
264
+ * ```
265
+ */
266
+ variablesPrefix?: string;
267
+ /**
268
+ * Define CSS variables with support for static values and autocomplete configuration.
269
+ *
270
+ * @default []
271
+ * @example
272
+ * ```ts
273
+ * {
274
+ * variables: [
275
+ * // Basic usage
276
+ * ['primary', '#ff0000'],
277
+ * // With autocomplete configuration
278
+ * ['accent', '#00ff00', {
279
+ * asValueOf: ['color', 'background-color'],
280
+ * asProperty: true
281
+ * }]
282
+ * ]
283
+ * }
284
+ * ```
285
+ */
286
+ variables?: VariableConfig<_CSSProperty>[];
287
+ /**
288
+ * Define CSS @keyframes animations with support for frame definitions
289
+ * and autocomplete suggestions.
290
+ *
291
+ * @default []
292
+ * @example
293
+ * ```ts
294
+ * {
295
+ * keyframes: [
296
+ * // Basic animation
297
+ * ['fade', {
298
+ * from: { opacity: 0 },
299
+ * to: { opacity: 1 }
300
+ * }],
301
+ * // With autocomplete suggestions
302
+ * ['slide', {
303
+ * from: { transform: 'translateX(-100%)' },
304
+ * to: { transform: 'translateX(0)' }
305
+ * }, ['slide 0.3s ease']]
306
+ * ]
307
+ * }
308
+ * ```
309
+ */
310
+ keyframes?: KeyframesConfig<_Properties>[];
311
+ /**
312
+ * Define selector transformation rules with support for static and dynamic rules.
313
+ *
314
+ * @default []
315
+ * @example
316
+ * ```ts
317
+ * {
318
+ * selectors: [
319
+ * // Static selector
320
+ * ['hover', '&:hover'],
321
+ * // Dynamic selector
322
+ * [/^screen-(\d+)$/, m => `@media (min-width: ${m[1]}px)`,
323
+ * ['screen-768', 'screen-1024']], // Autocomplete suggestions
324
+ * ]
325
+ * }
326
+ * ```
327
+ */
328
+ selectors?: SelectorConfig<_Selector>[];
329
+ /**
330
+ * Define style shortcuts for reusable style combinations.
331
+ *
332
+ * @default []
333
+ * @example
334
+ * ```ts
335
+ * {
336
+ * shortcuts: [
337
+ * // Static shortcut
338
+ * ['flex-center', {
339
+ * display: 'flex',
340
+ * alignItems: 'center',
341
+ * justifyContent: 'center'
342
+ * }],
343
+ * // Dynamic shortcut
344
+ * [/^m-(\d+)$/, m => ({ margin: `${m[1]}px` }),
345
+ * ['m-4', 'm-8']] // Autocomplete suggestions
346
+ * ]
347
+ * }
348
+ * ```
349
+ */
350
+ shortcuts?: ShortcutConfig<_StyleItem>[];
351
+ [key: string]: any;
352
+ }
353
+ interface ResolvedEngineConfig {
354
+ rawConfig: EngineConfig;
355
+ prefix: string;
356
+ defaultSelector: string;
357
+ plugins: EnginePlugin[];
358
+ preflights: PreflightFn[];
359
+ autocomplete: ResolvedAutocompleteConfig;
360
+ }
361
+
362
+ interface Autocomplete {
363
+ Selector: UnionString;
364
+ StyleItemString: UnionString;
365
+ ExtraProperty: UnionString;
366
+ ExtraCssProperty: UnionString;
367
+ PropertiesValue: Record<string, unknown>;
368
+ CssPropertiesValue: Record<string, UnionString | UnionNumber>;
369
+ }
370
+ interface EmptyAutocomplete extends Autocomplete {
371
+ Selector: never;
372
+ StyleItemString: never;
373
+ ExtraProperty: never;
374
+ ExtraCssProperty: never;
375
+ PropertiesValue: never;
376
+ CssPropertiesValue: never;
377
+ }
378
+ interface CSSVariables {
379
+ [K: (`--${string}` & {})]: UnionString | UnionNumber;
380
+ }
381
+ interface CSSProperties extends CSS.Properties, CSS.PropertiesHyphen, CSSVariables {
382
+ }
383
+ type CSSProperty = keyof CSSProperties;
384
+ type MakePropertyValue<T> = T | [value: T, fallback: T[]] | null | undefined;
385
+ type Properties<Autocomplete_ extends Autocomplete = EmptyAutocomplete> = {
386
+ [Key in keyof CSSProperties | Autocomplete_['ExtraCssProperty'] | Autocomplete_['ExtraProperty']]?: Key extends Autocomplete_['ExtraProperty'] ? GetValue<Autocomplete_['PropertiesValue'], Key> : MakePropertyValue<Exclude<UnionString | UnionNumber | GetValue<CSSProperties, Key> | GetValue<Autocomplete_['CssPropertiesValue'], ToKebab<Key>> | GetValue<Autocomplete_['CssPropertiesValue'], FromKebab<Key>> | GetValue<Autocomplete_['CssPropertiesValue'], '*'>, undefined | null>>;
387
+ };
388
+ type CSSPseudos = `${'&'}${CSS.Pseudos}`;
389
+ type CSSBlockAtRules = Exclude<CSS.AtRules, '@charset' | 'import' | '@namespace'>;
390
+ type CSSSelectors = CSSBlockAtRules | CSSPseudos;
391
+ type WrapWithSelector<Autocomplete_ extends Autocomplete, T> = {
392
+ [S in UnionString | Autocomplete_['Selector'] | CSSSelectors]?: T | StyleItem<Autocomplete_>[];
393
+ };
394
+ type MakeStyleDefinition<Autocomplete_ extends Autocomplete, MaxDepth extends number, Tuple extends any[] = []> = Tuple['length'] extends MaxDepth ? Tuple[number] : Tuple['length'] extends 0 ? MakeStyleDefinition<Autocomplete_, MaxDepth, [Properties<Autocomplete_>]> : MakeStyleDefinition<Autocomplete_, MaxDepth, [...Tuple, WrapWithSelector<Autocomplete_, [0, ...Tuple][Tuple['length']]>]>;
395
+ type StyleDefinition<Autocomplete_ extends Autocomplete = EmptyAutocomplete> = MakeStyleDefinition<Autocomplete_, 5>;
396
+ type StyleItem<Autocomplete_ extends Autocomplete = EmptyAutocomplete> = UnionString | Autocomplete_['StyleItemString'] | StyleDefinition<Autocomplete_>;
397
+
398
+ declare function appendAutocompleteSelectors(config: Pick<ResolvedEngineConfig, 'autocomplete'>, ...selectors: string[]): void;
399
+ declare function appendAutocompleteStyleItemStrings(config: Pick<ResolvedEngineConfig, 'autocomplete'>, ...styleItemStrings: string[]): void;
400
+ declare function appendAutocompleteExtraProperties(config: Pick<ResolvedEngineConfig, 'autocomplete'>, ...properties: string[]): void;
401
+ declare function appendAutocompleteExtraCssProperties(config: Pick<ResolvedEngineConfig, 'autocomplete'>, ...properties: string[]): void;
402
+ declare function appendAutocompletePropertyValues(config: Pick<ResolvedEngineConfig, 'autocomplete'>, property: string, ...tsTypes: string[]): void;
403
+ declare function appendAutocompleteCssPropertyValues(config: Pick<ResolvedEngineConfig, 'autocomplete'>, property: string, ...values: (string | number)[]): void;
404
+
405
+ declare function createDefineEnginePluginFn<A extends Autocomplete = EmptyAutocomplete>(): <C extends Record<string, any>>(plugin: EnginePlugin<C, A["Selector"] | CSSSelectors, A["ExtraCssProperty"] | CSSProperty, Properties<A>, StyleDefinition<A>, StyleItem<A>>) => EnginePlugin<C>;
406
+ declare const defineEnginePlugin: <C extends Record<string, any>>(plugin: EnginePlugin<C, CSSSelectors, keyof CSSProperties, Properties<EmptyAutocomplete>, StyleDefinition<EmptyAutocomplete>, StyleItem<EmptyAutocomplete>>) => EnginePlugin<C, string, string, Properties$1, StyleDefinition$1, StyleItem$1>;
407
+ declare function createDefineEngineConfigFn<A extends Autocomplete = EmptyAutocomplete>(): <P extends EnginePlugin[] = []>(config: EngineConfig<P, A["Selector"] | CSSSelectors, A["ExtraCssProperty"] | CSSProperty, Properties<A>, StyleDefinition<A>, StyleItem<A>> & PluginsCustomConfig<P>) => EngineConfig<P> & PluginsCustomConfig<P>;
408
+ declare const defineEngineConfig: <P extends EnginePlugin[] = []>(config: EngineConfig<P, CSSSelectors, keyof CSSProperties, Properties<EmptyAutocomplete>, StyleDefinition<EmptyAutocomplete>, StyleItem<EmptyAutocomplete>> & PluginsCustomConfig<P, {}>) => EngineConfig<P, string, string, Properties$1, StyleDefinition$1, StyleItem$1> & PluginsCustomConfig<P, {}>;
409
+
410
+ export { type Arrayable, type Autocomplete, type Awaitable, type EmptyAutocomplete, Engine, type EngineConfig, type EnginePlugin, type FromKebab, type GetValue, type IsEqual, type PluginsCustomConfig, type Properties, type Simplify, type StyleDefinition, type StyleItem, type ToKebab, type UnionNumber, type UnionString, type UnionToIntersection, appendAutocompleteCssPropertyValues, appendAutocompleteExtraCssProperties, appendAutocompleteExtraProperties, appendAutocompletePropertyValues, appendAutocompleteSelectors, appendAutocompleteStyleItemStrings, createDefineEngineConfigFn, createDefineEnginePluginFn, createEngine, defineEngineConfig, defineEnginePlugin };