elegance-js 1.13.0 → 1.14.0

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/dist/global.d.ts CHANGED
@@ -27,10 +27,7 @@ declare global {
27
27
  children: null;
28
28
  options: Record<string, any> | Child;
29
29
  };
30
- type PageOptions = {
31
- build: "once" | "interval" | "request";
32
- };
33
- type Page = (AnyBuiltElement) | (() => AnyBuiltElement);
30
+ type Page = (AnyBuiltElement) | (() => AnyBuiltElement) | (() => Promise<AnyBuiltElement>);
34
31
  type ObjectAttribute<T> = T extends ObjectAttributeType.STATE ? {
35
32
  type: ObjectAttributeType;
36
33
  id: string | number;
@@ -646,6 +646,12 @@ var generateClientPageData = async (pageLocation, state, objectAttributes, pageL
646
646
  console.error("Failed to transform client page js!", error);
647
647
  });
648
648
  if (!transformedResult) return { sendHardReloadInstruction };
649
+ if (fs.existsSync(pageDataPath)) {
650
+ const content = fs.readFileSync(pageDataPath).toString();
651
+ if (content !== transformedResult.code) {
652
+ sendHardReloadInstruction = true;
653
+ }
654
+ }
649
655
  fs.writeFileSync(pageDataPath, transformedResult.code, "utf-8");
650
656
  return { sendHardReloadInstruction };
651
657
  };
@@ -730,7 +736,7 @@ return __exports
730
736
  console.warn(`WARNING: ${filePath} should export a const page, which is of type () => BuiltElement<"body">.`);
731
737
  }
732
738
  if (typeof pageElements === "function") {
733
- if (pageElements.constructor.name === "AsyncFunctino") {
739
+ if (pageElements.constructor.name === "AsyncFunction") {
734
740
  pageElements = await pageElements();
735
741
  } else {
736
742
  pageElements = pageElements();
@@ -19,7 +19,7 @@ type ReactiveMap<T extends any[], D extends Dependencies> = (this: {
19
19
  bind: string | undefined;
20
20
  }, template: (item: T[number], index: number, ...deps: {
21
21
  [K in keyof D]: ClientSubjectGeneric<D[K]>["value"];
22
- }) => Child, deps: [...D]) => Child;
22
+ }) => Child, deps?: [...D]) => Child;
23
23
  type Dependencies = {
24
24
  type: ObjectAttributeType;
25
25
  value: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elegance-js",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "Web-Framework",
5
5
  "type": "module",
6
6
  "bin": {
@@ -18,12 +18,34 @@ const envDtsPath = "env.d.ts";
18
18
  const tsconfigPath = "tsconfig.json";
19
19
 
20
20
  const pageTsContent = `
21
- import { eventListener, state } from "elegance-js/server/state";
22
- import { loadHook } from "elegance-js/server/loadHook";
23
- import { observe } from "elegance-js/server/observe";
21
+ import { observe, loadHook, eventListener, state } from "elegance-js";
24
22
 
23
+ /*
24
+ This is state.
25
+ It uses a simple observer model in the browser (see the observer function)
26
+
27
+ You can update it's value using state.value, and when you want the observers
28
+ of said state to refresh, call state.signal().
29
+
30
+ You can update the state to whatever value you want it to be,
31
+ before the page is finished building.
32
+
33
+ Once the page is built, all state values are shipped to the browser as-is.
34
+ */
25
35
  const counter = state(0);
26
36
 
37
+ /*
38
+ The server keeps track of every call to the loadHook() function,
39
+ does some preprocessing, and ships the loadhook to the browser.
40
+ The browser, after the page loads, runs the content of the function (the second paramater)
41
+ passed into the loadHook call.
42
+
43
+ Loadhook takes in as it's first parameter, a dependency list of state()'s that
44
+ the loadHook can then reference in the browser.
45
+
46
+ For newbies, note that the content of loadHook is *browser code*, and thus
47
+ cannot be trusted!
48
+ */
27
49
  loadHook(
28
50
  [counter],
29
51
  (_, counter) => {
@@ -36,57 +58,149 @@ loadHook(
36
58
  },
37
59
  )
38
60
 
39
- export const page = body ({
40
- class: "text-white flex min-h-screen items-start sm:justify-center p-4 bg-black flex-col gap-4 max-w-[500px] w-full mx-auto",
41
- },
42
- h1 ({
43
- class: "text-4xl font-inter font-semibold bg-clip-text text-transparent bg-gradient-to-tl from-[#EEB844] to-[#FF4FED] oveflow-clip",
44
- },
45
- "Welcome to Elegance.JS!",
46
- ),
61
+ /*
62
+ State can also be an array!
63
+ In which case, the reactiveMap() method is added onto the state.
64
+ This allows you to run client-side code, which dynamically changes
65
+ the page's HTML content based on the state of the array in the browser.
66
+ */
67
+ const ReactiveMap = () => {
68
+ const arrayState = state([
69
+ "John","Mary","William","Kimberly",
70
+ ]);
47
71
 
48
- p ({
49
- },
50
- "Edit page.ts to get started.",
51
- ),
72
+ return arrayState.reactiveMap((item, index) => {
73
+ index += 1;
74
+
75
+ return div({
76
+ },
77
+ index + ". ", item,
78
+ )
79
+ })
80
+ };
81
+
82
+ /*
83
+ This is the actual content of the page.
84
+
85
+ It does not *have* to be an async function.
86
+ Page can be any value, as long as it resolves into a Child (a built element, eg. string, a return value of an element creation call, etc)
87
+ */
88
+ export const page: Page = async () => {
89
+ const pageName = state("Elegance.JS");
52
90
 
53
- div({
54
- class: "flex items-start gap-4 mt-2",
91
+ /*
92
+ The below is an element creation function.
93
+ The syntax is roughly similar to how HTML works.
94
+
95
+ A call like: h1("Hello World!")
96
+
97
+ Would generate the HTML: <h1>Hello World!</h1>
98
+
99
+ The first parameter to an element may be another element (child), or an options object.
100
+
101
+ Options objects are used to set things like classNames, style, ids. etc.
102
+
103
+ For example this call: h1({
104
+ id: "my-id",
105
+ },
106
+ "Hello World!"
107
+ )
108
+
109
+ Turns into this HTML: <h1 id="my-id">Hello World!</h1>
110
+ */
111
+ return body ({
112
+ class: "text-white flex min-h-screen items-start sm:justify-center p-4 bg-black flex-col gap-4 max-w-[500px] w-full mx-auto",
55
113
  },
56
- a ({
57
- class: "px-4 py-2 rounded-md bg-red-400 text-black font-semibold relative group hover:scale-[1.05] duration-200",
58
- href: "https://elegance.js.org/",
59
- target: "_blank",
114
+ h1 ({
115
+ class: "text-4xl font-inter font-semibold bg-clip-text text-transparent bg-gradient-to-tl from-[#EEB844] to-[#FF4FED] oveflow-clip",
60
116
  },
61
- "Documentation",
62
-
63
- div ({
64
- class: "blur-[50px] absolute group-hover:bg-red-400 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
65
- "aria-hidden": "true",
66
- }),
117
+ `Welcome to ${pageName.value}!`,
67
118
  ),
68
-
69
- button ({
70
- class: "hover:cursor-pointer px-4 py-2 rounded-md bg-zinc-200 text-black font-semibold relative group hover:scale-[1.05] duration-200",
71
- onClick: eventListener(
72
- [counter],
73
- (_, counter) => {
74
- counter.value++;
119
+
120
+ ReactiveMap(),
121
+
122
+ p ({
123
+ },
124
+ "Edit page.ts to get started.",
125
+ ),
126
+
127
+ div({
128
+ class: "flex items-start gap-4 mt-2",
129
+ },
130
+ a ({
131
+ class: "px-4 py-2 rounded-md bg-red-400 text-black font-semibold relative group hover:scale-[1.05] duration-200",
132
+ href: "https://elegance.js.org/",
133
+ target: "_blank",
134
+ },
135
+ "Documentation",
136
+
137
+ div ({
138
+ class: "blur-[50px] absolute group-hover:bg-red-400 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
139
+ "aria-hidden": "true",
140
+ }),
141
+ ),
142
+
143
+ button ({
144
+ class: "hover:cursor-pointer px-4 py-2 rounded-md bg-zinc-200 text-black font-semibold relative group hover:scale-[1.05] duration-200",
145
+ /*
146
+ Normally, element attributes can only be a string, number or boolean.
147
+
148
+ However, exceptions are made for *object attributes*.
149
+ These are special values that usually perform client-side actions.
150
+
151
+ Take the below for example.
152
+ The eventListener() takes in a dependency array, and a callback function.
153
+
154
+ It then returns an ObjectAttribute of type EVENT_LISTENER.
155
+
156
+ The elegance-compiler, when it sees this, packs up the callback function and state references,
157
+ and ships it to the page.
75
158
 
76
- counter.signal();
77
- },
159
+ The page, when it loads, then binds the eventListener callback to the corresponding event (in this case, "onclick").
160
+
161
+ ObjectAttributes do not show up in HTML!
162
+ */
163
+ onClick: eventListener(
164
+ [counter],
165
+ (_, counter) => {
166
+ counter.value++;
167
+
168
+ counter.signal();
169
+ },
170
+ ),
171
+
172
+ /*
173
+ This is another ObjectAttribute, just like eventListener
174
+ It takes in an array of state it should watch,
175
+ and when that state calls its state.signal() method,
176
+
177
+ The observer calls it's callback function with the new values,
178
+ and whatever is returned, is the new value of the property.
179
+
180
+ So in this instance, whenever counter.signal() is called
181
+ this observer displays Counter: VALUE, and makes it the
182
+ innerText of this button.
183
+ */
184
+ innerText: observe([counter], (counter) => \`Counter: \${counter}\`),
185
+ },
186
+ div ({
187
+ class: "blur-[50px] absolute group-hover:bg-zinc-200 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
188
+ "aria-hidden": "true",
189
+ }),
78
190
  ),
79
-
80
- innerText: observe([counter], (counter) => \`Counter: \${counter}\`),
81
- },
82
- div ({
83
- class: "blur-[50px] absolute group-hover:bg-zinc-200 inset-0 bg-transparent duration-200 pointer-events-none -z-10",
84
- "aria-hidden": "true",
85
- }),
86
- ),
87
- )
88
- );
89
-
191
+ )
192
+ );
193
+ }
194
+
195
+ /*
196
+ This is the metadata of the page.
197
+ Aka the <head> element which gets served alongside the page content.
198
+
199
+ It *must* be a function that resolves into a head() result.
200
+
201
+ In it, you should do things like link your stylesheets,
202
+ set page titles, all that goodness.
203
+ */
90
204
  export const metadata = () => head ({},
91
205
  link ({
92
206
  rel: "stylesheet",
@@ -94,7 +208,7 @@ export const metadata = () => head ({},
94
208
  }),
95
209
 
96
210
  title ({},
97
- "Elegance.JS"
211
+ "Elegance.JS Demo"
98
212
  ),
99
213
  )
100
214
 
@@ -114,6 +228,9 @@ const tsconfigContent = JSON.stringify({
114
228
  },
115
229
  include: ["pages/**/*", "env.d.ts"],
116
230
  exclude: ["node_modules"],
231
+ paths: {
232
+ "@/": ["./"],
233
+ }
117
234
  }, null, 2);
118
235
 
119
236
  const indexCssContent = `