@ktjs/core 0.17.4 → 0.18.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.
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
package/dist/index.d.ts CHANGED
@@ -6,9 +6,15 @@ type otherstring = string & {};
6
6
  type HTMLTag = keyof HTMLElementTagNameMap;
7
7
  type SVGTag = keyof SVGElementTagNameMap;
8
8
 
9
+ type ChangeHandler<T> = (newValue: T, oldValue: T) => void;
9
10
  interface KTRef<T> {
11
+ /**
12
+ * If new value and old value are both nodes, the old one will be replaced in the DOM
13
+ */
10
14
  value: T;
11
15
  isKT: true;
16
+ addOnChange: (callback: ChangeHandler<T>) => void;
17
+ removeOnChange: (callback: ChangeHandler<T>) => void;
12
18
  }
13
19
  /**
14
20
  * Reference to the created HTML element.
@@ -172,7 +178,7 @@ type HTML<T extends (HTMLTag | SVGTag) & otherstring> = T extends SVGTag ? SVGEl
172
178
  * ## About
173
179
  * @package @ktjs/core
174
180
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
175
- * @version 0.17.4 (Last Update: 2026.01.29 13:40:17.223)
181
+ * @version 0.18.1 (Last Update: 2026.01.30 21:24:36.481)
176
182
  * @license MIT
177
183
  * @link https://github.com/baendlorel/kt.js
178
184
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -215,7 +215,7 @@ var __ktjs_core__ = (function (exports) {
215
215
  * ## About
216
216
  * @package @ktjs/core
217
217
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
218
- * @version 0.17.4 (Last Update: 2026.01.29 13:40:17.223)
218
+ * @version 0.18.1 (Last Update: 2026.01.30 21:24:36.481)
219
219
  * @license MIT
220
220
  * @link https://github.com/baendlorel/kt.js
221
221
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -244,7 +244,41 @@ var __ktjs_core__ = (function (exports) {
244
244
  * @param value mostly an HTMLElement
245
245
  */
246
246
  function ref(value) {
247
- return { value: value, isKT: true };
247
+ let _value = value;
248
+ let _onChanges = [];
249
+ return {
250
+ isKT: true,
251
+ get value() {
252
+ return _value;
253
+ },
254
+ set value(newValue) {
255
+ if (newValue === _value) {
256
+ return;
257
+ }
258
+ // replace the old node with the new one in the DOM if both are nodes
259
+ if (_value instanceof Node && newValue instanceof Node) {
260
+ if (newValue.contains(_value)) {
261
+ _value.remove();
262
+ }
263
+ _value.replaceWith(newValue);
264
+ }
265
+ const oldValue = _value;
266
+ _value = newValue;
267
+ for (let i = 0; i < _onChanges.length; i++) {
268
+ _onChanges[i](newValue, oldValue);
269
+ }
270
+ },
271
+ addOnChange: (callback) => _onChanges.push(callback),
272
+ removeOnChange: (callback) => {
273
+ for (let i = _onChanges.length - 1; i >= 0; i--) {
274
+ if (_onChanges[i] === callback) {
275
+ _onChanges.splice(i, 1);
276
+ return true;
277
+ }
278
+ }
279
+ return false;
280
+ },
281
+ };
248
282
  }
249
283
 
250
284
  const dummyRef = { value: null };
@@ -240,7 +240,7 @@ var __ktjs_core__ = (function (exports) {
240
240
  * ## About
241
241
  * @package @ktjs/core
242
242
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
243
- * @version 0.17.4 (Last Update: 2026.01.29 13:40:17.223)
243
+ * @version 0.18.1 (Last Update: 2026.01.30 21:24:36.481)
244
244
  * @license MIT
245
245
  * @link https://github.com/baendlorel/kt.js
246
246
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -302,7 +302,41 @@ var __ktjs_core__ = (function (exports) {
302
302
  * @param value mostly an HTMLElement
303
303
  */
304
304
  function ref(value) {
305
- return { value: value, isKT: true };
305
+ var _value = value;
306
+ var _onChanges = [];
307
+ return {
308
+ isKT: true,
309
+ get value() {
310
+ return _value;
311
+ },
312
+ set value(newValue) {
313
+ if (newValue === _value) {
314
+ return;
315
+ }
316
+ // replace the old node with the new one in the DOM if both are nodes
317
+ if (_value instanceof Node && newValue instanceof Node) {
318
+ if (newValue.contains(_value)) {
319
+ _value.remove();
320
+ }
321
+ _value.replaceWith(newValue);
322
+ }
323
+ var oldValue = _value;
324
+ _value = newValue;
325
+ for (var i = 0; i < _onChanges.length; i++) {
326
+ _onChanges[i](newValue, oldValue);
327
+ }
328
+ },
329
+ addOnChange: function (callback) { return _onChanges.push(callback); },
330
+ removeOnChange: function (callback) {
331
+ for (var i = _onChanges.length - 1; i >= 0; i--) {
332
+ if (_onChanges[i] === callback) {
333
+ _onChanges.splice(i, 1);
334
+ return true;
335
+ }
336
+ }
337
+ return false;
338
+ },
339
+ };
306
340
  }
307
341
 
308
342
  var dummyRef = { value: null };
package/dist/index.mjs CHANGED
@@ -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.1 (Last Update: 2026.01.30 21:24:36.481)
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 };
@@ -6,9 +6,15 @@ type otherstring = string & {};
6
6
  type HTMLTag = keyof HTMLElementTagNameMap;
7
7
  type SVGTag = keyof SVGElementTagNameMap;
8
8
 
9
+ type ChangeHandler<T> = (newValue: T, oldValue: T) => void;
9
10
  interface KTRef<T> {
11
+ /**
12
+ * If new value and old value are both nodes, the old one will be replaced in the DOM
13
+ */
10
14
  value: T;
11
15
  isKT: true;
16
+ addOnChange: (callback: ChangeHandler<T>) => void;
17
+ removeOnChange: (callback: ChangeHandler<T>) => void;
12
18
  }
13
19
  /**
14
20
  * Reference to the created HTML element.
@@ -158,7 +164,7 @@ type HTML<T extends (HTMLTag | SVGTag) & otherstring> = T extends SVGTag ? SVGEl
158
164
  * ## About
159
165
  * @package @ktjs/core
160
166
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
161
- * @version 0.17.4 (Last Update: 2026.01.29 13:40:17.223)
167
+ * @version 0.18.1 (Last Update: 2026.01.30 21:24:36.481)
162
168
  * @license MIT
163
169
  * @link https://github.com/baendlorel/kt.js
164
170
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -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.1 (Last Update: 2026.01.30 21:24:36.481)
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 };
@@ -6,9 +6,15 @@ type otherstring = string & {};
6
6
  type HTMLTag = keyof HTMLElementTagNameMap;
7
7
  type SVGTag = keyof SVGElementTagNameMap;
8
8
 
9
+ type ChangeHandler<T> = (newValue: T, oldValue: T) => void;
9
10
  interface KTRef<T> {
11
+ /**
12
+ * If new value and old value are both nodes, the old one will be replaced in the DOM
13
+ */
10
14
  value: T;
11
15
  isKT: true;
16
+ addOnChange: (callback: ChangeHandler<T>) => void;
17
+ removeOnChange: (callback: ChangeHandler<T>) => void;
12
18
  }
13
19
 
14
20
  type Redraw = (props?: KTAttribute, ...args: any[]) => KTHTMLElement;
@@ -152,7 +158,7 @@ type HTML<T extends (HTMLTag | SVGTag) & otherstring> = T extends SVGTag ? SVGEl
152
158
  * ## About
153
159
  * @package @ktjs/core
154
160
  * @author Kasukabe Tsumugi <futami16237@gmail.com>
155
- * @version 0.17.4 (Last Update: 2026.01.29 13:40:17.223)
161
+ * @version 0.18.1 (Last Update: 2026.01.30 21:24:36.481)
156
162
  * @license MIT
157
163
  * @link https://github.com/baendlorel/kt.js
158
164
  * @link https://baendlorel.github.io/ Welcome to my site!
@@ -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.1 (Last Update: 2026.01.30 21:24:36.481)
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ktjs/core",
3
- "version": "0.17.4",
3
+ "version": "0.18.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",