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 ΒΆ
- Variables
- type Manager
- func (m *Manager) ActiveScope() *dom.FocusScope
- func (m *Manager) ActiveScopeInternal() *dom.FocusScope
- func (m *Manager) Blur()
- func (m *Manager) Current() dom.Element
- func (m *Manager) Document() dom.Document
- func (m *Manager) Focus(el dom.Element)
- func (m *Manager) IsFocusVisible(el dom.Element) bool
- func (m *Manager) IsFocusable(el dom.Element, scope *dom.FocusScope) bool
- func (m *Manager) IsFocused(el dom.Element) bool
- func (m *Manager) MoveCaret(dir dom.Direction) bool
- func (m *Manager) NavigateFocus(dir dom.Direction) bool
- func (m *Manager) Next() bool
- func (m *Manager) PopScope()
- func (m *Manager) Previous() bool
- func (m *Manager) PushScope(s *dom.FocusScope)
- func (m *Manager) Reason() Reason
- func (m *Manager) ResetScope()
- func (m *Manager) SetFocus(el dom.Element, reason Reason) bool
- func (m *Manager) SetInitialFocus()
- type Reason
- type Scope
Constants ΒΆ
This section is empty.
Variables ΒΆ
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) IsFocusVisible ΒΆ
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 ΒΆ
IsFocusable reports whether el is a valid focus target within scope. An element is focusable iff all four conditions hold:
- el's IsFocusable() returns true.
- If the element implements dom.Disableable, IsDisabled() must be false.
- el's render object must exist and ComputedStyle().Display must not be DisplayNone.
- 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) NavigateFocus ΒΆ
NavigateFocus implements dom.FocusHandle.
func (*Manager) Next ΒΆ
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 ΒΆ
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) 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 ΒΆ
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.