vaderjs 1.3.3-alpha-20 → 1.3.3-alpha-21

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
@@ -1,174 +1,176 @@
1
- <p align="center">
2
- <a href="https://vader-js.pages.dev">
3
- <picture>
4
- <source media="(prefers-color-scheme: dark)" srcset="/icon.jpeg">
5
- <img src="./logo.png" height="128">
6
- </picture>
7
- <h1 align="center">Vader.js</h1>
8
- </a>
9
- </p>
10
-
11
- # VaderJS: A Powerful Reactive Framework for SPAs inspired by react.js!
12
-
13
- [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Postr-Inc/Vader.js/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/vaderjs.svg?style=flat)](https://www.npmjs.com/package/vaderjs)
14
-
15
-
16
-
17
- ## Get Started
18
-
19
- 2. Install vaderjs
20
-
21
- ```bash
22
- npm i vaderjs@latest
23
- ```
24
-
25
- 3. Install five server - recommended to watch the index.html file as you edit your code
26
-
27
- [Vscode 5 Server](https://marketplace.visualstudio.com/items?itemName=yandeu.five-server)
28
-
29
- 4. Create Proper Folders
30
-
31
- Create a pages folder - which allows you to have nextjs page like routing via buns file based router
32
-
33
- Tip: Each folder can be deep nested up to 4 levels!
34
-
35
- ```bash
36
- /pages/index.jsx = /
37
- /pages/home/[page].jsx = /home/:page
38
- /pages/path/index.jsx = /path/file
39
- ```
40
- Keyword folders - all files are passed from these folders to the `dist` folder
41
-
42
- ```bash
43
-
44
- pages - used for jsx route files
45
- src - used for your jsx components / javascript files
46
- public - used for anything
47
-
48
- ```
49
-
50
-
51
-
52
- 5. And your done - Run `npx vaderjs` and the compiled output is visible inside of the `/dist/` folder!
53
-
54
-
55
- ## Key Features & Examples
56
-
57
- ### File based routing
58
- vader's compiler automatically handles routing so you wont need to! - it uses a similar page routing to nextjs
59
-
60
- ```bash
61
- /pages/index.jsx = /
62
- /pages/home/[page].jsx = /home/:page
63
- /pages/path/index.jsx = /path/file
64
-
65
-
66
- ```
67
- For pages that have [params] you can derive it using this.request
68
-
69
-
70
- ### Simplified Component Creation
71
-
72
- ```jsx
73
- // pages/home.jsx
74
- let {Component, useState} = await import('vaderjs/client') // this will be automatically handled by vader in compile time
75
- let Mycomponent = await require('./pages/mycomponent')
76
- class Home extends Vader {
77
- constructor() {
78
- super();
79
- this.key = '2'
80
- }
81
- render() {
82
- return <>
83
- <div key={this.key}>
84
- <p>Hello World</p>
85
- </div>
86
- <Mycomponent ..props />
87
- </>
88
- }
89
- }
90
-
91
- return {default:Home}
92
- ```
93
-
94
-
95
-
96
- ### State Management
97
- Vaderjs uses partial hydration which is much better than react vdom, simply pass the ref key to state and only that element will update - no vdom required!!
98
-
99
- ```jsx
100
- let {Component, useState, useRef} = await import('vaderjs/client')
101
-
102
- class MyApp extends Component{
103
- contructor(){
104
- super()
105
- this.key = 'static key for state changes'
106
- }
107
-
108
- render(){
109
- let [count, setCount] = useState(0)
110
- let ref = useRef('')
111
-
112
- return <>
113
- <p ref={ref.bind}>Count is ${count}</p>
114
- ${/**
115
- pass anything used from the toplevel render to the lowerlevel function params to be able to invoke!
116
- **/}
117
- <button onclick={(setCount, count, ref)=>{
118
- setCount(count + 1, ref.bind)
119
- }}>Increment</button>
120
- </>
121
-
122
- }
123
- }
124
-
125
- return {default:MyApp}
126
- ```
127
-
128
-
129
- ### Function Binding
130
-
131
- Vaderjs allows you to bind functions directly to html elements just like react
132
- there are two ways - top level invokes like below
133
-
134
- ```javascript
135
- // vader uses params[0] as the event target object and other parameters resolve after
136
-
137
- function click(event, otherparams){
138
- console.log(event.target, otherparams)
139
- }
140
-
141
- const hello = function(event, otherparams){
142
-
143
- }
144
-
145
- return <>
146
- <button onclick={()=>click()}>Click Me</button>
147
- </>
148
- ```
149
-
150
- and lower level invokes - these operate the same just allow you to pass items from top level to lower level: ex - I have a variable named car and i want the button to log it i can pass it as a parameter to allow it to be added to the buttons function scope
151
-
152
- ```jsx
153
- let car = {
154
- model: 'tesla',
155
- price: 'toomiuch'
156
- }
157
- return <>
158
- <button onclick={(car)=>{
159
- console.log(car.model)
160
- }}>Log</button>
161
- ```
162
-
163
-
164
-
165
-
166
-
167
-
168
- ## License
169
-
170
- VaderJS is released under the MIT License. See the [LICENSE](https://github.com/Postr-Inc/Vader.js/blob/main/LICENSE) file for details.
171
-
172
- ## Join the Community
173
-
174
- Connect with the VaderJS community on [GitHub](https://github.com/Postr-Inc/Vader.js). Contribute, share feedback, and improve VaderJS for SPA development.
1
+ <p align="center">
2
+ <a href="https://vader-js.pages.dev">
3
+ <picture>
4
+ <source media="(prefers-color-scheme: dark)" srcset="/icon.jpeg">
5
+ <img src="./logo.png" height="128">
6
+ </picture>
7
+ <h1 align="center">Vader.js</h1>
8
+ </a>
9
+ </p>
10
+
11
+ # VaderJS: A Powerful Reactive Framework for SPAs inspired by react.js!
12
+
13
+ [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/Postr-Inc/Vader.js/blob/main/LICENSE) [![npm version](https://img.shields.io/npm/v/vaderjs.svg?style=flat)](https://www.npmjs.com/package/vaderjs)
14
+
15
+
16
+
17
+ ## Get Started
18
+
19
+ 2. Install vaderjs
20
+
21
+ ```bash
22
+ npm i vaderjs@latest
23
+ ```
24
+
25
+ 3. Install five server - recommended to watch the index.html file as you edit your code
26
+
27
+ [Vscode 5 Server](https://marketplace.visualstudio.com/items?itemName=yandeu.five-server)
28
+
29
+ 4. Create Proper Folders
30
+
31
+ Create a pages folder - which allows you to have nextjs page like routing via buns file based router
32
+
33
+ Tip: Each folder can be deep nested up to 4 levels!
34
+
35
+ ```bash
36
+ /pages/index.jsx = /
37
+ /pages/home/[page].jsx = /home/:page
38
+ /pages/path/index.jsx = /path/file
39
+ ```
40
+ Keyword folders - all files are passed from these folders to the `dist` folder
41
+
42
+ ```bash
43
+
44
+ pages - used for jsx route files
45
+ src - used for your jsx components / javascript files
46
+ public - used for anything
47
+
48
+ ```
49
+
50
+
51
+
52
+ 5. And your done - Run `npx vaderjs` and the compiled output is visible inside of the `/dist/` folder!
53
+
54
+
55
+ ## Key Features & Examples
56
+
57
+ ### File based routing
58
+ vader's compiler automatically handles routing so you wont need to! - it uses a similar page routing to nextjs
59
+
60
+ ```bash
61
+ /pages/index.jsx = /
62
+ /pages/home/[page].jsx = /home/:page
63
+ /pages/path/index.jsx = /path/file
64
+
65
+
66
+ ```
67
+ For pages that have [params] you can derive it using this.request
68
+
69
+
70
+ ### Simplified Component Creation
71
+
72
+ ```jsx
73
+ // pages/home.jsx
74
+ let {Component, useState} = await import('vaderjs/client') // this will be automatically handled by vader in compile time
75
+ let Mycomponent = await require('./pages/mycomponent')
76
+ class Home extends Vader {
77
+ constructor() {
78
+ super();
79
+ this.key = '2'
80
+ }
81
+ render() {
82
+ return <>
83
+ <div key={this.key}>
84
+ <p>Hello World</p>
85
+ </div>
86
+ <Mycomponent ..props />
87
+ </>
88
+ }
89
+ }
90
+
91
+ return {default:Home}
92
+ ```
93
+
94
+
95
+
96
+ ### State Management
97
+ Vaderjs uses partial hydration & full reflection
98
+
99
+ You can pass a reference to the dom target like an id for the element u want to change - or you can just swap the value and the entire component will rerender
100
+
101
+ ```jsx
102
+ let {Component, useState, useRef} = await import('vaderjs/client')
103
+
104
+ class MyApp extends Component{
105
+ contructor(){
106
+ super()
107
+ this.key = 'static key for state changes'
108
+ }
109
+
110
+ render(){
111
+ let [count, setCount] = useState(0)
112
+ let ref = useRef('')
113
+
114
+ return <>
115
+ <p ref={ref.bind}>Count is ${count}</p>
116
+ ${/**
117
+ pass anything used from the toplevel render to the lowerlevel function params to be able to invoke!
118
+ **/}
119
+ <button onclick={(setCount, count, ref)=>{
120
+ setCount(count + 1, ref.bind)
121
+ }}>Increment</button>
122
+ </>
123
+
124
+ }
125
+ }
126
+
127
+ return {default:MyApp}
128
+ ```
129
+
130
+
131
+ ### Function Binding
132
+
133
+ Vaderjs allows you to bind functions directly to html elements just like react
134
+ there are two ways - top level invokes like below
135
+
136
+ ```javascript
137
+ // vader uses params[0] as the event target object and other parameters resolve after
138
+
139
+ function click(event, otherparams){
140
+ console.log(event.target, otherparams)
141
+ }
142
+
143
+ const hello = function(event, otherparams){
144
+
145
+ }
146
+
147
+ return <>
148
+ <button onclick={()=>click()}>Click Me</button>
149
+ </>
150
+ ```
151
+
152
+ and lower level invokes - these operate the same just allow you to pass items from top level to lower level: ex - I have a variable named car and i want the button to log it i can pass it as a parameter to allow it to be added to the buttons function scope
153
+
154
+ ```jsx
155
+ let car = {
156
+ model: 'tesla',
157
+ price: 'toomiuch'
158
+ }
159
+ return <>
160
+ <button onclick={(car)=>{
161
+ console.log(car.model)
162
+ }}>Log</button>
163
+ ```
164
+
165
+
166
+
167
+
168
+
169
+
170
+ ## License
171
+
172
+ VaderJS is released under the MIT License. See the [LICENSE](https://github.com/Postr-Inc/Vader.js/blob/main/LICENSE) file for details.
173
+
174
+ ## Join the Community
175
+
176
+ Connect with the VaderJS community on [GitHub](https://github.com/Postr-Inc/Vader.js). Contribute, share feedback, and improve VaderJS for SPA development.
package/client/index.js CHANGED
@@ -434,6 +434,12 @@ export class Component {
434
434
 
435
435
  paramNames = paramNames.replace(/,,/g, ',');
436
436
  let newparamnames = paramNames.replaceAll(',,', ',')
437
+ params.forEach((param, index) => {
438
+ if(param && Object.keys(param).includes('detail')){
439
+ param = param['detail']['target']['event']
440
+ params[index] = param
441
+ }
442
+ });
437
443
  // Remove trailing commas
438
444
  paramNames.endsWith(',') ? paramNames = paramNames.slice(0, -1) : null;
439
445
  console.log(paramNames)
@@ -711,6 +717,12 @@ export const constant = (value) => {
711
717
  }
712
718
  return constants[key];
713
719
  };
720
+ /**
721
+ * @method useRef
722
+ * @description Allows you to use a reference to a DOM element
723
+ * @param {*} initialState
724
+ * @returns
725
+ */
714
726
 
715
727
  export const useRef = (initialState) => {
716
728
  return {
package/logo.png CHANGED
File without changes
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "vaderjs",
3
3
  "description": "A Reactive library aimed to helping you build reactive applications inspired by react.js",
4
4
  "module": "vader.js",
5
- "version": "1.3.3-alpha-20",
5
+ "version": "1.3.3-alpha-21",
6
6
  "bin": {
7
7
  "vader": "./vader.js"
8
8
  },
File without changes
package/runtime/router.js CHANGED
@@ -198,8 +198,9 @@ class VaderRouter{
198
198
  });
199
199
  }
200
200
  },
201
- redirect: (path) => {
202
- window.location.hash = path;
201
+ redirect: (path) => {
202
+ !path.startsWith('/') ? path = `/${path}` : null;
203
+ window.location.hash = `#${path}`;
203
204
  },
204
205
  render: async (/**@type {Component} */ Component, req, res) => {
205
206
 
package/runtime/vader.js CHANGED
@@ -167,22 +167,10 @@ export class Component {
167
167
  this.checkIFMounted();
168
168
  this.memoizes = []
169
169
 
170
- this.vdom = []
170
+
171
171
 
172
172
  this.children = []
173
- let dom = new DOMParser().parseFromString(this.render(), 'text/html').body.firstChild;
174
- dom.querySelectorAll('*').forEach((el) => {
175
- let obj = {
176
- el: el,
177
- tag: el.tagName,
178
- html: el.innerHTML,
179
- content: el.textContent,
180
- value: el.value,
181
- attributes: el.attributes,
182
- children: el.childNodes,
183
- }
184
- this.vdom.push(obj)
185
- });
173
+
186
174
 
187
175
  /**
188
176
  * Parent of the current component.
@@ -434,6 +422,12 @@ export class Component {
434
422
 
435
423
  paramNames = paramNames.replace(/,,/g, ',');
436
424
  let newparamnames = paramNames.replaceAll(',,', ',')
425
+ params.forEach((param, index) => {
426
+ if(param && Object.keys(param).includes('detail')){
427
+ param = param['detail']['target']['event']
428
+ params[index] = param
429
+ }
430
+ });
437
431
  // Remove trailing commas
438
432
  paramNames.endsWith(',') ? paramNames = paramNames.slice(0, -1) : null;
439
433
  console.log(paramNames)
@@ -546,28 +540,45 @@ export class Component {
546
540
  }
547
541
 
548
542
  /**
549
- * Uses state to dynamically update the component.
550
- * @method useState
551
- * @param {string} [key=null] - The auto-generated key.
552
- * @param {*} initialState - The initial state.
553
- * @param {Component.render} [func=null] - The render function.
554
- * @returns {Array} - An array containing the state value and the setter function.
555
- */
556
- useState(key = null, initialState) {
557
- if (!this.state[key]) {
558
- this.state[key] = initialState;
559
- }
560
- let updatedValue = () => this.state[key];
561
- const getValue = updatedValue()
562
- const set = (newValue, hook) => {
563
- this.state[key] = newValue;
564
- this.hydrate(hook);
565
- };
566
- this[`set${key}`] = set;
567
- this[`get${key}`] = getValue;
568
- return [getValue, set];
543
+ * useState hook.
544
+ *
545
+ * @template T
546
+ * @param {string} key - The key for the state property.
547
+ * @param {T} initialState - The initial state value.
548
+ * @returns {[() => T, (newValue: T, hook: Function) => void]} - A tuple with getter and setter functions.
549
+ */
550
+ useState(key, initialState) {
551
+ if (!this.state[key]) {
552
+ this.state[key] = initialState;
569
553
  }
570
554
 
555
+ /**
556
+ * Get the current state value.
557
+ *
558
+ * @returns {T} The current state value.
559
+ */
560
+ let updatedValue = () => this.state[key];
561
+
562
+ const getValue = updatedValue();
563
+
564
+ /**
565
+ * Set a new value for the state.
566
+ *
567
+ * @param {T} newValue - The new value to set.
568
+ * @param {Function} hook - The hook to hydrate after setting the value.
569
+ */
570
+ const set = (newValue, hook) => {
571
+ this.state[key] = newValue;
572
+ this.hydrate(hook);
573
+ };
574
+
575
+
576
+
577
+ return [this.state[key], set];
578
+ }
579
+
580
+
581
+
571
582
  useRef(key = null, initialState) {
572
583
  if (!this.state[key]) {
573
584
  this.state[key] = initialState;
@@ -675,22 +686,41 @@ export const require = async (path, noresolve = false) => {
675
686
 
676
687
 
677
688
  window.require = require;
678
-
679
689
  /**
680
- * @method useState - type
681
- * @param {*} initialState
682
- * @returns {Array} [value, set]
683
- * @description Allows you to use state to dynamically update your component
690
+ * useState hook.
691
+ *
692
+ * @param {string} key - The key for the state property.
693
+ * @param {*} initialState - The initial state value.
694
+ * @returns {[*]} - A tuple with the current state value and a setter function.
684
695
  */
685
- export const useState = (initialState) => {
686
- let key = ''
687
- let value = initialState;
688
- if (key && !states[key]) {
689
- this.states[key] = initialState;
696
+ export const useState = (key, initialState) => {
697
+ if (!states[key]) {
698
+ states[key] = initialState;
690
699
  }
691
- return [value, (newValue) => {}];
700
+
701
+ /**
702
+ * Get the current state value.
703
+ *
704
+ * @returns {*} The current state value.
705
+ */
706
+ let updatedValue = () => states[key];
707
+
708
+ /**
709
+ * Set a new value for the state.
710
+ *
711
+ * @param {*} newValue - The new value to set.
712
+ * @param {Function} hook - The hook to hydrate after setting the value.
713
+ */
714
+ const set = (newValue, hook) => {
715
+ states[key] = newValue;
716
+ this.hydrate(hook);
717
+ };
718
+
719
+ return [states[key], set];
692
720
  };
693
721
 
722
+
723
+
694
724
  /**
695
725
  * @method useReducer
696
726
  * @param {*} initialState
@@ -712,12 +742,26 @@ export const constant = (value) => {
712
742
  return constants[key];
713
743
  };
714
744
 
745
+ /**
746
+ * useRef hook.
747
+ *
748
+ * @param {*} initialState - The initial state value for the ref.
749
+ * @returns {{ current: *, bind: string }} - An object containing the current value and a bind string.
750
+ */
715
751
  export const useRef = (initialState) => {
716
752
  return {
753
+ /**
754
+ * @description The current value of the ref.
755
+
756
+ */
717
757
  current: initialState,
758
+ /**
759
+ * @description A unique string that can be used to bind the ref to an element.
760
+ */
718
761
  bind: '',
719
762
  };
720
763
  };
764
+
721
765
  export default {
722
766
  Component,
723
767
  require,
package/vader.js CHANGED
@@ -447,18 +447,6 @@ function Compiler(func) {
447
447
  value = value.match(regex) ? value.match(regex)[0].split("useRef(")[1].split(")")[0].trim() : value
448
448
  let newState = `${type} ${key} = this.useRef('${key}', ${value}`;
449
449
 
450
- string = string.replace(line, newState);
451
- }else if (line.includes("useReducer") && !line.includes("import")){
452
- line = line.trim();
453
- // let ref = useRef(null)
454
- let type = line.split(" ")[0];
455
- let key = line.split("=")[0].split(" ")[1].trim();
456
- let value = line.split("=")[1].split("useReducer(")[1]
457
-
458
- let regex = /useReducer\((.*)\)/gs
459
- value = value.match(regex) ? value.match(regex)[0].split("useReducer(")[1].split(")")[0].trim() : value
460
- let newState = `${type} ${key} = this.useReducer('${key}', ${value}`;
461
-
462
450
  string = string.replace(line, newState);
463
451
  }
464
452
  }
@@ -757,7 +745,9 @@ switch (true) {
757
745
  case process.argv.includes('--watch'):
758
746
 
759
747
  console.log(`
760
- Vader.js v1.3.3
748
+ Vader.js v1.3.3
749
+ - Watching for changes in ./pages
750
+ - Watching for changes in ./src
761
751
  `)
762
752
  Build()
763
753