vaderjs 1.9.9 → 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
@@ -59,6 +59,19 @@ export const useFetch = (url: string, options: any) => {
59
59
  return [null, true, null];
60
60
  };
61
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
+
62
75
  /**
63
76
  * @description - Handle asyncronous promises and return the data or error;
64
77
  * @param promise
@@ -67,7 +80,7 @@ export const useFetch = (url: string, options: any) => {
67
80
  export const useAsyncState = (promise: Promise<any>) => {
68
81
  return [null, () => { }];
69
82
  }
70
- export const useEffect = (callback: any, dependencies: any[] = []) => {
83
+ export const useEffect = (callback: any, dependencies: any[] = []) => {
71
84
  dependencies = dependencies.map((dep) => JSON.stringify(dep));
72
85
  if (dependencies.length === 0) {
73
86
  callback();
@@ -96,8 +109,11 @@ export const A = (props: {
96
109
  if (props.openInNewTab) {
97
110
  window.open(props.href, "_blank");
98
111
  return void 0;
99
- }
100
- window.location.href = props.href;
112
+ }
113
+ window.history.pushState({}, "", props.href);
114
+ window.dispatchEvent(new Event("popstate"));
115
+ window.dispatchEvent(new Event("load"));
116
+ window.location.reload();
101
117
  return void 0;
102
118
  }
103
119
  return e("a", { ...props, onClick: handleClick }, props.children);
@@ -153,8 +169,8 @@ export const e = (element, props, ...children) => {
153
169
  instance.children = children;
154
170
  if (!firstEl)
155
171
  firstEl = { type: "div", props: { key: instance.key, ...props }, children };
156
- firstEl.props = { key: instance.key, ...firstEl.props, ...props };
157
- firstEl.props["idKey"] = instance.key;
172
+ firstEl.props = { key: instance.key, ...firstEl.props, ...props };
173
+ firstEl.props["idKey"] = instance.props?.ref?.key || instance.key;
158
174
  instance.props = firstEl.props;
159
175
  return firstEl;
160
176
  default:
@@ -163,7 +179,7 @@ export const e = (element, props, ...children) => {
163
179
  }
164
180
  let el = { type: element, props: props || {}, children: children || [] };
165
181
  if (el.type !== "head") {
166
- el.props = { idKey: crypto.randomUUID(), ...el.props };
182
+ el.props = { idKey: el.props?.ref?.key || crypto.randomUUID(), ...el.props };
167
183
  }
168
184
 
169
185
  // if element == false return empty string
@@ -192,7 +208,9 @@ interface SwitchProps {
192
208
  export function Switch({ children = [] }: SwitchProps) {
193
209
  for (let child of children) {
194
210
  if (child.props.when) {
195
- return child
211
+ return { type: "div", props: {
212
+ idKey: crypto.randomUUID()
213
+ }, children: [child] };
196
214
  }
197
215
  }
198
216
  return { type: "div", props: {}, children: [] };
@@ -284,10 +302,10 @@ export class Component {
284
302
  }
285
303
  useRef = (key, value) => {
286
304
  if (!this.refs.find((r) => r.key == key)) {
287
- this.refs.push({ key, value });
305
+ this.refs.push({ key, current: value});
288
306
  }
289
307
 
290
- return this.refs.find((r) => r.key == key).value;
308
+ return { key, current: this.refs.find((r) => r.key == key).current };
291
309
  }
292
310
  useEffect(callback, dependencies = []) {
293
311
  const callbackId = callback.toString(); // Unique ID based on callback string representation
@@ -612,11 +630,18 @@ export class Component {
612
630
 
613
631
  // Set attributes
614
632
  let attributes = element.props || {};
615
- for (let key in attributes) {
616
- if(typeof attributes[key] === "object" && key !== "style"){
617
- continue;
618
- }
619
- 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") {
620
645
  el.key = attributes[key];
621
646
  } else if (key === "className") {
622
647
  el.setAttribute("class", attributes[key]);
@@ -640,7 +665,9 @@ export class Component {
640
665
  } catch (error) {
641
666
 
642
667
  }
643
- }
668
+ } else if(typeof attributes[key] === "object" && key !== "style"){
669
+ continue;
670
+ }
644
671
  }
645
672
 
646
673
  // Handle children
@@ -675,7 +702,7 @@ export class Component {
675
702
  }
676
703
  toElement() {
677
704
  let children = this.render(this.props);
678
- let el = this.parseToElement(children);
705
+ let el = this.parseToElement(children);
679
706
  el.setAttribute("idKey", this.key);
680
707
  return el;
681
708
  }
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()
@@ -552,13 +551,23 @@ if (mode == 'development' || mode == 'serve') {
552
551
  if (mode == "development") {
553
552
  return new Response(data + `
554
553
  <script>
555
- let ws = new WebSocket('ws://localhost:${server.port}')
554
+ let ws = new WebSocket(\`\${location.protocol === 'https:' ? 'wss' : 'ws'}://\${location.host}\`)
556
555
  ws.onmessage = (e) => {
557
556
  if(e.data === 'reload'){
558
557
  console.log('Reloading to display changes from server')
559
558
  window.location.reload()
560
559
  }
561
560
  }
561
+ ws.onopen = () => {
562
+ console.log('Connected to hmr server')
563
+ }
564
+
565
+ ws.onclose = () => {
566
+ // try to reconnect
567
+ console.log('Reconnecting to hmr server')
568
+ ws = new WebSocket(\`\${location.protocol === 'https:' ? 'wss' : 'ws'}://\${location.host}\`)
569
+ }
570
+
562
571
  </script>
563
572
  `, {
564
573
  headers: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vaderjs",
3
- "version": "1.9.9",
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"