hx

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2025 License: MIT Imports: 6 Imported by: 0

README

logo

Hix

Hix is a reactive, component-based Golang WASM frontend framework. Example:

Here is the demo

func counter() hx.INode {
	c := hx.Signal(0)
	hx.EffectFunc(func() {
		fmt.Println("Change!", c.Get())
	})
	str := hx.Computed(func() string {
		return strconv.FormatInt(int64(c.Get()), 10)
	})

	div := hx.Div().
		BindText(str).
		Style("float", "left").
		Style("padding-left", "20px").
		Style("padding-right", "20px")
	return hx.Div().Body(
		hx.Button().
			Text("-").
			Style("float", "left").
			OnClick(func(ctx hx.EventContext) {
				c.Set(c.Get() - 1)
			}),
		div,
		hx.Button().
			Text("+").
			Style("float", "left").
			OnClick(func(ctx hx.EventContext) {
				c.Set(c.Get() + 1)
			}),
	)
}

func showMessage(msg string) hx.INode {
	show := hx.Signal(false)
	div := hx.Div().Id("43")

	hx.EffectFunc(func() {
		div.Body(
			hx.If(show, hx.P().Text(msg)),
			hx.Button().Text("Toggle").OnClick(func(ctx hx.EventContext) {
				show.Set(!show.Get())
			}),
			hx.A().Href("http://google.es").Text("Go to google"),
		)
	})

	return div
}

func demoEach() hx.INode {
	elements := hx.Signal([]string{})
	textInput := hx.Signal("")

	main := hx.Div()

	hx.EffectFunc(func() {
		input := hx.Input().BindOnInput(textInput)
		addBtn := hx.Button().Text("Add").OnClick(func(ctx hx.EventContext) {
			ee := elements.Get()
			ee = append(ee, textInput.Get())
			textInput.Set("")
			elements.Set(ee)
		})

		main.Body(
			input,
			addBtn,
			hx.Ul().Body(
				hx.Each(elements, func(index int, value string) hx.INode {
					return hx.Li().Body(
						hx.P().Text(fmt.Sprintf("[%d] %s", index, value)),
						hx.Button().Text("Delete").OnClick(func(ctx hx.EventContext) {
							ee := elements.Get()
							ee = append(ee[:index], ee[index+1:]...)
							elements.Set(ee)
						}),
					)
				}),
			),
		)
	})
	return hx.Div().Body(
		hx.H1().Text("List of items"),
		main,
	)
}

func reactTextInput() hx.INode {
	text := hx.Signal("")
	return hx.Div().Body(
		hx.P().BindText(text),
		hx.Input().BindOnInput(text),
	)
}

func main() {
	point := hx.NewFromId("wasm_mount_point")
	point.Body(
		hx.Div().Body(
			hx.P().
				Style("background-color", "blue").
				Style("color", "white").
				Text("Hola").
				On(hx.EventClick, func(ctx hx.EventContext) {
					ctx.Target.
						Style("background-color", "black").
						Style("color", "red")
				}),
			hx.Button().
				Text("Show something in console").
				On(hx.EventClick, func(ctx hx.EventContext) {
					fmt.Println("Hi!")
				}),
			counter(),
			hx.Br(),
			showMessage("Toggle text"),
			reactTextInput(),
			demoEach(),
		),
	)
	select {}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AVNode

type AVNode struct {
	VNode
}

func A

func A() *AVNode

func (*AVNode) Href

func (e *AVNode) Href(v string) *AVNode

type ComputedT

type ComputedT[T comparable] struct {
	// contains filtered or unexported fields
}

func Computed

func Computed[T comparable](fn func() T) *ComputedT[T]

func (*ComputedT[T]) Get

func (c *ComputedT[T]) Get() T

type DiffRenderer added in v0.1.4

type DiffRenderer struct {
	// contains filtered or unexported fields
}

func (DiffRenderer) GetMarkedCommonAncestor added in v0.1.4

func (renderer DiffRenderer) GetMarkedCommonAncestor() *VNode

func (*DiffRenderer) Mark added in v0.1.4

func (r *DiffRenderer) Mark(element *VNode)

func (*DiffRenderer) ScheduleRender added in v0.1.4

func (r *DiffRenderer) ScheduleRender()

type Effect

type Effect struct {
	// contains filtered or unexported fields
}

func EffectFunc

func EffectFunc(fn func()) *Effect

type Event

type Event string
const (
	EventClick  Event = "click"
	EventInput  Event = "input"
	EventChange Event = "change"
)

type EventContext

type EventContext struct {
	Target *VNode
	Event  dom.Event
}

type Gettable

type Gettable[T any] interface {
	Get() T
}

type INode

type INode interface {
	Id(id string) INode

	Text(t string) INode
	BindText(signal Gettable[string]) INode

	Body(childs ...INode) INode
	BodyList(childs []INode) INode

	Attribute(key, value string) INode
	RemoveAttribute(key string) INode
	Style(key, value string) INode
	RemoveStyle(key string) INode
	Class(classes ...string) INode
	RemoveClass(classes ...string) INode

	On(event Event, handler func(ctx EventContext)) INode
	OnClick(handler func(ctx EventContext)) INode
}

func Each

func Each[T any](src Gettable[[]T], renderOne func(index int, value T) INode) INode

func EachMap added in v0.1.2

func EachMap[K comparable, T any](src Gettable[map[K]T], renderOne func(index K, value T) INode) INode

func If

func If(condition Gettable[bool], child INode) INode

func Show added in v0.1.3

func Show(condition Gettable[bool], ifPath, elsePath INode) INode

type InputVNode

type InputVNode struct {
	VNode
}

func Input

func Input() *InputVNode

func (*InputVNode) BindOnChange added in v0.1.3

func (element *InputVNode) BindOnChange(signal Settable[string]) *InputVNode

func (*InputVNode) BindOnInput

func (element *InputVNode) BindOnInput(signal Settable[string]) *InputVNode

func (*InputVNode) BindValue added in v0.1.2

func (element *InputVNode) BindValue(signal Gettable[string]) *InputVNode

func (*InputVNode) Placeholder

func (e *InputVNode) Placeholder(v string) *InputVNode

func (*InputVNode) Type

func (e *InputVNode) Type(v string) *InputVNode

func (*InputVNode) Value

func (element *InputVNode) Value(t string) INode

type NoopNode added in v0.1.2

type NoopNode struct {
	VNode
}

func Noop added in v0.1.2

func Noop() *NoopNode

func (*NoopNode) Attribute added in v0.1.2

func (nop *NoopNode) Attribute(key, value string) INode

func (*NoopNode) BindText added in v0.1.2

func (nop *NoopNode) BindText(signal Gettable[string]) INode

func (*NoopNode) Body added in v0.1.2

func (nop *NoopNode) Body(childs ...INode) INode

func (*NoopNode) BodyList added in v0.1.2

func (nop *NoopNode) BodyList(childs []INode) INode

func (*NoopNode) Class added in v0.1.2

func (nop *NoopNode) Class(classes ...string) INode

func (*NoopNode) Id added in v0.1.2

func (nop *NoopNode) Id(id string) INode

func (*NoopNode) On added in v0.1.2

func (nop *NoopNode) On(event Event, handler func(ctx EventContext)) INode

func (*NoopNode) OnClick added in v0.1.2

func (nop *NoopNode) OnClick(handler func(ctx EventContext)) INode

func (*NoopNode) RemoveAttribute added in v0.1.2

func (nop *NoopNode) RemoveAttribute(key string) INode

func (*NoopNode) RemoveClass added in v0.1.2

func (nop *NoopNode) RemoveClass(classes ...string) INode

func (*NoopNode) RemoveStyle added in v0.1.2

func (nop *NoopNode) RemoveStyle(key string) INode

func (*NoopNode) Style added in v0.1.2

func (nop *NoopNode) Style(key, value string) INode

func (*NoopNode) Text added in v0.1.2

func (nop *NoopNode) Text(t string) INode

type Renderer added in v0.1.4

type Renderer interface {
	ScheduleRender()
	Mark(element *VNode)
}

type Settable

type Settable[T any] interface {
	Set(T)
}

type SignalT

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

func Signal

func Signal[T any](initial T) *SignalT[T]

func (*SignalT[T]) Get

func (signal *SignalT[T]) Get() T

func (*SignalT[T]) Set

func (signal *SignalT[T]) Set(v T)

func (*SignalT[T]) Update

func (signal *SignalT[T]) Update(fn func(T) T)

type StringRenderer added in v0.1.4

type StringRenderer struct {
	// contains filtered or unexported fields
}

func (*StringRenderer) Mark added in v0.1.4

func (ssr *StringRenderer) Mark(element *VNode)

func (*StringRenderer) Render added in v0.1.4

func (ssr *StringRenderer) Render(current *VNode)

func (*StringRenderer) Reset added in v0.1.4

func (ssr *StringRenderer) Reset()

func (*StringRenderer) ScheduleRender added in v0.1.4

func (ssr *StringRenderer) ScheduleRender()

func (*StringRenderer) String added in v0.1.4

func (ssr *StringRenderer) String() string

type TextAreaNode added in v0.1.4

type TextAreaNode struct {
	VNode
}

func TextArea

func TextArea() *TextAreaNode

func (*TextAreaNode) BindValue added in v0.1.4

func (element *TextAreaNode) BindValue(signal Gettable[string]) *TextAreaNode

func (*TextAreaNode) Value added in v0.1.4

func (element *TextAreaNode) Value(t string) INode

type VNode

type VNode struct {
	// contains filtered or unexported fields
}

func Article

func Article() *VNode

func Aside

func Aside() *VNode

func Audio

func Audio() *VNode

func Br

func Br() *VNode

func Button

func Button() *VNode

func Canvas

func Canvas() *VNode

func Code added in v0.1.4

func Code() *VNode

func Div

func Div() *VNode

func Em

func Em() *VNode
func Footer() *VNode

func Form

func Form() *VNode

func H1

func H1() *VNode

func H2

func H2() *VNode

func H3

func H3() *VNode

func H4

func H4() *VNode

func H5

func H5() *VNode

func H6

func H6() *VNode
func Header() *VNode

func Hr

func Hr() *VNode

func Img

func Img() *VNode

func Label

func Label() *VNode

func Li

func Li() *VNode

func Main

func Main() *VNode
func Nav() *VNode

func New

func New(element dom.Element) *VNode

func NewFromId

func NewFromId(id string) *VNode

func NewFromIdWithRenderer added in v0.1.4

func NewFromIdWithRenderer(id string, renderer Renderer) *VNode

func NewWithRenderer added in v0.1.4

func NewWithRenderer(element dom.Element, renderer Renderer) *VNode

func NewWithoutMount added in v0.1.4

func NewWithoutMount(tag string, renderer Renderer) *VNode

func Ol

func Ol() *VNode

func Option

func Option() *VNode

func P

func P() *VNode

func Path

func Path() *VNode

func Pre added in v0.1.4

func Pre() *VNode

func Section

func Section() *VNode

func Select

func Select() *VNode

func Small

func Small() *VNode

func Source

func Source() *VNode

func Span

func Span() *VNode

func Strong

func Strong() *VNode

func Svg

func Svg() *VNode

func TBody

func TBody() *VNode

func TFoot

func TFoot() *VNode

func THead

func THead() *VNode

func Table

func Table() *VNode

func Td

func Td() *VNode

func Th

func Th() *VNode

func Tr

func Tr() *VNode

func Ul

func Ul() *VNode

func Video

func Video() *VNode

func (*VNode) Attribute

func (element *VNode) Attribute(key, value string) INode

func (*VNode) BindText

func (element *VNode) BindText(signal Gettable[string]) INode

func (*VNode) Body

func (element *VNode) Body(childs ...INode) INode

func (*VNode) BodyList

func (element *VNode) BodyList(childs []INode) INode

func (*VNode) Class

func (element *VNode) Class(classes ...string) INode

func (*VNode) Id

func (element *VNode) Id(id string) INode

func (*VNode) On

func (element *VNode) On(event Event, handler func(ctx EventContext)) INode

func (*VNode) OnClick

func (element *VNode) OnClick(handler func(ctx EventContext)) INode

func (*VNode) RemoveAttribute

func (element *VNode) RemoveAttribute(key string) INode

func (*VNode) RemoveClass

func (element *VNode) RemoveClass(classes ...string) INode

func (*VNode) RemoveStyle

func (element *VNode) RemoveStyle(key string) INode

func (*VNode) Src

func (e *VNode) Src(v string) INode

func (*VNode) Style

func (element *VNode) Style(key, value string) INode

func (*VNode) Text

func (element *VNode) Text(t string) INode

type ValueWrapper added in v0.1.2

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

func Value added in v0.1.2

func Value[T any](t T) ValueWrapper[T]

func (ValueWrapper[T]) Get added in v0.1.2

func (v ValueWrapper[T]) Get() T

func (*ValueWrapper[T]) Set added in v0.1.2

func (v *ValueWrapper[T]) Set(t T)

Jump to

Keyboard shortcuts

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