- π Real-Time Sync: Changes sync across all clients automatically
- π¨ Familiarity: Regular Rails partials with instant reactivity
- π― Simple State Management: Just update values to trigger changes
- π‘οΈ Backend-Validated Updates: All updates flow through your Rails controllers
- π WebSocket Integration: Built-in Action Cable support for real-time updates
- β‘ Lightweight Client API: Modify state directly in JavaScript with minimal setup
class HomeController < ApplicationController
include LivePartial
end
<%= live_partial "path/to/partial",
id: "color-picker",
for: @resource, # Required: Resource object or use :action option
action: "set_color", # Optional: Override default update action, defaults to #update
state: { color: "red" } # Optional: Initial state
%>
# _partial.erb
<div id="color-picker">
<input type="color" value="<%= color %>" />
Current color: <%= color %>
</div>
# home_controller.rb
def set_color
# Update your data as needed
live_render(state: params[:state]) # Broadcasts new state to clients
end
The LivePartial JavaScript API provides simple state management:
// Get a reference to your partial
const picker = livePartial('color-picker')
// Read current state
console.log(picker.state.color) // => "red"
// Update state - automatically triggers re-render
picker.state.color = 'blue'
// Update multiple properties at once
Object.assign(picker.state, {
color: 'green',
opacity: 0.5
})
LivePartial emits events during the update lifecycle:
const picker = livePartial('color-picker')
picker.on('beforeRender', (event) => {
console.log('About to update:', event.state)
})
picker.on('afterRender', (event) => {
console.log('Updated with:', event.state)
})
picker.on('error', (event) => {
console.error('Update failed:', event.error)
})
beforeUpdate
- Before sending update to serverbeforeRender
- Before re-rendering the partialafterRender
- After re-rendering completeserror
- When an error occursstateChange
- When any state property changesbeforeWebsocketRender
/afterWebsocketRender
- For WebSocket updates
State updates are automatically debounced (100ms) to prevent excessive updates. This behavior can be customized per partial:
<%= live_partial "partial_name", id: "example", debounce: 200, state: {} %>
Update the state directly in JavaScript for instant UI changes. Server-rendered updates will reconcile as needed.
When using the live_partial
helper, these options are required:
id
: Unique identifier for the partial (string)- Either
for
oraction
:for
: Resource object to updateaction
: Controller action for updates ("controller#action")
Optional options:
state
: Initial state hash (default:{}
)- Additional Options: Passed as locals to the partial
- π Real-Time Updates: State is synchronized across all clients viewing the same partial.
- π Backend Security: All state updates flow through your Rails controller for validation.
- π οΈ Focus on Simplicity: Keep partials single-purpose for best performance.
- π‘ WebSocket Ready: Updates via Action Cable for seamless reactivity.
LivePartial offers:
- A lightweight, intuitive alternative to Hotwire.
- A balance between server-driven updates and client-side flexibility.
- Minimal boilerplate while maintaining Rails convention.