@ktjs/core 0.29.6 → 0.29.8

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,6 @@
1
+ export declare const handlers: Record<string, (element: HTMLElement | SVGElement | MathMLElement, key: string, value: any) => void>;
2
+ type InputElement = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
3
+ type StringEventHandler = (element: InputElement, handler: (value: string) => void) => void;
4
+ type NumberEventHandler = (element: InputElement, handler: (value: number) => void) => void;
5
+ export declare const ktEventHandlers: Record<string, StringEventHandler | NumberEventHandler>;
6
+ export {};
@@ -0,0 +1,2 @@
1
+ import { KTRawAttr } from '../types/h.js';
2
+ export declare function applyAttr(element: HTMLElement | SVGElement | MathMLElement, attr: KTRawAttr): void;
@@ -0,0 +1,2 @@
1
+ import { KTRawContent } from '../types/h.js';
2
+ export declare function applyContent(element: HTMLElement | SVGElement | MathMLElement, content: KTRawContent): void;
@@ -0,0 +1,12 @@
1
+ import { HTMLTag, MathMLTag, SVGTag } from '@ktjs/shared';
2
+ import { KTRawAttr, KTRawContent, HTML } from '../types/h.js';
3
+ /**
4
+ * Create an enhanced HTMLElement.
5
+ * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
6
+ * @param tag tag of an `HTMLElement`
7
+ * @param attr attribute object or className
8
+ * @param content a string or an array of HTMLEnhancedElement as child nodes
9
+ *
10
+ * __PKG_INFO__
11
+ */
12
+ export declare const h: <T extends HTMLTag | SVGTag | MathMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>;
@@ -0,0 +1,3 @@
1
+ import { InputElementTag } from '@ktjs/shared';
2
+ import { KTRef } from '../reactive/index.js';
3
+ export declare function applyKModel(element: HTMLElementTagNameMap[InputElementTag], valueRef: KTRef<any>): void;
package/dist/index.d.ts CHANGED
@@ -1,428 +1,10 @@
1
- import { HTMLTag } from '@ktjs/shared';
2
- import { InputElementTag } from '@ktjs/shared';
3
- import { JSXTag } from '@ktjs/shared';
4
- import { MathMLTag } from '@ktjs/shared';
5
- import { otherstring } from '@ktjs/shared';
6
- import { SVGTag } from '@ktjs/shared';
7
-
8
- /**
9
- * Whether `props.ref` is a `KTRef` only needs to be checked in the initial render
10
- */
11
- export declare const $initRef: <T extends Node>(props: {
12
- ref?: KTRef<T>;
13
- }, node: T) => RefSetter<T>;
14
-
15
- /**
16
- * Assert k-model to be a ref object
17
- */
18
- export declare const $modelOrRef: <T = any>(props: any, defaultValue?: T) => KTRef<T>;
19
-
20
- /**
21
- * Create a reactive computed value
22
- * @param computeFn
23
- * @param reactives refs and computeds that this computed depends on
24
- */
25
- export declare function computed<T = JSX.Element>(computeFn: () => T, reactives: Array<KTReactive<any>>): KTComputed<T>;
26
-
27
- /**
28
- * A helper to create redrawable elements
29
- * ```tsx
30
- * export function MyComponent() {
31
- * let aa = 10;
32
- * // ...
33
- * // aa might be changed
34
- * return createRedrawable(() => <div>{aa}</div>);
35
- * }
36
- * ```
37
- * Then the returned element has a `redraw` method to redraw itself with new values.
38
- * @param creator a simple creator function that returns an element
39
- * @returns created element's ref
40
- */
41
- export declare function createRedrawable<T>(creator: () => T): KTRef<T> & {
42
- redraw: () => T;
43
- };
44
-
45
- /**
46
- * Extracts the value from a KTReactive, or returns the value directly if it's not reactive.
47
- */
48
- export declare function dereactive<T = JSX.Element>(value: T | KTReactive<T>): T;
49
-
50
- /**
51
- * Register a reactive effect with options.
52
- * @param effectFn The effect function to run when dependencies change
53
- * @param reactives The reactive dependencies
54
- * @param options Effect options: lazy, onCleanup, debugName
55
- * @returns stop function to remove all listeners
56
- */
57
- export declare function effect(effectFn: () => void, reactives: Array<KTReactive<any>>, options?: Partial<KTEffectOptions>): () => void;
58
-
59
- /**
60
- * Event handler type for DOM events
61
- */
62
- export declare type EventHandler<T extends Event = Event> = (this: HTMLElement, ev: T) => any;
63
-
64
- /**
65
- * Extract component props type (excluding ref and children)
66
- */
67
- declare type ExtractComponentProps<T> = T extends (props: infer P) => any ? Omit<P, 'ref' | 'children'> : {};
68
-
69
- /**
70
- * Fragment support - returns an array of children
71
- * Enhanced Fragment component that manages arrays of elements
72
- */
73
- export declare function Fragment(props: {
74
- children?: KTRawContent;
75
- }): JSX.Element;
76
-
77
- /**
78
- * Create an enhanced HTMLElement.
79
- * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
80
- * @param tag tag of an `HTMLElement`
81
- * @param attr attribute object or className
82
- * @param content a string or an array of HTMLEnhancedElement as child nodes
83
- *
84
- * __PKG_INFO__
85
- */
86
- declare const h: <T extends HTMLTag | SVGTag | MathMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>;
87
- export { h as createElement }
88
- export { h }
89
-
90
- declare type HTML<T extends (HTMLTag | SVGTag | MathMLTag) & otherstring> = T extends SVGTag
91
- ? SVGElementTagNameMap[T]
92
- : T extends HTMLTag
93
- ? HTMLElementTagNameMap[T]
94
- : T extends MathMLTag
95
- ? MathMLElementTagNameMap[T]
96
- : HTMLElement;
97
-
98
- export { HTMLTag }
99
-
100
- export { InputElementTag }
101
-
102
- export declare const isComputed: <T = any>(obj: any) => obj is KTComputed<T>;
103
-
104
- export declare const isKT: <T = any>(obj: any) => obj is KTReactive<T>;
105
-
106
- export declare const isRef: <T = any>(obj: any) => obj is KTRef<T>;
107
-
108
- /**
109
- * @param tag html tag or function component
110
- * @param props properties/attributes
111
- */
112
- export declare function jsx(tag: JSXTag, props: KTAttribute): JSX.Element;
113
-
114
- /**
115
- * JSX Development runtime - same as jsx but with additional dev checks
116
- */
117
- export declare const jsxDEV: typeof jsx;
118
-
119
- /**
120
- * JSX runtime for React 17+ automatic runtime
121
- * This is called when using jsx: "react-jsx" or "react-jsxdev"
122
- */
123
- export declare const jsxs: typeof jsx;
124
-
125
- export declare function KTAsync<T extends KTComponent>(props: {
126
- ref?: KTRef<JSX.Element>;
127
- skeleton?: JSX.Element;
128
- component: T;
129
- children?: KTRawContent;
130
- } & ExtractComponentProps<T>): JSX.Element;
131
-
132
- export declare type KTAttribute = KTBaseAttribute & KTPrefixedEventAttribute;
133
-
134
- declare type KTAvailableContent = SingleContent | KTAvailableContent[];
135
-
136
- /**
137
- * Used to create enhanced HTML elements
138
- */
139
- declare interface KTBaseAttribute {
140
- [k: string]: any;
141
-
142
- // # kt-specific attributes
143
- ref?: KTRef<JSX.Element>;
144
-
145
- /**
146
- * If a `KTRef` is bound, it will be reactive; otherwise, it will be static.
147
- */
148
- 'k-if'?: any;
149
-
150
- /**
151
- * Register two-way data binding between an input element and a KTRef.
152
- * - Default to regist `input` event and `value` property(`checked` for checkboxes and radios).
153
- */
154
- 'k-model'?: KTRef<any>;
155
-
156
- /**
157
- * Directly apply html string to `innerHTML`.
158
- * - Would be reactive if `KTRef` instance is provided
159
- */
160
- 'k-html'?: any;
161
-
162
- // # normal HTML attributes
163
- id?: string;
164
- class?: string;
165
- className?: string;
166
- style?: string | Partial<CSSStyleDeclaration>;
167
-
168
- type?:
169
- | 'text'
170
- | 'password'
171
- | 'email'
172
- | 'number'
173
- | 'tel'
174
- | 'url'
175
- | 'search'
176
- | 'date'
177
- | 'datetime-local'
178
- | 'time'
179
- | 'month'
180
- | 'week'
181
- | 'color'
182
- | 'range'
183
- | 'file'
184
- | 'checkbox'
185
- | 'radio'
186
- | 'hidden'
187
- | 'submit'
188
- | 'reset'
189
- | 'button'
190
- | 'image'
191
- | otherstring;
192
- for?: string;
193
-
194
- name?: string;
195
- title?: string;
196
- placeholder?: string;
197
- contenteditable?: boolean;
198
- value?: any;
199
- valueAsDate?: Date;
200
- valueAsNumber?: number;
201
- label?: string;
202
- disabled?: boolean;
203
-
204
- min?: string | number;
205
- max?: string | number;
206
- step?: string | number;
207
-
208
- selected?: boolean;
209
- checked?: boolean;
210
-
211
- action?: string;
212
- method?: 'POST' | 'GET' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE' | otherstring;
213
- }
214
-
215
- declare type KTComponent = (
216
- props: {
217
- ref?: KTRef<JSX.Element>;
218
- children?: KTRawContent;
219
- } & KTAttribute &
220
- any,
221
- ) => JSX.Element | Promise<JSX.Element> | any;
222
-
223
- export declare class KTComputed<T> implements KTReactive<T> {
224
- /**
225
- * Indicates that this is a KTRef instance
226
- */
227
- isKT: true;
228
- ktType: KTReactiveType;
229
- /* Excluded from this release type: _calculator */
230
- /* Excluded from this release type: _value */
231
- /* Excluded from this release type: _onChanges */
232
- /* Excluded from this release type: _emit */
233
- /* Excluded from this release type: _recalculate */
234
- /* Excluded from this release type: _subscribe */
235
- constructor(_calculator: () => T, reactives: Array<KTReactive<unknown>>);
236
- /**
237
- * If new value and old value are both nodes, the old one will be replaced in the DOM
238
- */
239
- get value(): T;
240
- set value(_newValue: T);
241
- /**
242
- * Force listeners to run once with the latest computed result.
243
- */
244
- notify(): void;
245
- /**
246
- * Computed values are derived from dependencies and should not be mutated manually.
247
- */
248
- mutate<R = void>(): R;
249
- /**
250
- * Register a callback when the value changes
251
- * @param callback (newValue, oldValue) => xxx
252
- */
253
- addOnChange(callback: ReactiveChangeHandler<T>): void;
254
- /**
255
- * Unregister a callback
256
- * @param callback (newValue, oldValue) => xxx
257
- */
258
- removeOnChange(callback: ReactiveChangeHandler<T>): boolean;
259
- }
260
-
261
- declare interface KTEffectOptions {
262
- lazy: boolean;
263
- onCleanup: () => void;
264
- debugName: string;
265
- }
266
-
267
- /**
268
- * KTFor - List rendering component with key-based optimization
269
- * Returns a Comment anchor node with rendered elements in __kt_for_list__
270
- */
271
- export declare function KTFor<T>(props: KTForProps<T>): KTForElement;
272
-
273
- export declare type KTForElement = JSX.Element;
274
-
275
- export declare interface KTForProps<T> {
276
- ref?: KTRef<KTForElement>;
277
- list: T[] | KTReactive<T[]>;
278
- key?: (item: T, index: number, array: T[]) => any;
279
- map: (item: T, index: number, array: T[]) => HTMLElement;
280
- }
281
-
282
- export declare type KTPrefixedEventAttribute = {
283
- [EventName in keyof HTMLElementEventMap as `on:${EventName}`]?: (ev: HTMLElementEventMap[EventName]) => void;
284
- };
285
-
286
- export declare type KTRawAttr = KTAttribute | null | undefined | '' | false;
287
-
288
- export declare type KTRawContent = KTAvailableContent | Promise<KTAvailableContent>;
289
-
290
- export declare type KTRawContents = KTAvailableContent;
291
-
292
- export declare type KTReactify<T> = T extends boolean ? KTReactive<boolean> : T extends any ? KTReactive<T> : never;
293
-
294
- export declare type KTReactifyObject<T extends object> = {
295
- [K in keyof T]: KTReactify<T[K]>;
296
- };
297
-
298
- export declare type KTReactifyProps<T extends object> = {
299
- [K in keyof T]: KTReactify<Exclude<T[K], undefined>> | T[K];
300
- };
301
-
302
- export declare class KTReactive<T> {
303
- /**
304
- * Indicates that this is a KTRef instance
305
- */
306
- isKT: boolean;
307
-
308
- ktType: KTReactiveType;
309
-
310
- /**
311
- * If new value and old value are both nodes, the old one will be replaced in the DOM
312
- */
313
- get value();
314
- set value(newValue: T);
315
-
316
- /**
317
- * Force all listeners to run even when reference identity has not changed.
318
- * Useful for in-place array/object mutations.
319
- */
320
- notify(): void;
321
-
322
- /**
323
- * Mutate current value in-place and notify listeners once.
324
- *
325
- * @example
326
- * const items = ref<number[]>([1, 2]);
327
- * items.mutate((list) => list.push(3));
328
- */
329
- mutate<R = void>(mutator: (currentValue: T) => R): R;
330
-
331
- /**
332
- * Register a callback when the value changes
333
- * - Value setter will check `Object.is(newValue, oldValue)`.
334
- * @param callback (newValue, oldValue) => xxx
335
- */
336
- addOnChange(callback: ReactiveChangeHandler<T>): void;
337
- removeOnChange(callback: ReactiveChangeHandler<T>): void;
338
- }
339
-
340
- export declare const enum KTReactiveType {
341
- REF = 1,
342
- COMPUTED = 2
343
- }
344
-
345
- export declare class KTRef<T> implements KTReactive<T> {
346
- /**
347
- * Indicates that this is a KTRef instance
348
- */
349
- isKT: true;
350
- ktType: KTReactiveType;
351
- /* Excluded from this release type: _value */
352
- /* Excluded from this release type: _onChanges */
353
- /* Excluded from this release type: _emit */
354
- constructor(_value: T, _onChanges: Array<ReactiveChangeHandler<T>>);
355
- /**
356
- * If new value and old value are both nodes, the old one will be replaced in the DOM
357
- */
358
- get value(): T;
359
- set value(newValue: T);
360
- /**
361
- * Force all listeners to run even when reference identity has not changed.
362
- * Useful for in-place array/object mutations.
363
- */
364
- notify(): void;
365
- /**
366
- * Mutate current value in-place and notify listeners once.
367
- *
368
- * @example
369
- * const items = ref<number[]>([1, 2]);
370
- * items.mutate((list) => list.push(3));
371
- */
372
- mutate<R = void>(mutator: (currentValue: T) => R): R;
373
- /**
374
- * Register a callback when the value changes
375
- * @param callback (newValue, oldValue) => xxx
376
- */
377
- addOnChange(callback: ReactiveChangeHandler<T>): void;
378
- removeOnChange(callback: ReactiveChangeHandler<T>): boolean;
379
- }
380
-
381
- export declare type KTSurfaceRef<T extends object> = {
382
- [K in keyof T]: KTRef<T[K]>;
383
- } & {
384
- /**
385
- * Get the dereferenced object like the original one
386
- */
387
- kcollect: () => T;
388
- };
389
-
390
- export { MathMLTag }
391
-
392
- export declare type ReactiveChangeHandler<T> = (newValue: T, oldValue: T) => void;
393
-
394
- /**
395
- * Reference to the created HTML element.
396
- * - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
397
- * - can alse be used to store normal values, but it is not reactive.
398
- * - if the value is already a `KTRef`, it will be returned **directly**.
399
- * @param value mostly an HTMLElement
400
- */
401
- export declare function ref<T = JSX.Element>(value?: T, onChange?: ReactiveChangeHandler<T>): KTRef<T>;
402
-
403
- declare type RefSetter<T> = (props: {
404
- ref?: KTRef<T>;
405
- }, node: T) => void;
406
-
407
- declare type SingleContent = KTRef<any> | HTMLElement | Element | Node | string | number | boolean | null | undefined;
408
-
409
- /**
410
- * Make all first-level properties of the object a `KTRef`.
411
- * - `obj.a.b` is not reactive
412
- */
413
- export declare const surfaceRef: <T extends object>(obj: T) => KTSurfaceRef<T>;
414
-
415
- export { SVGTag }
416
-
417
- export declare const toReactive: <T>(value: T | KTReactive<T>, onChange?: ReactiveChangeHandler<T>) => KTReactive<T>;
418
-
419
- /**
420
- * Convert a value to `KTRef`.
421
- * - Returns the original value if it is already a `KTRef`.
422
- * - Throws error if the value is a `KTComputed`.
423
- * - Otherwise wraps the value with `ref()`.
424
- * @param o value to convert
425
- */
426
- export declare const toRef: <T = any>(o: any) => KTRef<T>;
427
-
428
- export { }
1
+ export * from './h/index.js';
2
+ export * from './jsx/index.js';
3
+ export * from './reactive/index.js';
4
+ export { KTAsync } from './jsx/async.js';
5
+ export { KTFor } from './jsx/for.js';
6
+ export type { KTForProps, KTForElement } from './jsx/for.js';
7
+ export type { HTMLTag, SVGTag, MathMLTag, InputElementTag } from '@ktjs/shared';
8
+ export type { KTRawContent, KTRawContents, KTRawAttr, EventHandler, KTAttribute, KTPrefixedEventAttribute, } from './types/h.js';
9
+ export type * from './types/jsx.js';
10
+ export type * from './types/reactive.js';