|
| 1 | +# Vident Architecture Overview |
| 2 | + |
| 3 | +## Purpose |
| 4 | + |
| 5 | +Vident is a Ruby gem that enhances Rails view components with type-safe properties and seamless Stimulus.js integration. |
| 6 | +It simplifies maintaining the server-side component and client-side interactivity by automatically generating |
| 7 | +Stimulus data attributes from Ruby component definitions. |
| 8 | + |
| 9 | +## Core Architecture |
| 10 | + |
| 11 | +### Component System |
| 12 | + |
| 13 | +Vident provides base classes for two popular rendering engines: |
| 14 | +- `Vident::ViewComponent::Base` - For ViewComponent-based components |
| 15 | +- `Vident::Phlex::HTML` - For Phlex-based components |
| 16 | + |
| 17 | +Both inherit from `Vident::Component` which provides: |
| 18 | +- Type-safe property declarations via Literal gem |
| 19 | +- Stimulus.js integration via declarative DSL |
| 20 | +- Intelligent CSS class management |
| 21 | +- Component caching capabilities |
| 22 | +- Root element rendering helpers |
| 23 | + |
| 24 | +### Key Modules and Classes |
| 25 | + |
| 26 | +#### Component Core (`lib/vident/component.rb`) |
| 27 | +- Base component functionality |
| 28 | +- Property system integration |
| 29 | +- Stimulus component mixing |
| 30 | +- Class management |
| 31 | + |
| 32 | +#### Stimulus Integration (`lib/vident/stimulus_component.rb`) |
| 33 | +- Stimulus controller generation |
| 34 | +- Data attribute building |
| 35 | +- Event scoping |
| 36 | +- Outlet management |
| 37 | + |
| 38 | +#### Property System |
| 39 | +- Uses Literal gem for type checking |
| 40 | +- Supports default values, nullable types, unions |
| 41 | +- Creates getter methods and predicate methods for booleans |
| 42 | +- Validates property values at initialization |
| 43 | + |
| 44 | +### Data Flow |
| 45 | + |
| 46 | +1. **Component Definition**: Developer defines component class with properties and Stimulus configuration |
| 47 | +2. **Instantiation**: Component is instantiated with property values |
| 48 | +3. **Attribute Resolution**: Component resolves all attributes including Stimulus data attributes |
| 49 | +4. **Rendering**: Component renders HTML with all necessary attributes |
| 50 | +5. **Client-side**: Stimulus controllers automatically connect and initialize |
| 51 | + |
| 52 | +## Key Concepts |
| 53 | + |
| 54 | +### Properties |
| 55 | +Properties are typed attributes that define the component's interface: |
| 56 | +- Enforced at runtime via Literal gem |
| 57 | +- Support complex types (arrays, hashes, unions) |
| 58 | +- Can have defaults (static or dynamic via procs) |
| 59 | +- Built-in properties: `element_tag`, `id`, `classes`, `html_options` |
| 60 | + |
| 61 | +### Stimulus DSL |
| 62 | +Declarative configuration for Stimulus controllers: |
| 63 | +- **Controllers**: Auto-generated based on component class name |
| 64 | +- **Actions**: DOM events mapped to controller methods |
| 65 | +- **Targets**: DOM element references |
| 66 | +- **Values**: Data passed to controller |
| 67 | +- **Classes**: CSS classes for different states |
| 68 | +- **Outlets**: References to other Stimulus controllers |
| 69 | + |
| 70 | +### Root Element |
| 71 | +Special helper that renders the component's outermost HTML element: |
| 72 | +- Configurable tag name |
| 73 | +- Merges all CSS classes intelligently |
| 74 | +- Includes all Stimulus data attributes |
| 75 | +- Supports custom HTML attributes |
| 76 | + |
| 77 | +### Class Management |
| 78 | +Intelligent merging of CSS classes from multiple sources: |
| 79 | +- Component-defined classes |
| 80 | +- Classes passed at render time |
| 81 | +- Stimulus class definitions |
| 82 | +- Tailwind CSS conflict resolution (when available) |
| 83 | + |
| 84 | +## Component Lifecycle |
| 85 | + |
| 86 | +1. **Initialization** |
| 87 | + - Properties validated and set |
| 88 | + - `after_component_initialize` hook called |
| 89 | + - Stimulus configuration evaluated |
| 90 | + |
| 91 | +2. **Attribute Resolution** |
| 92 | + - `root_element_attributes` method called |
| 93 | + - Stimulus attributes generated |
| 94 | + - CSS classes merged |
| 95 | + - HTML options combined |
| 96 | + |
| 97 | +3. **Rendering** |
| 98 | + - Template rendered (ERB for ViewComponent, Ruby for Phlex) |
| 99 | + - Root element helper injects all attributes |
| 100 | + - Child components rendered if present |
| 101 | + |
| 102 | +4. **Client-side Connection** |
| 103 | + - Stimulus finds elements with data-controller |
| 104 | + - Controllers instantiated |
| 105 | + - Values, targets, and actions connected |
| 106 | + |
| 107 | +## Integration Points |
| 108 | + |
| 109 | +### Stimulus Integration |
| 110 | +- Generates standard Stimulus data attributes |
| 111 | +- Compatible with stimulus-rails gem |
| 112 | +- Supports sidecar controller files |
| 113 | + |
| 114 | +### CSS Framework Integration |
| 115 | +- Built-in Tailwind CSS class merging |
| 116 | +- Maintains class order precedence |
| 117 | + |
| 118 | +## Performance Considerations |
| 119 | + |
| 120 | +- Supports fragment caching via `Vident::Caching` |
| 121 | + |
| 122 | +## Extensibility |
| 123 | + |
| 124 | +- Custom base classes for shared behavior |
| 125 | +- Pluggable class merging strategies |
| 126 | +- Hookable lifecycle methods |
| 127 | + |
1 | 128 | # Vident LLM Reference |
2 | 129 |
|
3 | 130 | Rails component library for building interactive, type-safe components with Stimulus.js integration. Supports ViewComponent and Phlex rendering engines. |
@@ -26,7 +153,7 @@ gem "vident-phlex" # Phlex support |
26 | 153 | # Type-safe properties using Literal gem |
27 | 154 | prop :text, String, default: "Click me" |
28 | 155 | prop :url, _Nilable(String) |
29 | | -prop :style, Symbol, in: [:primary, :secondary], default: :primary |
| 156 | +prop :style, _Union(:primary, :secondary), default: :primary |
30 | 157 | prop :enabled, _Boolean, default: false |
31 | 158 | prop :items, _Array(String), default: -> { [] } |
32 | 159 | prop :count, Integer, default: 0 |
|
0 commit comments