@ktjs/core 0.20.3 → 0.21.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.
@@ -9,6 +9,7 @@ const $throw = (message) => {
9
9
 
10
10
  // DOM manipulation utilities
11
11
  // # dom natives
12
+ const $replaceWith = Element.prototype.replaceWith;
12
13
  /**
13
14
  * & Remove `bind` because it is shockingly slower than wrapper
14
15
  * & `window.document` is safe because it is not configurable and its setter is undefined
@@ -48,7 +49,7 @@ const { get: $buttonDisabledGetter, set: $buttonDisabledSetter } = Object.getOwn
48
49
 
49
50
  // Shared utilities and cached native methods for kt.js framework
50
51
  // Re-export all utilities
51
- Object.defineProperty(window, '@ktjs/shared', { value: '0.20.0' });
52
+ Object.defineProperty(window, '@ktjs/shared', { value: '0.20.2' });
52
53
 
53
54
  const booleanHandler = (element, key, value) => {
54
55
  if (key in element) {
@@ -191,7 +192,6 @@ function applyContent(element, content) {
191
192
  }
192
193
  }
193
194
 
194
- document.createElement('div');
195
195
  const htmlCreator = (tag) => document.createElement(tag);
196
196
  const svgCreator = (tag) => document.createElementNS('http://www.w3.org/2000/svg', tag);
197
197
  const mathMLCreator = (tag) => document.createElementNS('http://www.w3.org/1998/Math/MathML', tag);
@@ -209,7 +209,7 @@ const MATHML_ATTR_FLAG = '__kt_mathml__';
209
209
  * ## About
210
210
  * @package @ktjs/core
211
211
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
212
- * @version 0.20.3 (Last Update: 2026.02.01 18:38:02.198)
212
+ * @version 0.21.1 (Last Update: 2026.02.02 09:20:07.959)
213
213
  * @license MIT
214
214
  * @link https://github.com/baendlorel/kt.js
215
215
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -238,14 +238,6 @@ const h = (tag, attr, content) => {
238
238
  // * Handle content
239
239
  applyAttr(element, attr);
240
240
  applyContent(element, content);
241
- // if (tag === 'svg') {
242
- // tempWrapper.innerHTML = element.outerHTML;
243
- // return tempWrapper.firstChild as HTML<T>;
244
- // }
245
- // if (tag === 'math') {
246
- // tempWrapper.innerHTML = element.outerHTML;
247
- // return tempWrapper.firstChild as HTML<T>;
248
- // }
249
241
  return element;
250
242
  };
251
243
 
@@ -302,8 +294,12 @@ class KTRef {
302
294
  return false;
303
295
  }
304
296
  }
297
+ const isKTRef = (obj) => {
298
+ return typeof obj === 'object' && obj !== null && obj.isKT === true;
299
+ };
305
300
  /**
306
301
  * Reference to the created HTML element.
302
+ * - **Only** respond to `ref.value` changes, not reactive to internal changes of the element.
307
303
  * - can alse be used to store normal values, but it is not reactive.
308
304
  * @param value mostly an HTMLElement
309
305
  */
@@ -312,27 +308,47 @@ function ref(value, onChange) {
312
308
  }
313
309
 
314
310
  const dummyRef = { value: null };
311
+ const create = (tag, props) => {
312
+ if (typeof tag === 'function') {
313
+ return tag(props);
314
+ }
315
+ else {
316
+ return h(tag, props, props.children);
317
+ }
318
+ };
319
+ const placeholder = () => document.createComment('k-if');
315
320
  /**
316
321
  * @param tag html tag or function component
317
322
  * @param props properties/attributes
318
323
  */
319
324
  function jsx(tag, props) {
320
- const ref = props.ref?.isKT ? props.ref : dummyRef;
325
+ const maybeDummyRef = isKTRef(props.ref) ? props.ref : dummyRef;
321
326
  let el;
322
- if ('k-if' in props && !props['k-if']) {
323
- // & make comment placeholder in case that ref might be redrawn later
324
- el = document.createComment('k-if');
325
- ref.value = el;
326
- return el;
327
- }
328
- // Handle function components
329
- if (typeof tag === 'function') {
330
- el = tag(props);
331
- }
332
- else {
333
- el = h(tag, props, props.children);
327
+ if ('k-if' in props) {
328
+ const kif = props['k-if'];
329
+ let condition = kif; // assume boolean by default
330
+ // Handle reactive k-if
331
+ if (isKTRef(kif)) {
332
+ kif.addOnChange((newValue, oldValue) => {
333
+ if (newValue === oldValue) {
334
+ return;
335
+ }
336
+ const oldEl = el;
337
+ el = newValue ? create(tag, props) : placeholder();
338
+ $replaceWith.call(oldEl, el);
339
+ maybeDummyRef.value = el;
340
+ });
341
+ condition = kif.value;
342
+ }
343
+ if (!condition) {
344
+ // & make comment placeholder in case that ref might be redrawn later
345
+ el = placeholder();
346
+ maybeDummyRef.value = el;
347
+ return el;
348
+ }
334
349
  }
335
- ref.value = el;
350
+ el = create(tag, props);
351
+ maybeDummyRef.value = el;
336
352
  return el;
337
353
  }
338
354
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/core",
3
- "version": "0.20.3",
3
+ "version": "0.21.1",
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",
@@ -44,7 +44,7 @@
44
44
  "directory": "packages/core"
45
45
  },
46
46
  "dependencies": {
47
- "@ktjs/shared": "0.20.0"
47
+ "@ktjs/shared": "0.20.2"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "rollup -c rollup.config.mjs",