kt.js 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.
@@ -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": "kt.js",
3
- "version": "0.14.6",
3
+ "version": "0.15.0",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"
@@ -41,7 +41,7 @@
41
41
  ],
42
42
  "license": "MIT",
43
43
  "dependencies": {
44
- "@ktjs/core": "0.14.6",
44
+ "@ktjs/core": "0.15.0",
45
45
  "@ktjs/shortcuts": "0.7.3"
46
46
  },
47
47
  "scripts": {