@builder/web/page

@builder/web/page reads live browser document and viewport state through a binding-backed package. It also offers opt-in navigation synchronisation.

Choose the explanation level: Browser page state

Browser page state

@builder/web/page reads browser state each time you call a getter. Import it explicitly in a .moth file.

@builder/web/page

address = page.url()
listed = page.sync_navigation("nav") catch then false
io.line([: [address] [listed]])

sync_navigation is optional. It needs a navigation root with the hooks described in @builder/web/page advanced. Call it again after History API changes.

page.url() reads the live browser address at runtime. In contrast, @builder/web/site's site.url builds static links at compile time.

@builder/web/page belongs to the Builder origin family with ExternalBinding backing. Binding-backed packages have no source facade. See Package origins and backing for the package model. It requires an explicit @builder/web/page dependency and isn't in the implicit scope of .mtf files. It supports HTML-JS only. HTML-Wasm rejects reachable calls.

Browser snapshots

Every getter reads the browser on each call. These are synchronous snapshots, not cached build values or subscriptions.

  • url() -> String: full browser address
  • path() -> String: location pathname
  • origin() -> String: location origin
  • query() -> String: location search, including ? when present
  • fragment() -> String: location hash, including # when present
  • title() -> String: document title
  • viewport_width() -> I32: viewport width in CSS pixels
  • viewport_height() -> I32: viewport height in CSS pixels
  • pixel_ratio() -> F64: device pixel ratio
  • scroll_x() -> F64: horizontal scroll position
  • scroll_y() -> F64: vertical scroll position
  • is_visible() -> Bool: whether the document is visible
  • has_focus() -> Bool: whether the document has focus
  • prefers_dark() -> Bool: whether the browser prefers a dark colour scheme
  • prefers_reduced_motion() -> Bool: whether the browser prefers reduced motion

page.url() reads the live browser address at runtime. For static links built at compile time use @builder/web/site's site.url instead.

Navigation synchronisation

sync_navigation(root_id String) -> Bool, Error! updates active links and a breadcrumb under the identified root. It requires these descendants:

  • data-page-home on a home anchor supplying the first breadcrumb link
  • data-page-links on a navigation element containing candidate anchors
  • data-page-current on an initially empty breadcrumb container

It returns true for an exact link or home match and false for an unlisted route. An empty root ID produces a typed invalid-argument error. A missing root or required hook produces a typed resource-not-found error.

Matching compares link paths with the current path and ignores query and fragment. Links resolve against the document base URL. /guide/index.html, /guide/ and /guide are the same route. Percent escapes and letter case are kept, so /a%2Fb/ never matches /a/b/ and /Guide/ never matches /guide/. Links that are missing an href or unparsable are skipped. So are links whose scheme, host or port differs from the current page. Pages opened from file: URLs therefore still match their own file: links but never mailto: links.

Each call first clears aria-current from the home anchor and every candidate link. On the home route it marks the home anchor, empties the breadcrumb container and hides it. On any other route it marks the first link whose path matches. The breadcrumb then lists each linked ancestor route from shallowest to deepest, followed by the current page label. An ancestor must end at a /, so /docs/pack is never an ancestor of /docs/packages. Ancestors whose label repeats the home or final label are dropped. The final label is the matched link's text. For an unlisted page it is the first <h1> or else the document title.

The helper is idempotent, installs no listeners and doesn't navigate. Call it again after History API changes.