Elements
Build a login form
Section titled “Build a login form”Pass children to constructors in display order. A string child becomes text; keep an element.Text node when you need to update the same label later. Box groups blocks, Span groups inline content, and element.Br() inserts a hard line break (the constructor is Br, not BR).
status := element.Text("Enter a password")feedback := element.Box(status).Style(demo.Default.MutedText)
password := element.Input(""). WithName("password"). WithPassword(true). WithPlaceholder("Enter password"). Style(demo.Default.Input.Width(style.Cells(22)))
submit := element.Button("Sign in"). Style(demo.Default.PrimaryButton). Disabled(true)
password.OnChange(func(value string) { empty := value == "" submit.Disabled(empty) if empty { status.SetData("Enter a password") feedback.Style(demo.Default.MutedText) return } status.SetData("Ready to sign in") feedback.Style(style.S().Foreground(demo.Default.Colors.Accent))})
field := element.Box(element.Span("Password"), element.Br(), password)login := element.Form(field, submit, feedback).WithID("login")login.Style(demo.Default.Column())login.AddEventListener(event.EventSubmit, func(event.Event) { if !submit.IsDisabled() { status.SetData("✓ Signed in") feedback.Style(demo.Default.StatusSuccess) }})Use WithID for lookup, WithClass to identify a group, and WithTabIndex to control keyboard navigation. For other attributes use SetAttribute(name, value), for example login.SetAttribute("data-screen", "auth"). Disable a control with Disabled(true) rather than merely changing its color. A password mask changes display, not the value delivered to the change handler; use WithMask for a custom mask rune. Do not log password values.
| Need | Constructor and usage |
|---|---|
| Edit text | Input(initialValue) for a single line; TextArea(initialValue) for multiple lines. |
| Toggle a setting | Checkbox(false) starts unchecked; its change handler receives a boolean. |
| Pick one value | Put Radio(value) children in a RadioGroup, or pass Option(label, value) children to Select. The group or select reports the selected value. |
| Submit related values | Place named controls inside Form. Values() reads submission data; SetValues() writes named controls in place. |
| Reset controls | form.Reset() or element.Button("Reset").Type("reset") restores control defaults unless a reset listener calls PreventDefault(). |
| Show a sequence | Put LI children in UL or OL; let layout draw the markers. |
| Show tabular data | Put TR rows inside THead, TBody, or TFoot. Use TD for cells, including header cells: there is no separate element.TH constructor. |
| Add transient UI | Overlay(anchor, children...) positions content relative to a target; Dialog(children...) is for modal interaction with trapped focus. |
Connect and disconnect
Section titled “Connect and disconnect”Register OnConnect(func() { ... }) to run setup each time an element joins a live document, including when it is reattached. Use OnDisconnect(func() { ... }) for cleanup just before it leaves. Both hooks run synchronously, in registration order, while IsConnected() is true. Adding a child to a detached tree does not run its connect hooks until the tree is mounted.
Label a control
Section titled “Label a control”Wrap a control and its caption in element.Label(element.Checkbox(false), " Send release notes"). The label is inline by default and targets its first labelable descendant. To put the caption elsewhere, give the control an ID, such as element.Input("").WithID("name"), and use element.Label("Your name").For("name") in the same document. An explicit ID that is missing or names a non-control leaves the label unassociated.
Try both associations in the same form:
form := element.Box( element.Label(element.Checkbox(false), " Send release notes"), element.Label("Your name").For("name"), element.Input(""). WithID("name"). WithPlaceholder("Type here"). Style(demo.Default.Input),)Clicking a caption focuses its input or text area, toggles a checkbox, selects a radio, presses a button, or opens a select. Disabled controls are not activated.
Choose a notification preference
Section titled “Choose a notification preference”Use a checkbox for an independent yes/no setting. Use a radio group when the alternatives should stay visible, or a select when space is tight. A checkbox reports a bool; the other two report a string. The select reports the option’s value, not its display text.
emailOn := falsefreq := "daily"reg := "eu"
summary := element.Text("Email: off · daily · eu")refresh := func() { state := "off" if emailOn { state = "on" } summary.SetData(fmt.Sprintf("Email: %s · %s · %s", state, freq, reg))}
root := element.Box( element.Label( element.Checkbox(false).OnChange(func(on bool) { emailOn = on refresh() }), " Email notifications", ), element.RadioGroup( element.Label(element.Radio("daily"), " Daily"), element.Label(element.Radio("weekly"), " Weekly"), ).DefaultValue("daily").OnChange(func(v string) { freq = v refresh() }), element.Label( "Region: ", element.Select( element.Option("Europe", "eu"), element.Option("North America", "na"), ).OnChange(func(v string) { reg = v refresh() }), ), element.Box(summary).Style(demo.Default.MutedText),).Style(demo.Default.Column())Present a sequence or compare rows
Section titled “Present a sequence or compare rows”Use an unordered list for independent items and an ordered list when the order matters. Supply the text, not the marker:
requirements := element.UL(element.LI("Go toolchain"), element.LI("Git"))steps := element.OL(element.LI("Build the binary"), element.LI("Run the tests"))release := element.Box("Requirements", requirements, "Release steps", steps)For a table, group rows by purpose and use TD even in the header. Here one platform owns two jobs. The second body row needs only a job cell because the platform cell already occupies its first column.
jobs := element.Table( element.THead(element.TR(element.TD("Platform"), element.TD("Job"))), element.TBody( element.TR(element.TD("Linux").SetRowSpan(2), element.TD("Build")), element.TR(element.TD("Test")), ), element.TFoot(element.TR(element.TD("2 jobs").SetColSpan(2))),)Open a hint or a modal dialog
Section titled “Open a hint or a modal dialog”Attach an Overlay to show it and detach it to dismiss it. The anchor determines position, not parentage. This hint prefers the space below its button; WithFlip(true) lets it move above when it would overflow. If neither side fits, layout chooses the side with more room.
func helpButton() *element.BoxElement { button := element.Button("Help") root := element.Box(button) hint := element.Overlay(button, "Press Tab to move between fields"). WithPlacement(geom.PlacementBottom).WithFlip(true) button.OnPress(func() { if hint.IsConnected() { root.RemoveChild(hint) } else { root.AddChild(hint) } }) return root}A Dialog covers the viewport and traps keyboard focus. Connecting it registers it in the top layer and opens its focus scope; disconnecting it undoes both. Use a dismissal action inside the dialog:
open := element.Button("About").Style(demo.Default.Button)root := element.Box(open).Style(demo.Default.Column())closeButton := element.Button("Close").Style(demo.Default.PrimaryButton)
panel := element.Box( element.Box("Release monitor"), closeButton,).Style( style.S(). Background(demo.Default.Colors.CardBg). Padding(1, 2),)
dialog := element.Dialog(panel).Style( style.S().Background(demo.Default.Colors.CanvasBg),)
open.OnPress(func() { root.AddChild(dialog) })closeButton.OnPress(func() { root.RemoveChild(dialog) })For an ordinary, mounted element, manage top-layer membership directly. Showing an existing entry updates its z-index; hiding it removes the entry, not the DOM node. This is not a substitute for disconnecting a modal dialog.
func pinNotice(doc dom.Document, notice dom.Element) func() { doc.ShowOverlay(notice, 10) return func() { doc.HideOverlay(notice) }}See Events and focus to connect handlers and Layout to size the tree.