router

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2026 License: MIT Imports: 8 Imported by: 0

README

GitHub tag (latest SemVer) PkgGoDev

router

A Vue-like router for go-app

Usage

Register your routes with router.Register instead of using go-app's RegisterRoute functions.

router.Register(ctx,
	router.Route{
		Path: "/",
		Component: func() app.Composer {
			return &layout.CenterLayout{}
		},
		Meta: map[string]string{
			"require-login": "false",
		},
		Children: []router.Route{
			{
				Path: "/login",
				Component: func() app.Composer {
					return &page.LoginPage{}
				},
			},
			{
				Path: "/signup",
				Component: func() app.Composer {
					return &page.SignupPage{}
				},
			},
		},
	},
)

If a parent route's Component function returns a component that implements router.RouterViewInterface, then that component will be instantiated and SetRouterView will be called with the child component (and so on).

A component that implements router.RouterViewInterface should embed router.RouterViewComponent and include RouterViewComponent.RouterView() somewhere in its Render function.

For example:

type CenterLayout struct {
	app.Compo
	router.RouterViewComponent
}

var _ router.RouterViewInterface = (*CenterLayout)(nil)

func (c *CenterLayout) Render() app.UI {
	return app.Div().
		Class("center-layout").
		Body(
			c.RouterViewComponent.RouterView(),
		)
}

Documentation

Index

Constants

View Source
const StateRoute = "route"

StateRoute is the state key for the active route.

Variables

This section is empty.

Functions

func Debug added in v0.1.1

func Debug(debug bool)

Debug toggles debug logging for this package.

func DebugLayoutWrapper added in v0.1.1

func DebugLayoutWrapper(debug bool)

DebugLayoutWrapper toggles debug logging for LayoutWrapper.

func DebugRegister added in v0.1.1

func DebugRegister(debug bool)

DebugRegister toggles debug logging for Register.

func DebugRouterView added in v0.1.1

func DebugRouterView(debug bool)

DebugRouterView toggles debug logging for RouterView.

func Register

func Register(ctx context.Context, routes ...Route) error

Register the various routes.

Types

type ActiveRoute

type ActiveRoute struct {
	Path      string
	Meta      map[string]string
	Variables map[string]string
}

ActiveRoute is the active route that is currently being displayed.

func GetActiveRoute

func GetActiveRoute(ctx app.Context) ActiveRoute

GetActiveRoute gets the active route from the context.

func GetRoute

func GetRoute(ctx app.Context, path string) *ActiveRoute

GetRoute returns the appropriate route from the registered routes.

If no matching route is found, then this returns nil.

func (ActiveRoute) ReadVariable

func (a ActiveRoute) ReadVariable(name string, value *string) bool

ReadVariable retrieves the requested variable from the active route and stores it in the provided value.

This returns true if the variable was found and false if it was not.

type LayoutWrapper

type LayoutWrapper struct {
	app.Compo

	// These need to be public so that the component is properly re-rendered.
	LayoutComponent        app.Composer
	Meta                   map[string]string // TODO: Consider using `any` and having wrapper functions to get the values.
	Route                  route.Route
	RouteVariables         map[string]string
	PathVariablesFunctions []func(ctx app.Context, variables map[string]string)
}

LayoutWrapper is a wrapper around the desired component that handles top-level events to ensure that the component is properly updated.

func (*LayoutWrapper) OnMount

func (c *LayoutWrapper) OnMount(ctx app.Context)

TODO: REMOVE THIS IF WE DON'T NEED IT

func (*LayoutWrapper) OnNav

func (c *LayoutWrapper) OnNav(ctx app.Context)

func (*LayoutWrapper) OnUpdate

func (c *LayoutWrapper) OnUpdate(ctx app.Context)

TODO: REMOVE THIS IF WE DON'T NEED IT

func (*LayoutWrapper) Render

func (c *LayoutWrapper) Render() app.UI

type Route

type Route struct {
	Path          string                                             // The path of the route.  This may optionally start with "/", and variables are of the form ":variable_name".
	PathVariables func(ctx app.Context, variables map[string]string) // A function that will be called with the current path variables; additional work can be done to set others.
	Component     func() app.Composer                                // A function that will be called to create the component for the route.
	Meta          map[string]string                                  // Metadata for the route.
	Children      []Route                                            // Children routes (if any).
}

Route is a route that can be applied to the application.

This is analogous to a Vue router route.

type RouterViewComponent

type RouterViewComponent struct {
	IRouterViewComponent app.Composer
}

RouterViewComponent can be embedded in a layout component.

The router wil set the specific router view component with `SetComponent`, and the embedding component can call `RouterViewComponent` in its `Render` function to get the component that should be rendered in the router view.

func (*RouterViewComponent) OnUpdate

func (v *RouterViewComponent) OnUpdate(ctx app.Context)

func (*RouterViewComponent) RouterView

func (v *RouterViewComponent) RouterView() app.Composer

RouterView returns the router view comonent. Put this where you want the route component to be rendered.

In Vue, this would be the `<router-view>` component.

func (*RouterViewComponent) SetRouterView

func (v *RouterViewComponent) SetRouterView(component app.Composer)

SetRouterView is used by the router to set the component that will be returned by `RouterView`.

This should not be called by anything but the router.

type RouterViewInterface

type RouterViewInterface interface {
	RouterView() app.Composer
	SetRouterView(app.Composer)
}

RouterViewInterface is the interface that must be implemented in order for a component to be properly used in a route as a layout.

Instead of implementing this interface, a component should embed `RouterViewComponent`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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