mvc

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2026 License: Apache-2.0 Imports: 12 Imported by: 4

README

MVC Package

The github.com/djthorpe/go-wasmbuild/pkg/mvc package provides a thin "model, view and controller" style abstraction on top of the DOM wrappers that ship with go-wasmbuild. It is designed for Go/WebAssembly applications that implement applications a declarative, component-oriented approach to building user interfaces.

Getting Started

Import the package alongside the base DOM facade:

import (
    mvc "github.com/djthorpe/go-wasmbuild/pkg/mvc"
)

Create an application root and populate it with content:

func main() {
    app := mvc.New()
    app.Content("Hello, world!")

    select {} // keep the WASM module alive
}

Views expose fluent helpers such as Content, Append, and AddEventListener so you can assemble larger components quickly. Many sub-packages (for example, pkg/bootstrap) build on these primitives to provide higher-level UI widgets.

View

A View is any component that implements the mvc.View interface. Views wrap a root DOM element and can be composed to build complex UIs.

type View interface {
    Name() string                                       // registered component name
    ID() string                                         // value of the id attribute, if set
    Root() dom.Element                                  // the root DOM element
    Parent() View                                       // nearest ancestor view, or nil
    Self() View                                         // concrete implementation (for embedding)
    Slot(name string) dom.Element                       // named slot element, or nil
    ReplaceSlot(name string, node any, opts ...Opt) View
    ReplaceSlotChildren(name string, args ...any) View
    Apply(opts ...Opt) View                             // apply class/attribute options
    Content(args ...any) View                           // replace body slot children
    AddEventListener(event string, fn func(dom.Event)) View
    RemoveEventListener(event string) View
}
Creating a view

Views must be registered before use. A minimal custom view embeds mvc.View and provides an initialisation function that stores the inner view:

type myWidget struct {
    mvc.View
}

func init() {
    mvc.RegisterView("my-widget", func(el dom.Element) mvc.View {
        return mvc.NewViewWithElement(new(myWidget), el, func(self, child mvc.View) {
            self.(*myWidget).View = child
        })
    })
}

func MyWidget(args ...any) mvc.View {
    return mvc.NewView(new(myWidget), "my-widget", "DIV", func(self, child mvc.View) {
        self.(*myWidget).View = child
    }, args...)
}

NewView creates the root element from a tag name (e.g. "DIV"), applies any Opt arguments, and inserts any content arguments into the body slot. The data-mvc attribute is set automatically to the registered name.

Slots

Slots are named sub-elements within a view's HTML template. The default slot is named "body" (the ContentSlot constant). Content(...) is shorthand for ReplaceSlotChildren("body", ...).

Custom templates can define additional slots by giving child elements a data-slot attribute:

<div data-mvc="my-widget">
    <header data-slot="header"></header>
    <div data-slot="body"></div>
    <footer data-slot="footer"></footer>
</div>

You can then target each slot by name:

w := MyWidget()
w.ReplaceSlotChildren("header", "Page title")
w.Content("Main body text")           // same as ReplaceSlotChildren("body", ...)
w.ReplaceSlotChildren("footer", "Footer text")
Creating elements

mvc.HTML creates a standalone DOM element (not a registered view) that can be passed as content to any view:

mvc.HTML("SPAN", "hello", mvc.WithClass("text-muted"))
mvc.HTML("BR")
mvc.HTML("A", "click me", mvc.WithAttr("href", "#home"))
Application root

mvc.New creates the root application container, prepends it to document.body, and returns it. Call Run() to block the main goroutine and keep the WASM module alive:

func main() {
    mvc.New(
        bs.Container(
            mvc.WithClass("p-3"),
            "Hello, world!",
        ),
    ).Run()
}

View Options

View options are functions with signature func(OptSet) error (the mvc.Opt type) that declaratively set classes and attributes on a view's root element. They can be passed to NewView, Content, ReplaceSlotChildren, ReplaceSlot, and Apply.

Common built-in options:

  • mvc.WithClass("name") — add a CSS class
  • mvc.WithoutClass("name") — remove a CSS class
  • mvc.WithAttr("key", "value") — set an attribute
  • mvc.WithoutAttr("key") — remove an attribute
  • mvc.WithID("id") — set the element id
  • mvc.WithStyle("css") — set the inline style attribute
  • mvc.WithSlotAttr("slot", "key", "value") — set an attribute on a named slot element

Custom options can be created by implementing the mvc.Opt function signature:

Event Listeners

Attach event listeners to a view's root element with AddEventListener. The method returns the view so it can be chained:

btn := bs.Button("Click me").AddEventListener("click", func(e dom.Event) {
    fmt.Println("clicked")
})

RemoveEventListener removes a previously registered listener by event type.

Models

There are two generic model types. Both are zero-value usable structs.

Model[T]

Model[T] stores an ordered slice of items of any type and notifies listeners on every mutation.

var m mvc.Model[string]

m.AddEventListener(func(items []string) {
    fmt.Println("changed:", items) // do not modify the slice
})

m.Set([]string{"a", "b", "c"}) // fires listener
m.Append("d")                   // fires listener
m.Clear()                       // fires listener

fmt.Println(m.Len())    // 0
fmt.Println(m.Items())  // [] (copy)
Method Description
Set([]T) Replace all items; notify listeners
Append(...T) Add items to the end; notify listeners
Clear() Remove all items; notify listeners
Items() []T Return a shallow copy
Len() int Number of items
AddEventListener(func([]T)) Register change listener
KeyedModel[K, T]

KeyedModel[K comparable, T Keyed[K]] is like Model[T] but requires items to implement PrimaryKey() K. It maintains an internal index for O(1) key lookups and emits fine-grained added/deleted/changed events in addition to the full-list OnSet event.

type Station struct{ Abbr, Name string }
func (s Station) PrimaryKey() string { return s.Abbr }

var m mvc.KeyedModel[string, Station]

m.OnSet(func(items []Station) { /* any mutation */ })
m.OnAdded(func(e mvc.AddedEvent[Station]) {
    fmt.Println("added at index", e.Index, ":", e.Item.Name)
})
m.OnDeleted(func(e mvc.DeletedEvent[Station]) {
    fmt.Println("removed from index", e.Index, ":", e.Item.Name)
})
m.OnChanged(func(e mvc.ChangedEvent[Station]) {
    fmt.Println("updated at index", e.Index, ":", e.Item.Name)
})

m.Set([]Station{{"RICH", "Richmond"}, {"12TH", "12th St."}}) // OnSet only
m.Append(Station{"EMBR", "Embarcadero"})                     // OnAdded + OnSet
m.Update(Station{"RICH", "Richmond (updated)"})              // OnChanged + OnSet
m.Remove("12TH")                                             // OnDeleted + OnSet

station, ok := m.Get("EMBR") // O(1) lookup

Each event struct carries Item T, Index int, and Items []T (full post-mutation slice), giving views everything needed for either a targeted DOM update or a full re-render.

To use KeyedModel, implement mvc.Keyed[K] on your type:

type Keyed[K comparable] interface {
    PrimaryKey() K
}

Controller

mvc.Controller is an interface for wiring views to application logic. A controller attaches to one or more views and reacts to events they emit.

type Controller interface {
    Views() []View
    Attach(...View)
    Detach(...View)
    EventListener(eventType string, source View)
}

Create a controller by embedding *mvc.controller in your own struct and calling mvc.NewController:

type myController struct {
    *mvc.controller // not exported; access via Controller interface
}

func NewMyController(views ...mvc.View) mvc.Controller {
    c := &myController{}
    c.controller = mvc.NewController(c, nil, views...)
    return c
}

func (c *myController) EventListener(event string, v mvc.View) {
    fmt.Printf("event %q from %s\n", event, v.Name())
}

For domain-specific controllers (e.g. wrapping a data provider and models), define your own struct that owns the provider and models directly and exposes typed methods — see the BART app's bart.Controller for a worked example.

Provider

Providers fetch data from remote sources and fan results out to registered listeners. There are two provider types.

Provider

mvc.Provider performs raw HTTP fetches and delivers *js.FetchResponse to listeners:

type Provider interface {
    Fetch(path string, opts ...js.FetchOption)
    FetchWithInterval(path string, interval time.Duration, opts ...js.FetchOption)
    Cancel()
    AddEventListener(fn func(*js.FetchResponse, error))
}

p := mvc.NewProvider(baseURL)
p.AddEventListener(func(resp *js.FetchResponse, err error) {
    if err != nil { return }
    resp.Text().Done(func(v js.Value, err error) {
        fmt.Println(v.String())
    })
})
p.Fetch("data.json")

FetchWithInterval cancels any existing interval, fires immediately, then repeats. Call Cancel() to stop.

JSONProvider[T]

mvc.JSONProvider[T] wraps Provider and automatically decodes JSON responses into T. It supports all HTTP verbs and marshals request bodies for mutation methods:

type JSONProvider[T any] interface {
    Get(path string, opts ...js.FetchOption)
    GetWithInterval(path string, interval time.Duration, opts ...js.FetchOption)
    Cancel()
    Post(path string, body T, opts ...js.FetchOption)
    Put(path string, body T, opts ...js.FetchOption)
    Patch(path string, body T, opts ...js.FetchOption)
    Delete(path string, opts ...js.FetchOption)
    AddEventListener(fn func(T, error))
}

type MyData struct {
    Name string `json:"name"`
}

p := mvc.NewJSONProvider[MyData](baseURL)
p.AddEventListener(func(data MyData, err error) {
    if err != nil { return }
    fmt.Println(data.Name)
})
p.Get("items/1")

A 204 No Content response calls the listener with the zero value of T and a nil error. Use js.WithQuery(url.Values{...}) to append query parameters.

Router

mvc.Router() creates a hash-based router view that swaps the displayed page based on window.location.hash. It listens for hashchange events and updates the content area automatically. If no hash matches, the first registered page is shown as the default.

Basic usage
router := mvc.Router().
    Page("#home",     homeView).
    Page("#stations", stationsView)

mvc.New(router).Run()

Navigating to #stations (via an <a href="#stations"> link or window.location.hash = "#stations") swaps the content area to stationsView.

Integrating with a nav group

Pass any mvc.ActiveGroup to Active(...) so the router automatically marks the correct nav items active on every navigation:

navHome     := bs.NavItem("Home",     mvc.WithAttr("href", "#home"))
navStations := bs.NavItem("Stations", mvc.WithAttr("href", "#stations"))
nav         := bs.Nav(navHome, navStations)  // Nav implements mvc.ActiveGroup

router := mvc.Router().
    Active(nav).
    Page("#home",     homeView,     navHome).
    Page("#stations", stationsView, navStations)

When the route changes to #stations, the router calls nav.SetActive(navStations). The ActiveGroup implementation marks navStations active and all other members inactive.

ActiveState and ActiveGroup interfaces

mvc.ActiveState is implemented by any view that can be marked active or inactive:

type ActiveState interface {
    SetActive(bool)
}

mvc.ActiveGroup is implemented by a container (such as a nav bar) that manages which of its members are currently active:

type ActiveGroup interface {
    SetActive(views ...View)  // activates given views; deactivates the rest
}

Passing no arguments to SetActive deactivates all members.

Documentation

Overview

Package mvc provides a thin model-view-controller layer for building declarative WASM user interfaces using the go-wasmbuild DOM wrappers.

Index

Constants

View Source
const (
	ViewRouter            = "mvc-router"
	EventRouterActivate   = "mvc-router-activate"
	EventRouterDeactivate = "mvc-router-deactivate"
)
View Source
const (
	ViewTable       = "mvc-table"
	ViewTableHeader = "mvc-table-header"
	ViewTableRow    = "mvc-table-row"
)
View Source
const (
	// The attribute key which identifies an mvc component
	DataComponentAttrKey = "data-mvc"

	// The attribute key which identifies a slot in a component
	DataSlotAttrKey = "data-slot"

	// The name of the default slot, when name atribute is missing
	ContentSlot = "body"
)
View Source
const (
	// ViewApp is the registered component name used for the root application
	// container within the MVC system.
	ViewApp = "mvc-app"
)

Variables

This section is empty.

Functions

func Counter added in v0.0.2

func Counter(label string) string

Counter returns an attribute value function which generates unique IDs

func HTML

func HTML(tagName string, args ...any) dom.Element

HTML returns an element with the given tag name and class/attribute options

func HasNextPage added in v0.0.2

func HasNextPage(state PaginationState) bool

HasNextPage reports whether there is a page after the current one.

func HasPreviousPage added in v0.0.2

func HasPreviousPage(state PaginationState) bool

HasPreviousPage reports whether there is a page before the current one.

func New

func New(args ...any) *app

New creates an empty application root, attaches it to the document body and returns the view so callers can begin composing content.

func NewController

func NewController(self Controller, fn func(self, child Controller), view ...View) *controller

func NodeFromAny

func NodeFromAny(child any) dom.Node

NodeFromAny returns a Node from a string, Element, Tag or View or returns nil if the type is unsupported

func Page added in v0.0.2

func Page(state PaginationState) uint

Page returns the current 1-based page number derived from offset and limit. Returns 0 when the page cannot be determined because limit is zero.

func PageCount added in v0.0.2

func PageCount(state PaginationState) uint

PageCount returns the total number of pages derived from count and limit. Returns 0 when the page count cannot be determined because limit is zero.

func PageEnd added in v0.0.2

func PageEnd(state PaginationState) uint

PageEnd returns the zero-based exclusive index of the last item on the current page, clamped to the known total count.

func PageStart added in v0.0.2

func PageStart(state PaginationState) uint

PageStart returns the zero-based inclusive index of the first item on the current page.

func Placeholder

func Placeholder(args ...any) dom.Element

Placeholder returns a placeholder element which is not rendered and has no effect

func RegisterView

func RegisterView(name string, constructor ViewConstructorFunc, eventtypes ...string)

RegisterView registers a view constructor function for a given name, so that the view can be created on-demand, and zero or more event types that a controller which attaches to this view should listen for.

func RegisteredEvents added in v0.0.2

func RegisteredEvents(name string) []string

RegisteredEvents returns the event types registered for the named view. Returns nil if the view is not registered.

func Router

func Router(args ...any) *router

Create a Router

func Table added in v0.0.2

func Table(args ...any) *table

Table creates a basic HTML table with header and body slots.

func TableHeader added in v0.0.2

func TableHeader(args ...any) *tableHeader

TableHeader creates a table header row.

func TableRow added in v0.0.2

func TableRow(args ...any) *tableRow

TableRow creates a table body row.

func Text

func Text(text string) dom.Text

CData returns a text node with the given text

Types

type ActiveGroup added in v0.0.2

type ActiveGroup interface {
	Active() []View
	SetActive(views ...View) View
}

ActiveGroup is implemented by a container that manages which of its member views are active. Calling SetActive with no arguments deactivates all members.

type ActiveState added in v0.0.2

type ActiveState interface {
	Active() bool
	SetActive(bool) View
}

ActiveState is implemented by a view that can be marked active or inactive.

type AddedEvent added in v0.0.2

type AddedEvent[T any] struct {
	Item  T   // the item that was added
	Index int // its position in the list
	Items []T // full slice after insertion (do not modify)
}

AddedEvent is emitted when a new item is inserted into a KeyedModel.

type ChangedEvent added in v0.0.2

type ChangedEvent[T any] struct {
	Item  T   // the updated item
	Index int // its position in the list
	Items []T // full slice after update (do not modify)
}

ChangedEvent is emitted when an existing item is updated in place.

type Controller

type Controller interface {
	// Return all the views
	Views() []View

	// Attach one or more views to this controller
	Attach(...View)

	// Detach one or more views from this controller
	Detach(...View)

	// Fire an action based on an event from a view
	EventListener(string, View)
}

A controller reacts to events from one or more views

type DeletedEvent added in v0.0.2

type DeletedEvent[T any] struct {
	Item  T   // the item that was removed
	Index int // the position it occupied before removal
	Items []T // full slice after removal (do not modify)
}

DeletedEvent is emitted when an item is removed from a KeyedModel.

type EnabledGroup added in v0.0.2

type EnabledGroup interface {
	Enabled() []View
	SetEnabled(views ...View) View
}

EnabledGroup is implemented by a container that manages the enabled/disabled state of its member views. The passed views are enabled; all others are disabled. Calling SetEnabled with no arguments disables all members.

type EnabledState added in v0.0.2

type EnabledState interface {
	Enabled() bool
	SetEnabled(bool) View
}

EnabledState is implemented by a view that can be marked enabled or disabled.

type JSONProvider added in v0.0.2

type JSONProvider[T any] interface {
	// Get fetches from the given path and decodes the response as T.
	Get(path string, opts ...js.FetchOption)

	// GetWithInterval calls Get immediately and then repeatedly at the given
	// interval. Call Cancel to stop.
	GetWithInterval(path string, interval time.Duration, opts ...js.FetchOption)

	// Cancel stops any active interval.
	Cancel()

	// Post marshals body as JSON, posts to path, and decodes the response as T.
	Post(path string, body T, opts ...js.FetchOption)

	// Put marshals body as JSON, puts to path, and decodes the response as T.
	Put(path string, body T, opts ...js.FetchOption)

	// Patch marshals body as JSON, patches path, and decodes the response as T.
	Patch(path string, body T, opts ...js.FetchOption)

	// Delete sends a DELETE to path and decodes any response body as T.
	// If the server returns no body (e.g. 204 No Content), the listener
	// is called with the zero value of T and a nil error.
	Delete(path string, opts ...js.FetchOption)

	// AddEventListener registers a listener called on every completed request.
	// value is the zero value of T on error; err is nil on success.
	AddEventListener(fn func(T, error))
}

JSONProvider fetches JSON data from a remote source, encoding and decoding values of type T. Use AddEventListener to receive decoded results.

func NewJSONProvider added in v0.0.2

func NewJSONProvider[T any](base *url.URL) JSONProvider[T]

NewJSONProvider creates a new JSONProvider with the given base URL. Returns nil if base is nil.

type Keyed added in v0.0.2

type Keyed[K comparable] interface {
	PrimaryKey() K
}

Keyed is implemented by types that have a unique, comparable primary key. KeyedModel uses this to emit fine-grained "added" and "deleted" events.

type KeyedModel added in v0.0.2

type KeyedModel[K comparable, T Keyed[K]] struct {
	// contains filtered or unexported fields
}

KeyedModel stores an ordered slice of items of type T (which must implement Keyed[K]).

Set and Clear only fire OnSet listeners — added/deleted/changed are not fired. Append fires OnAdded for each new key, then OnSet. Remove fires OnDeleted, then OnSet. Update fires OnChanged (existing key) or OnAdded (new key), then OnSet.

func (*KeyedModel[K, T]) Append added in v0.0.2

func (m *KeyedModel[K, T]) Append(items ...T)

Append adds items that do not already exist (by key). For each new item, OnAdded fires before OnSet. Items with an existing key are silently skipped.

func (*KeyedModel[K, T]) Clear added in v0.0.2

func (m *KeyedModel[K, T]) Clear()

Clear removes all items. Only OnSet listeners fire.

func (*KeyedModel[K, T]) Get added in v0.0.2

func (m *KeyedModel[K, T]) Get(key K) (T, bool)

Get returns the item with the given key and true, or the zero value and false if no such item exists.

func (*KeyedModel[K, T]) Items added in v0.0.2

func (m *KeyedModel[K, T]) Items() []T

Items returns a shallow copy of the stored items.

func (*KeyedModel[K, T]) Len added in v0.0.2

func (m *KeyedModel[K, T]) Len() int

Len returns the number of items in the model.

func (*KeyedModel[K, T]) OnAdded added in v0.0.2

func (m *KeyedModel[K, T]) OnAdded(fn func(AddedEvent[T]))

OnAdded registers fn to be called when a new item is inserted.

func (*KeyedModel[K, T]) OnChanged added in v0.0.2

func (m *KeyedModel[K, T]) OnChanged(fn func(ChangedEvent[T]))

OnChanged registers fn to be called when an existing item is updated.

func (*KeyedModel[K, T]) OnDeleted added in v0.0.2

func (m *KeyedModel[K, T]) OnDeleted(fn func(DeletedEvent[T]))

OnDeleted registers fn to be called when an item is removed.

func (*KeyedModel[K, T]) OnSet added in v0.0.2

func (m *KeyedModel[K, T]) OnSet(fn func([]T))

OnSet registers fn to be called after any mutation with the full item slice. fn must not modify the slice.

func (*KeyedModel[K, T]) Remove added in v0.0.2

func (m *KeyedModel[K, T]) Remove(key K)

Remove deletes the item with the given key. OnDeleted fires (with the former index) then OnSet fires. No-op if the key does not exist.

func (*KeyedModel[K, T]) Set added in v0.0.2

func (m *KeyedModel[K, T]) Set(items []T)

Set replaces all items wholesale. Only OnSet listeners fire; added, deleted, and changed listeners are not called.

func (*KeyedModel[K, T]) Update added in v0.0.2

func (m *KeyedModel[K, T]) Update(item T)

Update replaces the item that shares the same primary key, firing OnChanged then OnSet. If no item with that key exists yet, it is appended and OnAdded fires instead.

type LabelState added in v0.0.2

type LabelState interface {
	Label() string
	SetLabel(string) View
}

LabelState is implemented by a view that can set a label on a control

type Model added in v0.0.2

type Model[T any] struct {
	// contains filtered or unexported fields
}

Model stores an ordered slice of items of type T and notifies registered listeners whenever the contents change.

func (*Model[T]) AddEventListener added in v0.0.2

func (m *Model[T]) AddEventListener(fn func([]T))

AddEventListener registers fn to be called whenever the model changes. fn receives the current item slice (not a copy — do not modify it).

func (*Model[T]) Append added in v0.0.2

func (m *Model[T]) Append(items ...T)

Append adds items to the end of the slice and notifies all listeners.

func (*Model[T]) Clear added in v0.0.2

func (m *Model[T]) Clear()

Clear removes all items and notifies all listeners.

func (*Model[T]) Items added in v0.0.2

func (m *Model[T]) Items() []T

Items returns a shallow copy of the stored items.

func (*Model[T]) Len added in v0.0.2

func (m *Model[T]) Len() int

Len returns the number of items in the model.

func (*Model[T]) Set added in v0.0.2

func (m *Model[T]) Set(items []T)

Set replaces the stored items and notifies all listeners.

type Opt

type Opt func(OptSet) error

Opt is a function which can apply options to a view

func WithAriaLabel

func WithAriaLabel(label string) Opt

WithAriaLabel adds an aria-label attribute to a view

func WithAttr

func WithAttr(key, value string) Opt

func WithClass

func WithClass(classes ...string) Opt

func WithID

func WithID(id string) Opt

func WithSlotAttr added in v0.0.2

func WithSlotAttr(slot, key, value string) Opt

func WithStyle

func WithStyle(style string) Opt

func WithoutAttr

func WithoutAttr(keys ...string) Opt

func WithoutClass

func WithoutClass(classes ...string) Opt

type OptSet

type OptSet interface {
	// Return the name of the view
	Name() string

	// Return the classes of the view
	Classes() []string

	// Return an attribute value
	Attr(key string) string
}

OptSet interface for applying options

type PaginationState added in v0.0.2

type PaginationState interface {
	Offset() uint
	SetOffset(uint) View
	Limit() uint
	SetLimit(uint) View
	Count() uint
	SetCount(uint) View
}

PaginationState is implemented by a view that tracks paginated collection state in terms of offset, limit, and total count.

Page numbers and navigation state are derived from these values rather than stored independently.

type Provider added in v0.0.2

type Provider interface {
	// Fetch performs a single fetch and calls all listeners with the response or error.
	Fetch(path string, opts ...js.FetchOption)

	// FetchWithInterval starts periodic fetching at the given interval, calling
	// all listeners on each attempt. Fetches immediately, then repeats.
	// Call Cancel to stop.
	FetchWithInterval(path string, interval time.Duration, opts ...js.FetchOption)

	// Cancel stops any active interval fetch.
	Cancel()

	// AddEventListener registers a listener called on every fetch completion.
	// The response is nil on error; err is nil on success.
	AddEventListener(fn func(*js.FetchResponse, error))
}

Provider fetches data from a remote source and notifies registered listeners. Use AddEventListener to react to each fetch result (data or error).

func NewProvider added in v0.0.2

func NewProvider(base *url.URL) Provider

NewProvider creates a new provider with a base URL.

type ValueState added in v0.0.2

type ValueState interface {
	Value() string
	SetValue(string) View
}

ValueState is implemented by a view that exposes a string value.

type View

type View interface {
	// Return the view name
	Name() string

	// Return the view ID, if set
	ID() string

	// Return the view's root element
	Root() dom.Element

	// Return the view's parent view
	Parent() View

	// Return the view's direct child views from the default content slot
	Children() []View

	// Return self
	Self() View

	// Return a slot element by name, or nil if not found
	Slot(string) dom.Element

	// Replace a named slot with a node amnd apply options to the slot
	ReplaceSlot(string, any, ...Opt) View

	// Replace the children of a named slot with views, elements or text and apply options to the slot
	ReplaceSlotChildren(name string, args ...any) View

	// Apply class and attribute options to the root element
	Apply(...Opt) View

	// Replace the content of the view
	Content(...any) View

	// Add an event listener to the view's root element
	AddEventListener(string, func(dom.Event)) View

	// Remove an event listener from the view's root element
	RemoveEventListener(string) View
}

View represents a web component in the interface

func NewView

func NewView(self View, name string, template string, fn func(View, View), args ...any) View

Create a new empty view, applying any options to it

func NewViewWithElement

func NewViewWithElement(self View, element dom.Element, fn func(View, View), opts ...Opt) View

Create view from an existing element, applying any options to it

func ViewFromElement added in v0.0.2

func ViewFromElement(element dom.Element) (View, error)

ViewFromElement returns the View registered for the given element, or nil if the element has no data-mvc attribute or the view is not registered.

func ViewFromEvent

func ViewFromEvent(e dom.Event, views ...string) View

ViewFromEvent returns a View from an Event, or nil if the type is unsupported or not found. If one or more view names are provided, only views with those names are returned.

func ViewFromEventTarget added in v0.0.2

func ViewFromEventTarget(e dom.Event, views ...string) View

ViewFromEventTarget returns a View only if the event target itself is a registered view. Unlike ViewFromEvent, it does not walk ancestor elements.

type ViewConstructorFunc

type ViewConstructorFunc func(dom.Element) View

Constructor function for views

type VisibleGroup added in v0.0.2

type VisibleGroup interface {
	Visible() []View
	SetVisible(views ...View) View
}

VisibleGroup is implemented by a container that manages the visible/hidden state of its member views. The passed views are made visible; all others are hidden. Calling SetVisible with no arguments hides all members.

type VisibleState added in v0.0.2

type VisibleState interface {
	Visible() bool
	SetVisible(bool) View
}

VisibleState is implemented by a view that can be shown or hidden.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL