Flight: move between screens
Use extras/flight when Back should mean “return to the previous destination.” For a temporary confirmation over the current screen, an overlay may be a better fit.
Define destinations as data
Section titled “Define destinations as data”flight.Route accepts a Go value. There is no path matcher or parameter parser: carry the parameters in fields. A stack requires both a non-nil initial route and a renderer. Here inbox and message are the two screen components.
type InboxRoute struct{}
type MessageRoute struct { ID string}
var Inbox = kitex.FC("Inbox", func(kitex.NoProps) kitex.Node { nav := flight.UseNavigation() messages := []string{"Build passed", "Review requested"}
rows := kitex.Map(messages, func(id string, _ int) kitex.Node { read := kitex.Button(kitex.ButtonProps{ Style: demo.Default.Button, OnPress: func() { nav.Push(MessageRoute{ID: id}) }, }, kitex.Text("Read message"))
return kitex.Box( kitex.BoxProps{Key: id, Style: demo.Default.ButtonRow}, kitex.Text(id), read, ) })
return kitex.Box.Style(demo.Default.Column(), kitex.Text("Inbox"), rows)})
var Message = kitex.FC("Message", func(p MessageRoute) kitex.Node { nav := flight.UseNavigation()
return kitex.Box.Style( demo.Default.Column(), kitex.Text("Message: "+p.ID), kitex.Button(kitex.ButtonProps{ Style: demo.Default.Button, OnPress: func() { nav.Pop() }, }, kitex.Text("← Back")), )})
var InboxStack = kitex.FC("InboxStack", func(_ kitex.NoProps) kitex.Node { render := func(r flight.Route) kitex.Node { switch p := r.(type) { case InboxRoute: return Inbox() case MessageRoute: return Message.Props(p) default: return kitex.Text("Unknown destination") } }
return flight.Stack(flight.StackProps{ InitialRoute: InboxRoute{}, RenderRoute: render, })})Choose what Back should do
Section titled “Choose what Back should do”Inside a screen component call nav := flight.UseNavigation(). Call its methods from event handlers, not during render. For example, an inbox row for id can return:
return kitex.Button(kitex.ButtonProps{ OnPress: func() { nav.Push(MessageRoute{ID: id}) },}, kitex.Text("Read message"))| Operation | Use it when… |
|---|---|
Push(route) |
The reader should be able to return here. |
Pop() |
Back should reveal the previous route. At the root it’s a no-op. |
PopToTop() |
You want to return directly to the initial root route (no-op at depth 1). |
CanPop() |
You need to decide whether to offer Back. |
Current() |
You need the active top-most route (or nil when empty). |
Depth() |
You need the number of routes currently on the stack. |
Replace(route) |
A completed step should not remain in history. |
Reset(route) |
A new session should start with a single destination. |
Keep shared data above the stack
Section titled “Keep shared data above the stack”Only the active route is rendered. Flight installs a focus scope for that view and removes it when the active route changes. You don’t need to disable every control on the previous screen. Route history is not a cache of mounted components: put drafts or records that must survive navigation in a provider above the stack, and pass IDs in routes.