Documentation
¶
Overview ¶
Package router provides client-side routing for single-page applications built with GoWebComponents.
This package implements hash-based and history-based routing with support for:
- Multiple router instances
- Route guards (beforeEnter)
- Browser history integration
- Programmatic navigation
- Route-specific page titles
Basic usage with hash routing:
import "github.com/monstercameron/GoWebComponents/router"
func main() {
r := router.NewHashRouter()
r.RegisterRoute("/", HomePage)
r.RegisterRoute("/about", AboutPage)
r.RegisterRoute("/contact", ContactPage)
r.RegisterRoute("*", NotFoundPage) // Wildcard for 404
// Render the current route
render.To(r.GetRoute(), "#app")
}
Route components are regular components:
func HomePage(props dom.Attrs) *fiber.Element {
return dom.Div(nil,
dom.H1(nil, "Welcome Home"),
dom.A(map[string]interface{}{
"onclick": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
router.Navigate("/about")
return nil
}),
}, "Go to About"),
)
}
Route options:
r.RegisterRoute("/admin", AdminPanel, router.Options{
Title: "Admin Panel",
BeforeEnter: func(path string) bool {
return isAuthenticated() // Return false to prevent navigation
},
})
The package supports both hash-based routing (using URL fragments like #/about) and history-based routing (using the HTML5 History API).
Index ¶
- func GetCurrentPath() string
- func GetRoute() *render.Element
- func Navigate(path string)
- func NavigateReplace(path string)
- func RegisterRoute(path string, component interface{}, options ...Options)
- func RouteWithElement(path string, elemRef js.Value)
- type Component
- type Options
- type Router
- func (r *Router) Current() *render.Element
- func (r *Router) GetCurrentRouterPath() string
- func (r *Router) GoGetRoute() *render.Element
- func (r *Router) GoRegisterRoute(path string, component interface{}, options ...Options)
- func (r *Router) Mount(selector string)
- func (r *Router) MountElement(elem js.Value)
- func (r *Router) Navigate(path string)
- func (r *Router) NavigateReplace(path string)
- func (r *Router) Register(path string, component interface{}, options ...Options)
- type RouterOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetCurrentPath ¶
func GetCurrentPath() string
GetCurrentPath returns the current route path from the global router.
func Navigate ¶
func Navigate(path string)
Navigate updates the URL using the appropriate method for the current router.
func NavigateReplace ¶
func NavigateReplace(path string)
NavigateReplace replaces the current history entry using the appropriate method for the current router.
func RegisterRoute ¶
RegisterRoute registers a route with the global router.
func RouteWithElement ¶
RouteWithElement renders a route directly to a DOM element and sets up hash listening.
Types ¶
type Options ¶
type Options struct {
Title string
}
Options represents configuration for individual routes. Placeholder for future per-route settings (e.g., titles, guards).
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router manages routes and navigation for single-page applications.
func NewHashRouter ¶
func NewHashRouter(options ...RouterOptions) *Router
NewHashRouter creates a hash-based router that reads from window.location.hash.
Example ¶
// Create a new hash-based router
r := router.NewHashRouter()
// Define page components
homePage := func(props dom.Attrs) *render.Element {
return dom.Div(nil, dom.H1(nil, dom.Text("Home")))
}
aboutPage := func(props dom.Attrs) *render.Element {
return dom.Div(nil, dom.H1(nil, dom.Text("About")))
}
// Register routes
r.GoRegisterRoute("/", homePage)
r.GoRegisterRoute("/about", aboutPage)
// In a real app, you would mount the router to the DOM
// r.Mount("#app")
func NewRouter ¶
func NewRouter(options RouterOptions) *Router
NewRouter creates a history-based router using the HTML5 History API. This router uses window.location.pathname instead of hash fragments. Requires server to redirect all routes to the app's entry point.
func (*Router) GetCurrentRouterPath ¶
GetCurrentRouterPath returns the current path from a router instance based on its type.
func (*Router) GoGetRoute ¶
GoGetRoute returns the element for the current route.
func (*Router) GoRegisterRoute ¶
GoRegisterRoute registers a route on the router instance.
func (*Router) Mount ¶
Mount renders the router into a DOM node selected by CSS selector and wires hashchange listeners.
func (*Router) MountElement ¶
MountElement renders the router into an existing DOM element reference.
func (*Router) Navigate ¶
Navigate navigates to a path using the appropriate method for this router type.
func (*Router) NavigateReplace ¶
NavigateReplace replaces the current history entry using the appropriate method for this router type.
type RouterOptions ¶
type RouterOptions struct {
DefaultRoute string
}
RouterOptions configures router defaults.