@ktjs/core 0.15.0 → 0.15.2

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/dist/index.d.ts CHANGED
@@ -139,10 +139,6 @@ type KTComponent = (
139
139
  ) => KTHTMLElement | Promise<KTHTMLElement> | any;
140
140
 
141
141
  type HTML<T extends HTMLTag & otherstring> = T extends HTMLTag ? HTMLElementTagNameMap[T] : HTMLElement;
142
- type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>) & {
143
- kDepth: number;
144
- kUpdater: (() => void)[];
145
- };
146
142
  /**
147
143
  * Create an enhanced HTMLElement.
148
144
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -153,14 +149,14 @@ type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent)
153
149
  * ## About
154
150
  * @package @ktjs/core
155
151
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
156
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
152
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
157
153
  * @license MIT
158
154
  * @link https://github.com/baendlorel/kt.js
159
155
  * @link https://baendlorel.github.io/ Welcome to my site!
160
156
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
161
157
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
162
158
  */
163
- declare const h: H;
159
+ declare const h: <T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>;
164
160
 
165
161
  type JSXTag = HTMLTag | ((props?: any) => HTMLElement) | ((props?: any) => Promise<HTMLElement>) | ((props?: any) => KTHTMLElement) | ((props?: any) => Promise<KTHTMLElement>);
166
162
  /**
@@ -192,14 +188,29 @@ declare const jsxs: typeof jsx;
192
188
  * let aa = 10;
193
189
  * // ...
194
190
  * // aa might be changed
195
- * return createRedrawable(() => <div>{aa}</div>);
191
+ * return createRedrawableNoref(() => <div>{aa}</div>);
196
192
  * }
197
193
  * ```
198
194
  * Then the returned element has a `redraw` method to redraw itself with new values.
199
195
  * @param creator a simple creator function that returns an element
200
196
  * @returns created element
201
197
  */
202
- declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
198
+ declare function createRedrawableNoref(creator: () => KTHTMLElement): KTHTMLElement;
199
+ /**
200
+ * A helper to create redrawable elements
201
+ * ```tsx
202
+ * export function MyComponent() {
203
+ * let aa = 10;
204
+ * // ...
205
+ * // aa might be changed
206
+ * return createRedrawable(() => <div>{aa}</div>);
207
+ * }
208
+ * ```
209
+ * Then the returned element has a `redraw` method to redraw itself with new values.
210
+ * @param creator a simple creator function that returns an element
211
+ * @returns created element's ref
212
+ */
213
+ declare function createRedrawable(creator: () => KTHTMLElement): KTRef<KTHTMLElement>;
203
214
 
204
215
  /**
205
216
  * Extract component props type (excluding ref and children)
@@ -212,5 +223,5 @@ declare function KTAsync<T extends KTComponent>(props: {
212
223
  children?: KTRawContent;
213
224
  } & ExtractComponentProps<T>): KTHTMLElement;
214
225
 
215
- export { Fragment, KTAsync, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs, ref };
226
+ export { Fragment, KTAsync, h as createElement, createRedrawable, createRedrawableNoref, h, jsx, jsxDEV, jsxs, ref };
216
227
  export type { EventHandler, HTMLTag, KTAttribute, KTHTMLElement, KTRawAttr, KTRawContent, KTRawContents, KTRef };
@@ -57,10 +57,10 @@ var __ktjs_core__ = (function (exports) {
57
57
  const defaultHandler = (element, key, value) => element.setAttribute(key, value);
58
58
  function attrIsObject(element, attr) {
59
59
  const classValue = attr.class;
60
- const style = attr.style;
61
60
  if (classValue !== undefined) {
62
61
  element.className = classValue;
63
62
  }
63
+ const style = attr.style;
64
64
  if (style) {
65
65
  if (typeof style === 'string') {
66
66
  element.setAttribute('style', style);
@@ -92,12 +92,12 @@ var __ktjs_core__ = (function (exports) {
92
92
  }
93
93
  }
94
94
  function applyAttr(element, attr) {
95
- if (typeof attr === 'string') {
96
- element.className = attr;
97
- }
98
- else if (typeof attr === 'object' && attr !== null) {
95
+ if (typeof attr === 'object' && attr !== null) {
99
96
  attrIsObject(element, attr);
100
97
  }
98
+ else if (typeof attr === 'string') {
99
+ element.className = attr;
100
+ }
101
101
  else {
102
102
  throw new Error('kt.js: attr must be an object/string.');
103
103
  }
@@ -194,6 +194,9 @@ var __ktjs_core__ = (function (exports) {
194
194
  }
195
195
  }
196
196
 
197
+ const defaultCreator = (tag) => document.createElement(tag);
198
+ const svgCreator = (tag) => document.createElementNS('http://www.w3.org/1999/xhtml', tag);
199
+ let creator = defaultCreator;
197
200
  /**
198
201
  * Create an enhanced HTMLElement.
199
202
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -204,31 +207,45 @@ var __ktjs_core__ = (function (exports) {
204
207
  * ## About
205
208
  * @package @ktjs/core
206
209
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
207
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
210
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
208
211
  * @license MIT
209
212
  * @link https://github.com/baendlorel/kt.js
210
213
  * @link https://baendlorel.github.io/ Welcome to my site!
211
214
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
212
215
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
213
216
  */
214
- const h = ((tag, attr = '', content = '') => {
217
+ const h = (tag, attr, content) => {
215
218
  if (typeof tag !== 'string') {
216
219
  $throw('tagName must be a string.');
217
220
  }
221
+ let lastCreator = creator;
222
+ if (tag === 'svg') {
223
+ creator = svgCreator;
224
+ }
218
225
  // * start creating the element
219
- const element = document.createElement(tag);
226
+ const element = creator(tag);
220
227
  // * Handle content
221
228
  applyAttr(element, attr);
222
229
  applyContent(element, content);
230
+ creator = lastCreator; // restore previous creator
223
231
  return element;
224
- });
232
+ };
233
+
234
+ /**
235
+ * Reference to the created HTML element.
236
+ * - can alse be used to store normal values, but it is not reactive.
237
+ * @param value mostly an HTMLElement
238
+ */
239
+ function ref(value) {
240
+ return { value: value, isKT: true };
241
+ }
225
242
 
226
243
  const dummyRef = { value: null };
244
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
227
245
  /**
228
246
  * @param tag html tag or function component
229
247
  * @param props properties/attributes
230
248
  */
231
- // todo 加入对k-if的全面支持
232
249
  function jsx(tag, props = {}) {
233
250
  const ref = props.ref?.isKT ? props.ref : dummyRef;
234
251
  let el;
@@ -303,14 +320,14 @@ var __ktjs_core__ = (function (exports) {
303
320
  * let aa = 10;
304
321
  * // ...
305
322
  * // aa might be changed
306
- * return createRedrawable(() => <div>{aa}</div>);
323
+ * return createRedrawableNoref(() => <div>{aa}</div>);
307
324
  * }
308
325
  * ```
309
326
  * Then the returned element has a `redraw` method to redraw itself with new values.
310
327
  * @param creator a simple creator function that returns an element
311
328
  * @returns created element
312
329
  */
313
- function createRedrawable(creator) {
330
+ function createRedrawableNoref(creator) {
314
331
  let el = creator();
315
332
  const redraw = () => {
316
333
  const old = el;
@@ -322,14 +339,32 @@ var __ktjs_core__ = (function (exports) {
322
339
  el.redraw = redraw;
323
340
  return el;
324
341
  }
325
-
326
342
  /**
327
- * Reference to the created HTML element.
328
- * - can alse be used to store normal values, but it is not reactive.
329
- * @param value mostly an HTMLElement
343
+ * A helper to create redrawable elements
344
+ * ```tsx
345
+ * export function MyComponent() {
346
+ * let aa = 10;
347
+ * // ...
348
+ * // aa might be changed
349
+ * return createRedrawable(() => <div>{aa}</div>);
350
+ * }
351
+ * ```
352
+ * Then the returned element has a `redraw` method to redraw itself with new values.
353
+ * @param creator a simple creator function that returns an element
354
+ * @returns created element's ref
330
355
  */
331
- function ref(value) {
332
- return { value: value, isKT: true };
356
+ function createRedrawable(creator) {
357
+ const elRef = ref();
358
+ elRef.value = creator();
359
+ const redraw = () => {
360
+ const old = elRef.value;
361
+ elRef.value = creator();
362
+ old.replaceWith(elRef.value);
363
+ elRef.value.redraw = redraw;
364
+ return elRef.value;
365
+ };
366
+ elRef.value.redraw = redraw;
367
+ return elRef;
333
368
  }
334
369
 
335
370
  function KTAsync(props) {
@@ -348,6 +383,7 @@ var __ktjs_core__ = (function (exports) {
348
383
  exports.KTAsync = KTAsync;
349
384
  exports.createElement = h;
350
385
  exports.createRedrawable = createRedrawable;
386
+ exports.createRedrawableNoref = createRedrawableNoref;
351
387
  exports.h = h;
352
388
  exports.jsx = jsx;
353
389
  exports.jsxDEV = jsxDEV;
@@ -69,10 +69,10 @@ var __ktjs_core__ = (function (exports) {
69
69
  var defaultHandler = function (element, key, value) { return element.setAttribute(key, value); };
70
70
  function attrIsObject(element, attr) {
71
71
  var classValue = attr.class;
72
- var style = attr.style;
73
72
  if (classValue !== undefined) {
74
73
  element.className = classValue;
75
74
  }
75
+ var style = attr.style;
76
76
  if (style) {
77
77
  if (typeof style === 'string') {
78
78
  element.setAttribute('style', style);
@@ -104,12 +104,12 @@ var __ktjs_core__ = (function (exports) {
104
104
  }
105
105
  }
106
106
  function applyAttr(element, attr) {
107
- if (typeof attr === 'string') {
108
- element.className = attr;
109
- }
110
- else if (typeof attr === 'object' && attr !== null) {
107
+ if (typeof attr === 'object' && attr !== null) {
111
108
  attrIsObject(element, attr);
112
109
  }
110
+ else if (typeof attr === 'string') {
111
+ element.className = attr;
112
+ }
113
113
  else {
114
114
  throw new Error('kt.js: attr must be an object/string.');
115
115
  }
@@ -219,6 +219,9 @@ var __ktjs_core__ = (function (exports) {
219
219
  }
220
220
  }
221
221
 
222
+ var defaultCreator = function (tag) { return document.createElement(tag); };
223
+ var svgCreator = function (tag) { return document.createElementNS('http://www.w3.org/1999/xhtml', tag); };
224
+ var creator = defaultCreator;
222
225
  /**
223
226
  * Create an enhanced HTMLElement.
224
227
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -229,26 +232,29 @@ var __ktjs_core__ = (function (exports) {
229
232
  * ## About
230
233
  * @package @ktjs/core
231
234
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
232
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
235
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
233
236
  * @license MIT
234
237
  * @link https://github.com/baendlorel/kt.js
235
238
  * @link https://baendlorel.github.io/ Welcome to my site!
236
239
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
237
240
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
238
241
  */
239
- var h = (function (tag, attr, content) {
240
- if (attr === void 0) { attr = ''; }
241
- if (content === void 0) { content = ''; }
242
+ var h = function (tag, attr, content) {
242
243
  if (typeof tag !== 'string') {
243
244
  $throw('tagName must be a string.');
244
245
  }
246
+ var lastCreator = creator;
247
+ if (tag === 'svg') {
248
+ creator = svgCreator;
249
+ }
245
250
  // * start creating the element
246
- var element = document.createElement(tag);
251
+ var element = creator(tag);
247
252
  // * Handle content
248
253
  applyAttr(element, attr);
249
254
  applyContent(element, content);
255
+ creator = lastCreator; // restore previous creator
250
256
  return element;
251
- });
257
+ };
252
258
 
253
259
  /******************************************************************************
254
260
  Copyright (c) Microsoft Corporation.
@@ -283,12 +289,21 @@ var __ktjs_core__ = (function (exports) {
283
289
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
284
290
  };
285
291
 
292
+ /**
293
+ * Reference to the created HTML element.
294
+ * - can alse be used to store normal values, but it is not reactive.
295
+ * @param value mostly an HTMLElement
296
+ */
297
+ function ref(value) {
298
+ return { value: value, isKT: true };
299
+ }
300
+
286
301
  var dummyRef = { value: null };
302
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
287
303
  /**
288
304
  * @param tag html tag or function component
289
305
  * @param props properties/attributes
290
306
  */
291
- // todo 加入对k-if的全面支持
292
307
  function jsx(tag, props) {
293
308
  var _a, _b;
294
309
  if (props === void 0) { props = {}; }
@@ -369,14 +384,14 @@ var __ktjs_core__ = (function (exports) {
369
384
  * let aa = 10;
370
385
  * // ...
371
386
  * // aa might be changed
372
- * return createRedrawable(() => <div>{aa}</div>);
387
+ * return createRedrawableNoref(() => <div>{aa}</div>);
373
388
  * }
374
389
  * ```
375
390
  * Then the returned element has a `redraw` method to redraw itself with new values.
376
391
  * @param creator a simple creator function that returns an element
377
392
  * @returns created element
378
393
  */
379
- function createRedrawable(creator) {
394
+ function createRedrawableNoref(creator) {
380
395
  var el = creator();
381
396
  var redraw = function () {
382
397
  var old = el;
@@ -388,14 +403,32 @@ var __ktjs_core__ = (function (exports) {
388
403
  el.redraw = redraw;
389
404
  return el;
390
405
  }
391
-
392
406
  /**
393
- * Reference to the created HTML element.
394
- * - can alse be used to store normal values, but it is not reactive.
395
- * @param value mostly an HTMLElement
407
+ * A helper to create redrawable elements
408
+ * ```tsx
409
+ * export function MyComponent() {
410
+ * let aa = 10;
411
+ * // ...
412
+ * // aa might be changed
413
+ * return createRedrawable(() => <div>{aa}</div>);
414
+ * }
415
+ * ```
416
+ * Then the returned element has a `redraw` method to redraw itself with new values.
417
+ * @param creator a simple creator function that returns an element
418
+ * @returns created element's ref
396
419
  */
397
- function ref(value) {
398
- return { value: value, isKT: true };
420
+ function createRedrawable(creator) {
421
+ var elRef = ref();
422
+ elRef.value = creator();
423
+ var redraw = function () {
424
+ var old = elRef.value;
425
+ elRef.value = creator();
426
+ old.replaceWith(elRef.value);
427
+ elRef.value.redraw = redraw;
428
+ return elRef.value;
429
+ };
430
+ elRef.value.redraw = redraw;
431
+ return elRef;
399
432
  }
400
433
 
401
434
  function KTAsync(props) {
@@ -415,6 +448,7 @@ var __ktjs_core__ = (function (exports) {
415
448
  exports.KTAsync = KTAsync;
416
449
  exports.createElement = h;
417
450
  exports.createRedrawable = createRedrawable;
451
+ exports.createRedrawableNoref = createRedrawableNoref;
418
452
  exports.h = h;
419
453
  exports.jsx = jsx;
420
454
  exports.jsxDEV = jsxDEV;
package/dist/index.mjs CHANGED
@@ -54,10 +54,10 @@ const ktEventHandlers = {
54
54
  const defaultHandler = (element, key, value) => element.setAttribute(key, value);
55
55
  function attrIsObject(element, attr) {
56
56
  const classValue = attr.class;
57
- const style = attr.style;
58
57
  if (classValue !== undefined) {
59
58
  element.className = classValue;
60
59
  }
60
+ const style = attr.style;
61
61
  if (style) {
62
62
  if (typeof style === 'string') {
63
63
  element.setAttribute('style', style);
@@ -89,12 +89,12 @@ function attrIsObject(element, attr) {
89
89
  }
90
90
  }
91
91
  function applyAttr(element, attr) {
92
- if (typeof attr === 'string') {
93
- element.className = attr;
94
- }
95
- else if (typeof attr === 'object' && attr !== null) {
92
+ if (typeof attr === 'object' && attr !== null) {
96
93
  attrIsObject(element, attr);
97
94
  }
95
+ else if (typeof attr === 'string') {
96
+ element.className = attr;
97
+ }
98
98
  else {
99
99
  throw new Error('kt.js: attr must be an object/string.');
100
100
  }
@@ -191,6 +191,9 @@ function applyContent(element, content) {
191
191
  }
192
192
  }
193
193
 
194
+ const defaultCreator = (tag) => document.createElement(tag);
195
+ const svgCreator = (tag) => document.createElementNS('http://www.w3.org/1999/xhtml', tag);
196
+ let creator = defaultCreator;
194
197
  /**
195
198
  * Create an enhanced HTMLElement.
196
199
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -201,31 +204,45 @@ function applyContent(element, content) {
201
204
  * ## About
202
205
  * @package @ktjs/core
203
206
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
204
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
207
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
205
208
  * @license MIT
206
209
  * @link https://github.com/baendlorel/kt.js
207
210
  * @link https://baendlorel.github.io/ Welcome to my site!
208
211
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
209
212
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
210
213
  */
211
- const h = ((tag, attr = '', content = '') => {
214
+ const h = (tag, attr, content) => {
212
215
  if (typeof tag !== 'string') {
213
216
  $throw('tagName must be a string.');
214
217
  }
218
+ let lastCreator = creator;
219
+ if (tag === 'svg') {
220
+ creator = svgCreator;
221
+ }
215
222
  // * start creating the element
216
- const element = document.createElement(tag);
223
+ const element = creator(tag);
217
224
  // * Handle content
218
225
  applyAttr(element, attr);
219
226
  applyContent(element, content);
227
+ creator = lastCreator; // restore previous creator
220
228
  return element;
221
- });
229
+ };
230
+
231
+ /**
232
+ * Reference to the created HTML element.
233
+ * - can alse be used to store normal values, but it is not reactive.
234
+ * @param value mostly an HTMLElement
235
+ */
236
+ function ref(value) {
237
+ return { value: value, isKT: true };
238
+ }
222
239
 
223
240
  const dummyRef = { value: null };
241
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
224
242
  /**
225
243
  * @param tag html tag or function component
226
244
  * @param props properties/attributes
227
245
  */
228
- // todo 加入对k-if的全面支持
229
246
  function jsx(tag, props = {}) {
230
247
  const ref = props.ref?.isKT ? props.ref : dummyRef;
231
248
  let el;
@@ -300,14 +317,14 @@ const jsxs = jsx;
300
317
  * let aa = 10;
301
318
  * // ...
302
319
  * // aa might be changed
303
- * return createRedrawable(() => <div>{aa}</div>);
320
+ * return createRedrawableNoref(() => <div>{aa}</div>);
304
321
  * }
305
322
  * ```
306
323
  * Then the returned element has a `redraw` method to redraw itself with new values.
307
324
  * @param creator a simple creator function that returns an element
308
325
  * @returns created element
309
326
  */
310
- function createRedrawable(creator) {
327
+ function createRedrawableNoref(creator) {
311
328
  let el = creator();
312
329
  const redraw = () => {
313
330
  const old = el;
@@ -319,14 +336,32 @@ function createRedrawable(creator) {
319
336
  el.redraw = redraw;
320
337
  return el;
321
338
  }
322
-
323
339
  /**
324
- * Reference to the created HTML element.
325
- * - can alse be used to store normal values, but it is not reactive.
326
- * @param value mostly an HTMLElement
340
+ * A helper to create redrawable elements
341
+ * ```tsx
342
+ * export function MyComponent() {
343
+ * let aa = 10;
344
+ * // ...
345
+ * // aa might be changed
346
+ * return createRedrawable(() => <div>{aa}</div>);
347
+ * }
348
+ * ```
349
+ * Then the returned element has a `redraw` method to redraw itself with new values.
350
+ * @param creator a simple creator function that returns an element
351
+ * @returns created element's ref
327
352
  */
328
- function ref(value) {
329
- return { value: value, isKT: true };
353
+ function createRedrawable(creator) {
354
+ const elRef = ref();
355
+ elRef.value = creator();
356
+ const redraw = () => {
357
+ const old = elRef.value;
358
+ elRef.value = creator();
359
+ old.replaceWith(elRef.value);
360
+ elRef.value.redraw = redraw;
361
+ return elRef.value;
362
+ };
363
+ elRef.value.redraw = redraw;
364
+ return elRef;
330
365
  }
331
366
 
332
367
  function KTAsync(props) {
@@ -341,4 +376,4 @@ function KTAsync(props) {
341
376
  return comp;
342
377
  }
343
378
 
344
- export { Fragment, KTAsync, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs, ref };
379
+ export { Fragment, KTAsync, h as createElement, createRedrawable, createRedrawableNoref, h, jsx, jsxDEV, jsxs, ref };
@@ -125,10 +125,6 @@ type KTSpecialEventHandlers = {
125
125
  type KTAttribute = KTBaseAttribute & KTPrefixedEventHandlers & KTSpecialEventHandlers;
126
126
 
127
127
  type HTML<T extends HTMLTag & otherstring> = T extends HTMLTag ? HTMLElementTagNameMap[T] : HTMLElement;
128
- type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>) & {
129
- kDepth: number;
130
- kUpdater: (() => void)[];
131
- };
132
128
  /**
133
129
  * Create an enhanced HTMLElement.
134
130
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -139,14 +135,14 @@ type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent)
139
135
  * ## About
140
136
  * @package @ktjs/core
141
137
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
142
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
138
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
143
139
  * @license MIT
144
140
  * @link https://github.com/baendlorel/kt.js
145
141
  * @link https://baendlorel.github.io/ Welcome to my site!
146
142
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
147
143
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
148
144
  */
149
- declare const h: H;
145
+ declare const h: <T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>;
150
146
 
151
147
  type JSXTag = HTMLTag | ((props?: any) => HTMLElement) | ((props?: any) => Promise<HTMLElement>) | ((props?: any) => KTHTMLElement) | ((props?: any) => Promise<KTHTMLElement>);
152
148
  /**
@@ -178,14 +174,29 @@ declare const jsxs: typeof jsx;
178
174
  * let aa = 10;
179
175
  * // ...
180
176
  * // aa might be changed
181
- * return createRedrawable(() => <div>{aa}</div>);
177
+ * return createRedrawableNoref(() => <div>{aa}</div>);
182
178
  * }
183
179
  * ```
184
180
  * Then the returned element has a `redraw` method to redraw itself with new values.
185
181
  * @param creator a simple creator function that returns an element
186
182
  * @returns created element
187
183
  */
188
- declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
184
+ declare function createRedrawableNoref(creator: () => KTHTMLElement): KTHTMLElement;
185
+ /**
186
+ * A helper to create redrawable elements
187
+ * ```tsx
188
+ * export function MyComponent() {
189
+ * let aa = 10;
190
+ * // ...
191
+ * // aa might be changed
192
+ * return createRedrawable(() => <div>{aa}</div>);
193
+ * }
194
+ * ```
195
+ * Then the returned element has a `redraw` method to redraw itself with new values.
196
+ * @param creator a simple creator function that returns an element
197
+ * @returns created element's ref
198
+ */
199
+ declare function createRedrawable(creator: () => KTHTMLElement): KTRef<KTHTMLElement>;
189
200
 
190
- export { Fragment, h as createElement, createRedrawable, jsx, jsxDEV, jsxs, ref };
201
+ export { Fragment, h as createElement, createRedrawable, createRedrawableNoref, jsx, jsxDEV, jsxs, ref };
191
202
  export type { KTHTMLElement, KTRef };
@@ -54,10 +54,10 @@ const ktEventHandlers = {
54
54
  const defaultHandler = (element, key, value) => element.setAttribute(key, value);
55
55
  function attrIsObject(element, attr) {
56
56
  const classValue = attr.class;
57
- const style = attr.style;
58
57
  if (classValue !== undefined) {
59
58
  element.className = classValue;
60
59
  }
60
+ const style = attr.style;
61
61
  if (style) {
62
62
  if (typeof style === 'string') {
63
63
  element.setAttribute('style', style);
@@ -89,12 +89,12 @@ function attrIsObject(element, attr) {
89
89
  }
90
90
  }
91
91
  function applyAttr(element, attr) {
92
- if (typeof attr === 'string') {
93
- element.className = attr;
94
- }
95
- else if (typeof attr === 'object' && attr !== null) {
92
+ if (typeof attr === 'object' && attr !== null) {
96
93
  attrIsObject(element, attr);
97
94
  }
95
+ else if (typeof attr === 'string') {
96
+ element.className = attr;
97
+ }
98
98
  else {
99
99
  throw new Error('kt.js: attr must be an object/string.');
100
100
  }
@@ -191,6 +191,9 @@ function applyContent(element, content) {
191
191
  }
192
192
  }
193
193
 
194
+ const defaultCreator = (tag) => document.createElement(tag);
195
+ const svgCreator = (tag) => document.createElementNS('http://www.w3.org/1999/xhtml', tag);
196
+ let creator = defaultCreator;
194
197
  /**
195
198
  * Create an enhanced HTMLElement.
196
199
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -201,31 +204,45 @@ function applyContent(element, content) {
201
204
  * ## About
202
205
  * @package @ktjs/core
203
206
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
204
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
207
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
205
208
  * @license MIT
206
209
  * @link https://github.com/baendlorel/kt.js
207
210
  * @link https://baendlorel.github.io/ Welcome to my site!
208
211
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
209
212
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
210
213
  */
211
- const h = ((tag, attr = '', content = '') => {
214
+ const h = (tag, attr, content) => {
212
215
  if (typeof tag !== 'string') {
213
216
  $throw('tagName must be a string.');
214
217
  }
218
+ let lastCreator = creator;
219
+ if (tag === 'svg') {
220
+ creator = svgCreator;
221
+ }
215
222
  // * start creating the element
216
- const element = document.createElement(tag);
223
+ const element = creator(tag);
217
224
  // * Handle content
218
225
  applyAttr(element, attr);
219
226
  applyContent(element, content);
227
+ creator = lastCreator; // restore previous creator
220
228
  return element;
221
- });
229
+ };
230
+
231
+ /**
232
+ * Reference to the created HTML element.
233
+ * - can alse be used to store normal values, but it is not reactive.
234
+ * @param value mostly an HTMLElement
235
+ */
236
+ function ref(value) {
237
+ return { value: value, isKT: true };
238
+ }
222
239
 
223
240
  const dummyRef = { value: null };
241
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
224
242
  /**
225
243
  * @param tag html tag or function component
226
244
  * @param props properties/attributes
227
245
  */
228
- // todo 加入对k-if的全面支持
229
246
  function jsx(tag, props = {}) {
230
247
  const ref = props.ref?.isKT ? props.ref : dummyRef;
231
248
  let el;
@@ -300,14 +317,14 @@ const jsxs = jsx;
300
317
  * let aa = 10;
301
318
  * // ...
302
319
  * // aa might be changed
303
- * return createRedrawable(() => <div>{aa}</div>);
320
+ * return createRedrawableNoref(() => <div>{aa}</div>);
304
321
  * }
305
322
  * ```
306
323
  * Then the returned element has a `redraw` method to redraw itself with new values.
307
324
  * @param creator a simple creator function that returns an element
308
325
  * @returns created element
309
326
  */
310
- function createRedrawable(creator) {
327
+ function createRedrawableNoref(creator) {
311
328
  let el = creator();
312
329
  const redraw = () => {
313
330
  const old = el;
@@ -319,14 +336,32 @@ function createRedrawable(creator) {
319
336
  el.redraw = redraw;
320
337
  return el;
321
338
  }
322
-
323
339
  /**
324
- * Reference to the created HTML element.
325
- * - can alse be used to store normal values, but it is not reactive.
326
- * @param value mostly an HTMLElement
340
+ * A helper to create redrawable elements
341
+ * ```tsx
342
+ * export function MyComponent() {
343
+ * let aa = 10;
344
+ * // ...
345
+ * // aa might be changed
346
+ * return createRedrawable(() => <div>{aa}</div>);
347
+ * }
348
+ * ```
349
+ * Then the returned element has a `redraw` method to redraw itself with new values.
350
+ * @param creator a simple creator function that returns an element
351
+ * @returns created element's ref
327
352
  */
328
- function ref(value) {
329
- return { value: value, isKT: true };
353
+ function createRedrawable(creator) {
354
+ const elRef = ref();
355
+ elRef.value = creator();
356
+ const redraw = () => {
357
+ const old = elRef.value;
358
+ elRef.value = creator();
359
+ old.replaceWith(elRef.value);
360
+ elRef.value.redraw = redraw;
361
+ return elRef.value;
362
+ };
363
+ elRef.value.redraw = redraw;
364
+ return elRef;
330
365
  }
331
366
 
332
- export { Fragment, h as createElement, createRedrawable, jsx, jsxDEV, jsxs, ref };
367
+ export { Fragment, h as createElement, createRedrawable, createRedrawableNoref, jsx, jsxDEV, jsxs, ref };
@@ -119,10 +119,6 @@ type KTSpecialEventHandlers = {
119
119
  type KTAttribute = KTBaseAttribute & KTPrefixedEventHandlers & KTSpecialEventHandlers;
120
120
 
121
121
  type HTML<T extends HTMLTag & otherstring> = T extends HTMLTag ? HTMLElementTagNameMap[T] : HTMLElement;
122
- type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>) & {
123
- kDepth: number;
124
- kUpdater: (() => void)[];
125
- };
126
122
  /**
127
123
  * Create an enhanced HTMLElement.
128
124
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -133,14 +129,14 @@ type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent)
133
129
  * ## About
134
130
  * @package @ktjs/core
135
131
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
136
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
132
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
137
133
  * @license MIT
138
134
  * @link https://github.com/baendlorel/kt.js
139
135
  * @link https://baendlorel.github.io/ Welcome to my site!
140
136
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
141
137
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
142
138
  */
143
- declare const h: H;
139
+ declare const h: <T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent) => HTML<T>;
144
140
 
145
141
  type JSXTag = HTMLTag | ((props?: any) => HTMLElement) | ((props?: any) => Promise<HTMLElement>) | ((props?: any) => KTHTMLElement) | ((props?: any) => Promise<KTHTMLElement>);
146
142
  /**
@@ -172,13 +168,28 @@ declare const jsxs: typeof jsx;
172
168
  * let aa = 10;
173
169
  * // ...
174
170
  * // aa might be changed
175
- * return createRedrawable(() => <div>{aa}</div>);
171
+ * return createRedrawableNoref(() => <div>{aa}</div>);
176
172
  * }
177
173
  * ```
178
174
  * Then the returned element has a `redraw` method to redraw itself with new values.
179
175
  * @param creator a simple creator function that returns an element
180
176
  * @returns created element
181
177
  */
182
- declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
178
+ declare function createRedrawableNoref(creator: () => KTHTMLElement): KTHTMLElement;
179
+ /**
180
+ * A helper to create redrawable elements
181
+ * ```tsx
182
+ * export function MyComponent() {
183
+ * let aa = 10;
184
+ * // ...
185
+ * // aa might be changed
186
+ * return createRedrawable(() => <div>{aa}</div>);
187
+ * }
188
+ * ```
189
+ * Then the returned element has a `redraw` method to redraw itself with new values.
190
+ * @param creator a simple creator function that returns an element
191
+ * @returns created element's ref
192
+ */
193
+ declare function createRedrawable(creator: () => KTHTMLElement): KTRef<KTHTMLElement>;
183
194
 
184
- export { Fragment, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs };
195
+ export { Fragment, h as createElement, createRedrawable, createRedrawableNoref, h, jsx, jsxDEV, jsxs };
@@ -54,10 +54,10 @@ const ktEventHandlers = {
54
54
  const defaultHandler = (element, key, value) => element.setAttribute(key, value);
55
55
  function attrIsObject(element, attr) {
56
56
  const classValue = attr.class;
57
- const style = attr.style;
58
57
  if (classValue !== undefined) {
59
58
  element.className = classValue;
60
59
  }
60
+ const style = attr.style;
61
61
  if (style) {
62
62
  if (typeof style === 'string') {
63
63
  element.setAttribute('style', style);
@@ -89,12 +89,12 @@ function attrIsObject(element, attr) {
89
89
  }
90
90
  }
91
91
  function applyAttr(element, attr) {
92
- if (typeof attr === 'string') {
93
- element.className = attr;
94
- }
95
- else if (typeof attr === 'object' && attr !== null) {
92
+ if (typeof attr === 'object' && attr !== null) {
96
93
  attrIsObject(element, attr);
97
94
  }
95
+ else if (typeof attr === 'string') {
96
+ element.className = attr;
97
+ }
98
98
  else {
99
99
  throw new Error('kt.js: attr must be an object/string.');
100
100
  }
@@ -191,6 +191,9 @@ function applyContent(element, content) {
191
191
  }
192
192
  }
193
193
 
194
+ const defaultCreator = (tag) => document.createElement(tag);
195
+ const svgCreator = (tag) => document.createElementNS('http://www.w3.org/1999/xhtml', tag);
196
+ let creator = defaultCreator;
194
197
  /**
195
198
  * Create an enhanced HTMLElement.
196
199
  * - Only supports HTMLElements, **NOT** SVGElements or other Elements.
@@ -201,31 +204,45 @@ function applyContent(element, content) {
201
204
  * ## About
202
205
  * @package @ktjs/core
203
206
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
204
- * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
207
+ * @version 0.15.2 (Last Update: 2026.01.25 14:25:37.357)
205
208
  * @license MIT
206
209
  * @link https://github.com/baendlorel/kt.js
207
210
  * @link https://baendlorel.github.io/ Welcome to my site!
208
211
  * @description Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support
209
212
  * @copyright Copyright (c) 2026 Kasukabe Tsumugi. All rights reserved.
210
213
  */
211
- const h = ((tag, attr = '', content = '') => {
214
+ const h = (tag, attr, content) => {
212
215
  if (typeof tag !== 'string') {
213
216
  $throw('tagName must be a string.');
214
217
  }
218
+ let lastCreator = creator;
219
+ if (tag === 'svg') {
220
+ creator = svgCreator;
221
+ }
215
222
  // * start creating the element
216
- const element = document.createElement(tag);
223
+ const element = creator(tag);
217
224
  // * Handle content
218
225
  applyAttr(element, attr);
219
226
  applyContent(element, content);
227
+ creator = lastCreator; // restore previous creator
220
228
  return element;
221
- });
229
+ };
230
+
231
+ /**
232
+ * Reference to the created HTML element.
233
+ * - can alse be used to store normal values, but it is not reactive.
234
+ * @param value mostly an HTMLElement
235
+ */
236
+ function ref(value) {
237
+ return { value: value, isKT: true };
238
+ }
222
239
 
223
240
  const dummyRef = { value: null };
241
+ // todo 是否进一步削减h函数的分支,比如去掉string为attr的情况
224
242
  /**
225
243
  * @param tag html tag or function component
226
244
  * @param props properties/attributes
227
245
  */
228
- // todo 加入对k-if的全面支持
229
246
  function jsx(tag, props = {}) {
230
247
  const ref = props.ref?.isKT ? props.ref : dummyRef;
231
248
  let el;
@@ -300,14 +317,14 @@ const jsxs = jsx;
300
317
  * let aa = 10;
301
318
  * // ...
302
319
  * // aa might be changed
303
- * return createRedrawable(() => <div>{aa}</div>);
320
+ * return createRedrawableNoref(() => <div>{aa}</div>);
304
321
  * }
305
322
  * ```
306
323
  * Then the returned element has a `redraw` method to redraw itself with new values.
307
324
  * @param creator a simple creator function that returns an element
308
325
  * @returns created element
309
326
  */
310
- function createRedrawable(creator) {
327
+ function createRedrawableNoref(creator) {
311
328
  let el = creator();
312
329
  const redraw = () => {
313
330
  const old = el;
@@ -319,5 +336,32 @@ function createRedrawable(creator) {
319
336
  el.redraw = redraw;
320
337
  return el;
321
338
  }
339
+ /**
340
+ * A helper to create redrawable elements
341
+ * ```tsx
342
+ * export function MyComponent() {
343
+ * let aa = 10;
344
+ * // ...
345
+ * // aa might be changed
346
+ * return createRedrawable(() => <div>{aa}</div>);
347
+ * }
348
+ * ```
349
+ * Then the returned element has a `redraw` method to redraw itself with new values.
350
+ * @param creator a simple creator function that returns an element
351
+ * @returns created element's ref
352
+ */
353
+ function createRedrawable(creator) {
354
+ const elRef = ref();
355
+ elRef.value = creator();
356
+ const redraw = () => {
357
+ const old = elRef.value;
358
+ elRef.value = creator();
359
+ old.replaceWith(elRef.value);
360
+ elRef.value.redraw = redraw;
361
+ return elRef.value;
362
+ };
363
+ elRef.value.redraw = redraw;
364
+ return elRef;
365
+ }
322
366
 
323
- export { Fragment, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs };
367
+ export { Fragment, h as createElement, createRedrawable, createRedrawableNoref, h, jsx, jsxDEV, jsxs };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/core",
3
- "version": "0.15.0",
3
+ "version": "0.15.2",
4
4
  "description": "Core functionality for kt.js - DOM manipulation utilities with JSX/TSX support",
5
5
  "type": "module",
6
6
  "module": "./dist/index.mjs",