12 ms·
Stimulus.js 2.0
- obiefernandez 6y agoGood timing. There was talk of a fork brewing...
- tpetry 6y agoYeah. This is happening with all libraries by basecamp: They release something, they don‘t work on it for months (no commits, many open pull requests) and people believe it‘s dead. Then one day (today) they out of the blue release a new version which nobody was expecting. Wouldnt stimulus and turbolinks not work so great i would prefer using different libraries.
- danjac 6y agoI like these libraries but have the same concerns. One alternative that might be worth a look is htmx (the new library based on intercooler.js): https://htmx.org/ https://htmx.org/
- tpetry 6y agoHtmx is more like all „we try to solve everything for you but fail“ solutions. The good part of stimulus is that it is only a binding library, no automatic content refetch etc. solution
- hopsoft 6y agoI think the much touted "NEW MAGIC" will be the thing that does the automatic content refresh.
- nickjj 6y ago> I think the much touted "NEW MAGIC" will be the thing that does the automatic content refresh. The StimulusJS handbook has an automatic content refresh example at https://stimulusjs.org/handbook/working-with-external-resources https://stimulusjs.org/handbook/working-with-external-resour.... As for NEW MAGIC, DHH mentioned it will have a Rails server side aspect to it so I don't think we can compare these tools directly. He did say other server side frameworks could implement similar behavior too but it would involve the framework having built-in support for websockets and job queues. This is based on a question I just asked him on Twitter at https://twitter.com/dhh/status/1334953167327547397 https://twitter.com/dhh/status/1334953167327547397. So it sounds like NEW MAGIC will probably make it super easy to push / broadcast stuff from the server to the client, but to get everything working with a really nice API and it being seamless will require a substantial amount of work if you planned to re-invent it in another framework.
- tpetry 6y agoThe new turbolinks they use on hey.com is using a concept named „portals“ so different parts of the web application can be refreshed independently. My linked inside hey.com articles are very interesting.
- nickjj 6y agoYeah that's the interesting part. It sounds like maybe there will be some overlaps in Turbolinks 6 and NEW MAGIC when it comes to partial updates. Maybe for basic partial page updates where you don't need to broadcast something to every connected client over websockets then Turbolinks 6 will be enough on it own. If that ends up being the case that's amazing because it'll be fully client side. In any case the near future is looking to be the best its ever been for developing very nice feeling apps without going the SPA route with any back-end technology.
- sandebert 6y agoHow does htmx fail?
- arvindamirtaa 6y agoSeriously! I've been following them since the intercooler days and they've been great (functionally) although maybe a little drowned out by all the SPA chatter. Still a great library IMO.
- mike1o1 6y agoStimulus has a very small surface area, but I find it really enjoyable to use when combined with Turbolinks. My only complaint/comment is that some of the HTML that I need to generate ends up being littered with so many data attributes, especially when combining multiple Stimulus controllers/behaviors. Has anybody found a clean way around that in Rails templates?
- jyunderwood 6y agoI've found it works well with [ViewComponent](https://github.com/github/view_component https://github.com/github/view_component) if you're in to that.
- swanson 6y agoI share some of the same issues, I have found that ERB templates work better than HAML for lots of data attributes. Basecamp seems to use liberal newlines / spacing when creating html tags (like one line per attribute) so that might help. Server-side "component partials" still don't have a great story, but view_component is one emerging option.
- nthj 6y agoAs another option, HAML lets you extract the attributes into a ruby helper method: https://haml.info/docs/yardoc/file.REFERENCE.html#attribute-methods https://haml.info/docs/yardoc/file.REFERENCE.html#attribute-...
- aaronbrethorst 6y agoSame. I’ve been a happy user of Haml for about ten years, but I think that Tailwind and Stimulus will permanently force me back to ERB :(
- inopinatus 6y agoSame advice as when using Tailwind CSS, or anything of this sort: refactor your application-specific front-end parts at the point of generation, into purposefully named units. For Rails, that means shifting your boilerplate into helpers, partials, and partial layouts. If you're ready for a sharper leading edge, look at ViewComponent. Once the boundaries of your server-side parts for something are congruent to the client-side parts, you have reached the nirvana state of wondering, "can I extract & package these into a library and share it?". This is general advice for all development but particularly and specifically true for Rails devs.
- Drakula2k 6y agoNice to see that it keeps improving.
- tpetry 6y agoNow i am waiting for the next turbolinks release. Their hey.com service has some interesting turbolinks extensions: https://dev.to/borama/a-few-sneak-peeks-into-hey-com-technology-iii-turbolinks-frames-5e4a https://dev.to/borama/a-few-sneak-peeks-into-hey-com-technol... and following
- shay_ker 6y agoIs there a convincing Stimulus vs. React-Rails post anywhere?
- newsbinator 6y agoOr a convincing Stimulus vs. Vue post would be helpful too, since Vue is so simple to sprinkle in as well, and doesn't pollute the html with data attributes as much as Stimulus.
- mike1o1 6y agoOne benefit is when combined with Turbolinks there is a need to bootstrap the Vue/React component. The Stimulus controllers respond to mutation observers and always work. If a new HTML element is added to the DOM and has a controller, it just works and is now active. This might not be the case with React or Vue, as you'd have to then bootstrap the component. This makes Stimulus a really good combo when used with Turbolinks or partial Javascript responses.
- phaedryx 6y agoI switched from Stimulus to Vue and have had a much better experience so far. It is much easier for me to use and I still get the value of "sprinking"
- Scarbutt 6y agoStimulus is paired with Turbolinks, which doesn't play well with almost anything in the JS ecosystem.
- deedubaya 6y agoThis used to be accurate in the days of auto initializing jquery plugins, but probably isn’t the case today if you’re actually using stimulus.
- hfourm 6y agoIt still is problematic if you use third party plugins / saas vendors. Not all. But many. Things like zendesk integrations for example. YMMV
- benzible 6y agoAlpine.js is worth a look as well [1]. I've been working with it as part of the newly christened "PETAL stack" [2] of Phoenix, Elixir, Tailwind, Alpine.js, and LiveView. [1] https://css-tricks.com/alpine-js-the-javascript-framework-thats-used-like-jquery-written-like-vue-and-inspired-by-tailwindcss/ https://css-tricks.com/alpine-js-the-javascript-framework-th... [2] https://thinkingelixir.com/podcast-episodes/021-tailwind-css-alpine-js-and-liveview-with-patrick-thompson/ https://thinkingelixir.com/podcast-episodes/021-tailwind-css...
- pspeter3 6y agoDo you use Alpine with Turbolinks?
- codetrotter 6y agoI was thinking that Alpine here was referring to Alpine Linux, which I found strange in the context because often these sorts of acronyms (LAMP, MEAN, etc etc) don’t mention specific distros and I also would not expect CSS-Tricks.com to write about Linux distros. But the answer of course was that it’s not Alpine Linux, it’s something called Alpine.js.
- 1_player 6y ago> "PETAL stack" of Phoenix, Elixir, Tailwind, Alpine.js, and LiveView Oh, finally a hipster stack after my own heart. My initial stack used Web Components instead of Alpine.js, but after one year trying to make it work (using lit-element) I'm battered, bruised and I declare Web Components a failed piece of technology [1]. Alpine is interesting, but not powerful enough for my use case, which is encapsulating common UI behaviours in reusable packages. Stimulus seem to be the missing ingredient in the mix for me. Any other suggestion for lightweight rich JS components that do not require one to buy into Vue, React or other "full-stack" JS libraries? Also, I wholeheartedly recommend Unpoly as well, fits nicely in the PETAL stack. 1: shadow DOM isn't great on your OWN components, styling isn't fun paired with global CSS/tailwind, dealing with properties/attributes/observables/reactivity in general is an absolute pain in the buttocks, good luck wrapping an <input> in a web component.
- 6y ago
- Scarbutt 6y agoA modest JavaScript framework for the HTML you already have Are they implying one shouldn't use it if you don't have any HTML already?
- ansonhoyt 6y agoYes. It is for "JS Sprinkles" on your existing HTML. This makes it "modest" compared to something like React that would typically generate HTML/DOM for you. To understand their philosophy, see DHH's blog post announcing Stimulus 1.0, https://m.signalvnoise.com/stimulus-1-0--a-modest-javascript-framework-for-the-html-you-already-have/ https://m.signalvnoise.com/stimulus-1-0--a-modest-javascript...
- hirako2000 6y agoThe idea is you render the html as is. The data attributes take care of the interactive parts, with the js scripts loaded after the page renders (as opposed to render html within js that executes on load with, the reactjs render() call).
- ksec 6y agoIs Stimulus 2.0 the NEW MAGIC DHH was referring to? I was expecting something much bigger.
- correlator 6y agoStable, efficient and intuitive. Thank you to the stimulus team!
- stepbeek 6y agoI love Stimulus and am super happy to see this update. It’s encoding conventions that we’ve fallen into using it for the last year. The callbacks in value changes are going to make my life much easier.
- gerry_shaw 6y agoUnpoly https://unpoly.com https://unpoly.com is also worth a look if you are interested in these things. It combines Turbolinks and Stimulus functionality in a very usable way.
- 1_player 6y agoMy killer unpoly features: * easy AJAX loading like turbolinks (<a href="?page=2" up-target=".content">) * easy AJAX modal (<a href="/users/new" up-modal="form">) * submit a form whenever any input changes (<input type="search" up-autosubmit up-delay="500">) * respects the way HTML is intended to be used, so the page still works even when JS is disabled
- NetOpWibby 6y agoI’m currently dealing with a modal issue within Svelte (pretty sure it’s due to createDispatch) and the Discord moves too fast to get help. Gonna check this out in the AM. EDIT: Couldn't sleep so checked it out now. Looking through the issues, there are a couple that are apparently "fixed in 1.0" but it doesn't look like 1.0 is coming anytime soon. Shame.
- deleted 6y ago[deleted]
- prox 6y agoWow! Thanks, that looks quite useful. Especially that it can work with all kinds of back ends.
- midrus 6y agoHappy unpoly user here too. I think it is way better than stimulus, turbolinks, htmlx, alpine, etc. Just doesn't have as good marketing as those.
- nickjj 6y ago> Just doesn't have as good marketing as those. And maybe documentation too. Turbolinks has really good docs and conceptually it's pretty easy to get going (1 line of JS and learning a few event handlers). Alpine and StimulusJS have really good docs / guides and tons of practical examples if you Google for a specific problem you're trying to solve. Unpoly's docs seem like it's mostly an HTML front-end to their API spec with very few examples. It's kind of difficult to figure out how to use most of what it does. There's also very little practical examples of using it if you Google around. I've heard of it in the past but never got into it because of the above.
- dang 6y agoIf curious see also 2018 https://news.ycombinator.com/item?id=16052105 https://news.ycombinator.com/item?id=16052105 2018 https://news.ycombinator.com/item?id=16443860 https://news.ycombinator.com/item?id=16443860 2018 https://news.ycombinator.com/item?id=17834514 https://news.ycombinator.com/item?id=17834514
- vmsp 6y agoSee also GitHub Catalyst https://github.github.io/catalyst/ https://github.github.io/catalyst/
- yborg 6y agoIs there a good comparison summary of the major JS frameworks available somewhere?
- dsego 6y agohttps://www.toptal.com/front-end/tips-and-practices https://www.toptal.com/front-end/tips-and-practices
- lostfocus 6y agoAnd just when Symfony announced their preferred way of doing frontend work is based on Stimulus, too. Great timing.
- throw_m239339 6y agoGiven how over-engineered symfony is, that's not a good thing...
- malinens 6y agosymfony4+ simplifies it's architecture
- cutler 6y agoI have great hopes that Stimulus 2.0 and Ruby 3.0 will put Rails back in the spotlight as the sane alternative to SPA abuse. Ruby and Rails have some really talented people on their teams and there's something special about the Ruby language which makes it such a fertile environment for innovation. Only Clojure compares in this respect.
- ljm 6y agoRails is already the sane alternative. Any full stack framework is. DotNet, Laravel, Django, Rails... If you think you need a JS framework to handle your UI, you've already lost, and you're doomed to reinventing wheels that these technologies have already perfected.
- Scarbutt 6y agoIsn't Stimilus a JS framework?
- KurtMueller 6y agoStimulusJS is one step up from JQuery and formalizes a lot of jQuery patterns that I'd come across - data-target, data-event attributes, etc.
- andrei_says_ 6y agoNot really. More like super simple and light helpers for dom manipulation - lets you add interactivity with minimal trouble.
- deleted 6y ago[deleted]
- andrei_says_ 6y agoThank you for saying this. I thought it but didn’t write it as I don’t want to offend. The amount of extra work and complexity that a separate FE introduces requires serious benefits on the other side of the equation.
- 6y ago
- jdhornby 6y agoIf anyone is interested in “the magic” DHH is talking about, this is a fantastic resource: https://dev.to/borama/a-few-sneak-peeks-into-hey-com-technology-iv-turbolinks-frames-continued-2bn https://dev.to/borama/a-few-sneak-peeks-into-hey-com-technol...
- cutler 6y agoWhat's the difference between Pjax and Turbolinks? Just a generic version of the same thing? Could Stimulus [Reflex] be hacked to work with Django or does it have strong ties to Rails?
- danjac 6y agoIt's easy to use with django, I use it with some side projects: https://github.com/danjac/localhub https://github.com/danjac/localhub https://github.com/danjac/kando https://github.com/danjac/kando https://github.com/danjac/django-saas-starter https://github.com/danjac/django-saas-starter
- Toutouxc 6y agoStimulus doesn't care about your backend, it's pure client side JS. You just load it (Stimulus itself and your controllers) and it looks for Stimulus stuff inside your HTML. StimulusReflex is a Rails thing that's much more involved than Stimulus.
- raihansaputra 6y agoStimulus Reflex already have a Django counterpart that uses the Stimulus Relex js and conventions. The package is django-sockpuppet.
- phaedryx 6y agoI worked with Stimulus for about a month and found it to be frustrating: 1. There is not a lot of documentation 2. They don't provide any testing guidelines, the best I've found is hand-wavy test-with-a-browser stuff 3. Everything is essentially global 4. Functions are disconnected from their parameters, i.e. I can't tell which bits of data a function is using without digging through a bunch of code. 5. Putting state in your HTML is tricky if you also want to modify the DOM. 6. Their naming scheme is cumbersome, e.g. data-controller="using-a--sub-directory" and data-target="some--nested--target-has-a.function", that is, the fact that everything is location-in-your-code-file-structure based. And a lot of other small things. Ironically, using Stimulus convinced me to switch to Vue because I liked their value proposition of "Javascript sprinkles for your HTML", which Vue lets me do, but more intuitively.
- hfourm 6y agoDitto. I think if you are an experienced web app dev, understand the pros and cons of something like Stimulus, and set out from the beginning of your project to make PRODUCT decisions within the constraints of something like Stimulus and being more SSR. Then it is a great tool. Anything more complicated though, and you are better off with Vue, React, Etc
- phaedryx 6y agoI am an experienced web dev and went into Stimulus excited to have something that seemed straightforward, but was disappointed. Vue has the flexibility to do SSR HTML with a bit of javascript for behavior way that works well for me. Just being able to put <button @click.prevent="save(<%= id %>)">Save</button> Instead of: <button data-action="click->some--long--path-to#save" data-id="<%= id %>">Save</button> ... save(event) { event.preventDefault() In my HTML has been worth the switch for me.
- SkyPuncher 6y agoI've personally hit the point where I feel like React is my go to for anything - especially with Gatsby. * If I'm keeping it simple, it's barely different than pure HTML. * Free templating * If I need Javascript (which I likely will), it's extremely easy to add anything I need.
- catwind7 6y agoWe adopted stimulusjs a couple of years ago for adding some basic interactivity on our pages and it was a decent tool, but we quickly grew out of it once we started adding more complex (form based) front-end behavior involving lots of state changes. We're currently trying to migrate a significant amount of stimulus code over to react. Not knocking on stimulusjs, but just be wary that it does not grow well with increasing client side complexity. You'll end up writing a lot of javascript boilerplate / DOM manipulation code / custom state management components. From my experience, it's a nice lightweight tool for when you: 1. Just need to add some light interactivity (toggling visibility of components, any basic view filtering) that do not involve a lot of logic 2. Don't need j.s unit tests (stimulus has not been the most testable tool - we lean mostly on integration tests).
- andrei_says_ 6y agoWhat UI behaviors necessitated you to move beyond stimulus? Could you provide an example?
- catwind7 6y agoSure! One typical example is a component that collects (and displays) education history provided by the user. I'll list out just a few key UI behavior: * A "read" view which represents a list of containers with titles that show each instance of a users answer (for example, if you provided 5 sets of answers, such as 5 universities you attended, we need to show those 5 in some sort of preview form on the page) * A button that when clicked opens up a modal which contains multiple "steps" - each step is a small set of form inputs that range from simple ones like date to more complex ones involving search / autocomplete or file uploading. * A button inside the modal that lets you navigate through the various steps / groups of form inputs. * A "save" button inside the modal that persists your answers. This closes the modal and shows your set of answers as an instance in the list view (see the first bullet). * In-line validation errors as the user is filling out each step of the modal * Messages to notify the user that a modal needs to be opened and completed (Like if the user closed the modal before filling everything out, we want to let them know it's not fully completed). Where some of the biggest complexity crop up: * Form input data handling and general state management. As I mentioned, we can have upwards of maybe 20 controls in this component. At minimum, we need to write code to listen to input changes and make sure this new information is kept in sync with both internal component state and other parts of the UI. If you gave us a valid answer, we need to make sure to update the UI to show a checkmark (in the simple case). We also have controls that show or hide based on your answers to other form controls, so we'll need to handle inputs by potentially hiding or showing other inputs. * Ok, so what if we need to write some UI updating code? Well, updating any part of the UI requires manual DOM manipulation. At minimum, this can be something simple like toggling visibility of an element by adding or removing a class. At worst, this involves appending html template strings (like if we need to show a list of errors on top of the modal) when the user attempts to save bad answers. * Cross-component communication is difficult. Want to add a subcomponent? And it needs to communicate with the parent? For example, each step in our view is sort of a "sub form" and it needs to talk to the parent "modal" to keep global state - you'll need to roll some basic event handling code between the two. * Testability. All these DOM changes - how do we test that our component is behaving? At the time we adopted stimulus, there wasn't a standard way of testing our stimulus controller so we've mostly leaned on (expensive) UI integration tests. Some of this complexity is sort of inherent to forms - forms are complex UI components. There are hosts of libraries in other frameworks for dealing with form inputs alone (in order to cut down on the boilerplate you have to write). For example, formik in react helps cut down on a ton of boilerplate you have to write in order to wire the form control DOM state to react state. Some of this complexity is also our own doing - we keep A LOT of state in the DOM. If a user blanks out an answer, we need to update the DOM with an additional form control whose value will get passed back to the backend to persist the change in the DB. Moving over to ReactJS won't help us here - this will require changes to our backend vs frontend API's. However, having to handle the bulk of those DOM changes yourself requires a lot of code, is pretty error prone, and quite hard overall to maintain (as anyone who has rolled apps using vanilla javascript can attest to). StimulusJS doesn't offer many facilities for changing your UI in a declarative way. Finally, cross-component communication is very common and while there is a number of ways to pass data back and form between stimulus controllers, there isn't really a nice way to do it outside of passing events. This is fine for simple cases, but error prone for our use cases. In reactjs, invoking callbacks that change parent state feels much more straightforward and has been easier to test. Hope that helps
- osxman 6y agoThis is good news. I work for 6 months with StimulusJS 1 and I'm very happy to use it. I use it mainly in a rails app combined with Turbolinks. My original thought was to go for an SPA with a rails API backend. I tried out both React & Vue extensively but got stuck in a while. Since implementing Rails and plain 'vanilla' JS it went better. But with StimulusJS 1+ it went simply great. Allready upgraded to 2.0 and it feels even more structured with the new features, like values and improved targets.