flight

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

✈️ Flight

Flight is a standard-compliant, wrapper-free stack-based navigation system for Kitex applications. By favoring a type-safe stack navigator (push/pop) over URL-based path routing, Flight provides a paradigm that matches TUI (Terminal User Interface) design patterns and enforces strict keyboard focus containment to the active screen.

✨ Features

  • ✈️ Type-Safe Stack Navigation: Define routes as user-defined structs with type-safe parameters, rather than fragile URL/string path routing.
  • 🔒 Keyboard Focus Containment: Seamlessly restricts Tab focus loop and keyboard interaction to the top-most/active route screen.
  • 📦 Wrapper-Free Design: Focus containment registers the active route's underlying raw DOM element directly with focus.Manager via UseElement(), avoiding layout-breaking wrapper components like <focus-scope> or <box>.
  • 🪝 Ergonomic Hooks: Access the navigator stack anywhere in the component tree with UseNavigation().

🚀 Getting Started

Basic Navigation

First, define your routes as structs. Concrete routes can hold parameters:

package main

import (
	"github.com/masterkeysrd/kite/extras/flight"
	"github.com/masterkeysrd/kite/extras/kitex"
	"github.com/masterkeysrd/kite/style"
)

// Define your routes
type HomeRoute struct{}
type DetailsRoute struct {
	ItemID string
}

// Parent App component rendering the stack
var App = kitex.FC("App", func(_ kitex.NoProps) kitex.Node {
	return flight.Stack(flight.StackProps{
		InitialRoute: HomeRoute{},
		RenderRoute: func(r flight.Route) kitex.Node {
			switch route := r.(type) {
			case HomeRoute:
				return HomeView()
			case DetailsRoute:
				return DetailsView.Props(route.ItemID)
			default:
				panic("unknown route")
			}
		},
	})
})
Navigating Between Screens

Use the flight.UseNavigation() hook to access the Navigator interface and execute actions:

var HomeView = kitex.FC("HomeView", func(_ kitex.NoProps) kitex.Node {
	nav := flight.UseNavigation()

	return kitex.Box.Style(
		style.S().Padding(1, 2),
		kitex.Span.Style(style.S().Bold(true), kitex.Text("Welcome Home")),
		kitex.Button(kitex.ButtonProps{
			OnPress: func() {
				// Push a new details route onto the stack
				nav.Push(DetailsRoute{ItemID: "42"})
			},
		}, kitex.Text("Go to Details")),
	)
})

var DetailsView = kitex.FC("DetailsView", func(props string) kitex.Node {
	nav := flight.UseNavigation()

	return kitex.Box.Style(
		style.S().Padding(1, 2),
		kitex.Text("Item Details: " + props),
		kitex.Button(kitex.ButtonProps{
			OnPress: func() {
				// Pop the screen to return to the home screen
				nav.Pop()
			},
		}, kitex.Text("Go Back")),
	)
})

🛠 Navigator API

The Navigator interface returned by UseNavigation() provides the following operations to manage the stack:

  • Push(r Route): Pushes a new route onto the stack, making it the active screen.
  • Pop(): Pops the current top route from the stack, returning to the previous one (does nothing if only the initial route is present).
  • PopToTop(): Pops all routes above the initial root route (no-op if depth is 1 or less).
  • CanPop() bool: Reports whether the stack has more than one route (i.e., whether Pop() will navigate back).
  • Current() Route: Returns the active top-most route on the stack, or nil if empty.
  • Depth() int: Returns the number of routes currently on the stack.
  • Replace(r Route): Replaces the current top route with a new one.
  • Reset(r Route): Clears the stack entirely and sets the given route as the sole entry.

🔒 Keyboard Focus Isolation

Under the hood, Flight leverages Kitex's UseElement() and UseLayoutEffectCleanup hooks to obtain the raw DOM node of the active screen. When a screen becomes active, Flight pushes a new focus.Scope pointing to this DOM node onto the focus.Manager stack. This ensures:

  • Keyboard tab/arrow navigation is locked inside the active view's boundary.
  • Child components do not need to wrap themselves in arbitrary navigation UI wrappers.
  • The layout is not broken by structural VDOM wrapping nodes.

Documentation

Overview

Package flight implements a stack-based navigation system for Kitex applications.

By favoring a type-safe stack navigator (push/pop) over URL-based path routing, flight provides a paradigm that matches TUI (Terminal User Interface) design patterns and enforces strict keyboard focus containment to the active screen.

# Type-Safe Routes Routes are represented as empty interfaces (flight.Route). Concrete routes are defined using user-defined structs which hold type-safe parameters:

type HomeRoute struct{}
type DetailsRoute struct {
    ItemID string
}

# Rendering and Type Switches The Stack component renders the active route by dispatching it to a developer-provided RenderRoute function, which uses a Go type switch to instantiate the corresponding component:

flight.Stack(flight.StackProps{
    InitialRoute: HomeRoute{},
    RenderRoute: func(r flight.Route) kitex.Node {
        switch route := r.(type) {
        case HomeRoute:
            return HomeView()
        case DetailsRoute:
            return DetailsView(route.ItemID)
        default:
            panic("unknown route")
        }
    },
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Stack

func Stack(props StackProps) kitex.Node

Stack is a reactive navigation component that maintains a stack of active routes. It wraps the active route with NavigatorContext provider and dynamically isolates keyboard interaction to the top-most view using the active route's DOM node.

Types

type Navigator interface {
	// Push pushes a new route onto the stack.
	Push(r Route)
	// Pop pops the current route from the stack, returning to the previous one.
	Pop()
	// PopToTop pops all routes above the initial root route.
	PopToTop()
	// CanPop reports whether there is a previous route on the stack to pop to.
	CanPop() bool
	// Current returns the top-most route on the stack, or nil if the stack is empty.
	Current() Route
	// Depth returns the number of routes currently on the stack.
	Depth() int
	// Replace replaces the current top route with a new one.
	Replace(r Route)
	// Reset clears the stack and sets the given route as the only entry.
	Reset(r Route)
}

Navigator defines the operations to manipulate and inspect the navigation history stack.

func UseNavigation

func UseNavigation() Navigator

UseNavigation retrieves the current Navigator instance from the kitex context. It panics if called outside of a flight.Stack component.

type Route

type Route any

Route represents a target screen or view to navigate to. Concrete route configurations are user-defined structs.

type StackProps

type StackProps struct {
	InitialRoute Route
	RenderRoute  func(Route) kitex.Node
}

StackProps specifies the configuration properties for the Stack navigation component.

Jump to

Keyboard shortcuts

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