vaderjs 1.3.2 → 1.3.3-2124766be812

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.
@@ -0,0 +1,705 @@
1
+ /**
2
+ * @method strictMount
3
+ * @description This method allows you to await until the component is mounted before running a callback
4
+ * @param {Component key} key
5
+ * @param {Function} callback
6
+ */
7
+ export const strictMount = (key, callback) => {
8
+
9
+ };
10
+
11
+ /**
12
+ * @method $metdata
13
+ * @description This method allows you to set the metadata for the current page
14
+ * @param {string} title
15
+ * @param {string} styles
16
+ * @param {string} description
17
+ * @param {string} keywords
18
+ * @param {string} author
19
+ * @param {string} image
20
+ * @param {string} url
21
+ * @param {string} robot
22
+ * @param {string} manifest
23
+ * @param {Array} tags
24
+ * @returns {Object} - The rendered content.
25
+ *
26
+ */
27
+
28
+ export const $metdata = {
29
+ title:'',
30
+ styles,
31
+ description:'',
32
+ keywords:'',
33
+ author:'',
34
+ image:'',
35
+ url:'',
36
+ robot:'',
37
+ manifest:'',
38
+ tags: []
39
+
40
+ }
41
+
42
+ /**
43
+ * @method $prerender
44
+ * @description This method disables the prerendering of the current page
45
+ */
46
+ export const $prerender = {
47
+
48
+ }
49
+
50
+
51
+
52
+
53
+
54
+ /**
55
+ * Represents a component in the Vader framework.
56
+ */
57
+ export class Component {
58
+ /**
59
+ * Creates an instance of Component.
60
+ */
61
+ constructor() {
62
+ this.state = {};
63
+ /**
64
+ * @type {string}
65
+ * @description The key for the component. used to identify the component in the DOM
66
+ */
67
+ this.key = null;
68
+ /**
69
+ * @private
70
+ */
71
+ this.components = {};
72
+ this.mounted = false;
73
+ this.checkIFMounted();
74
+ this.memoizes = []
75
+ /**
76
+ * @private
77
+ */
78
+ this.functions = []
79
+ this.children = []
80
+
81
+
82
+ /**
83
+ * Parent of the current component.
84
+ * @type {Component}
85
+ */
86
+ this.parentNode = {}
87
+
88
+ /**
89
+ * Request object.
90
+ */
91
+ this.request = {
92
+ /**
93
+ * @type {string}
94
+ * @description The headers for the current route
95
+ */
96
+ headers:{},
97
+ /**
98
+ * @type {string}
99
+ * @description The method for the current route
100
+ */
101
+ method: "GET",
102
+ /**
103
+ * @type {string}
104
+ * @description params for the given route /:id/:name etc
105
+ */
106
+ params: {},
107
+ /**
108
+ * @type {string}
109
+ * @description path: current route path
110
+ */
111
+ path: "",
112
+ /**
113
+ * @type {string}
114
+ * @description query: query object for the current route ?name=hello -> {name: 'hello'}
115
+ */
116
+ query: {},
117
+ },
118
+ /**
119
+ * @type {string}
120
+ * @description The response object for the current route
121
+ */
122
+ this.response = {
123
+ /**
124
+ * @method json
125
+ * @description This method allows you to send json data to the client
126
+ * @param {*} data
127
+ */
128
+ json: (data) => {},
129
+ /**
130
+ * @method send
131
+ * @description This method allows you to send text data to the client
132
+ * @param {*} data
133
+ */
134
+ send: (data) => {},
135
+ /**
136
+ * @method redirect
137
+ * @description This method allows you to redirect the client to a new route
138
+ * @param {*} path
139
+ */
140
+ redirect: (path) => {},
141
+ /**
142
+ * @method render
143
+ * @description render a new component to the client
144
+ * @param {*} Component
145
+ */
146
+ render: async (Component) => {},
147
+ /**
148
+ * @method log
149
+ * @description This method is used to log the request and response
150
+ * @param {String} type
151
+ */
152
+ log: (type) => {},
153
+ /**
154
+ * @method setQuery
155
+ * @description This method is used to set the query object for the current route
156
+ */
157
+ setQuery: (query) => {},
158
+
159
+ }
160
+ /**
161
+ * @method router
162
+ * @description use router methods directly from the parent component
163
+ */
164
+
165
+ this.router = {
166
+ /**
167
+ * @method use
168
+ * @description add a middleware to the current route
169
+ * @param {Function} middleware
170
+ * @returns {void}
171
+ */
172
+ use: (/**@type {Function} */ middleware) => {},
173
+ }
174
+ }
175
+
176
+ /**
177
+ * @method createComponent
178
+ * @description This method allows you to create a component from a class or function
179
+ * @param {Component} component
180
+ * @param {Object} props
181
+ * @param {Array} children
182
+ */
183
+ createComponent(/**@type {Component}**/component, props, children) {
184
+
185
+ }
186
+ /**
187
+ * @private
188
+ */
189
+ reset(){
190
+
191
+ }
192
+ /**
193
+ * @method memoize
194
+ * @description This method allows you to memoize a component which when rerendered will not be reinstantiated
195
+ * @param {Component} component
196
+ */
197
+ memoize(/**@type {Component}**/component){
198
+
199
+ }
200
+ /**
201
+ * @method parseStyle
202
+ * @description This method allows you to parse a jsx style object to a string
203
+ * @param {object} styles
204
+ */
205
+ parseStyle(styles){
206
+
207
+ }
208
+ /**
209
+ * @private
210
+ */
211
+ bindMount(){
212
+ }
213
+
214
+ /**
215
+ * Hydrates the component by updating the HTML content if it has changed.
216
+ * @private
217
+ */
218
+
219
+ domDifference(oldDom, newDom) {
220
+
221
+ }
222
+ /**
223
+ * @private
224
+ * @param {*} diff
225
+ */
226
+
227
+ updateChangedElements(diff) {
228
+
229
+ }
230
+
231
+ /**
232
+ * @method hydrate
233
+ * @description This method allows you to hydrate a component
234
+ * @private
235
+ * @param {*} hook
236
+ */
237
+ hydrate(hook) {
238
+
239
+ }
240
+
241
+
242
+ /**
243
+ * @method patch
244
+ * @description This method allows you to patch the dom
245
+ * @private
246
+ * @param {*} oldElements
247
+ * @param {*} newElements
248
+ */
249
+
250
+ patch(oldElements, newElements) {
251
+
252
+ }
253
+
254
+
255
+
256
+
257
+
258
+ /**
259
+ * Handles an object by parsing it as JSON and evaluating it.
260
+ * @param {string} obj - The object to handle.
261
+ * @returns {*} - The evaluated object.
262
+ * @prvate
263
+ */
264
+ handleObject(obj) {
265
+
266
+ }
267
+
268
+
269
+
270
+ /**
271
+ * Binds a function to the component.
272
+ * @param {string} funcData - The function data.
273
+ * @param {string} p - The parameter.
274
+ * @param {string} ref - The reference.
275
+ * @param {string} paramNames - The parameter names.
276
+ * @param {...*} params - The parameters.
277
+ * @returns {string} - A valid inline JS function call.
278
+ */
279
+ bind(funcTion,isTerny, jsx,ref, paramNames, ...params) {
280
+
281
+ }
282
+
283
+
284
+
285
+ /**
286
+ * useState hook.
287
+ *
288
+ * @template T
289
+ * @param {string} key - The key for the state property.
290
+ * @param {T} initialState - The initial state value.
291
+ * @returns {[() => T, (newValue: T, hook: Function) => void]} - A tuple with getter and setter functions.
292
+ */
293
+ useState(key, initialState) {
294
+ if (!this.state[key]) {
295
+ this.state[key] = initialState;
296
+ }
297
+
298
+ /**
299
+ * Get the current state value.
300
+ *
301
+ * @returns {T} The current state value.
302
+ */
303
+ let updatedValue = () => this.state[key];
304
+
305
+ const getValue = updatedValue();
306
+
307
+ /**
308
+ * Set a new value for the state.
309
+ *
310
+ * @param {T} newValue - The new value to set.
311
+ * @param {Function} hook - The hook to hydrate after setting the value.
312
+ */
313
+ const set = (newValue, hook) => {
314
+ this.state[key] = newValue;
315
+ this.hydrate(hook);
316
+ };
317
+
318
+
319
+
320
+ return [getValue, set];
321
+ }
322
+
323
+
324
+
325
+ /**
326
+ * useRef hook.
327
+ * @param {string} key
328
+ * @param {any} initialState
329
+ * @returns {{ current: HTMLElement|any, bind: string }} - An object containing the current value and a bind string.
330
+ */
331
+ useRef(key = null, initialState) {
332
+
333
+ }
334
+
335
+ /**
336
+ * useReducer hook.
337
+ * @param {string} key - The key for the state property.
338
+ * @param {*} initialState - The initial state value.
339
+ * @param {Function} func - The reducer function.
340
+ * @returns {[*, (newValue: *, hook: Function) => void]} - A tuple with getter and setter functions.
341
+ * **/
342
+
343
+ useReducer(key = null, initialState, func = null) {
344
+
345
+ }
346
+
347
+
348
+ /**
349
+ * Placeholder for content to be rendered.
350
+ * @method render
351
+ * @returns {string} - The rendered content.
352
+ */
353
+ render() {}
354
+
355
+ /**
356
+ * Checks if the component is mounted and triggers the onMount method.
357
+ * @private
358
+ */
359
+ checkIFMounted() {
360
+ let observer = new MutationObserver((mutations) => {
361
+ mutations.forEach((mutation) => {
362
+
363
+ if (mutation.target.querySelector(`[key="${this.key}"]`) && !this.mounted) {
364
+ this.onMount();
365
+ this.mounted = true;
366
+ }
367
+
368
+ if(Array.from(mutation.removedNodes).find((node) => node.attributes && node.attributes.key && node.attributes.key.value === this.key)){
369
+ this.onUnmount();
370
+ this.reset();
371
+ }
372
+ })
373
+ })
374
+ observer.observe(document.body, {
375
+ childList: true,
376
+ subtree: true,
377
+ });
378
+ }
379
+
380
+ /**
381
+ * Method that is called when the component is mounted.
382
+ * @method onMount
383
+ */
384
+ onMount() {}
385
+ /**
386
+ * Method that is called when the component is unmounted.
387
+ * @method onUnmount
388
+ */
389
+ onUnmount() {}
390
+ }
391
+
392
+
393
+
394
+ /**
395
+ * useState hook.
396
+ *
397
+ * @param {string} key - The key for the state property.
398
+ * @param {*} initialState - The initial state value.
399
+ * @returns {[*]} - A tuple with the current state value and a setter function.
400
+ */
401
+ export const useState = (key, initialState) => {
402
+ if (!states[key]) {
403
+ states[key] = initialState;
404
+ }
405
+
406
+ /**
407
+ * Get the current state value.
408
+ *
409
+ * @returns {*} The current state value.
410
+ */
411
+ let updatedValue = () => states[key];
412
+
413
+ /**
414
+ * Set a new value for the state.
415
+ *
416
+ * @param {*} newValue - The new value to set.
417
+ * @param {Function} hook - The hook to hydrate after setting the value.
418
+ */
419
+ const set = (newValue, hook) => {
420
+ states[key] = newValue;
421
+ this.hydrate(hook);
422
+ };
423
+
424
+ return [states[key], set];
425
+ };
426
+
427
+
428
+
429
+ /**
430
+ * @method useReducer
431
+ * @param {*} initialState
432
+ * @param {*} reducer
433
+ * @returns {Array} [value, set]
434
+ */
435
+ export const useReducer = (/**@type {*}**/initialState, /**@type {function}**/reducer) => {
436
+ return [initialState, (newValue) => {}];
437
+ };
438
+
439
+
440
+ /**
441
+ * useRef hook.
442
+ * @param {string} key
443
+ * @param {any} initialState
444
+ * @returns {{ current: HTMLElement|any, bind: string }} - An object containing the current value and a bind string.
445
+ */
446
+ export const useRef = (initialState) => {
447
+ return {
448
+ /**
449
+ * @description The current value of the ref.
450
+ @type {*}
451
+ */
452
+ current: initialState,
453
+ /**
454
+ * @description A unique string that can be used to bind the ref to an element.
455
+ * @type {HTMLElement|string}
456
+ */
457
+ bind: '',
458
+ };
459
+ };
460
+
461
+ /**
462
+ * @class Link
463
+ * @description Link component
464
+ * @extends Component
465
+ * @example
466
+ * <Link href="/some-path" class="custom-link" style="color: blue;">Click me!</Link>
467
+ */
468
+ export class Link extends Component {
469
+ /**
470
+ * @constructor
471
+ * @param {object} props - Component props
472
+ * @param {string} props.href - URL for the link
473
+ * @param {string} props.className - CSS class for the link
474
+ * @param {string} props.title - Title for the link
475
+ * @param {string} props.key - Unique identifier for the link
476
+ * @param {string} props.style - Inline CSS style for the link
477
+ * @param {string} props.children - Content to be displayed inside the link
478
+ */
479
+ constructor(props) {
480
+ super(props);
481
+
482
+ /**
483
+ * @type {object}
484
+ * @property {string} href - URL for the link
485
+ * @property {string} [class] - CSS class for the link
486
+ * @property {string} [style] - Inline CSS style for the link
487
+ * @property {string} [children] - Content to be displayed inside the link
488
+ */
489
+ this.props = props;
490
+
491
+ /**
492
+ * @type {HTMLAnchorElement}
493
+ */
494
+ this.link = document.createElement('a');
495
+
496
+ /**
497
+ * @type {string}
498
+ */
499
+ this.key = props.href + Math.random();
500
+ }
501
+
502
+ /**
503
+ * @function
504
+ * @returns {string} - Rendered HTML for the Link component
505
+ */
506
+ render() {
507
+
508
+ return this.link.outerHTML;
509
+ }
510
+ }
511
+
512
+
513
+
514
+ /**
515
+ * @class Image
516
+ * @description Image component
517
+ * @extends Component
518
+ * @example
519
+ * <Image src="https://via.placeholder.com/150" alt="image" />
520
+ */
521
+ export class Image extends Component {
522
+ /**
523
+ * @constructor
524
+ * @param {object} props - Component props
525
+ * @param {string} props.src - Image source URL
526
+ * @param {string} props.class - CSS class for the image
527
+ * @param {string} props.style - Inline CSS style for the image
528
+ * @param {number} props.blur - Blur value for the image (optional)
529
+ * @param {number} props.width - Width of the image (optional)
530
+ * @param {number} props.height - Height of the image (optional)
531
+ * @param {boolean} props.optimize - Optimize the image (optional, default: true)
532
+ * @param {boolean} props.loader - Show a placeholder loader (optional, default: true)
533
+ * @param {string} props.alt - Alt text for the image (optional, default: 'image')
534
+ */
535
+ constructor(props) {
536
+ super(props);
537
+
538
+ /**
539
+ * @type {object}
540
+ * @property {string} src - Image source URL
541
+ * @property {string} [class] - CSS class for the image
542
+ * @property {string} [style] - Inline CSS style for the image
543
+ * @property {number} [blur] - Blur value for the image (optional)
544
+ * @property {number} [width] - Width of the image (optional)
545
+ * @property {number} [height] - Height of the image (optional)
546
+ * @property {boolean} [optimize] - Optimize the image (optional, default: true)
547
+ * @property {boolean} [loader] - Show a placeholder loader (optional, default: true)
548
+ * @property {string} [alt] - Alt text for the image (optional, default: 'image')
549
+ * @property {string} [children] - Content to be displayed inside the image
550
+ * @property {string} [key] - Unique identifier for the image
551
+ * @property {string} [onLoad] - Function to be called when the image is loaded
552
+ */
553
+ this.props = {
554
+ src: props.src,
555
+ class: props.class,
556
+ style: props.style,
557
+ blur: props.blur,
558
+ width: props.width,
559
+ height: props.height,
560
+ optimize: props.optimize || true,
561
+ loader: props.loader || true,
562
+ alt: props.alt || 'image',
563
+ };
564
+
565
+ /**
566
+ * @type {string}
567
+ */
568
+ this.key = props.src + Math.random();
569
+
570
+ /**
571
+ * @type {HTMLImageElement}
572
+ * @private
573
+ */
574
+ this.img = document.createElement('img');
575
+
576
+ /**
577
+ * @type {HTMLDivElement}
578
+ * @private
579
+ */
580
+ this.placeholder = document.createElement('div');
581
+ }
582
+
583
+ /**
584
+ * @function
585
+ * @returns {string} - Rendered JSX for the Image component
586
+ */
587
+ render() {
588
+ // adjust width and height to the user's screen size
589
+
590
+
591
+ return this.img.outerHTML;
592
+ }
593
+ }
594
+
595
+ export class Head extends Component {
596
+ /**
597
+ * @constructor
598
+ * @param {object} props - Component props
599
+ * @param {string} props.children - Content to be displayed inside the head
600
+ */
601
+ constructor(props) {
602
+ super(props);
603
+ this.props = {
604
+ children: props.children,
605
+ }
606
+ this.key = 'head';
607
+ this.head = document.createElement('head');
608
+ }
609
+
610
+
611
+
612
+ render() {
613
+ this.head.innerHTML = this.props.children;
614
+ return this.head.outerHTML;
615
+ }
616
+
617
+ onMount() {
618
+ document.head.innerHTML = this.head.innerHTML;
619
+ document.body.querySelector(`[key="${this.key}"]`).remove();
620
+ }
621
+ }
622
+
623
+ export class Script extends Component {
624
+ /**
625
+ * @constructor
626
+ * @param {object} props - Component props
627
+ * @param {string} props.children - Content to be displayed inside the script
628
+ */
629
+ constructor(props) {
630
+ super(props);
631
+ this.props = {
632
+ children: props.children,
633
+ }
634
+ this.key = 'script';
635
+ this.script = document.createElement('script');
636
+ }
637
+
638
+
639
+
640
+ render() {
641
+ this.script.innerHTML = this.props.children.split('\n').join(';\n');
642
+ return this.script.outerHTML;
643
+ }
644
+
645
+ onMount() {
646
+ document.head.appendChild(this.script);
647
+ document.body.querySelector(`[key="${this.key}"]`).remove();
648
+ }
649
+
650
+ }
651
+
652
+ /**
653
+ * @class HTML
654
+ * @description HTML component
655
+ * @extends Component
656
+ */
657
+ export class Html extends Component {
658
+ /**
659
+ * @constructor
660
+ * @param {object} props - Component props
661
+ * @param {string} props.children - Content to be displayed inside the HTML
662
+ * @param {string} props.lang - Language for the HTML
663
+ */
664
+ constructor(props) {
665
+ super(props);
666
+ /**
667
+ * @type {object}
668
+ * @property {string} children - Content to be displayed inside the HTML
669
+ * @property {string} lang - Language for the HTML
670
+ * @property {object} attributes - Attributes for the HTML
671
+ */
672
+ this.props = {
673
+ children: props.children,
674
+ lang: props.lang || 'en',
675
+ attributes: props.attributes || {},
676
+ }
677
+ this.key = 'html';
678
+ this.html = document.createElement('html');
679
+ }
680
+
681
+ render() {
682
+ this.html.innerHTML = this.props.children;
683
+ return this.html.outerHTML;
684
+ }
685
+
686
+ onMount() {
687
+ if (window.isServer) {
688
+ document.documentElement.innerHTML = this.html.outerHTML;
689
+ }
690
+ console.log('Document Has Been Mounted')
691
+ }
692
+
693
+ }
694
+ export default {
695
+ Component,
696
+ useRef,
697
+ useReducer,
698
+ useState,
699
+ strictMount,
700
+ Link,
701
+ Image,
702
+ Head,
703
+ Html,
704
+
705
+ }