HyperChad Actions
Interactive action system for HyperChad UI components with triggers and effects.
Overview
The HyperChad Actions package provides:
- Action System: Comprehensive interactive action framework
- Trigger Types: Various event triggers (click, hover, change, etc.)
- Element Targeting: Flexible element targeting system
- Style Actions: Dynamic styling and visibility control
- Logic Integration: Conditional actions and parameterization
- Multi-Actions: Composite action sequences
Features
Action Triggers
- Click: Standard click events
- ClickOutside: Click outside element detection
- MouseDown: Mouse button down events
- KeyDown: Keyboard key down events
- Hover: Mouse hover events
- Change: Form input change events
- Resize: Window/element resize events
- Custom Events: User-defined event triggers
- Immediate: Execute immediately without trigger
- HttpBeforeRequest: Before HTTP request is sent
- HttpAfterRequest: After HTTP request completes
- HttpRequestSuccess: When HTTP request succeeds
- HttpRequestError: When HTTP request fails
- HttpRequestAbort: When HTTP request is aborted
- HttpRequestTimeout: When HTTP request times out
Element Targeting
- String ID: Target elements by string identifier
- Numeric ID: Target elements by numeric ID
- Class: Target elements by CSS class
- Child Class: Target child elements by class
- Self Target: Target the current element
- Last Child: Target the last child element
Action Types
- Style Actions: Visibility, display, background, and focus control
- Input Actions: Element selection and focus management
- Navigation: URL navigation and routing
- Logging: Debug and error logging
- Custom Actions: User-defined action types
- Events: Trigger other actions via events
- Variable Assignment: Let bindings for storing values (with DSL)
- Multi-Actions: Execute multiple actions sequentially
Style Control
- Visibility: Show/hide elements with visibility property
- Display: Show/hide elements with display property
- Background: Set/remove background colors
- Focus: Set focus state on elements
- Flexible Targeting: Apply styles to various element targets
Installation
Add this to your Cargo.toml:
[]
= { = "../hyperchad/actions" }
# Enable additional features (default includes handler, logic, serde, and arb)
= {
path = "../hyperchad/actions",
= ["handler", "logic", "serde", "arb"]
}
Usage
Basic Actions
use ;
// Simple click action to hide element
let action = Action ;
// Show element on hover
let show_action = Action ;
Element Targeting
use ;
use Visibility;
// Target by string ID
let hide_modal = hide_by_id;
// Target by class
let show_menu = set_display_class;
// Target self
let hide_self = hide_self;
// Target last child
let show_last = show_last_child;
Style Actions
// Visibility control
let hide_action = hide_by_id;
let show_action = show_by_id;
// Display control
let display_action = display_by_id;
let no_display_action = no_display_by_id;
// Background control
let set_bg = set_background_by_id;
let remove_bg = remove_background_self;
Multi-Actions
// Combine multiple actions
let multi_action = Multi;
// Chain actions with `and`
let chained = hide_by_id
.and;
Action Effects
// Add throttling to prevent rapid firing
let throttled_action = hide_by_id
.throttle; // 500ms throttle
// Add delay before turning off
let delayed_action = show_by_id
.delay_off; // Hide after 2 seconds
// Make action unique (prevent duplicates)
let unique_action = display_by_id
.unique;
Input Actions
// Select/focus input elements
let select_input = select_by_id;
let focus_button = focus_by_id;
let focus_by_class = focus_class;
Custom Actions
// Custom action type
let custom = Custom ;
// `action` is evaluated as a JavaScript expression in the vanilla-js renderer.
// For a literal string payload there, include JS quotes in the value, e.g.
// "'my-custom-action'".
// Event-based actions
let event_action = on_event;
// Navigation
let navigate = Navigate ;
Conditional Logic (with logic feature)
use ;
// Conditional action based on element state
let conditional = Logic;
// Toggle visibility using logic
let toggle = toggle_visibility_by_id;
Value Calculations (with logic feature)
use ;
// Calculate values from element properties
let mouse_x = get_mouse_x_self; // Mouse X relative to element
let width = get_width_px_self; // Element width in pixels
// Arithmetic operations
let half_width = width.divide;
let clamped = mouse_x.clamp;
// Use calculated values in actions (requires handler implementation)
Action Structure
Action
- trigger: When the action should be triggered (ActionTrigger)
- effect: The effect to execute (ActionEffect)
ActionEffect
- action: The action type to execute (ActionType)
- delay_off: Optional delay in milliseconds before deactivation
- throttle: Optional throttling in milliseconds to prevent rapid execution
- unique: Whether to prevent duplicate executions
ActionTrigger
- Click/ClickOutside/MouseDown/KeyDown/Hover: User interaction events
- Change: Input change events
- Resize: Window resize events
- Event(String): Custom named events
- Immediate: Execute immediately without waiting for a trigger
- HttpBeforeRequest/HttpAfterRequest/HttpRequestSuccess/HttpRequestError/HttpRequestAbort/HttpRequestTimeout: HTTP lifecycle events
Feature Flags
handler: Enable action handler implementation (includeslogicfeature, requireshyperchad_color)logic: Enable conditional logic, parameterized actions, and value calculationsserde: Enable serialization/deserialization supportarb: Enable arbitrary data generation for property-based testingfail-on-warnings: Treat compiler warnings as errors (development only)
Default features: handler, logic, serde, arb
Dependencies
- hyperchad_transformer_models: Core UI model types (Visibility, etc.)
- hyperchad_color: Color parsing and manipulation (optional, with
handlerfeature) - moosicbox_assert: Assertion utilities used by action and logic internals
- switchy_time: Cross-platform timing utilities
- moosicbox_arb: Arbitrary data generation (optional, with
arbfeature) - proptest: Property-based testing support (optional, with
arbfeature) - serde: Serialization/deserialization (optional, with
serdefeature) - serde_json: JSON serialization/deserialization backend (optional, with
serdefeature) - log: Logging facade
Advanced Features
DSL Support
The package includes a Domain-Specific Language (DSL) module for representing Rust-like syntax in action definitions. This is primarily for internal use and advanced integration scenarios:
use ;
// DSL expressions for representing complex action logic
// Note: This is an advanced feature still under development
Action Handler (with handler feature)
The handler feature provides a complete action execution system with style management, timing controls, and element queries:
use ;
// Implement traits for your UI framework to enable action handling
// See handler module documentation for integration examples
Integration
This package is designed for:
- Interactive UIs: Dynamic user interface behaviors
- Event Handling: Comprehensive event response system
- Animation Control: Show/hide and styling animations with timing
- Form Interactions: Form validation, input selection, and feedback
- Component Libraries: Reusable interactive components
- Game UIs: Responsive UI with mouse tracking and element queries