feat(html): add aria role description helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
19ab9d246a
commit
ad01f04a51
4 changed files with 34 additions and 2 deletions
|
|
@ -45,6 +45,7 @@ Accessibility-oriented helpers are also provided for common attribute patterns:
|
|||
- `AriaBusy(node, busy)`
|
||||
- `AriaLive(node, live)`
|
||||
- `AriaDescription(node, description)`
|
||||
- `AriaRoleDescription(node, description)`
|
||||
- `Role(node, role)`
|
||||
- `Lang(node, locale)`
|
||||
- `Dir(node, direction)`
|
||||
|
|
|
|||
|
|
@ -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`, `AriaControls`, `AriaHasPopup`, `AriaOwns`, `AriaKeyShortcuts`, `Alt`/`AltText`, `AriaBusy`, `AriaLive`, `AriaDisabled`, `AriaModal`, `AriaChecked`, `AriaInvalid`, `AriaRequired`, `AriaPressed`, `AriaSelected`, `Hidden`, `TabIndex`, and `AutoFocus` helpers |
|
||||
| `node.go` | `Node` interface and all node types: `El`, `Text`, `Raw`, `If`, `Unless`, `Each`, `EachSeq`, `Switch`, `Entitled`, plus `AriaLabel`, `AriaControls`, `AriaHasPopup`, `AriaOwns`, `AriaKeyShortcuts`, `Alt`/`AltText`, `AriaBusy`, `AriaLive`, `AriaDescription`, `AriaRoleDescription`, `AriaDisabled`, `AriaModal`, `AriaChecked`, `AriaInvalid`, `AriaRequired`, `AriaPressed`, `AriaSelected`, `Hidden`, `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 and visibility helpers such as `AriaLabel()`, `AriaControls()`, `AriaHasPopup()`, `AriaOwns()`, `AriaKeyShortcuts()`, `AriaCurrent()`, `AriaBusy()`, `AriaLive()`, `AriaHidden()`, `Hidden()`, `AriaDisabled()`, `AriaModal()`, `AriaChecked()`, `AriaInvalid()`, `AriaRequired()`, `AriaReadOnly()`, 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 and visibility helpers such as `AriaLabel()`, `AriaControls()`, `AriaHasPopup()`, `AriaOwns()`, `AriaKeyShortcuts()`, `AriaCurrent()`, `AriaBusy()`, `AriaLive()`, `AriaDescription()`, `AriaRoleDescription()`, `AriaHidden()`, `Hidden()`, `AriaDisabled()`, `AriaModal()`, `AriaChecked()`, `AriaInvalid()`, `AriaRequired()`, `AriaReadOnly()`, 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`.
|
||||
|
||||
|
|
|
|||
11
node.go
11
node.go
|
|
@ -299,6 +299,17 @@ func AriaDescription(n Node, description string) Node {
|
|||
return n
|
||||
}
|
||||
|
||||
// node.go: AriaRoleDescription sets the aria-roledescription attribute on an
|
||||
// element node.
|
||||
// Example: AriaRoleDescription(El("section"), "carousel").
|
||||
// An empty value leaves the node unchanged so callers can opt out cleanly.
|
||||
func AriaRoleDescription(n Node, description string) Node {
|
||||
if value := trimmedNonEmpty(description); value != "" {
|
||||
return Attr(n, "aria-roledescription", value)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// node.go: Role sets the role attribute on an element node.
|
||||
// Example: Role(El("aside"), "complementary").
|
||||
func Role(n Node, role string) Node {
|
||||
|
|
|
|||
20
node_test.go
20
node_test.go
|
|
@ -494,6 +494,26 @@ func TestAriaDescriptionHelper_IgnoresWhitespace(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAriaRoleDescriptionHelper(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := AriaRoleDescription(El("section", Raw("content")), "carousel")
|
||||
got := node.Render(ctx)
|
||||
want := `<section aria-roledescription="carousel">content</section>`
|
||||
if got != want {
|
||||
t.Errorf("AriaRoleDescription() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAriaRoleDescriptionHelper_IgnoresWhitespace(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := AriaRoleDescription(El("section", Raw("content")), " \n ")
|
||||
got := node.Render(ctx)
|
||||
want := `<section>content</section>`
|
||||
if got != want {
|
||||
t.Errorf("AriaRoleDescription() with whitespace = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoleHelper(t *testing.T) {
|
||||
ctx := NewContext()
|
||||
node := Role(El("button", Raw("menu")), "navigation")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue