@ktjs/core 0.14.6 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,7 +12,7 @@ Core DOM manipulation utilities for KT.js framework with built-in JSX/TSX suppor
12
12
 
13
13
  `@ktjs/core` is the foundation of KT.js, providing the essential `h` function and DOM utilities for building web applications with direct DOM manipulation. It emphasizes performance, type safety, and minimal abstraction over native DOM APIs.
14
14
 
15
- **Current Version:** 0.13.0
15
+ **Current Version:** 0.14.6
16
16
 
17
17
  ## Features
18
18
 
@@ -25,7 +25,9 @@ Core DOM manipulation utilities for KT.js framework with built-in JSX/TSX suppor
25
25
  - Zero virtual DOM - JSX compiles directly to `h()` function calls
26
26
  - Full HTML element type inference (`<button>` returns `HTMLButtonElement`)
27
27
  - Support for function components
28
- - **NEW**: `redraw()` method for controlled re-rendering
28
+ - `redraw()` method for controlled re-rendering
29
+ - **k-if directive**: Conditional element creation with `k-if` attribute
30
+ - Array children support for seamless list rendering
29
31
  - **KTAsync Component**: Handle async components with ease
30
32
  - Automatic handling of Promise-based components
31
33
  - Seamless integration with JSX/TSX
@@ -87,7 +89,7 @@ const button1 = h(
87
89
  {
88
90
  'on:click': () => alert('Clicked!'),
89
91
  },
90
- 'Button 1'
92
+ 'Button 1',
91
93
  );
92
94
 
93
95
  // Function attribute (also treated as event listener)
@@ -97,7 +99,7 @@ const button2 = h(
97
99
  click: (e) => console.log('Event:', e),
98
100
  'data-id': '123', // Regular attribute
99
101
  },
100
- 'Button 2'
102
+ 'Button 2',
101
103
  );
102
104
 
103
105
  // Both regular and event handler for same name
@@ -136,9 +138,43 @@ const Greeting = ({ name }: { name: string }) => (
136
138
  const app = <Greeting name="World" />;
137
139
  ```
138
140
 
141
+ ### Conditional Rendering with k-if (v0.14.6+)
142
+
143
+ The `k-if` directive allows conditional element creation:
144
+
145
+ ```tsx
146
+ import { h } from '@ktjs/core';
147
+
148
+ // Element will only be created if condition is true
149
+ const isVisible = true;
150
+ const element = <div k-if={isVisible}>This will be rendered</div>;
151
+
152
+ // Element will not be created if condition is false
153
+ const isHidden = false;
154
+ const hidden = <div k-if={isHidden}>This will NOT be rendered</div>;
155
+ // hidden will be undefined/null
156
+
157
+ // Practical example
158
+ const UserProfile = ({ user, isLoggedIn }: { user: any; isLoggedIn: boolean }) => (
159
+ <div>
160
+ <h1>User Profile</h1>
161
+ <div k-if={isLoggedIn}>
162
+ <p>Welcome, {user.name}!</p>
163
+ <button>Logout</button>
164
+ </div>
165
+ <div k-if={!isLoggedIn}>
166
+ <p>Please log in to continue</p>
167
+ <button>Login</button>
168
+ </div>
169
+ </div>
170
+ );
171
+ ```
172
+
173
+ **Note**: `k-if` is evaluated **once** at element creation time. It's not reactive - if you need dynamic visibility, use CSS or manually recreate the element.
174
+
139
175
  ### Redraw Mechanism (v0.11+)
140
176
 
141
- The new `redraw()` method allows you to update components efficiently:
177
+ The `redraw()` method allows you to update components efficiently:
142
178
 
143
179
  ```tsx
144
180
  import { h, KTHTMLElement } from '@ktjs/core';
@@ -164,6 +200,39 @@ const div = (<div>Old content</div>) as KTHTMLElement;
164
200
  div.redraw(undefined, 'New content');
165
201
  ```
166
202
 
203
+ ### Array Children Support (v0.14.1+)
204
+
205
+ Children can now be arrays for easier list rendering:
206
+
207
+ ```tsx
208
+ import { h } from '@ktjs/core';
209
+
210
+ // Map arrays directly as children
211
+ const items = ['Apple', 'Banana', 'Orange'];
212
+ const list = (
213
+ <ul>
214
+ {items.map((item) => (
215
+ <li>{item}</li>
216
+ ))}
217
+ </ul>
218
+ );
219
+
220
+ // Mix mapped elements with other elements
221
+ const TodoList = ({ todos }: { todos: string[] }) => (
222
+ <div>
223
+ <h2>Todo List</h2>
224
+ <ul>
225
+ {todos.map((todo) => (
226
+ <li>{todo}</li>
227
+ ))}
228
+ <li>
229
+ <button>Add More</button>
230
+ </li>
231
+ </ul>
232
+ </div>
233
+ );
234
+ ```
235
+
167
236
  ### Async Components
168
237
 
169
238
  ```typescript
package/dist/index.d.ts CHANGED
@@ -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 {
@@ -153,7 +153,7 @@ type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent)
153
153
  * ## About
154
154
  * @package @ktjs/core
155
155
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
156
- * @version 0.14.6 (Last Update: 2026.01.17 11:35:33.824)
156
+ * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
157
157
  * @license MIT
158
158
  * @link https://github.com/baendlorel/kt.js
159
159
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -196,7 +196,7 @@ declare const jsxs: typeof jsx;
196
196
  * }
197
197
  * ```
198
198
  * Then the returned element has a `redraw` method to redraw itself with new values.
199
- * @param creator
199
+ * @param creator a simple creator function that returns an element
200
200
  * @returns created element
201
201
  */
202
202
  declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
@@ -56,12 +56,6 @@ var __ktjs_core__ = (function (exports) {
56
56
 
57
57
  const defaultHandler = (element, key, value) => element.setAttribute(key, value);
58
58
  function attrIsObject(element, attr) {
59
- // & deal k-if first
60
- if ('k-if' in attr) {
61
- if (!attr['k-if']) {
62
- return false;
63
- }
64
- }
65
59
  const classValue = attr.class;
66
60
  const style = attr.style;
67
61
  if (classValue !== undefined) {
@@ -78,39 +72,31 @@ var __ktjs_core__ = (function (exports) {
78
72
  }
79
73
  }
80
74
  for (const key in attr) {
81
- if (key === 'class' || key === 'style' || key === 'k-if') {
75
+ if (key === 'class' || key === 'style' || key === 'children' || key === 'k-if' || key === 'ref') {
82
76
  continue;
83
77
  }
84
78
  const o = attr[key];
85
- // force register on:xxx as an event handler
86
- // !if o is not valid, the throwing job will be done by `on`, not kt.js
87
79
  // # special handling for kt.js specific events
88
80
  const ktEvent = ktEventHandlers[key];
89
81
  if (ktEvent) {
90
82
  ktEvent(element, o);
91
- continue;
92
83
  }
93
- // # normal event handler
94
- if (key.startsWith('on:')) {
84
+ // normal event handler
85
+ else if (key.startsWith('on:')) {
95
86
  element.addEventListener(key.slice(3), o); // chop off the `@`
96
- continue;
97
- }
98
- if (typeof o === 'function') {
99
- (handlers[key] || defaultHandler)(element, key, o());
100
87
  }
88
+ // normal attributes
101
89
  else {
102
90
  (handlers[key] || defaultHandler)(element, key, o);
103
91
  }
104
92
  }
105
- return true;
106
93
  }
107
94
  function applyAttr(element, attr) {
108
95
  if (typeof attr === 'string') {
109
96
  element.className = attr;
110
- return true;
111
97
  }
112
98
  else if (typeof attr === 'object' && attr !== null) {
113
- return attrIsObject(element, attr);
99
+ attrIsObject(element, attr);
114
100
  }
115
101
  else {
116
102
  throw new Error('kt.js: attr must be an object/string.');
@@ -218,7 +204,7 @@ var __ktjs_core__ = (function (exports) {
218
204
  * ## About
219
205
  * @package @ktjs/core
220
206
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
221
- * @version 0.14.6 (Last Update: 2026.01.17 11:35:33.824)
207
+ * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
222
208
  * @license MIT
223
209
  * @link https://github.com/baendlorel/kt.js
224
210
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -232,68 +218,43 @@ var __ktjs_core__ = (function (exports) {
232
218
  // * start creating the element
233
219
  const element = document.createElement(tag);
234
220
  // * Handle content
235
- const kif = applyAttr(element, attr);
236
- if (!kif) {
237
- return document.createComment('k-if');
238
- }
221
+ applyAttr(element, attr);
239
222
  applyContent(element, content);
240
223
  return element;
241
224
  });
242
225
 
226
+ const dummyRef = { value: null };
243
227
  /**
244
228
  * @param tag html tag or function component
245
229
  * @param props properties/attributes
246
230
  */
231
+ // todo 加入对k-if的全面支持
247
232
  function jsx(tag, props = {}) {
248
- const ref = props.ref?.isKT ? props.ref : null;
249
- if (ref) {
250
- delete props.ref;
233
+ const ref = props.ref?.isKT ? props.ref : dummyRef;
234
+ let el;
235
+ const redraw = (newProps) => {
236
+ props = newProps ? { ...props, ...newProps } : props;
237
+ const old = el;
238
+ el = jsx(tag, props);
239
+ old.replaceWith(el);
240
+ return el;
241
+ };
242
+ if ('k-if' in props && !props['k-if']) {
243
+ el = document.createComment('k-if');
244
+ ref.value = el;
245
+ el.redraw = redraw;
246
+ return el;
251
247
  }
252
248
  // Handle function components
253
249
  if (typeof tag === 'function') {
254
- let el = tag(props);
255
- if (!el.redraw) {
256
- el.redraw = (newProps) => {
257
- props = newProps ? { ...props, ...newProps } : props;
258
- // $ same as below
259
- const old = el;
260
- el = tag(props);
261
- el.redraw = old.redraw; // inherit redraw
262
- if (ref) {
263
- ref.value = el;
264
- }
265
- old.replaceWith(el);
266
- return el;
267
- };
268
- }
269
- if (ref) {
270
- ref.value = el;
271
- }
272
- return el;
250
+ el = tag(props);
273
251
  }
274
252
  else {
275
- // & deal children here
276
- let children = props.children;
277
- delete props.children;
278
- let el = h(tag, props, children);
279
- if (ref) {
280
- ref.value = el;
281
- }
282
- el.redraw = (newProps, newChildren) => {
283
- props = newProps ? { ...props, ...newProps } : props;
284
- children = (newChildren ?? children);
285
- // $ same as above
286
- const old = el;
287
- el = h(tag, props, children);
288
- el.redraw = old.redraw; // inherit redraw
289
- if (ref) {
290
- ref.value = el;
291
- }
292
- old.replaceWith(el);
293
- return el;
294
- };
295
- return el;
253
+ el = h(tag, props, props.children);
296
254
  }
255
+ el.redraw ??= redraw;
256
+ ref.value = el;
257
+ return el;
297
258
  }
298
259
  /**
299
260
  * Fragment support - returns an array of children
@@ -346,20 +307,20 @@ var __ktjs_core__ = (function (exports) {
346
307
  * }
347
308
  * ```
348
309
  * Then the returned element has a `redraw` method to redraw itself with new values.
349
- * @param creator
310
+ * @param creator a simple creator function that returns an element
350
311
  * @returns created element
351
312
  */
352
313
  function createRedrawable(creator) {
353
- let element = creator();
314
+ let el = creator();
354
315
  const redraw = () => {
355
- const old = element;
356
- element = creator();
357
- old.replaceWith(element);
358
- element.redraw = redraw;
359
- return element;
316
+ const old = el;
317
+ el = creator();
318
+ old.replaceWith(el);
319
+ el.redraw = redraw;
320
+ return el;
360
321
  };
361
- element.redraw = redraw;
362
- return element;
322
+ el.redraw = redraw;
323
+ return el;
363
324
  }
364
325
 
365
326
  /**
@@ -68,12 +68,6 @@ 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
72
  var style = attr.style;
79
73
  if (classValue !== undefined) {
@@ -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
107
  if (typeof attr === 'string') {
121
108
  element.className = attr;
122
- return true;
123
109
  }
124
110
  else if (typeof attr === 'object' && attr !== null) {
125
- return attrIsObject(element, attr);
111
+ attrIsObject(element, 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.0 (Last Update: 2026.01.24 00:25:38.464)
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,40 @@ var __ktjs_core__ = (function (exports) {
300
283
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
301
284
  };
302
285
 
286
+ var dummyRef = { value: null };
303
287
  /**
304
288
  * @param tag html tag or function component
305
289
  * @param props properties/attributes
306
290
  */
291
+ // todo 加入对k-if的全面支持
307
292
  function jsx(tag, props) {
308
- var _a;
293
+ var _a, _b;
309
294
  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;
295
+ var ref = ((_a = props.ref) === null || _a === void 0 ? void 0 : _a.isKT) ? props.ref : dummyRef;
296
+ var el;
297
+ var redraw = function (newProps) {
298
+ props = newProps ? __assign(__assign({}, props), newProps) : props;
299
+ var old = el;
300
+ el = jsx(tag, props);
301
+ old.replaceWith(el);
302
+ return el;
303
+ };
304
+ if ('k-if' in props && !props['k-if']) {
305
+ el = document.createComment('k-if');
306
+ ref.value = el;
307
+ el.redraw = redraw;
308
+ return el;
313
309
  }
314
310
  // Handle function components
315
311
  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;
312
+ el = tag(props);
335
313
  }
336
314
  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;
315
+ el = h(tag, props, props.children);
358
316
  }
317
+ (_b = el.redraw) !== null && _b !== void 0 ? _b : (el.redraw = redraw);
318
+ ref.value = el;
319
+ return el;
359
320
  }
360
321
  /**
361
322
  * Fragment support - returns an array of children
@@ -412,20 +373,20 @@ var __ktjs_core__ = (function (exports) {
412
373
  * }
413
374
  * ```
414
375
  * Then the returned element has a `redraw` method to redraw itself with new values.
415
- * @param creator
376
+ * @param creator a simple creator function that returns an element
416
377
  * @returns created element
417
378
  */
418
379
  function createRedrawable(creator) {
419
- var element = creator();
380
+ var el = creator();
420
381
  var redraw = function () {
421
- var old = element;
422
- element = creator();
423
- old.replaceWith(element);
424
- element.redraw = redraw;
425
- return element;
382
+ var old = el;
383
+ el = creator();
384
+ old.replaceWith(el);
385
+ el.redraw = redraw;
386
+ return el;
426
387
  };
427
- element.redraw = redraw;
428
- return element;
388
+ el.redraw = redraw;
389
+ return el;
429
390
  }
430
391
 
431
392
  /**
package/dist/index.mjs CHANGED
@@ -53,12 +53,6 @@ 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
57
  const style = attr.style;
64
58
  if (classValue !== undefined) {
@@ -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
92
  if (typeof attr === 'string') {
106
93
  element.className = attr;
107
- return true;
108
94
  }
109
95
  else if (typeof attr === 'object' && attr !== null) {
110
- return attrIsObject(element, attr);
96
+ attrIsObject(element, 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.0 (Last Update: 2026.01.24 00:25:38.464)
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,43 @@ 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
+ const dummyRef = { value: null };
240
224
  /**
241
225
  * @param tag html tag or function component
242
226
  * @param props properties/attributes
243
227
  */
228
+ // todo 加入对k-if的全面支持
244
229
  function jsx(tag, props = {}) {
245
- const ref = props.ref?.isKT ? props.ref : null;
246
- if (ref) {
247
- delete props.ref;
230
+ const ref = props.ref?.isKT ? props.ref : dummyRef;
231
+ let el;
232
+ const redraw = (newProps) => {
233
+ props = newProps ? { ...props, ...newProps } : props;
234
+ const old = el;
235
+ el = jsx(tag, props);
236
+ old.replaceWith(el);
237
+ return el;
238
+ };
239
+ if ('k-if' in props && !props['k-if']) {
240
+ el = document.createComment('k-if');
241
+ ref.value = el;
242
+ el.redraw = redraw;
243
+ return el;
248
244
  }
249
245
  // Handle function components
250
246
  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;
247
+ el = tag(props);
270
248
  }
271
249
  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;
250
+ el = h(tag, props, props.children);
293
251
  }
252
+ el.redraw ??= redraw;
253
+ ref.value = el;
254
+ return el;
294
255
  }
295
256
  /**
296
257
  * Fragment support - returns an array of children
@@ -343,20 +304,20 @@ const jsxs = jsx;
343
304
  * }
344
305
  * ```
345
306
  * Then the returned element has a `redraw` method to redraw itself with new values.
346
- * @param creator
307
+ * @param creator a simple creator function that returns an element
347
308
  * @returns created element
348
309
  */
349
310
  function createRedrawable(creator) {
350
- let element = creator();
311
+ let el = creator();
351
312
  const redraw = () => {
352
- const old = element;
353
- element = creator();
354
- old.replaceWith(element);
355
- element.redraw = redraw;
356
- return element;
313
+ const old = el;
314
+ el = creator();
315
+ old.replaceWith(el);
316
+ el.redraw = redraw;
317
+ return el;
357
318
  };
358
- element.redraw = redraw;
359
- return element;
319
+ el.redraw = redraw;
320
+ return el;
360
321
  }
361
322
 
362
323
  /**
@@ -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.0 (Last Update: 2026.01.24 00:25:38.464)
143
143
  * @license MIT
144
144
  * @link https://github.com/baendlorel/kt.js
145
145
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -182,7 +182,7 @@ declare const jsxs: typeof jsx;
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
188
  declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
@@ -53,12 +53,6 @@ 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
57
  const style = attr.style;
64
58
  if (classValue !== undefined) {
@@ -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
92
  if (typeof attr === 'string') {
106
93
  element.className = attr;
107
- return true;
108
94
  }
109
95
  else if (typeof attr === 'object' && attr !== null) {
110
- return attrIsObject(element, attr);
96
+ attrIsObject(element, 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.0 (Last Update: 2026.01.24 00:25:38.464)
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,43 @@ 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
+ const dummyRef = { value: null };
240
224
  /**
241
225
  * @param tag html tag or function component
242
226
  * @param props properties/attributes
243
227
  */
228
+ // todo 加入对k-if的全面支持
244
229
  function jsx(tag, props = {}) {
245
- const ref = props.ref?.isKT ? props.ref : null;
246
- if (ref) {
247
- delete props.ref;
230
+ const ref = props.ref?.isKT ? props.ref : dummyRef;
231
+ let el;
232
+ const redraw = (newProps) => {
233
+ props = newProps ? { ...props, ...newProps } : props;
234
+ const old = el;
235
+ el = jsx(tag, props);
236
+ old.replaceWith(el);
237
+ return el;
238
+ };
239
+ if ('k-if' in props && !props['k-if']) {
240
+ el = document.createComment('k-if');
241
+ ref.value = el;
242
+ el.redraw = redraw;
243
+ return el;
248
244
  }
249
245
  // Handle function components
250
246
  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;
247
+ el = tag(props);
270
248
  }
271
249
  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;
250
+ el = h(tag, props, props.children);
293
251
  }
252
+ el.redraw ??= redraw;
253
+ ref.value = el;
254
+ return el;
294
255
  }
295
256
  /**
296
257
  * Fragment support - returns an array of children
@@ -343,20 +304,20 @@ const jsxs = jsx;
343
304
  * }
344
305
  * ```
345
306
  * Then the returned element has a `redraw` method to redraw itself with new values.
346
- * @param creator
307
+ * @param creator a simple creator function that returns an element
347
308
  * @returns created element
348
309
  */
349
310
  function createRedrawable(creator) {
350
- let element = creator();
311
+ let el = creator();
351
312
  const redraw = () => {
352
- const old = element;
353
- element = creator();
354
- old.replaceWith(element);
355
- element.redraw = redraw;
356
- return element;
313
+ const old = el;
314
+ el = creator();
315
+ old.replaceWith(el);
316
+ el.redraw = redraw;
317
+ return el;
357
318
  };
358
- element.redraw = redraw;
359
- return element;
319
+ el.redraw = redraw;
320
+ return el;
360
321
  }
361
322
 
362
323
  /**
@@ -10,12 +10,12 @@ interface KTRef<T> {
10
10
  isKT: true;
11
11
  }
12
12
 
13
- type KTHTMLElement = HTMLElement & {
13
+ type KTHTMLElement<El extends HTMLElement = HTMLElement> = El & {
14
14
  /**
15
15
  * Automically generate a redraw function if it is not provided
16
16
  * @param props
17
17
  */
18
- redraw: (props?: KTAttribute, children?: KTRawContent) => KTHTMLElement;
18
+ redraw: (props?: KTAttribute, ...args: any[]) => KTHTMLElement;
19
19
  };
20
20
 
21
21
  declare global {
@@ -133,7 +133,7 @@ type H = (<T extends HTMLTag>(tag: T, attr?: KTRawAttr, content?: KTRawContent)
133
133
  * ## About
134
134
  * @package @ktjs/core
135
135
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
136
- * @version 0.14.6 (Last Update: 2026.01.17 11:35:33.824)
136
+ * @version 0.15.0 (Last Update: 2026.01.24 00:25:38.464)
137
137
  * @license MIT
138
138
  * @link https://github.com/baendlorel/kt.js
139
139
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -176,7 +176,7 @@ declare const jsxs: typeof jsx;
176
176
  * }
177
177
  * ```
178
178
  * Then the returned element has a `redraw` method to redraw itself with new values.
179
- * @param creator
179
+ * @param creator a simple creator function that returns an element
180
180
  * @returns created element
181
181
  */
182
182
  declare function createRedrawable(creator: () => KTHTMLElement): KTHTMLElement;
@@ -53,12 +53,6 @@ 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
57
  const style = attr.style;
64
58
  if (classValue !== undefined) {
@@ -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
92
  if (typeof attr === 'string') {
106
93
  element.className = attr;
107
- return true;
108
94
  }
109
95
  else if (typeof attr === 'object' && attr !== null) {
110
- return attrIsObject(element, attr);
96
+ attrIsObject(element, 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.0 (Last Update: 2026.01.24 00:25:38.464)
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,43 @@ 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
+ const dummyRef = { value: null };
240
224
  /**
241
225
  * @param tag html tag or function component
242
226
  * @param props properties/attributes
243
227
  */
228
+ // todo 加入对k-if的全面支持
244
229
  function jsx(tag, props = {}) {
245
- const ref = props.ref?.isKT ? props.ref : null;
246
- if (ref) {
247
- delete props.ref;
230
+ const ref = props.ref?.isKT ? props.ref : dummyRef;
231
+ let el;
232
+ const redraw = (newProps) => {
233
+ props = newProps ? { ...props, ...newProps } : props;
234
+ const old = el;
235
+ el = jsx(tag, props);
236
+ old.replaceWith(el);
237
+ return el;
238
+ };
239
+ if ('k-if' in props && !props['k-if']) {
240
+ el = document.createComment('k-if');
241
+ ref.value = el;
242
+ el.redraw = redraw;
243
+ return el;
248
244
  }
249
245
  // Handle function components
250
246
  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;
247
+ el = tag(props);
270
248
  }
271
249
  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;
250
+ el = h(tag, props, props.children);
293
251
  }
252
+ el.redraw ??= redraw;
253
+ ref.value = el;
254
+ return el;
294
255
  }
295
256
  /**
296
257
  * Fragment support - returns an array of children
@@ -343,20 +304,20 @@ const jsxs = jsx;
343
304
  * }
344
305
  * ```
345
306
  * Then the returned element has a `redraw` method to redraw itself with new values.
346
- * @param creator
307
+ * @param creator a simple creator function that returns an element
347
308
  * @returns created element
348
309
  */
349
310
  function createRedrawable(creator) {
350
- let element = creator();
311
+ let el = creator();
351
312
  const redraw = () => {
352
- const old = element;
353
- element = creator();
354
- old.replaceWith(element);
355
- element.redraw = redraw;
356
- return element;
313
+ const old = el;
314
+ el = creator();
315
+ old.replaceWith(el);
316
+ el.redraw = redraw;
317
+ return el;
357
318
  };
358
- element.redraw = redraw;
359
- return element;
319
+ el.redraw = redraw;
320
+ return el;
360
321
  }
361
322
 
362
323
  export { Fragment, h as createElement, createRedrawable, h, jsx, jsxDEV, jsxs };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/core",
3
- "version": "0.14.6",
3
+ "version": "0.15.0",
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",