vaderjs 2.3.6 → 2.3.7

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.
Files changed (2) hide show
  1. package/index.ts +42 -7
  2. package/package.json +3 -3
package/index.ts CHANGED
@@ -104,9 +104,16 @@ function createDom(fiber: Fiber): Node {
104
104
  }
105
105
 
106
106
  updateDom(dom, {}, fiber.props);
107
+
108
+ // Assign ref if this fiber has a ref prop
109
+ if (fiber.props && fiber.props.ref) {
110
+ fiber.props.ref.current = dom;
111
+ }
112
+
107
113
  return dom;
108
114
  }
109
115
 
116
+
110
117
  function isSvgElement(fiber: Fiber): boolean {
111
118
  // Check if the fiber is an <svg> itself or inside an <svg>
112
119
  let parent = fiber.parent;
@@ -126,11 +133,19 @@ function isSvgElement(fiber: Fiber): boolean {
126
133
  * @param {object} nextProps - The new properties.
127
134
  */
128
135
  function updateDom(dom: Node, prevProps: any, nextProps: any): void {
129
- prevProps = prevProps || {};
136
+ prevProps = prevProps || {};
130
137
  nextProps = nextProps || {};
131
138
 
132
139
  const isSvg = dom instanceof SVGElement;
133
140
 
141
+ // Handle ref updates
142
+ if (prevProps.ref && prevProps.ref !== nextProps.ref) {
143
+ prevProps.ref.current = null;
144
+ }
145
+ if (nextProps.ref && nextProps.ref !== prevProps.ref) {
146
+ nextProps.ref.current = dom;
147
+ }
148
+
134
149
  // Remove old or changed event listeners
135
150
  Object.keys(prevProps)
136
151
  .filter(isEvent)
@@ -231,18 +246,31 @@ function commitRoot(): void {
231
246
  * @param {Fiber} fiber - The fiber to commit.
232
247
  */
233
248
  function commitWork(fiber: Fiber | null): void {
234
- if (!fiber) {
235
- return;
236
- }
249
+ if (!fiber) return;
237
250
 
238
251
  let domParentFiber = fiber.parent;
239
252
  while (domParentFiber && !domParentFiber.dom) {
240
253
  domParentFiber = domParentFiber.parent;
241
254
  }
242
- const domParent = domParentFiber ? domParentFiber.dom : null;
255
+ const domParent = domParentFiber?.dom ?? null;
243
256
 
244
257
  if (fiber.effectTag === "PLACEMENT" && fiber.dom != null) {
245
258
  if (domParent) domParent.appendChild(fiber.dom);
259
+
260
+ // ⚡ Assign refs immediately when DOM is placed
261
+ if (fiber.props && fiber.props.ref) {
262
+ fiber.props.ref.current = fiber.dom;
263
+ }
264
+
265
+ // Also check for useRef hooks in this fiber
266
+ if (fiber.hooks) {
267
+ for (const hook of fiber.hooks) {
268
+ if ("current" in hook && !hook._isRef && fiber.dom) {
269
+ // This is likely a DOM ref hook
270
+ hook.current = fiber.dom;
271
+ }
272
+ }
273
+ }
246
274
  } else if (fiber.effectTag === "UPDATE" && fiber.dom != null) {
247
275
  updateDom(fiber.dom, fiber.alternate?.props ?? {}, fiber.props);
248
276
  } else if (fiber.effectTag === "DELETION") {
@@ -253,14 +281,21 @@ function commitWork(fiber: Fiber | null): void {
253
281
  commitWork(fiber.sibling);
254
282
  }
255
283
 
284
+
256
285
  /**
257
286
  * Recursively removes a fiber and its children from the DOM.
258
287
  * @param {Fiber} fiber - The fiber to remove.
259
288
  */
260
- function commitDeletion(fiber: Fiber | null): void {
289
+ function commitDeletion(fiber: Fiber | null): void {
261
290
  if (!fiber) {
262
291
  return;
263
292
  }
293
+
294
+ // Clear refs when element is removed
295
+ if (fiber.props && fiber.props.ref) {
296
+ fiber.props.ref.current = null;
297
+ }
298
+
264
299
  if (fiber.dom) {
265
300
  if (fiber.dom.parentNode) {
266
301
  fiber.dom.parentNode.removeChild(fiber.dom);
@@ -641,7 +676,7 @@ export function useRef<T>(initial: T): { current: T } {
641
676
 
642
677
  let hook = wipFiber.hooks[hookIndex];
643
678
  if (!hook) {
644
- hook = { current: initial };
679
+ hook = { current: initial, _isRef: true };
645
680
  wipFiber.hooks[hookIndex] = hook;
646
681
  }
647
682
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "2.3.6",
4
- "description": "A simple and powerful JavaScript library for building modern web applications.",
3
+ "version": "2.3.7",
4
+ "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "bin": {
6
6
  "vaderjs": "./main.js"
7
7
  },
@@ -9,7 +9,7 @@
9
9
  "type": "git",
10
10
  "url": "https://github.com/Postr-Inc/Vader.js"
11
11
  },
12
- "license":"MIT",
12
+ "license": "MIT",
13
13
  "dependencies": {
14
14
  "ansi-colors": "latest"
15
15
  }