unabridged / motion

Reactive frontend UI components for Rails in pure Ruby
https://github.com/unabridged/motion
MIT License
697 stars 19 forks source link

Events capture #63

Closed KevinBerthier closed 3 years ago

KevinBerthier commented 3 years ago

Hi,

First thanks for bringing reactivity to Rails with such a powerful simplicity. I have one question. How would you trigger a JS callback after DOM update ?

Does Motion provides event capture such as StimulusReflex ?

<div data-action="cable-ready:after-morph@document->chat#scroll">

Or would you use Stimulus on top of Motion ?

latortuga commented 3 years ago

You can use Stimulus in combination with Motion, yes! There should be no issue.

Also, Motion provides a few callbacks related to the lifecycle of a motion. They are not currently very well documented but we are using them in production. You can see the code here, and the list is as follows:

'motion:before-connect' // Fired right before trying to get the ActionCable subscription
'motion:connect' // Fired right after connection is successful to ActionCable
'motion:connect-failed' // Fired when ActionCable connection fails
'motion:disconnect' // Fired when disconnected from ActionCable, for example on page navigation
'motion:before-render' // Fired right before rendering an update from a motion
'motion:render' // Fired right after rendering an update from a motion

Note that render-based events are not guaranteed to always be called on every motion. If nothing changed in the server-side state, no before-render or render will get fired because no re-render occurs.