Rook
РусскийRU

Web components

@html-tag before comp makes a component a web component as well: a custom element that works in plain HTML, in other frameworks and from JS, like <video>. The tag is the name of the component in kebab case, @html-tag comp mainPage() gives <main-page>, or the one in the decorator: @html-tag("app-page") comp MainPage(). HTML wants a hyphen in the name of a custom element, so @html-tag comp Page() is a compile error.

// @html-tag turns a component into a web component: <fruit-badge> works in any HTML,
// even without Rook. Its markup is in a shadow root: the styles of the page do
// not get in, except inherited ones like color and CSS variables like --accent-soft
const STYLE = `
    span { padding: 2px 10px; border-radius: 999px; background: var(--accent-soft) }
    .ripe { font-weight: bold }
`

@html-tag("fruit-badge")
comp FruitBadge(name string, count number = 1, ripe bool) {
    return <>
        <style>{STYLE}</style>
        <span class={ripe ? "ripe" : ""}>{name}: {count}</span>
    </>
}

// From JS it is an element like any other. Attributes are text: count="3" becomes the
// number 3, and ripe is true because the attribute is there
const badge = document.createElement("fruit-badge")
badge.setAttribute("name", "Guava")
badge.setAttribute("count", "3")
badge.setAttribute("ripe", "")
document.body.append(badge)

// Markup sets the properties as they are, and the element follows the state
comp Basket() {
    state kiwis = 1
    return <p>
        <fruit-badge name="Kiwi" count={kiwis} />
        <button onClick={kiwis++}>One more kiwi</button>
    </p>
}

document.body.append(<Basket />)

If the board is added to a page built with another framework, expose it as a custom element and pass its task collection as a JavaScript property.

  • The markup goes into the element's shadow root when the element gets into the document. {children} is a <slot>: what is written between the tags shows up there.
  • Every property is a JS property of the element, and setting it updates the markup. Strings, numbers and bools also have attributes, converted by the type of the property: a string as it is, a number through Number(), a bool is true when the attribute is there. itemCount is the attribute item-count. Without the attribute the property has its default value.
  • Arrays, objects and functions are set only as JS properties: list.items = [...]. An element written in HTML has none of them yet, so such properties need a default value or a nullable type.
  • mount runs when the element is in the document, and what it defers runs when the element is removed. Moving the element keeps its markup and state.
  • The element is defined with customElements.define at the end of the module, when the names it uses have their values. Properties set before that are kept.
  • Properties cannot be named like those of HTMLElement that the element needs: id, style, hidden...
  • The playground on this page defines the elements again on every run, so their tags get a number: <fruit-badge-3>.