The Loup Web Book — contents
  1. Chapter 1: Getting Started
  2. Chapter 2: The Web Modes
  3. Chapter 3: Variables and Data Types
  4. Chapter 4: Functions
  5. Chapter 5: Control Flow
  6. Chapter 6: Collections
  7. Chapter 7: Structs
  8. Chapter 8: Enums and Pattern Matching
  9. Chapter 9: Classes and Objects
  10. Chapter 10: Interfaces
  11. Chapter 11: Generics
  12. Chapter 12: Error Handling
  13. Chapter 13: Modules and Packages
  14. Chapter 14: Template Fences — HTML and CSS
  15. Chapter 15: Async and Concurrency
  16. Chapter 16: Reactive UI
  17. Chapter 17: Web Components
  18. Chapter 18: DOM, Storage, Workers, and JavaScript Interop
  19. Chapter 19: SSR, Hydration, and Deployment
  20. Chapter 20: Memory Safety in Web Apps

Chapter 17: Web Components

⚠ Partly planned. The first section, on web.components, runs today in the browser runtime (Chapter 1, "Running Loup in the browser"). The rest of the chapter describes the design in the Language Reference (§2-4.10, with §1-16.7.1 for CSS and §1-20.6 for bootstrap registration). The toolchain does not implement that design yet, so its listings are design sketches and are not verified.

The Reference specifies W3C Custom Elements v1 support: a class annotated @webcomponent("tag-name") becomes a custom element that any HTML page, framework or Loup fence can use. The browser runtime registers it with customElements.define(), renders it into a shadow root and keeps its observed attributes in sync.

What runs today: web.components

The browser runtime defines custom elements with a function instead of a class. defineComponent(tag, observedAttrs, render) registers the tag with customElements.define(); each element gets an open shadow root, and render(host, shadow) runs when the element is connected and again whenever one of the observed attributes (a comma-separated list) changes:

Fragment — not a complete program
@mode(web.scripted)
from web.dom import (Element);
from web.components import (defineComponent, attrOr, escHtml);

let STYLE = "<style>span { padding: 2px 10px; border-radius: 999px; }"
    + " .info { background: #dbeafe; } .warn { background: #fef3c7; }</style>";

defineComponent("hello-badge", "name,tone", func(host: Element, shadow: Element): void {
    let name = escHtml(attrOr(host, "name", "world"));
    let tone = escHtml(attrOr(host, "tone", "info"));
    shadow.setHtml(STYLE + "<span class=\"" + tone + "\">Hello, " + name + "!</span>");
});

Listing 17-1: A custom element defined with web.components (runs in the browser)

With <hello-badge name="Ada"></hello-badge> and <hello-badge name="Grace" tone="warn"></hello-badge> on the page, the two shadow roots render Hello, Ada! in the info style and Hello, Grace! in the warn style. Setting the first element's name attribute from JavaScript to Ada <b>Lovelace</b> re-renders it as Hello, Ada &lt;b&gt;Lovelace&lt;/b&gt;!: the markup is shown as text, because the render function passes every attribute through escHtml before it builds HTML. Nothing escapes for you here; setHtml takes the string as it is.

The module's other helpers are boolAttr(el, name) (true when the attribute is present and not "false"), shadowRoot(host), emit(host, name, detail) (dispatches a bubbling, composed CustomEvent with a string detail) and eventDetail(event). The WebWorks component library in the Loup source tree (source/testsites/webworks/, about 200 ww-* elements) is written this way.

The planned @webcomponent classes are not implemented. The compiler does not know the annotation: it accepts the class, ignores @webcomponent, and warns after the program's output:

main.lpweb:3:1: warning: Unknown annotation '@webcomponent'

Declaring a component

Fragment — not a complete program
@mode(web.compiled)

from ui import (signal, View, Signal);

@webcomponent("my-counter")
class CounterElement implements WebComponentLifecycle {
    @attribute var label: string = "Count";
    @attribute var step: int = 1;

    let count: Signal[int] = signal(0);

    override func attributeChanged(name: string, oldValue: string?, newValue: string?): void {
        # the @attribute field already holds the new value
    }

    func render(): View {
        return ```(ui)
        <Column spacing={$ 8 $}>
          <Text>{$ this.label $}: {$ this.count.get() $}</Text>
          <Button onclick={$ func(): void { this.count.update(func(n: int): int => n + this.step); } $}>
            <Text>+{$ this.step $}</Text>
          </Button>
          <Slot />
        </Column>
        ```;
    }
}

Listing 17-2: A counter custom element (design sketch)

A page then writes <my-counter label="Score" step="5"><span>…</span></my-counter>. The rules (§2-4.10.2):

  • The tag is a compile-time string literal containing a hyphen and only ASCII lowercase letters, digits, -, . and _. Invalid or duplicate tags are compile-time errors.
  • The class must declare func render(): View, which re-renders when a signal it reads changes.
  • @webcomponent is valid only in web.scripted and web.compiled files on the browser target; elsewhere it is E_WEBCOMPONENT_NOT_WEB.

Attributes and lifecycle

@attribute marks an observed attribute (§2-4.10.3). On a var field the type is string, int, float or bool: numbers are parsed (a malformed value keeps the default and logs a warning), and a bool is true when the attribute is present. Attribute names are the member names lowercased, so myLabel observes mylabel; write @attribute("my-label") to choose another. A get/set property pair may also be an attribute; one with no stored backing field is E_ATTRIBUTE_NO_BACKING_FIELD, and @attribute outside a @webcomponent class is E_ATTRIBUTE_NOT_WEBCOMPONENT.

WebComponentLifecycle (core §1-11.2) declares connected(), disconnected(), adopted() and attributeChanged(name, oldValue, newValue), each with a default no-op body, so a class overrides only what it needs. Register outside listeners in connected() and remove them in disconnected().

Shadow DOM, slots and CSS

Each element renders into an open shadow root, available as this.shadowRoot (type ShadowRoot from web.dom). <Slot /> in a ui fence or <slot> in an html fence marks where light-DOM children appear; a slot anywhere but a @webcomponent's render() is E_SLOT_NOT_WEBCOMPONENT (§2-4.10.4).

A css fence evaluated in render() is attached to the shadow root with Constructable Stylesheets, and the class-name mangling that css fences normally get (§1-16.7.1) is turned off, since the shadow boundary already isolates names:

Fragment — not a complete program
@mode(web.compiled)

from ui import (View);

@webcomponent("status-badge")
class StatusBadge implements WebComponentLifecycle {
    @attribute var tone: string = "info";

    func render(): View {
        let styles = ```(css)
        .badge { background: navy; color: white; padding: 4px 8px; }
        ```;
        return ```(html):(none)
        <span class="{$ styles.badge $}" data-tone="{$ this.tone $}"><slot></slot></span>
        ```;
    }
}

Listing 17-3: Component-scoped CSS (design sketch)

Today a css fence is an ordinary template whose render({}) returns its text unchanged; there is no StyleSheet value or mangling yet (Chapter 14).

Using components in fences

A hyphenated tag in a fence is looked up in the ComponentRegistry named by components= (§2-4.10.6); a @webcomponent match maps the tag's attributes to its @attribute fields:

Fragment — not a complete program
@mode(web.compiled)

from ui import (ComponentRegistry, View);

pub func Scoreboard(): View {
    let registry = ComponentRegistry.of({"my-counter": CounterElement});
    return ```(html):(components=registry)
    <section class="scores">
      <my-counter label="Home" step={$ 1 $}></my-counter>
      <my-counter label="Away" step={$ 1 $}></my-counter>
    </section>
    ```;
}

Listing 17-4: Using a registered element (design sketch)

The runtime registers every @webcomponent class during bootstrap, before mount: instantiate → initialize → register custom elements → wire events → hydrate → mount (§1-20.6). A parent passes data down through attributes, as above. The Reference does not specify how a child sends events up; the this.dispatch and on:event syntax of earlier drafts of this book is not in it.

Summary

  • In the browser runtime, web.components.defineComponent(tag, observedAttrs, render) defines a custom element with an open shadow root; escape attribute values with escHtml before building HTML.
  • The Reference's @webcomponent classes (§2-4.10) are planned; the annotation is currently unknown and ignored with a warning.
  • @webcomponent("tag-name") needs render(): View and is valid only in web modes on the browser target.
  • @attribute fields (string, int, float, bool) become observed attributes; WebComponentLifecycle methods all default to no-ops.
  • Elements render into an open shadow root; <Slot /> projects children; css in render() attaches to the shadow root unmangled.
  • A ComponentRegistry makes elements usable in fences; registration happens during bootstrap, before mount.