focus

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: 4 Imported by: 0 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 ΒΆ

Package focus

Package focus implements focus management and keyboard navigation for Kite.

🧠 Focus Management

Focus state is managed by the focus.Manager, which tracks:

  • Current Focus: The dom.Node that currently has focus.
  • Reason: How focus was acquired (ReasonProgrammatic, ReasonPointer, ReasonKeyboard, ReasonRestore).
  • Scopes: A stack of focus.Scope objects that restrict navigation to specific subtrees (e.g., for modals).

🎯 Focusable Criteria

A node is considered focusable if it meets all of the following conditions:

  1. Semantic Capability: The logical node must implement dom.Focusable and IsFocusable() must return true.
  2. Enabled State: If the node implements dom.Disableable, IsDisabled() must return false.
  3. Visibility: Its associated render.Object must exist, and its ComputedStyle().Display must not be DisplayNone.
  4. Scope: The node must be within the currently active focus scope.

⌨️ Navigation

Tab Navigation

The focus.Manager provides Next() and Previous() methods to navigate through focusable elements in DOM tree order.

Spatial Navigation

The focus/spatial sub-package provides directional navigation (Up, Down, Left, Right). It uses the physical coordinates of the elements (retrieved via RenderObject().Fragment()) to determine the best candidate in the requested direction.

πŸ’ Focus Rings

Painters use Manager.IsFocusVisible(node) to decide whether to draw a focus indicator. This follows the web's :focus-visible heuristic, returning true only if focus was acquired via the keyboard.

Documentation ΒΆ

Overview ΒΆ

Package focus implements focus management for Kite.

focus.Manager owns the current focus node, the Reason (programmatic, pointer, keyboard, restore), and a Scope stack. IsFocusVisible returns true only when the reason is keyboard, matching the web :focus-visible heuristic. Tab navigation is DOM-order within the active scope. The spatial sub-package provides directional (arrow-key) navigation.

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

View Source
var SpatialNavigateFunc func(m *Manager, dir dom.Direction) bool

SpatialNavigateFunc is registered by the spatial focus package to avoid circular package dependencies.

Functions ΒΆ

This section is empty.

Types ΒΆ

type Manager ΒΆ

type Manager struct {
	// contains filtered or unexported fields
}

Manager owns focus state, the scope stack, and tab-navigation for a single logical tree. All methods must be called from the single main-loop goroutine.

A Manager must be constructed with NewManager; the zero value is invalid.

func NewManager ΒΆ

func NewManager(
	root dom.Document,
	dispatcher event.Dispatcher,
) *Manager

NewManager creates a Manager backed by dispatcher. A default root scope covering the entire tree (with root as Root) is established automatically via PushScope; this mirrors the engine startup contract.

dispatcher routes focus / blur event through the logical tree.

func (*Manager) ActiveScope ΒΆ

func (m *Manager) ActiveScope() *dom.FocusScope

ActiveScope implements dom.FocusHandle.

func (*Manager) ActiveScopeInternal ΒΆ

func (m *Manager) ActiveScopeInternal() *dom.FocusScope

ActiveScopeInternal returns the top of the scope stack, or nil if the stack is empty (should not happen after NewManager).

func (*Manager) Blur ΒΆ

func (m *Manager) Blur()

Blur clears the current focus without moving it to another element. blur/focusout event are dispatched on the previously focused node.

func (*Manager) Current ΒΆ

func (m *Manager) Current() dom.Element

Current returns the currently focused logical element, or nil.

func (*Manager) Document ΒΆ

func (m *Manager) Document() dom.Document

Document returns the logical document root.

func (*Manager) Focus ΒΆ

func (m *Manager) Focus(el dom.Element)

Focus implements dom.FocusHandle.

func (*Manager) IsFocusVisible ΒΆ

func (m *Manager) IsFocusVisible(el dom.Element) bool

IsFocusVisible reports whether el is focused AND focus was acquired via the keyboard. Painters consult this to decide whether to draw a focus ring, matching the web :focus-visible heuristic.

func (*Manager) IsFocusable ΒΆ

func (m *Manager) IsFocusable(el dom.Element, scope *dom.FocusScope) bool

IsFocusable reports whether el is a valid focus target within scope. An element is focusable iff all four conditions hold:

  1. el's IsFocusable() returns true.
  2. If the element implements dom.Disableable, IsDisabled() must be false.
  3. el's render object must exist and ComputedStyle().Display must not be DisplayNone.
  4. el is a descendant of (or equal to) scope.Root

IsFocusable is the single source of truth; all callers (Focus, Next, Previous, PushScope Autofocus) delegate to it.

func (*Manager) IsFocused ΒΆ

func (m *Manager) IsFocused(el dom.Element) bool

IsFocused reports whether el is the currently focused element.

func (*Manager) MoveCaret ΒΆ

func (m *Manager) MoveCaret(dir dom.Direction) bool

MoveCaret implements dom.FocusHandle.

func (*Manager) NavigateFocus ΒΆ

func (m *Manager) NavigateFocus(dir dom.Direction) bool

NavigateFocus implements dom.FocusHandle.

func (*Manager) Next ΒΆ

func (m *Manager) Next() bool

Next moves focus to the next focusable node in DOM tree order (depth-first, left-to-right) within the active scope, wrapping to the first focusable if at the end. Returns false if no focusable candidate exists.

func (*Manager) PopScope ΒΆ

func (m *Manager) PopScope()

PopScope removes the top scope from the stack. If the captured PreviousFocus is still focusable (passes the new active scope's filter after the pop), focus is restored to it with ReasonRestore; otherwise Blur is called.

PopScope is a no-op when only the root scope remains.

func (*Manager) Previous ΒΆ

func (m *Manager) Previous() bool

Previous moves focus to the previous focusable node in DOM tree order within the active scope, wrapping to the last focusable if at the beginning. Returns false if no focusable candidate exists.

func (*Manager) PushScope ΒΆ

func (m *Manager) PushScope(s *dom.FocusScope)

PushScope pushes s onto the scope stack. The current focus is captured into s.PreviousFocus. If s.Autofocus is set and passes the focusable filter, focus is moved to it with ReasonProgrammatic. PushScope pushes s onto the scope stack. The current focus is captured into s.PreviousFocus. If s.Autofocus is set and passes the focusable filter, focus is moved to it with ReasonProgrammatic.

func (*Manager) Reason ΒΆ

func (m *Manager) Reason() Reason

Reason returns the reason the current focus was acquired.

func (*Manager) ResetScope ΒΆ

func (m *Manager) ResetScope()

ResetScope clears all scopes except the root, and restores focus to the root's PreviousFocus if possible. This is useful for recovering from unexpected states (e.g. focus trapped in a modal that failed to close).

func (*Manager) SetFocus ΒΆ

func (m *Manager) SetFocus(el dom.Element, reason Reason) bool

SetFocus attempts to move keyboard focus to el with the supplied reason. It returns false (and is a no-op) if el does not pass the focusable filter:

  • el's IsFocusable() must be true
  • if el implements dom.Disableable, IsDisabled() must be false
  • its render object's ComputedStyle().Display must not be DisplayNone
  • el must be a descendant of (or equal to) the active scope's Root

When the focus changes, blur/focusout are dispatched on the previous target and focus/focusin are dispatched on el. DirtyPaint is marked on both the old and new nodes so painters can update focus rings.

func (*Manager) SetInitialFocus ΒΆ

func (m *Manager) SetInitialFocus()

SetInitialFocus establishes the initial focus on startup or mount. It is a no-op if initial focus has already been set since the last reset.

type Reason ΒΆ

type Reason uint8

Reason describes how focus was acquired on the current element. It is used by painters to decide whether to draw a visible focus indicator (ring).

const (
	// ReasonProgrammatic means the application called Focus() directly.
	ReasonProgrammatic Reason = iota
	// ReasonPointer means the element was focused via mouse or touch.
	ReasonPointer
	// ReasonKeyboard means the element was focused via the keyboard (Tab,
	// arrow keys, keyboard shortcut). Painters should show a focus ring.
	ReasonKeyboard
	// ReasonRestore means focus was restored automatically when a Scope was
	// popped (e.g. a modal was closed).
	ReasonRestore
)

type Scope ΒΆ

type Scope = dom.FocusScope

Scope is a type alias for dom.FocusScope to preserve backwards compatibility.

Directories ΒΆ

Path Synopsis
Package spatial ranks focusable elements by their position within the active focus scope.
Package spatial ranks focusable elements by their position within the active focus scope.

Jump to

Keyboard shortcuts

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