Skip to content

Commit f32810b

Browse files
committed
refactor: use declarative <my-counter> tags in wc-counter HTML files
Move static markup (heading, custom element tags, event log) from programmatic DOM construction in MoonBit to declarative HTML. MoonBit now only registers the custom element and wires the event listener.
1 parent ff7fcc0 commit f32810b

3 files changed

Lines changed: 14 additions & 27 deletions

File tree

examples/wc-counter/main.mbt

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,9 @@ fn main {
6060
})
6161
})
6262

63-
// Build host page
64-
let app = doc.get_element_by_id("app").unwrap()
65-
66-
let heading = doc.create_element("h1")
67-
heading.set_text_content("Web Components Counter")
68-
69-
let counter1 = doc.create_element("my-counter")
70-
counter1.set_attribute("id", "counter-1")
71-
72-
let counter2 = doc.create_element("my-counter")
73-
counter2.set_attribute("id", "counter-2")
74-
75-
let log = doc.create_element("div")
76-
log
77-
..set_attribute("id", "event-log")
78-
.set_attribute(
79-
"style", "margin-top: 1em; padding: 0.5em; border: 1px solid #ddd; border-radius: 4px; font-family: monospace; min-height: 2em;",
80-
)
81-
log.set_text_content("Event log:")
82-
83-
app.append_child(heading) |> ignore
84-
app.append_child(counter1) |> ignore
85-
app.append_child(counter2) |> ignore
86-
app.append_child(log) |> ignore
87-
8863
// Listen for count-changed events (bubbles up through composed boundary)
64+
let app = doc.get_element_by_id("app").unwrap()
65+
let log = doc.get_element_by_id("event-log").unwrap()
8966
app.add_event_listener("count-changed", fn(event) {
9067
let ce : @webapi.CustomEvent = event.into()
9168
let detail = ce.detail()

examples/wc-counter/wc-counter.js.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
<title>WC Counter Example</title>
88
</head>
99
<body>
10-
<div id="app"></div>
10+
<div id="app">
11+
<h1>Web Components Counter</h1>
12+
<my-counter id="counter-1"></my-counter>
13+
<my-counter id="counter-2"></my-counter>
14+
<div id="event-log" style="margin-top: 1em; padding: 0.5em; border: 1px solid #ddd; border-radius: 4px; font-family: monospace; min-height: 2em;">Event log:</div>
15+
</div>
1116
<script type="module" src="../_build/js/release/build/wc-counter/wc-counter.js"></script>
1217
</body>
1318
</html>

examples/wc-counter/wc-counter.wasm.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
<title>WC Counter Example (wasm-gc)</title>
88
</head>
99
<body>
10-
<div id="app"></div>
10+
<div id="app">
11+
<h1>Web Components Counter</h1>
12+
<my-counter id="counter-1"></my-counter>
13+
<my-counter id="counter-2"></my-counter>
14+
<div id="event-log" style="margin-top: 1em; padding: 0.5em; border: 1px solid #ddd; border-radius: 4px; font-family: monospace; min-height: 2em;">Event log:</div>
15+
</div>
1116
<script type="module">
1217
import { wasmImportObject } from "../../src/webapi.mjs";
1318
const { instance } = await WebAssembly.instantiateStreaming(

0 commit comments

Comments
 (0)