Skip to content

Commit 5f731e0

Browse files
committed
docs: (WIP) merge examples and docs projects
1 parent 570625e commit 5f731e0

File tree

82 files changed

+311
-395
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+311
-395
lines changed

docs/articles/api/animation.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,6 @@ and a duration. They can be invoked from event handlers registered in the DOM.
1616
`Keyframes` accepts arrays of frame definitions and option maps that are
1717
passed directly to the browser's `Element.animate` API.
1818

19-
## Example
19+
The following example animates elements using these helpers.
2020

21-
```go
22-
dom.RegisterHandlerFunc("animateFade", func() {
23-
anim.Fade("#fadeBox", 1, 0, 500*time.Millisecond)
24-
})
25-
26-
dom.RegisterHandlerFunc("animateSpin", func() {
27-
frames := []map[string]any{
28-
{"transform": "rotate(0deg)"},
29-
{"transform": "rotate(360deg)"},
30-
}
31-
opts := map[string]any{"duration": 500, "iterations": 1}
32-
anim.Keyframes("#spinBox", frames, opts)
33-
})
34-
```
35-
36-
1. `dom.RegisterHandlerFunc` links the `animateFade` ID to a Go function.
37-
2. When triggered, `anim.Fade` fades `#fadeBox` from opaque to transparent
38-
over half a second.
39-
3. `anim.Keyframes` delegates to the Web Animations API to spin `#spinBox`
40-
once around its center.
21+
@include:ExampleFrame:{code:"/examples/components/animation_component.go", uri:"/examples/animations"}

docs/articles/api/core.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ For structs embedding `*core.HTMLComponent`, use `core.NewComponentWith(name, tp
2929
Components are created with `core.NewComponent` by passing a name, template
3030
and initial properties. Dependencies are added with `AddDependency`.
3131

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`.
32+
The example below shows how core components are composed.
33+
34+
@include:ExampleFrame:{code:"/examples/components/parent_component.go", uri:"/examples/parent"}

docs/articles/api/dom.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ are available for advanced use:
1515
Besides the methods shown, the package exposes `RegisterHandlerFunc` to bind
1616
Go functions to named DOM events.
1717

18-
## Example
18+
The snippet demonstrates direct DOM interactions.
1919

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.
20+
@include:ExampleFrame:{code:"/examples/components/event_component.go", uri:"/examples/event"}

docs/articles/api/events.md

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,6 @@ separate mechanism from DOM events.
1717
`Listen` turns browser events into Go channels. You can range over the
1818
channel to react to events concurrently.
1919

20-
## Example
20+
This example listens for browser events and reacts in Go.
2121

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.
22+
@include:ExampleFrame:{code:"/examples/components/event_listener_component.go", uri:"/examples/event/listener"}

docs/articles/api/plugins.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,6 @@ Plugins must be registered before the application starts using
2020
`core.RegisterPlugin` or `app.Use`. During `Setup` they can modify the app or
2121
add features.
2222

23-
## Example
23+
Plugins can hook into the app lifecycle as illustrated here.
2424

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.
25+
@include:ExampleFrame:{code:"/examples/plugins/plugins_component.go", uri:"/examples/plugins"}

docs/articles/api/router.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ Client-side router with lazy loaded components and guards.
1111
Routes are defined with `router.RegisterRoute` by specifying the path and the
1212
component to mount. `router.InitRouter` starts the router.
1313

14-
## Example
14+
Routing parameters are handled as in the following component.
1515

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.
16+
@include:ExampleFrame:{code:"/examples/components/another_component.go", uri:"/examples/user/jane"}

docs/articles/api/state.md

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,6 @@ Stores are created with `state.NewStore` and read or updated via `Get` and
2222
`Set`. Derived values can be defined with `RegisterComputed` or convenience
2323
helpers like `Map` and `Map2`.
2424

25-
## Example
25+
State stores drive UI updates in this example.
2626

27-
```go
28-
if store.Get("double") == nil {
29-
state.Map(store, "double", "count", func(v int) int {
30-
return v * 2
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. `Map` 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.
45-
46-
### Advanced computed values
47-
48-
`RegisterComputed` provides full control for deriving data from multiple
49-
dependencies. It accepts a `Computed` definition listing the keys to watch
50-
and a function that receives their current values in a map. Use it when the
51-
`Map` helpers are insufficient:
52-
53-
```go
54-
import "fmt"
55-
56-
if store.Get("profile") == nil {
57-
store.RegisterComputed(state.NewComputed(
58-
"profile",
59-
[]string{"first", "last", "age"},
60-
func(m map[string]any) any {
61-
first, _ := m["first"].(string)
62-
last, _ := m["last"].(string)
63-
age, _ := m["age"].(int)
64-
return fmt.Sprintf("%s %s (%d)", first, last, age)
65-
},
66-
))
67-
}
68-
```
69-
70-
This example combines three fields into a single formatted string, but the
71-
function can perform any transformation and return any type.
27+
@include:ExampleFrame:{code:"/examples/components/state_management_component.go", uri:"/examples/state"}

docs/articles/guide/animation.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ anim.Translate("#box", anim.Point{X:0, Y:0}, anim.Point{X:100, Y:0}, time.Second
77
```
88

99
Helpers like `Fade`, `Scale` and `ColorCycle` operate on elements selected by CSS selectors.
10+
This demo animates boxes with helper functions.
11+
12+
@include:ExampleFrame:{code:"/examples/components/animation_component.go", uri:"/examples/animations"}

docs/articles/guide/api-integration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ go func() {
1414
The accompanying example component triggers a fetch to a public API when a button is
1515
clicked, performing the request in a goroutine so the event loop stays responsive,
1616
and displays the received data once available.
17+
It fetches data from an API and renders the result.
18+
19+
@include:ExampleFrame:{code:"/examples/components/api_integration_component.go", uri:"/examples/api"}

docs/articles/guide/complex-routing.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ router.RegisterRoute(router.Route{
1313

1414
Navigating to `/complex/alice/settings` renders the component with the
1515
route parameters injected as props, enabling deep links and hierarchies.
16+
It shows routing with multiple parameters.
17+
18+
@include:ExampleFrame:{code:"/examples/components/complex_routing_component.go", uri:"/examples/complex/jane/profile"}

0 commit comments

Comments
 (0)