kt.js 0.17.4 → 0.18.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/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.16.0
15
+ **Current Version:** 0.18.1
16
16
 
17
17
  ## Features
18
18
 
@@ -42,6 +42,10 @@ Core DOM manipulation utilities for KT.js framework with built-in JSX/TSX suppor
42
42
  - Update props and children selectively
43
43
  - Efficient replacement strategy
44
44
  - Works with both native elements and function components
45
+ - **Ref Enhancement**: Change event binding support for `ref` (v0.18.1)
46
+ - New methods `addOnChange` and `removeOnChange` for listening to value changes
47
+ - Automatically calls registered callbacks when `ref.value` is updated
48
+ - When both old and new values are DOM nodes, automatically replaces old node with new one in the DOM
45
49
  - **DOM Utilities**: Helper functions for common DOM operations
46
50
  - Native method caching for performance
47
51
  - Symbol-based private properties for internal state
@@ -212,7 +212,7 @@ const svgTempWrapper = document.createElement('div');
212
212
  * ## About
213
213
  * @package @ktjs/core
214
214
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
215
- * @version 0.17.4 (Last Update: 2026.01.29 13:40:17.223)
215
+ * @version 0.18.2 (Last Update: 2026.01.30 22:52:42.408)
216
216
  * @license MIT
217
217
  * @link https://github.com/baendlorel/kt.js
218
218
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -241,7 +241,41 @@ const h = (tag, attr, content) => {
241
241
  * @param value mostly an HTMLElement
242
242
  */
243
243
  function ref(value) {
244
- return { value: value, isKT: true };
244
+ let _value = value;
245
+ let _onChanges = [];
246
+ return {
247
+ isKT: true,
248
+ get value() {
249
+ return _value;
250
+ },
251
+ set value(newValue) {
252
+ if (newValue === _value) {
253
+ return;
254
+ }
255
+ // replace the old node with the new one in the DOM if both are nodes
256
+ if (_value instanceof Node && newValue instanceof Node) {
257
+ if (newValue.contains(_value)) {
258
+ _value.remove();
259
+ }
260
+ _value.replaceWith(newValue);
261
+ }
262
+ const oldValue = _value;
263
+ _value = newValue;
264
+ for (let i = 0; i < _onChanges.length; i++) {
265
+ _onChanges[i](newValue, oldValue);
266
+ }
267
+ },
268
+ addOnChange: (callback) => _onChanges.push(callback),
269
+ removeOnChange: (callback) => {
270
+ for (let i = _onChanges.length - 1; i >= 0; i--) {
271
+ if (_onChanges[i] === callback) {
272
+ _onChanges.splice(i, 1);
273
+ return true;
274
+ }
275
+ }
276
+ return false;
277
+ },
278
+ };
245
279
  }
246
280
 
247
281
  const dummyRef = { value: null };
@@ -254,9 +288,10 @@ function jsx(tag, props = {}) {
254
288
  let el;
255
289
  const redraw = (newProps) => {
256
290
  props = newProps ? { ...props, ...newProps } : props;
257
- const old = el;
258
291
  el = jsx(tag, props);
259
- old.replaceWith(el);
292
+ if (ref !== dummyRef) {
293
+ ref.value = el; // ref setter automatically replaces old element in DOM
294
+ }
260
295
  return el;
261
296
  };
262
297
  if ('k-if' in props && !props['k-if']) {
@@ -280,7 +315,7 @@ function jsx(tag, props = {}) {
280
315
  * Fragment support - returns an array of children
281
316
  * Note: kt.js doesn't have a real Fragment concept,
282
317
  */
283
- function Fragment(props) {
318
+ function Fragment(_props) {
284
319
  throw new Error("kt.js doesn't have a Fragment concept");
285
320
  // const { children } = props ?? {};
286
321
  // if (!children) {
@@ -360,9 +395,7 @@ function createRedrawable(creator) {
360
395
  const elRef = ref();
361
396
  elRef.value = creator();
362
397
  const redraw = () => {
363
- const old = elRef.value;
364
- elRef.value = creator();
365
- old.replaceWith(elRef.value);
398
+ elRef.value = creator(); // ref setter automatically calls replaceWith
366
399
  elRef.value.redraw = redraw;
367
400
  return elRef.value;
368
401
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kt.js",
3
- "version": "0.17.4",
3
+ "version": "0.18.2",
4
4
  "author": {
5
5
  "name": "Kasukabe Tsumugi",
6
6
  "email": "futami16237@gmail.com"
@@ -41,8 +41,8 @@
41
41
  ],
42
42
  "license": "MIT",
43
43
  "dependencies": {
44
- "@ktjs/shortcuts": "0.7.3",
45
- "@ktjs/core": "0.17.4"
44
+ "@ktjs/core": "0.18.2",
45
+ "@ktjs/shortcuts": "0.7.3"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rollup -c rollup.config.mjs",