Skip to content

Commit bd5309e

Browse files
committed
docs: improve api documentation
1 parent 2baf32e commit bd5309e

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed

docs/articles/api/animation.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,20 @@ Helpers for driving simple animations without the Web Animations API.
66
- `Fade(sel, from, to, dur)` adjusts opacity.
77
- `Scale(sel, from, to, dur)` scales elements.
88
- `ColorCycle(sel, colors, dur)` cycles background colors.
9+
10+
## Usage
11+
12+
Call the animation helpers with a CSS selector, starting and ending values,
13+
and a duration. They can be invoked from event handlers registered in the DOM.
14+
15+
## Example
16+
17+
```go
18+
dom.RegisterHandlerFunc("animateFade", func() {
19+
anim.Fade("#fadeBox", 1, 0, 500*time.Millisecond)
20+
})
21+
```
22+
23+
1. `dom.RegisterHandlerFunc` links the `animateFade` ID to a Go function.
24+
2. When triggered, `anim.Fade` fades `#fadeBox` from opaque to transparent
25+
over half a second.

docs/articles/api/core.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,24 @@ mounted with `app.Mount(component)`.
2323

2424
`core.NewComponent(name, tpl, props)` returns an initialized `*core.HTMLComponent` with the provided template and props.
2525
For structs embedding `*core.HTMLComponent`, use `core.NewComponentWith(name, tpl, props, self)` to bind lifecycle hooks without manual setup.
26+
27+
## Usage
28+
29+
Components are created with `core.NewComponent` by passing a name, template
30+
and initial properties. Dependencies are added with `AddDependency`.
31+
32+
## Example
33+
34+
```go
35+
c := core.NewComponent("MainComponent", mainComponentTpl, nil)
36+
headerComponent := NewHeaderComponent(map[string]any{
37+
"title": "Main Component",
38+
})
39+
c.AddDependency("header", headerComponent)
40+
```
41+
42+
1. `core.NewComponent` instantiates the component with its template.
43+
2. `NewHeaderComponent` creates a dependency to inject into the main
44+
component.
45+
3. `AddDependency` makes the dependency available in the template under the
46+
name `header`.

docs/articles/api/dom.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,23 @@ are available for advanced use:
99
| `CreateElement(tag)` | Returns a new element. |
1010
| `ByID(id)` | Fetches an element by id. |
1111
| `SetInnerHTML(el, html)` | Replaces an element's children with raw HTML. |
12+
13+
## Usage
14+
15+
Besides the methods shown, the package exposes `RegisterHandlerFunc` to bind
16+
Go functions to named DOM events.
17+
18+
## Example
19+
20+
```go
21+
dom.RegisterHandlerFunc("increment", func() {
22+
if val, ok := c.Store.Get("count").(int); ok {
23+
c.Store.Set("count", val+1)
24+
}
25+
})
26+
```
27+
28+
1. `dom.RegisterHandlerFunc` associates the `increment` identifier with a
29+
function.
30+
2. When called from the DOM, the function reads `count` from the store and
31+
increments it.

docs/articles/api/events.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,29 @@ Utilities for observing browser events and DOM mutations.
1111
`Listen` converts native DOM events into Go channels. For application
1212
state changes use reactive stores which emit their own notifications – a
1313
separate mechanism from DOM events.
14+
15+
## Usage
16+
17+
`Listen` turns browser events into Go channels. You can range over the
18+
channel to react to events concurrently.
19+
20+
## Example
21+
22+
```go
23+
btn := js.Document().Call("getElementById", "clickBtn")
24+
ch := events.Listen("click", btn)
25+
go func() {
26+
for range ch {
27+
switch v := cmp.Store.Get("clicks").(type) {
28+
case float64:
29+
cmp.Store.Set("clicks", v+1)
30+
case int:
31+
cmp.Store.Set("clicks", float64(v)+1)
32+
}
33+
}
34+
}()
35+
```
36+
37+
1. Obtain the button from the DOM.
38+
2. `events.Listen` creates a channel that receives clicks on the button.
39+
3. A goroutine increments the store whenever an event arrives.

docs/articles/api/plugins.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,24 @@ type Plugin interface {
1313
Plugins are registered with `app.Use(...)` before compiling the WASM
1414
bundle. During `Setup` they can register components, add routes or inject
1515
scripts.
16+
17+
## Usage
18+
19+
Plugins must be registered before the application starts using
20+
`core.RegisterPlugin` or `app.Use`. During `Setup` they can modify the app or
21+
add features.
22+
23+
## Example
24+
25+
```go
26+
core.RegisterPlugin(logger.New())
27+
core.RegisterPlugin(i18n.New(map[string]map[string]string{
28+
"en": {"hello": "Hello"},
29+
"it": {"hello": "Ciao"},
30+
}))
31+
core.RegisterPlugin(mon.New())
32+
```
33+
34+
1. Multiple plugins are registered before the app starts.
35+
2. The `logger` plugin intercepts logs while `i18n` provides translations.
36+
3. `mon.New()` installs an additional monitoring plugin.

docs/articles/api/router.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,21 @@ Client-side router with lazy loaded components and guards.
55
- `RegisterRoute(Route)` adds a route definition.
66
- `Navigate(path)` programmatically changes the URL.
77
- Guards: `Guard` functions run before navigation and can cancel by returning `false`.
8+
9+
## Usage
10+
11+
Routes are defined with `router.RegisterRoute` by specifying the path and the
12+
component to mount. `router.InitRouter` starts the router.
13+
14+
## Example
15+
16+
```go
17+
router.RegisterRoute(router.Route{
18+
Path: "/",
19+
Component: func() core.Component { return components.NewMainComponent() },
20+
})
21+
router.InitRouter()
22+
```
23+
24+
1. `RegisterRoute` associates the root path with the main component.
25+
2. `InitRouter` listens for URL changes and renders the current route.

docs/articles/api/state.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,32 @@ Centralized reactive data stores.
1313
Stores are the primary mechanism for application state. They emit
1414
notifications to any component that reads their values, keeping most apps
1515
free of handwritten JavaScript.
16+
17+
## Usage
18+
19+
Stores are created with `state.NewStore` and read or updated via `Get` and
20+
`Set`. Derived values can be defined with `RegisterComputed`.
21+
22+
## Example
23+
24+
```go
25+
if store.Get("double") == nil {
26+
store.RegisterComputed(state.NewComputed("double", []string{"count"}, func(m map[string]any) any {
27+
if v, ok := m["count"].(int); ok {
28+
return v * 2
29+
}
30+
return 0
31+
}))
32+
}
33+
34+
func (c *StateManagementComponent) Increment() {
35+
if v, ok := c.Store.Get("count").(int); ok {
36+
c.Store.Set("count", v+1)
37+
}
38+
}
39+
```
40+
41+
1. `RegisterComputed` defines the derived value `double` based on `count`.
42+
2. The `Increment` function reads `count` and updates it with `Set`.
43+
3. Each change automatically updates parts of the UI that depend on these
44+
values.

0 commit comments

Comments
 (0)