feat(html): add aria-live helper
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-03 18:46:36 +00:00
parent 667965da19
commit 3d841efa12
4 changed files with 29 additions and 2 deletions

View file

@ -40,6 +40,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
- `AriaControls(node, ids...)`
- `AriaCurrent(node, current)`
- `AriaBusy(node, busy)`
- `AriaLive(node, live)`
- `Role(node, role)`
- `Lang(node, locale)`
- `Dir(node, direction)`

View file

@ -39,7 +39,7 @@ This builds a Header-Content-Footer layout with semantic HTML elements (`<header
| Path | Purpose |
|------|---------|
| `node.go` | `Node` interface and all node types: `El`, `Text`, `Raw`, `If`, `Unless`, `Each`, `EachSeq`, `Switch`, `Entitled`, plus `AriaLabel`, `Alt`/`AltText`, `AriaBusy`, `AriaDisabled`, `AriaPressed`, `AriaSelected`, `TabIndex`, and `AutoFocus` helpers |
| `node.go` | `Node` interface and all node types: `El`, `Text`, `Raw`, `If`, `Unless`, `Each`, `EachSeq`, `Switch`, `Entitled`, plus `AriaLabel`, `Alt`/`AltText`, `AriaBusy`, `AriaLive`, `AriaDisabled`, `AriaPressed`, `AriaSelected`, `TabIndex`, and `AutoFocus` helpers |
| `layout.go` | HLCRF compositor with semantic HTML elements and ARIA roles |
| `responsive.go` | Multi-variant breakpoint wrapper (`data-variant` containers, CSS scoping helpers) |
| `context.go` | Rendering context: identity, locale, entitlements, i18n service |
@ -52,7 +52,7 @@ This builds a Header-Content-Footer layout with semantic HTML elements (`<header
## Key Concepts
**Node tree** -- All renderable units implement `Node`, a single-method interface: `Render(ctx *Context) string`. The library composes nodes into trees using `El()` for elements, `Text()` for translated text, and control-flow constructors (`If`, `Unless`, `Each`, `Switch`, `Entitled`), plus accessibility helpers such as `AriaLabel()`, `AriaControls()`, `AriaCurrent()`, `AriaBusy()`, `AriaHidden()`, `AriaDisabled()`, and `TabIndex()`.
**Node tree** -- All renderable units implement `Node`, a single-method interface: `Render(ctx *Context) string`. The library composes nodes into trees using `El()` for elements, `Text()` for translated text, and control-flow constructors (`If`, `Unless`, `Each`, `Switch`, `Entitled`), plus accessibility helpers such as `AriaLabel()`, `AriaControls()`, `AriaCurrent()`, `AriaBusy()`, `AriaLive()`, `AriaHidden()`, `AriaDisabled()`, and `TabIndex()`.
**HLCRF Layout** -- A five-slot compositor that maps to semantic HTML: `<header>` (H), `<aside>` (L/R), `<main>` (C), `<footer>` (F). The variant string controls which slots render: `"HLCRF"` for all five, `"HCF"` for three, `"C"` for content only. Layouts nest: placing a `Layout` inside another layout's slot produces hierarchical `data-block` paths like `L-0-C-0`.

10
node.go
View file

@ -223,6 +223,16 @@ func AriaBusy(n Node, busy bool) Node {
return Attr(n, "aria-busy", "false")
}
// node.go: AriaLive sets the aria-live attribute on an element node.
// Example: AriaLive(El("div"), "polite").
// An empty value leaves the node unchanged so callers can opt out cleanly.
func AriaLive(n Node, live string) Node {
if live == "" {
return n
}
return Attr(n, "aria-live", live)
}
// node.go: Role sets the role attribute on an element node.
// Example: Role(El("aside"), "complementary").
func Role(n Node, role string) Node {

View file

@ -321,6 +321,22 @@ func TestAriaBusyHelper(t *testing.T) {
}
}
func TestAriaLiveHelper(t *testing.T) {
ctx := NewContext()
polite := AriaLive(El("div", Raw("updates")), "polite")
gotPolite := polite.Render(ctx)
if !strings.Contains(gotPolite, `aria-live="polite"`) {
t.Errorf("AriaLive(\"polite\") = %q, want aria-live=\"polite\"", gotPolite)
}
silent := AriaLive(El("div", Raw("updates")), "")
gotSilent := silent.Render(ctx)
if strings.Contains(gotSilent, `aria-live=`) {
t.Errorf("AriaLive(\"\") = %q, want no aria-live attribute", gotSilent)
}
}
func TestRoleHelper(t *testing.T) {
ctx := NewContext()
node := Role(El("button", Raw("menu")), "navigation")