Documentation
¶
Overview ¶
Package dom provides HTML element constructors and DOM manipulation utilities for GoWebComponents.
This package offers a type-safe way to create HTML elements programmatically in Go, with automatic conversion of children and optimized performance through element pooling and smart string handling.
Basic usage:
import "github.com/monstercameron/GoWebComponents/dom"
// Create simple elements
element := dom.Div(nil,
dom.H1(nil, "Hello World"),
dom.P(nil, "Welcome to GoWebComponents"),
)
The package includes constructors for all standard HTML5 elements, organized by category:
- Document Structure: Html, Head, Body, Title, Meta, Link, Style, Script
- Layout: Div, Span, P, Section, Article, Aside, Header, Footer, Main, Nav
- Headings: H1, H2, H3, H4, H5, H6
- Text: Strong, Em, Small, Mark, Code, Pre, Blockquote, Cite
- Lists: Ul, Ol, Li, Dl, Dt, Dd
- Links & Media: A, Img, Video, Audio, Source, Canvas, Svg
- Forms: Form, Input, Textarea, Button, Select, Option, Label, Fieldset
- Tables: Table, Thead, Tbody, Tfoot, Tr, Th, Td, Caption
- Interactive: Details, Summary, Dialog
All element constructors accept attributes as a map and variadic children:
dom.Div(dom.Attrs{"class": "container", "id": "main"},
dom.H1(nil, "Title"),
dom.P(nil, "Content"),
)
Helper functions are provided for common patterns:
dom.ClassProps("btn-primary") // Creates map with class attribute
dom.IdProps("my-element") // Creates map with id attribute
dom.HrefProps("/about") // Creates map with href attribute
dom.ClassIdProps("container", "main") // Creates map with both class and id
Index ¶
- func ClassIdProps(className, id string) map[string]interface{}
- func ClassProps(className string) map[string]interface{}
- func CreateElement(typ interface{}, props map[string]interface{}, children ...interface{}) *runtime.Element
- func EmptyProps() map[string]interface{}
- func Fragment(props map[string]interface{}, children ...interface{}) *runtime.Element
- func HrefProps(href string) map[string]interface{}
- func IdProps(id string) map[string]interface{}
- func InputTypeProps(inputType string) map[string]interface{}
- func NilProps() map[string]interface{}
- func Text(content string) *runtime.Element
- type Attributes
- type Attrs
- type Element
- func A(props Attrs, children ...interface{}) *Element
- func Abbr(props Attrs, children ...interface{}) *Element
- func Address(props Attrs, children ...interface{}) *Element
- func Article(props Attrs, children ...interface{}) *Element
- func Aside(props Attrs, children ...interface{}) *Element
- func Audio(props Attrs, children ...interface{}) *Element
- func Bdi(props Attrs, children ...interface{}) *Element
- func Bdo(props Attrs, children ...interface{}) *Element
- func Blockquote(props Attrs, children ...interface{}) *Element
- func Body(props Attrs, children ...interface{}) *Element
- func Br(props Attrs) *Element
- func Button(props Attrs, children ...interface{}) *Element
- func Canvas(props Attrs, children ...interface{}) *Element
- func Caption(props Attrs, children ...interface{}) *Element
- func Cite(props Attrs, children ...interface{}) *Element
- func Code(props Attrs, children ...interface{}) *Element
- func Col(props Attrs) *Element
- func Colgroup(props Attrs, children ...interface{}) *Element
- func Data(props Attrs, children ...interface{}) *Element
- func Datalist(props Attrs, children ...interface{}) *Element
- func Dd(props Attrs, children ...interface{}) *Element
- func Del(props Attrs, children ...interface{}) *Element
- func Details(props Attrs, children ...interface{}) *Element
- func Dfn(props Attrs, children ...interface{}) *Element
- func Dialog(props Attrs, children ...interface{}) *Element
- func Div(props Attrs, children ...interface{}) *Element
- func DivWithComponents(props map[string]interface{}, ...) *Element
- func Dl(props Attrs, children ...interface{}) *Element
- func Dt(props Attrs, children ...interface{}) *Element
- func Em(props Attrs, children ...interface{}) *Element
- func Embed(props Attrs) *Element
- func Fieldset(props Attrs, children ...interface{}) *Element
- func Figcaption(props Attrs, children ...interface{}) *Element
- func Figure(props Attrs, children ...interface{}) *Element
- func Footer(props Attrs, children ...interface{}) *Element
- func Form(props Attrs, children ...interface{}) *Element
- func H1(props Attrs, children ...interface{}) *Element
- func H2(props Attrs, children ...interface{}) *Element
- func H3(props Attrs, children ...interface{}) *Element
- func H4(props Attrs, children ...interface{}) *Element
- func H5(props Attrs, children ...interface{}) *Element
- func H6(props Attrs, children ...interface{}) *Element
- func Head(props Attrs, children ...interface{}) *Element
- func Header(props Attrs, children ...interface{}) *Element
- func Hgroup(props Attrs, children ...interface{}) *Element
- func Hr(props Attrs) *Element
- func Html(props Attrs, children ...interface{}) *Element
- func Img(props Attrs) *Element
- func Input(props Attrs) *Element
- func Ins(props Attrs, children ...interface{}) *Element
- func Kbd(props Attrs, children ...interface{}) *Element
- func Label(props Attrs, children ...interface{}) *Element
- func Legend(props Attrs, children ...interface{}) *Element
- func Li(props Attrs, children ...interface{}) *Element
- func Link(props Attrs) *Element
- func Main(props Attrs, children ...interface{}) *Element
- func MainWithComponents(props map[string]interface{}, ...) *Element
- func Mark(props Attrs, children ...interface{}) *Element
- func Meta(props Attrs) *Element
- func Meter(props Attrs, children ...interface{}) *Element
- func Nav(props Attrs, children ...interface{}) *Element
- func Object(props Attrs, children ...interface{}) *Element
- func Ol(props Attrs, children ...interface{}) *Element
- func Optgroup(props Attrs, children ...interface{}) *Element
- func Option(props Attrs, children ...interface{}) *Element
- func Output(props Attrs, children ...interface{}) *Element
- func P(props Attrs, children ...interface{}) *Element
- func Param(props Attrs) *Element
- func Picture(props Attrs, children ...interface{}) *Element
- func Portal(props Attrs) *Element
- func Pre(props Attrs, children ...interface{}) *Element
- func Progress(props Attrs, children ...interface{}) *Element
- func Q(props Attrs, children ...interface{}) *Element
- func Rp(props Attrs, children ...interface{}) *Element
- func Rt(props Attrs, children ...interface{}) *Element
- func Ruby(props Attrs, children ...interface{}) *Element
- func Samp(props Attrs, children ...interface{}) *Element
- func Script(props Attrs, children ...interface{}) *Element
- func Section(props Attrs, children ...interface{}) *Element
- func SectionWithComponents(props map[string]interface{}, ...) *Element
- func Select(props Attrs, children ...interface{}) *Element
- func Slot(props Attrs, children ...interface{}) *Element
- func Small(props Attrs, children ...interface{}) *Element
- func Source(props Attrs) *Element
- func Span(props Attrs, children ...interface{}) *Element
- func Strong(props Attrs, children ...interface{}) *Element
- func Style(props Attrs, children ...interface{}) *Element
- func Sub(props Attrs, children ...interface{}) *Element
- func Summary(props Attrs, children ...interface{}) *Element
- func Sup(props Attrs, children ...interface{}) *Element
- func Svg(props Attrs, children ...interface{}) *Element
- func Table(props Attrs, children ...interface{}) *Element
- func Tbody(props Attrs, children ...interface{}) *Element
- func Td(props Attrs, children ...interface{}) *Element
- func Template(props Attrs, children ...interface{}) *Element
- func Textarea(props Attrs, children ...interface{}) *Element
- func Tfoot(props Attrs, children ...interface{}) *Element
- func Th(props Attrs, children ...interface{}) *Element
- func Thead(props Attrs, children ...interface{}) *Element
- func Time(props Attrs, children ...interface{}) *Element
- func Title(props Attrs, children ...interface{}) *Element
- func Tr(props Attrs, children ...interface{}) *Element
- func Track(props Attrs) *Element
- func Ul(props Attrs, children ...interface{}) *Element
- func Var(props Attrs, children ...interface{}) *Element
- func Video(props Attrs, children ...interface{}) *Element
- func Wbr(props Attrs) *Element
- func WithComponents(tagName string, props map[string]interface{}, ...) *Element
- type GoEvent
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClassIdProps ¶
ClassIdProps creates a props map with both class and id attributes.
func ClassProps ¶
ClassProps creates a props map with only a class attribute. Uses pre-allocated maps for common CSS classes to reduce allocations.
func CreateElement ¶
func CreateElement(typ interface{}, props map[string]interface{}, children ...interface{}) *runtime.Element
CreateElement creates a virtual DOM element with the given type, props, and children. This is the low-level element creation function used by all HTML element constructors.
The type can be:
- A string representing an HTML tag name (e.g., "div", "span")
- A component function that returns an Element
Props is a map of HTML attributes and event handlers. Children can be Elements, strings (auto-converted to Text nodes), or component functions.
Example:
dom.CreateElement("div", map[string]interface{}{
"class": "container",
"id": "main",
}, dom.Text("Content"))
func Fragment ¶
Fragment groups multiple elements without creating a wrapper DOM node. This is equivalent to React's Fragment (<> syntax). Fragments allow you to return multiple elements from a component without introducing an extra div or other wrapper element.
Fragments are useful for:
- Avoiding unnecessary wrapper divs in lists
- Keeping semantic HTML structure clean
- Avoiding CSS layout issues from wrapper elements
- Grouping related elements without a container
Example with Fragment to group items without wrapper:
func ListItems(props dom.Attrs) *dom.Element {
return dom.Fragment(nil,
dom.Li(nil, dom.Text("Item 1")),
dom.Li(nil, dom.Text("Item 2")),
dom.Li(nil, dom.Text("Item 3")),
)
}
// Renders as:
// <li>Item 1</li>
// <li>Item 2</li>
// <li>Item 3</li>
// NOT wrapped in an extra div or span
Example in a list to avoid key warnings:
func TodoList(props dom.Attrs) *dom.Element {
items := []string{"Buy milk", "Write code", "Review PR"}
children := make([]interface{}, 0)
for i, item := range items {
children = append(children,
dom.Fragment(nil,
dom.H3(nil, dom.Text(fmt.Sprintf("Task %d", i+1))),
dom.P(nil, dom.Text(item)),
),
)
}
return dom.Ul(nil, children...)
}
Note: Fragment props are currently ignored. In the future, only the "key" prop will be supported for list reconciliation purposes.
func InputTypeProps ¶
InputTypeProps creates a props map with only a type attribute. Uses pre-allocated maps for common input types to reduce allocations.
Types ¶
type Attributes ¶
type Attributes = map[string]interface{}
type Element ¶
func Blockquote ¶
func Button ¶
Example ¶
package main
import (
"fmt"
"github.com/monstercameron/GoWebComponents/dom"
)
func main() {
// Create a button with an onclick handler
// Note: In a real app, you would use hooks.GoUseFunc for the handler
btn := dom.Button(
dom.Attrs{
"class": "btn btn-primary",
"type": "button",
},
dom.Text("Click Me"),
)
fmt.Printf("Created button: %v\n", btn != nil)
}
Output: Created button: true
func Div ¶
Example ¶
package main
import (
"fmt"
"github.com/monstercameron/GoWebComponents/dom"
)
func main() {
// Create a simple div with text content
element := dom.Div(
dom.Attrs{"class": "container"},
dom.H1(nil, dom.Text("Hello World")),
dom.P(nil, dom.Text("This is a paragraph")),
)
// In a real application, this element would be rendered to the DOM
// using render.ToElement(element, container)
fmt.Printf("Created element: %v\n", element != nil)
}
Output: Created element: true
func DivWithComponents ¶
func DivWithComponents(props map[string]interface{}, componentRefs ...func(map[string]interface{}) *Element) *Element
DivWithComponents creates a div element with component references as children.
func Figcaption ¶
func Form ¶
Example ¶
package main
import (
"fmt"
"github.com/monstercameron/GoWebComponents/dom"
)
func main() {
// Create a login form
form := dom.Form(
dom.Attrs{"class": "login-form"},
dom.Div(
dom.Attrs{"class": "form-group"},
dom.Label(dom.Attrs{"for": "username"}, dom.Text("Username")),
dom.Input(dom.Attrs{
"type": "text",
"id": "username",
"name": "username",
}),
),
dom.Div(
dom.Attrs{"class": "form-group"},
dom.Label(dom.Attrs{"for": "password"}, dom.Text("Password")),
dom.Input(dom.Attrs{
"type": "password",
"id": "password",
"name": "password",
}),
),
dom.Button(
dom.Attrs{"type": "submit"},
dom.Text("Login"),
),
)
fmt.Printf("Created form: %v\n", form != nil)
}
Output: Created form: true
func MainWithComponents ¶
func MainWithComponents(props map[string]interface{}, componentRefs ...func(map[string]interface{}) *Element) *Element
MainWithComponents creates a main element with component references as children.
func SectionWithComponents ¶
func SectionWithComponents(props map[string]interface{}, componentRefs ...func(map[string]interface{}) *Element) *Element
SectionWithComponents creates a section element with component references as children.
func Ul ¶
Example ¶
package main
import (
"fmt"
"github.com/monstercameron/GoWebComponents/dom"
)
func main() {
// Create an unordered list
list := dom.Ul(
dom.Attrs{"class": "item-list"},
dom.Li(nil, dom.Text("Item 1")),
dom.Li(nil, dom.Text("Item 2")),
dom.Li(nil, dom.Text("Item 3")),
)
fmt.Printf("Created list: %v\n", list != nil)
}
Output: Created list: true
type GoEvent ¶
GoEvent is a Go-friendly wrapper around the JavaScript event object. It provides a safe, convenient API for working with DOM events in Go.
GoEvent is automatically provided to event handlers registered with hooks.GoUseFunc when using the func(GoEvent) signature.
Example:
handleClick := hooks.GoUseFunc(func(event dom.GoEvent) {
event.PreventDefault()
println("Clicked!")
})
Button(Attrs{"onclick": handleClick}, "Click Me")
Example with form input:
handleChange := hooks.GoUseFunc(func(event dom.GoEvent) {
value := event.GetValue()
println("New value:", value)
})
Input(Attrs{"oninput": handleChange}, nil)
Methods:
- GetValue(): Retrieves input element value
- IsChecked(): Gets checkbox/radio button state
- GetKey(): Gets the key from keyboard events
- PreventDefault(): Prevents browser default action
- StopPropagation(): Stops event bubbling
- JSValue(): Gets the underlying js.Value