Stimulus is advertised as a modest framework for the HTML you already have. It still packs quite a few features that you (didn’t) know (or have forg

Stimulus Features You (Didn't) Know

submited by
Style Pass
2024-11-28 14:00:03

Stimulus is advertised as a modest framework for the HTML you already have. It still packs quite a few features that you (didn’t) know (or have forgotten about).

Every API in stimulus (targets, classes, values and outlets) has the existential attribute option. Meaning you can check if an attribute is available.

You most likely know about the connect and disconnect lifecycle methods for Stimulus classes. But there are also [name]TargetConnected() and [name]TargetDisconnected() method. These are called when a target becomes connected or disconnected.

It follows this structure data-[identifier]-[param-name]-param. It can take any type for a value from a String, Number to an Object and a Boolean. Refer to the docs for more.

Talking about actions. There are various action-options available. :prevent is one of them. You probably have used event.preventDefault() in your methods. Stimulus provides a shortcut for it. Let’s take this example:

Without :prevent (preventDefault()), invalid characters (non-numbers) would still appear in the input even after the validation check.

Leave a Comment