|
1 | | -import { Component, h, state, putContext, context } from "../index.js"; |
| 1 | +import { Component, h, state, putContext, context, app } from "../index.js"; |
2 | 2 |
|
3 | 3 | // 1. Fragments Example |
4 | 4 | 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 | + } |
14 | 14 | } |
15 | 15 |
|
16 | 16 | // 2. State Management Example |
17 | 17 | const counter = state(0); |
18 | 18 |
|
19 | 19 | 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 | + } |
32 | 29 | } |
33 | 30 |
|
34 | 31 | // 3. Context Example |
35 | 32 | // Define a context (normally this would be in a separate file) |
36 | 33 | class ThemeContext { |
37 | | - constructor() { |
38 | | - this.theme = state("light"); |
39 | | - } |
| 34 | + constructor() { |
| 35 | + this.theme = state("light"); |
| 36 | + } |
40 | 37 |
|
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 | + } |
44 | 41 | } |
45 | 42 |
|
46 | 43 | // Register the context |
47 | 44 | // putContext(referenceName, qualifiedName) |
48 | 45 | // Since we are not loading from a file here, we just register it manually for this example |
49 | 46 | // In a real app, you might use: putContext("Theme", "contexts.ThemeContext") |
50 | 47 | 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); |
52 | 50 |
|
53 | 51 | 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 | + } |
59 | 57 |
|
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 | + } |
75 | 74 | } |
76 | 75 |
|
77 | 76 | export { FragmentComponent, CounterComponent, ThemedComponent }; |
0 commit comments