@ktjs/core 0.14.6 → 0.15.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.
@@ -68,17 +68,11 @@ var __ktjs_core__ = (function (exports) {
68
68
 
69
69
  var defaultHandler = function (element, key, value) { return element.setAttribute(key, value); };
70
70
  function attrIsObject(element, attr) {
71
- // & deal k-if first
72
- if ('k-if' in attr) {
73
- if (!attr['k-if']) {
74
- return false;
75
- }
76
- }
77
71
  var classValue = attr.class;
78
- var style = attr.style;
79
72
  if (classValue !== undefined) {
80
73
  element.className = classValue;
81
74
  }
75
+ var style = attr.style;
82
76
  if (style) {
83
77
  if (typeof style === 'string') {
84
78
  element.setAttribute('style', style);
@@ -90,39 +84,31 @@ var __ktjs_core__ = (function (exports) {
90
84
  }
91
85
  }
92
86
  for (var key in attr) {
93
- if (key === 'class' || key === 'style' || key === 'k-if') {
87
+ if (key === 'class' || key === 'style' || key === 'children' || key === 'k-if' || key === 'ref') {
94
88
  continue;
95
89
  }
96
90
  var o = attr[key];
97
- // force register on:xxx as an event handler
98
- // !if o is not valid, the throwing job will be done by `on`, not kt.js
99
91
  // # special handling for kt.js specific events
100
92
  var ktEvent = ktEventHandlers[key];
101
93
  if (ktEvent) {
102
94
  ktEvent(element, o);
103
- continue;
104
95
  }
105
- // # normal event handler
106
- if (key.startsWith('on:')) {
96
+ // normal event handler
97
+ else if (key.startsWith('on:')) {
107
98
  element.addEventListener(key.slice(3), o); // chop off the `@`
108
- continue;
109
- }
110
- if (typeof o === 'function') {
111
- (handlers[key] || defaultHandler)(element, key, o());
112
99
  }
100
+ // normal attributes
113
101
  else {
114
102
  (handlers[key] || defaultHandler)(element, key, o);
115
103
  }
116
104
  }
117
- return true;
118
105
  }
119
106
  function applyAttr(element, attr) {
120
- if (typeof attr === 'string') {
121
- element.className = attr;
122
- return true;
107
+ if (typeof attr === 'object' && attr !== null) {
108
+ attrIsObject(element, attr);
123
109
  }
124
- else if (typeof attr === 'object' && attr !== null) {
125
- return attrIsObject(element, attr);
110
+ else if (typeof attr === 'string') {
111
+ element.className = attr;
126
112
  }
127
113
  else {
128
114
  throw new Error('kt.js: attr must be an object/string.');
@@ -243,7 +229,7 @@ var __ktjs_core__ = (function (exports) {
243
229
  * ## About
244
230
  * @package @ktjs/core
245
231
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
246
- * @version 0.14.6 (Last Update: 2026.01.17 11:35:33.824)
232
+ * @version 0.15.1 (Last Update: 2026.01.25 13:09:33.161)
247
233
  * @license MIT
248
234
  * @link https://github.com/baendlorel/kt.js
249
235
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -259,10 +245,7 @@ var __ktjs_core__ = (function (exports) {
259
245
  // * start creating the element
260
246
  var element = document.createElement(tag);
261
247
  // * Handle content
262
- var kif = applyAttr(element, attr);
263
- if (!kif) {
264
- return document.createComment('k-if');
265
- }
248
+ applyAttr(element, attr);
266
249
  applyContent(element, content);
267
250
  return element;
268
251
  });
@@ -300,62 +283,49 @@ var __ktjs_core__ = (function (exports) {
300
283
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
301
284
  };
302
285
 
286
+ /**
287
+ * Reference to the created HTML element.
288
+ * - can alse be used to store normal values, but it is not reactive.
289
+ * @param value mostly an HTMLElement
290
+ */
291
+ function ref(value) {
292
+ return { value: value, isKT: true };
293
+ }
294
+
295
+ var dummyRef = { value: null };
296
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
303
297
  /**
304
298
  * @param tag html tag or function component
305
299
  * @param props properties/attributes
306
300
  */
307
301
  function jsx(tag, props) {
308
- var _a;
302
+ var _a, _b;
309
303
  if (props === void 0) { props = {}; }
310
- var ref = ((_a = props.ref) === null || _a === void 0 ? void 0 : _a.isKT) ? props.ref : null;
311
- if (ref) {
312
- delete props.ref;
304
+ var ref = ((_a = props.ref) === null || _a === void 0 ? void 0 : _a.isKT) ? props.ref : dummyRef;
305
+ var el;
306
+ var redraw = function (newProps) {
307
+ props = newProps ? __assign(__assign({}, props), newProps) : props;
308
+ var old = el;
309
+ el = jsx(tag, props);
310
+ old.replaceWith(el);
311
+ return el;
312
+ };
313
+ if ('k-if' in props && !props['k-if']) {
314
+ el = document.createComment('k-if');
315
+ ref.value = el;
316
+ el.redraw = redraw;
317
+ return el;
313
318
  }
314
319
  // Handle function components
315
320
  if (typeof tag === 'function') {
316
- var el_1 = tag(props);
317
- if (!el_1.redraw) {
318
- el_1.redraw = function (newProps) {
319
- props = newProps ? __assign(__assign({}, props), newProps) : props;
320
- // $ same as below
321
- var old = el_1;
322
- el_1 = tag(props);
323
- el_1.redraw = old.redraw; // inherit redraw
324
- if (ref) {
325
- ref.value = el_1;
326
- }
327
- old.replaceWith(el_1);
328
- return el_1;
329
- };
330
- }
331
- if (ref) {
332
- ref.value = el_1;
333
- }
334
- return el_1;
321
+ el = tag(props);
335
322
  }
336
323
  else {
337
- // & deal children here
338
- var children_1 = props.children;
339
- delete props.children;
340
- var el_2 = h(tag, props, children_1);
341
- if (ref) {
342
- ref.value = el_2;
343
- }
344
- el_2.redraw = function (newProps, newChildren) {
345
- props = newProps ? __assign(__assign({}, props), newProps) : props;
346
- children_1 = (newChildren !== null && newChildren !== void 0 ? newChildren : children_1);
347
- // $ same as above
348
- var old = el_2;
349
- el_2 = h(tag, props, children_1);
350
- el_2.redraw = old.redraw; // inherit redraw
351
- if (ref) {
352
- ref.value = el_2;
353
- }
354
- old.replaceWith(el_2);
355
- return el_2;
356
- };
357
- return el_2;
324
+ el = h(tag, props, props.children);
358
325
  }
326
+ (_b = el.redraw) !== null && _b !== void 0 ? _b : (el.redraw = redraw);
327
+ ref.value = el;
328
+ return el;
359
329
  }
360
330
  /**
361
331
  * Fragment support - returns an array of children
@@ -408,33 +378,51 @@ var __ktjs_core__ = (function (exports) {
408
378
  * let aa = 10;
409
379
  * // ...
410
380
  * // aa might be changed
411
- * return createRedrawable(() => <div>{aa}</div>);
381
+ * return createRedrawableNoref(() => <div>{aa}</div>);
412
382
  * }
413
383
  * ```
414
384
  * Then the returned element has a `redraw` method to redraw itself with new values.
415
- * @param creator
385
+ * @param creator a simple creator function that returns an element
416
386
  * @returns created element
417
387
  */
418
- function createRedrawable(creator) {
419
- var element = creator();
388
+ function createRedrawableNoref(creator) {
389
+ var el = creator();
420
390
  var redraw = function () {
421
- var old = element;
422
- element = creator();
423
- old.replaceWith(element);
424
- element.redraw = redraw;
425
- return element;
391
+ var old = el;
392
+ el = creator();
393
+ old.replaceWith(el);
394
+ el.redraw = redraw;
395
+ return el;
426
396
  };
427
- element.redraw = redraw;
428
- return element;
397
+ el.redraw = redraw;
398
+ return el;
429
399
  }
430
-
431
400
  /**
432
- * Reference to the created HTML element.
433
- * - can alse be used to store normal values, but it is not reactive.
434
- * @param value mostly an HTMLElement
401
+ * A helper to create redrawable elements
402
+ * ```tsx
403
+ * export function MyComponent() {
404
+ * let aa = 10;
405
+ * // ...
406
+ * // aa might be changed
407
+ * return createRedrawable(() => <div>{aa}</div>);
408
+ * }
409
+ * ```
410
+ * Then the returned element has a `redraw` method to redraw itself with new values.
411
+ * @param creator a simple creator function that returns an element
412
+ * @returns created element's ref
435
413
  */
436
- function ref(value) {
437
- return { value: value, isKT: true };
414
+ function createRedrawable(creator) {
415
+ var elRef = ref();
416
+ elRef.value = creator();
417
+ var redraw = function () {
418
+ var old = elRef.value;
419
+ elRef.value = creator();
420
+ old.replaceWith(elRef.value);
421
+ elRef.value.redraw = redraw;
422
+ return elRef.value;
423
+ };
424
+ elRef.value.redraw = redraw;
425
+ return elRef;
438
426
  }
439
427
 
440
428
  function KTAsync(props) {
@@ -454,6 +442,7 @@ var __ktjs_core__ = (function (exports) {
454
442
  exports.KTAsync = KTAsync;
455
443
  exports.createElement = h;
456
444
  exports.createRedrawable = createRedrawable;
445
+ exports.createRedrawableNoref = createRedrawableNoref;
457
446
  exports.h = h;
458
447
  exports.jsx = jsx;
459
448
  exports.jsxDEV = jsxDEV;
package/dist/index.mjs CHANGED
@@ -53,17 +53,11 @@ const ktEventHandlers = {
53
53
 
54
54
  const defaultHandler = (element, key, value) => element.setAttribute(key, value);
55
55
  function attrIsObject(element, attr) {
56
- // & deal k-if first
57
- if ('k-if' in attr) {
58
- if (!attr['k-if']) {
59
- return false;
60
- }
61
- }
62
56
  const classValue = attr.class;
63
- const style = attr.style;
64
57
  if (classValue !== undefined) {
65
58
  element.className = classValue;
66
59
  }
60
+ const style = attr.style;
67
61
  if (style) {
68
62
  if (typeof style === 'string') {
69
63
  element.setAttribute('style', style);
@@ -75,39 +69,31 @@ function attrIsObject(element, attr) {
75
69
  }
76
70
  }
77
71
  for (const key in attr) {
78
- if (key === 'class' || key === 'style' || key === 'k-if') {
72
+ if (key === 'class' || key === 'style' || key === 'children' || key === 'k-if' || key === 'ref') {
79
73
  continue;
80
74
  }
81
75
  const o = attr[key];
82
- // force register on:xxx as an event handler
83
- // !if o is not valid, the throwing job will be done by `on`, not kt.js
84
76
  // # special handling for kt.js specific events
85
77
  const ktEvent = ktEventHandlers[key];
86
78
  if (ktEvent) {
87
79
  ktEvent(element, o);
88
- continue;
89
80
  }
90
- // # normal event handler
91
- if (key.startsWith('on:')) {
81
+ // normal event handler
82
+ else if (key.startsWith('on:')) {
92
83
  element.addEventListener(key.slice(3), o); // chop off the `@`
93
- continue;
94
- }
95
- if (typeof o === 'function') {
96
- (handlers[key] || defaultHandler)(element, key, o());
97
84
  }
85
+ // normal attributes
98
86
  else {
99
87
  (handlers[key] || defaultHandler)(element, key, o);
100
88
  }
101
89
  }
102
- return true;
103
90
  }
104
91
  function applyAttr(element, attr) {
105
- if (typeof attr === 'string') {
106
- element.className = attr;
107
- return true;
92
+ if (typeof attr === 'object' && attr !== null) {
93
+ attrIsObject(element, attr);
108
94
  }
109
- else if (typeof attr === 'object' && attr !== null) {
110
- return attrIsObject(element, attr);
95
+ else if (typeof attr === 'string') {
96
+ element.className = attr;
111
97
  }
112
98
  else {
113
99
  throw new Error('kt.js: attr must be an object/string.');
@@ -215,7 +201,7 @@ function applyContent(element, content) {
215
201
  * ## About
216
202
  * @package @ktjs/core
217
203
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
218
- * @version 0.14.6 (Last Update: 2026.01.17 11:35:33.824)
204
+ * @version 0.15.1 (Last Update: 2026.01.25 13:09:33.161)
219
205
  * @license MIT
220
206
  * @link https://github.com/baendlorel/kt.js
221
207
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -229,68 +215,52 @@ const h = ((tag, attr = '', content = '') => {
229
215
  // * start creating the element
230
216
  const element = document.createElement(tag);
231
217
  // * Handle content
232
- const kif = applyAttr(element, attr);
233
- if (!kif) {
234
- return document.createComment('k-if');
235
- }
218
+ applyAttr(element, attr);
236
219
  applyContent(element, content);
237
220
  return element;
238
221
  });
239
222
 
223
+ /**
224
+ * Reference to the created HTML element.
225
+ * - can alse be used to store normal values, but it is not reactive.
226
+ * @param value mostly an HTMLElement
227
+ */
228
+ function ref(value) {
229
+ return { value: value, isKT: true };
230
+ }
231
+
232
+ const dummyRef = { value: null };
233
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
240
234
  /**
241
235
  * @param tag html tag or function component
242
236
  * @param props properties/attributes
243
237
  */
244
238
  function jsx(tag, props = {}) {
245
- const ref = props.ref?.isKT ? props.ref : null;
246
- if (ref) {
247
- delete props.ref;
239
+ const ref = props.ref?.isKT ? props.ref : dummyRef;
240
+ let el;
241
+ const redraw = (newProps) => {
242
+ props = newProps ? { ...props, ...newProps } : props;
243
+ const old = el;
244
+ el = jsx(tag, props);
245
+ old.replaceWith(el);
246
+ return el;
247
+ };
248
+ if ('k-if' in props && !props['k-if']) {
249
+ el = document.createComment('k-if');
250
+ ref.value = el;
251
+ el.redraw = redraw;
252
+ return el;
248
253
  }
249
254
  // Handle function components
250
255
  if (typeof tag === 'function') {
251
- let el = tag(props);
252
- if (!el.redraw) {
253
- el.redraw = (newProps) => {
254
- props = newProps ? { ...props, ...newProps } : props;
255
- // $ same as below
256
- const old = el;
257
- el = tag(props);
258
- el.redraw = old.redraw; // inherit redraw
259
- if (ref) {
260
- ref.value = el;
261
- }
262
- old.replaceWith(el);
263
- return el;
264
- };
265
- }
266
- if (ref) {
267
- ref.value = el;
268
- }
269
- return el;
256
+ el = tag(props);
270
257
  }
271
258
  else {
272
- // & deal children here
273
- let children = props.children;
274
- delete props.children;
275
- let el = h(tag, props, children);
276
- if (ref) {
277
- ref.value = el;
278
- }
279
- el.redraw = (newProps, newChildren) => {
280
- props = newProps ? { ...props, ...newProps } : props;
281
- children = (newChildren ?? children);
282
- // $ same as above
283
- const old = el;
284
- el = h(tag, props, children);
285
- el.redraw = old.redraw; // inherit redraw
286
- if (ref) {
287
- ref.value = el;
288
- }
289
- old.replaceWith(el);
290
- return el;
291
- };
292
- return el;
259
+ el = h(tag, props, props.children);
293
260
  }
261
+ el.redraw ??= redraw;
262
+ ref.value = el;
263
+ return el;
294
264
  }
295
265
  /**
296
266
  * Fragment support - returns an array of children
@@ -339,33 +309,51 @@ const jsxs = jsx;
339
309
  * let aa = 10;
340
310
  * // ...
341
311
  * // aa might be changed
342
- * return createRedrawable(() => <div>{aa}</div>);
312
+ * return createRedrawableNoref(() => <div>{aa}</div>);
343
313
  * }
344
314
  * ```
345
315
  * Then the returned element has a `redraw` method to redraw itself with new values.
346
- * @param creator
316
+ * @param creator a simple creator function that returns an element
347
317
  * @returns created element
348
318
  */
349
- function createRedrawable(creator) {
350
- let element = creator();
319
+ function createRedrawableNoref(creator) {
320
+ let el = creator();
351
321
  const redraw = () => {
352
- const old = element;
353
- element = creator();
354
- old.replaceWith(element);
355
- element.redraw = redraw;
356
- return element;
322
+ const old = el;
323
+ el = creator();
324
+ old.replaceWith(el);
325
+ el.redraw = redraw;
326
+ return el;
357
327
  };
358
- element.redraw = redraw;
359
- return element;
328
+ el.redraw = redraw;
329
+ return el;
360
330
  }
361
-
362
331
  /**
363
- * Reference to the created HTML element.
364
- * - can alse be used to store normal values, but it is not reactive.
365
- * @param value mostly an HTMLElement
332
+ * A helper to create redrawable elements
333
+ * ```tsx
334
+ * export function MyComponent() {
335
+ * let aa = 10;
336
+ * // ...
337
+ * // aa might be changed
338
+ * return createRedrawable(() => <div>{aa}</div>);
339
+ * }
340
+ * ```
341
+ * Then the returned element has a `redraw` method to redraw itself with new values.
342
+ * @param creator a simple creator function that returns an element
343
+ * @returns created element's ref
366
344
  */
367
- function ref(value) {
368
- return { value: value, isKT: true };
345
+ function createRedrawable(creator) {
346
+ const elRef = ref();
347
+ elRef.value = creator();
348
+ const redraw = () => {
349
+ const old = elRef.value;
350
+ elRef.value = creator();
351
+ old.replaceWith(elRef.value);
352
+ elRef.value.redraw = redraw;
353
+ return elRef.value;
354
+ };
355
+ elRef.value.redraw = redraw;
356
+ return elRef;
369
357
  }
370
358
 
371
359
  function KTAsync(props) {
@@ -380,4 +368,4 @@ function KTAsync(props) {
380
368
  return comp;
381
369
  }
382
370
 
383
- export { Fragment, KTAsync, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs, ref };
371
+ export { Fragment, KTAsync, h as createElement, createRedrawable, createRedrawableNoref, h, jsx, jsxDEV, jsxs, ref };
@@ -16,12 +16,12 @@ interface KTRef<T> {
16
16
  */
17
17
  declare function ref<T = HTMLElement>(value?: T): KTRef<T>;
18
18
 
19
- type KTHTMLElement = HTMLElement & {
19
+ type KTHTMLElement<El extends HTMLElement = HTMLElement> = El & {
20
20
  /**
21
21
  * Automically generate a redraw function if it is not provided
22
22
  * @param props
23
23
  */
24
- redraw: (props?: KTAttribute, children?: KTRawContent) => KTHTMLElement;
24
+ redraw: (props?: KTAttribute, ...args: any[]) => KTHTMLElement;
25
25
  };
26
26
 
27
27
  declare global {
@@ -139,7 +139,7 @@ type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent)
139
139
  * ## About
140
140
  * @package @ktjs/core
141
141
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
142
- * @version 0.14.6 (Last Update: 2026.01.17 11:35:33.824)
142
+ * @version 0.15.1 (Last Update: 2026.01.25 13:09:33.161)
143
143
  * @license MIT
144
144
  * @link https://github.com/baendlorel/kt.js
145
145
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -178,14 +178,29 @@ declare const jsxs: typeof jsx;
178
178
  * let aa = 10;
179
179
  * // ...
180
180
  * // aa might be changed
181
- * return createRedrawable(() => <div>{aa}</div>);
181
+ * return createRedrawableNoref(() => <div>{aa}</div>);
182
182
  * }
183
183
  * ```
184
184
  * Then the returned element has a `redraw` method to redraw itself with new values.
185
- * @param creator
185
+ * @param creator a simple creator function that returns an element
186
186
  * @returns created element
187
187
  */
188
- declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
188
+ declare function createRedrawableNoref(creator: () => KTHTMLElement): KTHTMLElement;
189
+ /**
190
+ * A helper to create redrawable elements
191
+ * ```tsx
192
+ * export function MyComponent() {
193
+ * let aa = 10;
194
+ * // ...
195
+ * // aa might be changed
196
+ * return createRedrawable(() => <div>{aa}</div>);
197
+ * }
198
+ * ```
199
+ * Then the returned element has a `redraw` method to redraw itself with new values.
200
+ * @param creator a simple creator function that returns an element
201
+ * @returns created element's ref
202
+ */
203
+ declare function createRedrawable(creator: () => KTHTMLElement): KTRef<KTHTMLElement>;
189
204
 
190
- export { Fragment, h as createElement, createRedrawable, jsx, jsxDEV, jsxs, ref };
205
+ export { Fragment, h as createElement, createRedrawable, createRedrawableNoref, jsx, jsxDEV, jsxs, ref };
191
206
  export type { KTHTMLElement, KTRef };