Summary
Hey there folks 😁,
Thanks for clicking on this article, I hope you'll enjoy it ☺️ (as always).
So, what are we talking about today? I don't remember anymore, I'm lost...
Oh right... about Stimulus and some really nice controllers that you can reuse in your applications.
Are you ready? Let's go!
As you probably know, Symfony since its 6.x versions has launched the Symfony-UX project, I've already talked about it many times on this blog.
And for this project, they chose to use the "Hotwired" stack (Stimulus and Turbo)...
Except that this stack doesn't come from nowhere, in fact, it comes from the Ruby on Rails world, it's simply the default front-end provided with Rails (at least, in its 8.0 version).
Being a big fan of this stack, I decided to really get interested in the Ruby on Rails world, and so the application I'm currently developing is a Ruby on Rails application.
This application is simply the little sister of ViteUneTable, but this time, for hairdressers...
Its little name is "Coupéo", the .com is already taken 😛...
To successfully carry out this development, I had to reuse some of my Stimulus controllers, update others and finally create new ones.
The fact that Rails and Symfony share the same front-end is great for learning Rails, I only have the Ruby and backend part to master. As for Turbo and Stimulus, I'm already at home.
And in fact, even if Ruby is confusing at first, it's a real pleasure once you're into it (and thanks to AIs for their help in understanding how it all works).
The problem with Stimulus controllers is that they can load other libraries. For example, you can perfectly load React from a Stimulus controller.
So, if your big application has lots of controllers and these controllers themselves have big dependencies, you're loading 1 billion Javascript at the beginning of the page.
So, you need to load all of this asynchronously, we only load when we need it.
From my research, it hasn't changed, you need to add a comment "/* stimulusFetch: 'lazy' */" above your controller.
Very simple and effective. After that, I must say that the project that uses Symfony-UX that I have still uses webpack and I imagine that things have probably changed with href="https://symfony.com/doc/current/frontend/asset_mapper.html" target="_blank">AssetMapper.
Well, let's not lie to ourselves, this is more recent, so I'm sure about this...
At the time of writing, I'm on Ruby on Rails 8.0.
First, we'll tell importmap not to preload the Stimulus controllers on the first page load.
By default, importmap imports absolutely everything, so the browser starts downloading everything, it can quickly become ultra heavy.
Very simple, where we define our Stimulus controllers, we define "preload: false".
This happens in the file: app / javascript / controllers / index.js
If you just installed Rails, you'll see that you don't have "lazyLoadControllersFrom", by default it's "eager".
By replacing things as I just told you, you'll load your controllers when Stimulus sees them in the HTML.
In the way Ruby on Rails 8.0 works, there is no JavaScript code obfuscation, the server sends you the files as I wrote them.
I decided to completely keep this approach, I like the idea that people can see "how it works".
Before sharing my controllers with you, I'd like to tell you about Stimulus Components...
You'll find many ready-to-use things on it, like an "auto submit" or an integration with Chart.js.
Alright, it's in this section that you'll be able to find the controller code.
Just like ViteUneTable, Coupéo's booking assistant is actually a Turbo Frame. Why change something that works? 🙂
The only thing is that in the header, I have a button that should only appear on the landing... I need it to disappear when the URL no longer matches.
For that, we intercept Turbo events...
A little demo?
And now, the code:
Let's start with the controller I use on Coupéo, for the mobile version of appointment time selection.
It's a super annoying interface to make on mobile but with a small Stimulus controller, it's easy.
And here's the code:
This summer, LFMaps came out, it's simply a hosted instance of OpenFreeMap...
And I personally needed it for Coupéo 😁.
On the salons' landing page, you can see the map and where they are located. A demo?
And here's the code, by default it loads LFMaps tiles.
You remember... it was in 2022.
Well, its new version has arrived, working with the latest version of intl-tel-input...
What does it look like? 😛
And the code? Watch out for the "Utils" URL. Also, to make all this compatible with Rails, I chose the easy way and put the translation directly in the file.
Make sure to make the .webp "flags" files accessible.
Let's talk about the anti no-show feature of ViteUneTable and Coupéo... 🙂
A little demo? 😁
And the controller code. Note here that I use "window.Turbo", because Rails by default exposes it with the package "@hotwired/turbo-rails".
Here we are, the controllers are given to you as is, as they are in Coupéo...
You'll need to adapt them to your needs... but it gives you a base if you want.
Have a great day and see you next time! 😁