Skip to content

Commit 7320d68

Browse files
committed
initiated modular openscript
1 parent 997316f commit 7320d68

7 files changed

Lines changed: 451 additions & 432 deletions

File tree

examples/advanced-features.js

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,76 @@
1-
import { Component, h, state, putContext, context } from "../index.js";
1+
import { Component, h, state, putContext, context, app } from "../index.js";
22

33
// 1. Fragments Example
44
class FragmentComponent extends Component {
5-
render(...args) {
6-
// h.$ or h._ creates a document fragment
7-
// This allows returning multiple elements without a parent wrapper
8-
return h.$(
9-
h.h3("Fragment Header"),
10-
h.p("This content is inside a fragment."),
11-
h.p("No extra div wrapper is added to the DOM.")
12-
);
13-
}
5+
render(...args) {
6+
// h.$ or h._ creates a document fragment
7+
// This allows returning multiple elements without a parent wrapper
8+
return h.$(
9+
h.h3("Fragment Header"),
10+
h.p("This content is inside a fragment."),
11+
h.p("No extra div wrapper is added to the DOM.")
12+
);
13+
}
1414
}
1515

1616
// 2. State Management Example
1717
const counter = state(0);
1818

1919
class CounterComponent extends Component {
20-
render(...args) {
21-
// Pass the state to the component to auto-subscribe
22-
// The component will re-render when 'counter' changes
23-
return h.div(
24-
h.h3(`Count: ${counter.value}`),
25-
h.button(
26-
{ onclick: () => counter.value++ },
27-
"Increment"
28-
),
29-
...args
30-
);
31-
}
20+
render(...args) {
21+
// Pass the state to the component to auto-subscribe
22+
// The component will re-render when 'counter' changes
23+
return h.div(
24+
h.h3(`Count: ${counter.value}`),
25+
h.button({ onclick: () => counter.value++ }, "Increment"),
26+
...args
27+
);
28+
}
3229
}
3330

3431
// 3. Context Example
3532
// Define a context (normally this would be in a separate file)
3633
class ThemeContext {
37-
constructor() {
38-
this.theme = state("light");
39-
}
34+
constructor() {
35+
this.theme = state("light");
36+
}
4037

41-
toggle() {
42-
this.theme.value = this.theme.value === "light" ? "dark" : "light";
43-
}
38+
toggle() {
39+
this.theme.value = this.theme.value === "light" ? "dark" : "light";
40+
}
4441
}
4542

4643
// Register the context
4744
// putContext(referenceName, qualifiedName)
4845
// Since we are not loading from a file here, we just register it manually for this example
4946
// In a real app, you might use: putContext("Theme", "contexts.ThemeContext")
5047
const themeCtx = new ThemeContext();
51-
context("Theme", themeCtx); // Manually putting it in the provider for this example
48+
// Manually putting it in the provider for this example using the IoC container
49+
app("contextProvider").map.set("Theme", themeCtx);
5250

5351
class ThemedComponent extends Component {
54-
constructor() {
55-
super();
56-
// Access the context
57-
this.themeContext = context("Theme");
58-
}
52+
constructor() {
53+
super();
54+
// Access the context
55+
this.themeContext = context("Theme");
56+
}
5957

60-
render(...args) {
61-
const currentTheme = this.themeContext.theme.value;
62-
63-
return h.div(
64-
{
65-
style: `background-color: ${currentTheme === 'light' ? '#fff' : '#333'}; color: ${currentTheme === 'light' ? '#000' : '#fff'}; padding: 20px;`
66-
},
67-
h.h3(`Current Theme: ${currentTheme}`),
68-
h.button(
69-
{ onclick: () => this.themeContext.toggle() },
70-
"Toggle Theme"
71-
),
72-
...args
73-
);
74-
}
58+
render(...args) {
59+
const currentTheme = this.themeContext.theme.value;
60+
61+
return h.div(
62+
{
63+
style: `background-color: ${
64+
currentTheme === "light" ? "#fff" : "#333"
65+
}; color: ${
66+
currentTheme === "light" ? "#000" : "#fff"
67+
}; padding: 20px;`,
68+
},
69+
h.h3(`Current Theme: ${currentTheme}`),
70+
h.button({ onclick: () => this.themeContext.toggle() }, "Toggle Theme"),
71+
...args
72+
);
73+
}
7574
}
7675

7776
export { FragmentComponent, CounterComponent, ThemedComponent };

examples/basic-usage.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1-
import { Runner, Component, h, State } from "../index.js";
1+
import { app, State, Component, ojs } from "openscriptjs";
22

33
// Define a State
44
const counter = State.state(0);
5+
const h = app("h");
56

67
// Define a Component
78
class CounterComponent extends Component {
8-
render(...args) {
9-
return h.div(
10-
h.h1(`Counter: ${counter.value}`),
11-
h.button(
12-
{
13-
onclick: () => counter.value++,
14-
},
15-
"Increment"
16-
),
17-
...args
18-
);
19-
}
9+
render(counter, ...args) {
10+
return h.div(
11+
h.h1(`Counter: ${counter.value}`),
12+
h.button(
13+
{
14+
onclick: this.method("increment"),
15+
},
16+
"Increment"
17+
),
18+
...args
19+
);
20+
}
21+
22+
increment() {
23+
counter.value++;
24+
}
2025
}
2126

2227
// Mount the Component
23-
new Runner().run(CounterComponent);
28+
ojs(CounterComponent);

examples/component-example.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
import { Component, h, broker } from "../index.js";
1+
import { Component, h, app } from "../index.js";
22

33
class SenderComponent extends Component {
4-
render(...args) {
5-
return h.button(
6-
{
7-
onclick: () => {
8-
broker.emit("message", "Hello from Sender!");
9-
},
10-
},
11-
"Send Message",
12-
...args
13-
);
14-
}
4+
render(...args) {
5+
return h.button(
6+
{
7+
onclick: () => {
8+
app("broker").emit("message", "Hello from Sender!");
9+
},
10+
},
11+
"Send Message",
12+
...args
13+
);
14+
}
1515
}
1616

1717
class ReceiverComponent extends Component {
18-
constructor() {
19-
super();
20-
this.message = "Waiting...";
21-
}
18+
constructor() {
19+
super();
20+
this.message = "Waiting...";
21+
}
2222

23-
render(...args) {
24-
return h.div(`Received: ${this.message}`, ...args);
25-
}
23+
render(...args) {
24+
return h.div(`Received: ${this.message}`, ...args);
25+
}
2626
}

examples/context-state-example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Demonstrates best practice: defining states in contexts and passing to components
44
*/
55

6-
import { Component, h, context, putContext, state, dom } from "../index.js";
6+
import { Component, h, context, putContext, state, dom } from "openscriptjs";
77

88
// ============================================
99
// 1. INITIALIZE CONTEXTS AND STATES

0 commit comments

Comments
 (0)