# santoku-web: component authoring
> rules for the author of a santoku.web.app component: connect and disconnect, attributes, markup, focus, slots and parts, modal dialogs, styles and main-thread work
This file is one tab of the santoku reference, at https://santoku.dev/santoku-web/components/llms.txt. https://santoku.dev/santoku-web/components is the same tab in a browser, https://santoku.dev/llms.txt indexes every tab, and https://santoku.dev/llms-small.txt summarizes them all on one page.
A santoku.web.app component is a custom element with a shadow root. The library runs its setup, teardown and rendering. These cards give the rules that the author of the spec and the template follows for attributes, markup, focus and styles.
- The component API is on the santoku-web tab: https://santoku.dev/santoku-web#santoku-web--components-with-app-define
- santoku-web's behaviour uses only what is Baseline widely available, plus the editor's Selection.getComposedRanges. A component needs no feature detection, no polyfill and no fallback.
- The later examples reuse app, dom, async and tpl from the first card. async is the app's santoku.async module: https://santoku.dev/santoku#santoku--santoku-async
## Lifecycle
### Connect and disconnect
spec.state and then spec.start run on each connect, and the c.on_stop steps run on each disconnect.
- A move in the page is a disconnect and a connect. A moved element keeps its shadow root and gets a new state table.
- Build the state table from attrs and scope in spec.state. A value that changed after the connect, such as text the user typed, is gone after a move.
- Since santoku-web 9.0.0, on disconnect every task the component started (start, route, actions, and the tasks they spawned, debounced calls included) stops for good. It never resumes, and its sleeps are cleared.
- Since santoku-web 11.0.0, the c.on_stop steps then run in order as one task, while the view is still in the root. A step can await, and c still works. When the task ends, the library releases its own listeners, the view and a scope without a name.
- Register each stop function that dom.listen returns with c.on_stop instead of keeping it on s. spec.stop is gone, and define raises when a spec sets it.
- Since santoku-web 10.0.0, the disconnect also aborts every fetch those tasks await, including those inside a latest call, so no request needs a c.on_stop.
- Define each tag once. Since santoku-web 6.0.0, a second define of one tag raises "define: TAG is already defined".
```lua
local app = require("santoku.web.app")
local dom = require("santoku.web.dom")
local async = require("myapp.async")
local tpl = require("myapp.templates")
app.define({
tag = "job-clock",
template = tpl.job_clock,
state = function (attrs)
return { zone = attrs.zone, width = 0, late = false }
end,
start = function (s, c)
c.on_stop(dom.listen("window", "resize", function ()
s.width = dom.read({ "scroll" })[3]
c.commit()
end))
async.sleep(60000)
s.late = true
end,
})
```
### Attributes at connect
spec.state gets the element's attributes as they are at connect. No hook runs when an attribute changes after that.
- Set every attribute before the element enters the page.
| Attributes on the element | attrs |
|---|---|
| context-path="/ctx" | context_path = "/ctx", with each - turned to _ |
| data-list-style="grid" | list_style = "grid", with the leading data- dropped |
| sales="", or a naked attribute such as compact | sales = "" and compact = "" |
| q="plain" data-q="chef" | q = "chef", since data-q wins over q |
- An absent attribute reads as nil. So test presence with attrs.compact ~= nil, since attrs.compact or default keeps "".
- The attributes arrive with the connect event, so spec.state reads them with no crossing of its own.
- A parent template that changes an attribute of a nested component after connect changes nothing in that component. Components that show one state share a scope or call each other's c.commit(): https://santoku.dev/santoku-web#santoku-web--components-with-app-define
- Every value is a string. Convert a number with tonumber(attrs.limit).
- Write a boolean attribute naked, such as compact. compact="false" is still present, so attrs.compact ~= nil is true for it.
- Don't give a component an attribute of its own with a global attribute's name, such as title, hidden, lang, dir, class, id, style, slot, part or tabindex. The browser acts on those names.
- Use a data- name for such an attribute: data-title reads as attrs.title.
- Since santoku-web 7.0.0, an attribute named for one of the spec's keys seeds the scope and wins over the URL: https://santoku.dev/santoku-web#santoku-web--components-with-app-define
- A key named like a global attribute reads that attribute, so name such a key another way.
Page:
```text
```
Client:
```lua
app.define({
tag = "job-list",
template = tpl.job_list,
state = function (attrs)
return {
title = attrs.title,
limit = tonumber(attrs.limit),
compact = attrs.compact ~= nil,
}
end,
})
```
### The open shadow root
Every component renders into an open shadow root. Page code and a browser test reach the root as el.shadowRoot. Code in the component uses c.root, the root's dom handle.
- Before santoku-web 6.0.0 the root was closed, and el.shadowRoot was nil.
- The root still scopes ids, selectors and styles. A page script can read and change what's inside.
- A root runs data-action entries for its own tree only. When a nested component runs no action for an event, the outer root gets the event, and ev.target is a handle for that component's host.
- santoku-cdp's wait, type and click selectors call document.querySelector, which doesn't look inside a shadow root. Go through the host, as the browser test below does.
Since santoku-web 6.0.0
Page code:
```lua
local dom = require("santoku.web.dom")
local js = require("santoku.web.js")
local root = dom.root(js.document:querySelector("job-list").shadowRoot)
dom.text({ root, "h2" }, "Jobs")
dom.flush()
```
Browser test:
```lua
local page = require("santoku.cdp.page")
local s = page.open({ port = 9333 })
page.navigate(s, "http://127.0.0.1:8080/")
local heading = "document.querySelector('job-list')?.shadowRoot?.querySelector('h2')?.textContent"
print(page.wait_until(s, heading, 10000))
s.close()
```
## Templates
### Markup in a template
Every component is its own shadow root, so an id is private to its template and every action needs an element that takes focus and keys.
- Put a click action on a