Raxol.UI.FocusHelper (Raxol v2.6.0)

View Source

Helpers for widgets to determine focus state from the render context.

The render context includes focused_element (the ID of the currently focused widget). Widgets call focused?/2 during render to check if they should display focus indicators.

Usage in a widget's render/2

def render(state, context) do
  am_focused = FocusHelper.focused?(state.id, context)
  style = if am_focused, do: FocusHelper.focus_style(state.style), else: state.style
  # ... render with style
end

Summary

Functions

Merges focus indicator styles into the given base style. Uses hardcoded defaults. Prefer focus_style/2 for theme-aware styling.

Merges focus indicator styles from the theme into the given base style. Falls back to hardcoded defaults when no theme focus config is present.

Returns true if the widget with the given ID is the currently focused element.

Returns the base style with focus styles merged in only if focused. Convenience for the common pattern in render/2.

Theme-aware variant of maybe_focus_style. Reads focus config from theme.

Looks up the style for a given pseudo-state from the theme. Falls back to built-in defaults when no theme config exists.

Determines the visual pseudo-state for a widget.

Functions

focus_style(style)

@spec focus_style(map()) :: map()

Merges focus indicator styles into the given base style. Uses hardcoded defaults. Prefer focus_style/2 for theme-aware styling.

focus_style(style, context)

@spec focus_style(map(), map()) :: map()

Merges focus indicator styles from the theme into the given base style. Falls back to hardcoded defaults when no theme focus config is present.

focused?(widget_id, context)

@spec focused?(any(), map()) :: boolean()

Returns true if the widget with the given ID is the currently focused element.

maybe_focus_style(widget_id, context, base_style)

@spec maybe_focus_style(any(), map(), map()) :: map()

Returns the base style with focus styles merged in only if focused. Convenience for the common pattern in render/2.

maybe_focus_style(widget_id, context, base_style, theme_context)

@spec maybe_focus_style(any(), map(), map(), map()) :: map()

Theme-aware variant of maybe_focus_style. Reads focus config from theme.

state_style(pseudo_state, context, base_style)

@spec state_style(atom(), map(), map()) :: map()

Looks up the style for a given pseudo-state from the theme. Falls back to built-in defaults when no theme config exists.

widget_state(widget_attrs, context)

@spec widget_state(map(), map()) :: :disabled | :active | :focused | :default

Determines the visual pseudo-state for a widget.

Priority: :disabled > :active > :focused > :default

widget_attrs should contain :disabled and/or :active boolean keys.