Reactive frontend UI components for Rails in pure Ruby
Reactive frontend UI components for Rails in pure Ruby
Motion allows you to build reactive, real-time frontend UI components in your Rails application using pure Ruby. Check out some live examples and the code for the examples.
Motion has Ruby and JavaScript parts, execute both of these commands:
bundle add motion
yarn add @unabridged/motionYou need a view component library to use Motion. Technically, any view component library that
implements the render_in interface that landed in Rails 6.1
should be compatible, but Motion is actively developed and tested against
Github's ViewComponent.
Installation instructions for ViewComponent are here.
Motion communicates over and therefore requires ActionCable.
After installing all libraries, run the install script:
bin/rails g motion:installThis will install 2 files, both of which you are free to leave alone.
Motion allows you to mount special DOM elements (henceforth "Motion components") in your standard Rails views that can be real-time updated from frontend interactions, backend state changes, or a combination of both.
Frontend interactions can update your Motion components using standard JavaScript events that you're already familiar with: change, blur, form submission, and more. You can invoke Motion actions manually using JavaScript if you need to.
The primary way to handle user interactions on the frontend is by using map_motion:
class MyComponent Every time the "Increment" button is clicked, MyComponent will call the add method, re-render your component and send it back to the frontend to replace the existing DOM. All invocations of mapped motions will cause the component to re-render, and unchanged rendered HTML will not perform any changes.
Backend changes can be streamed to your Motion components in 2 steps.
…See the code for full API for Event and Element.
Motion has callbacks which will let you pass data from a child component back up to the parent. Callbacks are created by calling bind with the name of a method on the parent component which will act as a handler. It returns a new callback which can be passed to child components like any other state. To invoke the handler from the callback, use call. If the handler accepts an argument, it will receive anything that is passed to call.
parent_component.rb
# this will be called from the child component
def do_something(message)
puts "Colonel Sandurz says: "
puts message
endparent_component.html.erb
child_component.rb
attr_reader :on_click
map_motion :click
def initialize(on_click:)
@on_click = on_click
end
def click
on_click.call("Do something!") # an argument can be passed into `call`
endchild_component.html.erb
Due to the way that Motion components are replaced on the page, component HTML templates are limited to a single top-level DOM element. If you have multiple DOM elements in your template at the top level, you must wrap them in a single element. This is a similar limitation that React enforced until React.Fragment appeared and is for a very similar reason.
Information about the request to the server is lost after the first re-render. This may cause things like _url helpers to fail to find the correct domain automatically. A workaround is to provide the domain to the helpers, and cache as a local variable any information from the request object that you need.
Broadly speaking, these initiatives are on our roadmap:
Bug reports and pull requests are welcome on GitHub at https://github.com/unabridged/motion.
The gem is available as open source under the terms of the MIT License.
No open issues yet, or sync has not completed.