vaderjs 2.0.0 → 2.0.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/bundler/index.js CHANGED
@@ -132,14 +132,13 @@ const handleReplacements = (code) => {
132
132
  line = updatedLine;
133
133
  }
134
134
 
135
- if (!hasImport && line.match(/\buseRef\d*\(/) && !line.includes("this")) {
136
-
137
-
138
- let key = line.split(' ')[1].split('=')[0];
139
-
140
- let updatedLine = line.replace(/\buseRef\d*\(/, `this.useRef('${key}',`);
141
-
142
- line = updatedLine;
135
+ if (!hasImport && line.match(/\buseRef\d*\(/) && line.includes('[') && !line.includes("this")) {
136
+ line = line.replace(' ', '')
137
+ let b4 = line
138
+ let key = line.split('=')[0].split(' ').filter(Boolean)[1]
139
+ console.log(key)
140
+ b4 = line.replace('useRef(', `this.useRef('${key}',`)
141
+ line = b4
143
142
  }
144
143
 
145
144
 
package/index.ts CHANGED
@@ -3,8 +3,6 @@ let isClassComponent = function (element) {
3
3
  return element.toString().startsWith("class");
4
4
  };
5
5
 
6
-
7
-
8
6
 
9
7
 
10
8
  const memoizes = new Map();
@@ -61,6 +59,19 @@ export const useFetch = (url: string, options: any) => {
61
59
  return [null, true, null];
62
60
  };
63
61
 
62
+ /**
63
+ * @description - useRef allows you to store a reference to a DOM element
64
+ * @param value
65
+ * @returns {current: HTMLElement}
66
+ * @example
67
+ * const inputRef = useRef();
68
+ * <input ref={inputRef} />
69
+ * console.log(inputRef.current) // <input />
70
+ */
71
+ export const useRef = (value) => {
72
+ return { key: crypto.randomUUID(), current: value };
73
+ }
74
+
64
75
  /**
65
76
  * @description - Handle asyncronous promises and return the data or error;
66
77
  * @param promise
@@ -69,7 +80,7 @@ export const useFetch = (url: string, options: any) => {
69
80
  export const useAsyncState = (promise: Promise<any>) => {
70
81
  return [null, () => { }];
71
82
  }
72
- export const useEffect = (callback: any, dependencies: any[] = []) => {
83
+ export const useEffect = (callback: any, dependencies: any[] = []) => {
73
84
  dependencies = dependencies.map((dep) => JSON.stringify(dep));
74
85
  if (dependencies.length === 0) {
75
86
  callback();
@@ -158,8 +169,8 @@ export const e = (element, props, ...children) => {
158
169
  instance.children = children;
159
170
  if (!firstEl)
160
171
  firstEl = { type: "div", props: { key: instance.key, ...props }, children };
161
- firstEl.props = { key: instance.key, ...firstEl.props, ...props };
162
- firstEl.props["idKey"] = instance.key;
172
+ firstEl.props = { key: instance.key, ...firstEl.props, ...props };
173
+ firstEl.props["idKey"] = instance.props?.ref?.key || instance.key;
163
174
  instance.props = firstEl.props;
164
175
  return firstEl;
165
176
  default:
@@ -168,7 +179,7 @@ export const e = (element, props, ...children) => {
168
179
  }
169
180
  let el = { type: element, props: props || {}, children: children || [] };
170
181
  if (el.type !== "head") {
171
- el.props = { idKey: crypto.randomUUID(), ...el.props };
182
+ el.props = { idKey: el.props?.ref?.key || crypto.randomUUID(), ...el.props };
172
183
  }
173
184
 
174
185
  // if element == false return empty string
@@ -291,10 +302,10 @@ export class Component {
291
302
  }
292
303
  useRef = (key, value) => {
293
304
  if (!this.refs.find((r) => r.key == key)) {
294
- this.refs.push({ key, value });
305
+ this.refs.push({ key, current: value});
295
306
  }
296
307
 
297
- return this.refs.find((r) => r.key == key).value;
308
+ return { key, current: this.refs.find((r) => r.key == key).current };
298
309
  }
299
310
  useEffect(callback, dependencies = []) {
300
311
  const callbackId = callback.toString(); // Unique ID based on callback string representation
@@ -619,11 +630,18 @@ export class Component {
619
630
 
620
631
  // Set attributes
621
632
  let attributes = element.props || {};
622
- for (let key in attributes) {
623
- if(typeof attributes[key] === "object" && key !== "style"){
624
- continue;
625
- }
626
- if (key === "key") {
633
+ for (let key in attributes) {
634
+ if(key === "ref") {
635
+ let _key = attributes[key].key;
636
+ // update the ref
637
+ let ref = this.refs.find((r) => r.key == _key);
638
+ if(ref) {
639
+ ref.current = document.querySelector(`[idKey="${_key}"]`) || el;
640
+ }
641
+ el.setAttribute("idKey", _key);
642
+ element.props.idKey = _key
643
+ }
644
+ else if (key === "key") {
627
645
  el.key = attributes[key];
628
646
  } else if (key === "className") {
629
647
  el.setAttribute("class", attributes[key]);
@@ -647,7 +665,9 @@ export class Component {
647
665
  } catch (error) {
648
666
 
649
667
  }
650
- }
668
+ } else if(typeof attributes[key] === "object" && key !== "style"){
669
+ continue;
670
+ }
651
671
  }
652
672
 
653
673
  // Handle children
package/main.js CHANGED
@@ -167,8 +167,9 @@ const handleReplacements = (code) => {
167
167
  line = b4
168
168
  }
169
169
  if (!hasImport && line.includes('useRef')) {
170
+ line = line.replace(' ', '')
170
171
  let b4 = line
171
- let key = line.split(' ')[1].split('=')[0]
172
+ let key = line.split('=')[0].split(' ').filter(Boolean)[1]
172
173
  b4 = line.replace('useRef(', `this.useRef('${key}',`)
173
174
  line = b4
174
175
  }
@@ -186,8 +187,7 @@ if (!fs.existsSync(process.cwd() + '/dev/bundler.js')) {
186
187
  let start = Date.now()
187
188
  async function generateApp() {
188
189
  globalThis.isBuilding = true;
189
- console.log(ansiColors.green('Building...'))
190
- console.log(`Starting build at ${new Date().toLocaleTimeString()}`)
190
+ console.log(ansiColors.green('Building...'))
191
191
  let plugins = config.plugins || []
192
192
  for (let plugin of plugins) {
193
193
  if (plugin.onBuildStart) {
@@ -275,8 +275,7 @@ async function generateApp() {
275
275
  },
276
276
  onExit({ exitCode: code }) {
277
277
  if (code === 0) {
278
- bindes = []
279
- console.log(`Built ${r} in ${Date.now() - start}ms`)
278
+ bindes = []
280
279
  resolve()
281
280
  } else {
282
281
  reject()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A simple and powerful JavaScript library for building modern web applications.",
5
5
  "bin": {
6
6
  "vaderjs": "./main.js"