Stimulus Use Cases
I’m finishing my first project with Stimulus and I’ve decided to create a document with examples of how we can implement different features with that framework.
I’ll cover:
- Confirmation alert
- Image upload preview
- Textarea auto height
- Form validation
If you are not familiar with Stimulus yet — it’s a simple framework that allows to write structured JavaScript code within controllers. Stimulus has good documentation on the official website.
Image upload preview
We have input for image uploads and now we need to create preview of an uploaded picture and possibility to clear it.
When user selects an image, stimulus executes updatePreview
function. It checks if there are any files were uploaded and then shows or hides the preview.
When a user clicks on the clear upload button, clearUpload
function is called. It nullifies the value of the upload input and triggers updatePreview
.
Thank you for your attention!
The full version of the examples is here. Feel free to correct the code and advise new use cases based on your experience 🙂