Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

motion

> 前端框架
Open source

Reactive frontend UI components for Rails in pure Ruby

694 stars0 likes1 views
WebsiteGitHub

About

Reactive frontend UI components for Rails in pure Ruby

Motion

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.

  • Plays nicely with the Rails monolith you have.
  • Peacefully coexists with your existing tech: Strong Parameters, Turbolinks, Trix, React, Vue, etc.
  • Real-time frontend UI updates from frontend user interaction AND server-side updates.
  • Leans on ActionCable and ViewComponent for the heavy lifting.
  • No more frontend models, stores, or syncing; your source of truth is the database you already have.
  • No JavaScript required!

Installation

  1. Install Motion gem and JS package

Motion has Ruby and JavaScript parts, execute both of these commands:

bash
bundle add motion
yarn add @unabridged/motion
  1. Install ViewComponent

You 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.

  1. Install ActionCable

Motion communicates over and therefore requires ActionCable.

  1. Run install script

After installing all libraries, run the install script:

bash
bin/rails g motion:install

This will install 2 files, both of which you are free to leave alone.

How does it work?

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.

  • Websockets Communication - Communication with your Rails backend is performed via ActionCable
  • No Full Page Reload - The current page for a user is updated in place.
  • Fast DOM Diffing - DOM diffing is performed when replacing existing content with new content.
  • Server Triggered Events - Server-side events can trigger updates to arbitrarily many components via WebSocket channels.
  • Partial Page Replacement - Motion does not use full page replacement, but rather replaces only the component on the page with new HTML, DOM diffed for performance.
  • Encapsulated, consistent stateful components - Components have continuous internal state that persists and updates. This means each time a component changes, new rendered HTML is generated and can replace what was there before.
  • Blazing Fast - Communication does not have to go through the full Rails router and controller stack. No changes to your routing or controller are required to get the full functionality of Motion.

Frontend interactions

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:

ruby
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 interactions

Backend changes can be streamed to your Motion components in 2 steps.

  1. Broadcast changes using ActionCable after an event you care about:
…

See the code for full API for Event and Element.

Callbacks

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

ruby
  # this will be called from the child component
  def do_something(message)
    puts "Colonel Sandurz says: "
    puts message
  end

parent_component.html.erb

erb
  

child_component.rb

ruby
  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`
  end

child_component.html.erb

erb
  

Limitations

  • 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.

Roadmap

Broadly speaking, these initiatives are on our roadmap:

  • Enhanced documentation and usage examples
  • Support more ViewComponent-like libraries
  • Support for AnyCable
  • Support for server-side state to reduce over-the-wire HTML size
  • Support communication via AJAX instead of (or in addition to) websockets

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/unabridged/motion.

License

The gem is available as open source under the terms of the MIT License.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubycomponentgemno-javascriptpresenter

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架