Some checks failed
CI / build (push) Failing after 36s
WASM reactive UI framework for Go: - reactive/ - Signal[T], Effect, Runtime - ui/ - Button, Text, Input, View, Canvas, SVG components - navigation/ - Router, guards, history management - auth/ - OIDC client for WASM applications - host/ - Static file server Extracted from arcadia as open-source component. Co-Authored-By: Claude <noreply@anthropic.com>
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
//build +js,wasm
|
|
|
|
package navigation
|
|
|
|
import (
|
|
"syscall/js"
|
|
)
|
|
|
|
type HistoryManager struct {
|
|
router *Router
|
|
window js.Value
|
|
}
|
|
|
|
func NewHistoryManager(router *Router) *HistoryManager {
|
|
return &HistoryManager{
|
|
router: router,
|
|
window: js.Global().Get("window"),
|
|
}
|
|
}
|
|
|
|
func (h *HistoryManager) Start() {
|
|
// Handle initial navigation
|
|
currentPath := h.getCurrentPath()
|
|
h.router.Navigate(currentPath)
|
|
|
|
// Listen for browser back/forward events
|
|
h.window.Call("addEventListener", "popstate", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
|
path := h.getCurrentPath()
|
|
h.router.Navigate(path)
|
|
return nil
|
|
}))
|
|
}
|
|
|
|
func (h *HistoryManager) PushState(path string) {
|
|
h.window.Get("history").Call("pushState", nil, "", path)
|
|
h.router.Navigate(path)
|
|
}
|
|
|
|
func (h *HistoryManager) ReplaceState(path string) {
|
|
h.window.Get("history").Call("replaceState", nil, "", path)
|
|
h.router.Navigate(path)
|
|
}
|
|
|
|
func (h *HistoryManager) Back() {
|
|
h.window.Get("history").Call("back")
|
|
}
|
|
|
|
func (h *HistoryManager) Forward() {
|
|
h.window.Get("history").Call("forward")
|
|
}
|
|
|
|
func (h *HistoryManager) getCurrentPath() string {
|
|
location := h.window.Get("location")
|
|
pathname := location.Get("pathname").String()
|
|
search := location.Get("search").String()
|
|
|
|
path := pathname
|
|
if search != "" {
|
|
path += search
|
|
}
|
|
|
|
return path
|
|
} |