Our great sponsors
-
WorkOS
The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.
-
tailwindcss-stimulus-components
A set of StimulusJS components for TailwindCSS apps similar to Bootstrap JS components.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
As usual, you can find the complete source code for this article on Github.
To get everything installed, we’re going to walk on the wild side by using the newly released, very-much-still-alpha jsbundling-rails and cssbundling-rails gems.
To get everything installed, we’re going to walk on the wild side by using the newly released, very-much-still-alpha jsbundling-rails and cssbundling-rails gems.
// Credit: This controller is an edited-to-the-essentials version // of the modal component created by @excid3 as part of the essential // tailwind-stimulus-components package found here: // https://github.com/excid3/tailwindcss-stimulus-components // In production, use the full component from the // library or expand this controller to allow for // keyboard closing and dealing with scroll positions import { Controller } from "@hotwired/stimulus" export default class extends Controller { static targets = ['container']; connect() { this.toggleClass = 'hidden'; this.backgroundId = 'modal-background'; this.backgroundHtml = this._backgroundHTML(); this.allowBackgroundClose = true; } disconnect() { this.close(); } open() { document.body.classList.add('fixed', 'inset-x-0', 'overflow-hidden'); this.containerTarget.classList.remove(this.toggleClass); document.body.insertAdjacentHTML('beforeend', this.backgroundHtml); this.background = document.querySelector(`#${this.backgroundId}`); } close() { if (typeof event !== 'undefined') { event.preventDefault() } this.containerTarget.classList.add(this.toggleClass); if (this.background) { this.background.remove() } } _backgroundHTML() { return `${this.backgroundId}" class="fixed top-0 left-0 w-full h-full" style="background-color: rgba(0, 0, 0, 0.7); z-index: 9998;">`; } }
Mrujs is a newer tool, under active development, and is intended to serve as a modern, stable replacement for rails/ujs, which is no longer under active development and which will be deprecated when Rails 7 releases.
To accomplish this, we’ll use Stimulus for the front-end interactivity, CableReady’s brand new CableCar feature to send content back from the server, and Mrujs to enable AJAX requests and to automatically process CableCar’s operations.
While the full Hotwire stack can deliver this experience with about the same amount of effort, the power and flexibility of CableReady's chainable operations makes CableReady + Mrujs a better fit for this particular use case than the full Hotwire stack, in my very, very humble opinion.
Stimulus and CableReady are two powerful, battle-tested tools with a mature feature set that should be considered for any modern Rails application. CableReady can stand alone as a way to deliver real-time updates to end users through a variety of methods or it can be powered-up with StimulusReflex to deliver a SPA-link experience, minus the SPA.
Related posts
- Rails, Hotwire, CableReady, and StimulusReflex are BFFs
- Sort tables (almost) instantly with Ruby on Rails and Turbo Frames
- Building a Real Time Scoreboard with Ruby on Rails and CableReady
- Am I Principal Skinner? Complexity of front-end is just baffling to me now
- Frontend, backend - let's be friends again