@@ -78,7 +78,7 @@ fn readme_counter() -> Unit {
7878 let mut count = 0
7979
8080 // Create count display element
81- let count_display : HTMLDivElement = document.create_element("div").into()
81+ let count_display : HTMLDivElement = document() .create_element("div").into()
8282 count_display
8383 ..set_attribute("id", "count-display")
8484 .set_attribute("style", "font-size: 3em; margin: 0.5em 0;")
@@ -89,15 +89,15 @@ fn readme_counter() -> Unit {
8989 }
9090
9191 // Create increment button — closures are accepted directly
92- let increment_btn = document.create_element("button")
92+ let increment_btn = document() .create_element("button")
9393 increment_btn.set_text_content("+")
9494 increment_btn.add_event_listener("click", fn(_event) {
9595 count = count + 1
9696 update_display()
9797 })
9898
9999 // Append to DOM
100- let app : Element = document.get_element_by_id("app")
100+ let app : Element = document() .get_element_by_id("app")
101101 app.append_child(count_display) |> ignore
102102 app.append_child(increment_btn) |> ignore
103103}
@@ -129,10 +129,10 @@ Demonstrates the Canvas 2D API with gradients, shapes, and text:
129129``` moonbit nocheck
130130///|
131131fn readme_canvas() -> Unit {
132- let canvas : HTMLCanvasElement = document.create_element("canvas").into()
132+ let canvas : HTMLCanvasElement = document() .create_element("canvas").into()
133133 canvas.set_width(800)
134134 canvas.set_height(500)
135- let app : Element = document.get_element_by_id("app")
135+ let app : Element = document() .get_element_by_id("app")
136136 app.append_child(canvas) |> ignore
137137
138138 // Get 2D rendering context
@@ -214,19 +214,19 @@ npx serve .
214214
215215### Global Objects
216216
217- The library provides direct access to browser global objects:
217+ The library provides zero-argument accessors for browser global objects:
218218
219219``` moonbit nocheck
220220///|
221221fn readme_globals() -> Unit {
222222 // Access the document object
223- let _ = document.get_element_by_id("my-id")
223+ let _ = document() .get_element_by_id("my-id")
224224
225225 // Access the window object
226- let _ = window.inner_width()
226+ let _ = window() .inner_width()
227227
228228 // Access the navigator object
229- let _ = navigator.user_agent()
229+ let _ = navigator() .user_agent()
230230}
231231```
232232
@@ -238,7 +238,7 @@ DOM elements are returned as generic `Element` types. Use `into()` to cast to sp
238238///|
239239fn readme_casting() -> Unit {
240240 // Create an element and cast to specific type
241- let canvas : HTMLCanvasElement = document.create_element("canvas").into()
241+ let canvas : HTMLCanvasElement = document() .create_element("canvas").into()
242242
243243 // Cast to access type-specific methods
244244 let _ctx : CanvasRenderingContext2D = canvas.get_context("2d").unwrap().into()
@@ -256,18 +256,18 @@ Methods that return a nullable interface type (e.g., `Element?`) have two varian
256256///|
257257fn readme_opt_methods() -> Unit {
258258 // Convenience: returns Element directly (panics if not found)
259- let app = document.get_element_by_id("app")
259+ let app = document() .get_element_by_id("app")
260260
261261 // Cast to a specific subtype with .into():
262- let canvas : HTMLCanvasElement = document
262+ let canvas : HTMLCanvasElement = document()
263263 .get_element_by_id("my-canvas")
264264 .into()
265265
266266 // Works on subtypes too (e.g., ShadowRoot inherits query_selector from trait):
267267 // shadow.query_selector("[data-ref=display]")
268268
269269 // _opt variant: returns Option for null-checking
270- match document.get_element_by_id_opt("maybe-missing") {
270+ match document() .get_element_by_id_opt("maybe-missing") {
271271 Some(el) => el.set_text_content("found")
272272 None => ()
273273 }
@@ -284,7 +284,7 @@ Event listeners and handlers accept closures directly:
284284``` moonbit nocheck
285285///|
286286fn readme_events() -> Unit {
287- let element = document.create_element("button")
287+ let element = document() .create_element("button")
288288
289289 // addEventListener with closure
290290 element.add_event_listener("click", fn(_event) { println("Clicked!") })
@@ -298,7 +298,7 @@ Use `JsPromise` to chain async operations like `fetch()`:
298298``` moonbit nocheck
299299///|
300300fn readme_promises() -> Unit {
301- window
301+ window()
302302 .fetch("https://api.example.com/data")
303303 .then(fn(response : Response) {
304304 response.text().then(fn(text : String) { Console::log([text]) }) |> ignore
@@ -313,7 +313,7 @@ On the JS backend, the `bikallem/webapi/js_promise` subpackage bridges `JsPromis
313313``` moonbit nocheck
314314///|
315315async fn readme_fetch_async(url : String) -> Unit {
316- let response : Response = @js_promise.to_async_promise(window.fetch(url)).wait()
316+ let response : Response = @js_promise.to_async_promise(window() .fetch(url)).wait()
317317 let text : String = @js_promise.to_async_promise(response.text()).wait()
318318 Console::log([text])
319319}
@@ -353,7 +353,7 @@ Most setter methods return `Unit`, enabling method chaining with `..` (use `.` f
353353``` moonbit nocheck
354354///|
355355fn readme_chaining() -> Unit {
356- let element = document.create_element("div")
356+ let element = document() .create_element("div")
357357 element..set_attribute("id", "my-element").set_attribute("class", "container")
358358 element.set_text_content("Hello!")
359359}
@@ -366,10 +366,10 @@ Many methods have optional parameters using MoonBit's `?` syntax:
366366``` moonbit nocheck
367367fn readme_optional() -> Unit {
368368 // With default options
369- let _ = document.create_element("div")
369+ let _ = document() .create_element("div")
370370
371371 // With explicit options
372- let _ = document.create_element(
372+ let _ = document() .create_element(
373373 "div",
374374 options=ElementCreationOptions::new(is="custom-div"),
375375 )
0 commit comments