21 ms·
Alpine.js
- hadrien01 4y agoPrevious discussion: https://news.ycombinator.com/item?id=27470397 https://news.ycombinator.com/item?id=27470397
- afhammad 4y agoAlpine.js is seeing an increase in utility as it pairs nicely with htmx[0], Phoenix LiveView[1] and the likes for lightweight interaction that doesn't require the server. [0]: https://htmx.org https://htmx.org [1]: https://github.com/phoenixframework/phoenix_live_view https://github.com/phoenixframework/phoenix_live_view
- rasso 4y agoActually, HTMX has a fundamental flaw if it comes to history restoration: In some scenarios it takes snapshots of the DOM for history navigation, just before navigating away from a page. If the DOM was previously modified by other JS libraries like Alpine, jQuery or any other tool, these changes end up in the history snapshot, leading to unexpected results [1]. I've had a much smoother experience with Alpine + swup [2]. Disclaimer: I became a maintainer of swup this year. [1] https://github.com/bigskysoftware/htmx/issues/1015 https://github.com/bigskysoftware/htmx/issues/1015 [2] https://github.com/swup/swup https://github.com/swup/swup
- afhammad 4y agoFair, I was going based on hearsay/docs. It looks like LiveView has been trying to accommodate the issue with some work arounds such as: https://github.com/phoenixframework/phoenix_live_view/issues/809 https://github.com/phoenixframework/phoenix_live_view/issues...
- dgb23 4y agoTo illustrate: Say you have a collapsible menu (Burger etc.) which you just implement with a simple CSS based toggle. You open the menu and then navigate to a different page (via htmx). All good. But now your back button will break because it will send you to the previous page but with the menu open. In this particular case it's _kind of fine_ depending on your navigation concept and design. It might even be a feature! But this behavior just proliferates little workarounds and caveats and can lead to surprising UI bugs, especially if you need some state management in-memory etc. So htmx is great the less JS you use on top of it, except you are very aware of how it operates and how you interact properly with it's events. It's a very well designed and small library overall - with caveats.
- foldr 4y agoI don't know as much about the underlying issue as the sibling, but I've also found that LiveView and AlpineJS interact in complex and unexpected ways in the case of components. The only sane way I found to integrate them was to make an Alpine-based component which is solely responsible for rendering itself, and then communicate updates back and forth with the LiveView process via custom events. That works but it's fairly heavyweight.
- sodapopcan 4y agoI would just make the jump to JS Commands if you're able.
- foldr 4y agoI would like to be able to use Alpine in precisely the cases where the JS commands aren't enough. This case was one of those accursed custom form controls that designers love to come up with (a dropdown with checkboxes and a search filter).
- sodapopcan 4y agoHa, right. I was in that same position. Unless you're working with a fixed, non-search, massive dataset, I found the performance is just fine doing it in pure Liveview over the socket, but I don't know your situation.
- foldr 4y agoYeah, indeed, doing it in pure LiveView is a realistic option, but it does degrade the user experience for e.g. someone on a crappy 3G mobile connection.
- sodapopcan 4y agoRight. Well, shit!
- budafish 4y agoHTMX has a complimentary framework like alpinejs called Hyperscript [0]. (On second thought not exactly like alpine..) [0]: https://hyperscript.org/ https://hyperscript.org/
- pictur 4y agoWhen I see tools like this, I remember once again what a great revolution angularjs was. If people are happy with tools like this, of course that's important, but I don't think I'll understand the logic of writing javascript code in an attribute.
- memorable 4y agoI tried Alpine.js a very long time ago. Managed to build some small web apps with Haml[0], but never everything big. It's generally very easy to use but when it comes to scaling it doesn't scale very well. Building a todo app with it requies many lines of JS objects, combined with that every object is defined in a string makes the code look a bit ugly. Useful for prototyping and small web apps if you dont want to use complex stuff. [0]: https://haml.info/ https://haml.info/
- flohofwoe 4y agoI was fully expecting Alpine Linux running in a browser when clicking the link. I am disappoint.
- pwiecz 4y agoI was expecting either the Alpine Linux, or a port to js of the mail client. :)
- FlyingSnake 4y agoHere you go! https://bellard.org/jslinux/ https://bellard.org/jslinux/
- spyremeown 4y agoOf course it's Bellard. I wish I had like, a day with him, just asking whichever questions I want.
- deleted 4y ago[deleted]
- FlyingSnake 4y agoHis email is right there on his website, I say just write to him!
- peterangular 4y agoYeah - 100%. I have pulled this off successfully with some crazy household names in tech. I really value some of those random emails - I go back and read them from time to time. People are often really chill and kind about answering questions and love to help people who take a genuine interest in their work!
- actionfromafar 4y agoYes, but imagine the luxury of casually hanging around with Bellard for a day and just ask random stuff you come to think of, no regrets.
- BuckyBeaver 4y agoIs what? You forgot the rest of the headline.
- lloydatkinson 4y agoI use this in one project but its bundle size is very large for what it does. I think Preact is usually a nicer option (and no, it doesn't require build tools and NPM if your IDE of choice is notepad) with a React compatible API. It's 3KB. Even less if you opt to not have any React compatibility at all. Alpine is 13KB. https://bundlephobia.com/package/alpinejs@3.10.5 https://bundlephobia.com/package/alpinejs@3.10.5 https://bundlephobia.com/package/preact@10.11.3 https://bundlephobia.com/package/preact@10.11.3
- LudwigNagasena 4y agoWhat is wrong with build tools?
- xigoi 4y agoMore complexity and friction.
- nisegami 4y agoI think it's more that these libraries are sometimes used in projects that aren't already using a build tool.
- dgb23 4y agoThe largest, most painful and apparent issue with JS related build tools is that they change and break. Frequently. And no, `package.lock` is not enough of a solution, because you will have to update dependencies at some point. Congrats, you now have multiple moving, breaking parts in your dependency tree that throw the weirdest and unrelated errors. So you're hunting down github issues, workarounds and patches, while still not really knowing what the problem was. The bonus here is that you need to remove these workarounds at some later point because your build tools and libraries have fixed the issues, so your code breaks again with very fun error messages or just straight up opaque and weird behaviors. At some point we have to ask why we're doing this to ourselves. It's not fun at all.
- clncy 4y ago
- ianpurton 4y agoWarning: Libraries like this don't work well with tight CSP policies. See. https://alpinejs.dev/advanced/csp https://alpinejs.dev/advanced/csp
- mtlynch 4y agoYeah, this is the dealbreaker for me with alpine.js and htmx. And I wouldn't even say "tight CSP" as much as "standard CSP." To make alpine.js play nice with CSP, you have to allow unsafe-eval, which severely weakens your protection against XSS. alpine.js claims to have a compatibility build with CSP, but it's not officially available, doesn't fully work, and the parts that are broken in the CSP build aren't documented.[0] htmx works under CSP, but it opens a new vector for attackers to inject JS into your page, which effectively neuters CSP.[1] [0] https://github.com/alpinejs/alpine/discussions/1944 https://github.com/alpinejs/alpine/discussions/1944 [1] https://htmx.org/docs/#security https://htmx.org/docs/#security
- canadiantim 4y agoIs there a minimalist js library you would suggest that is better for security? Mithril?
- mtlynch 4y agoI've desperately searched for one, but I can't find anything. Stimulus[0] I believe is compatible with CSP, but I've found that it's more work than just writing vanilla JS. I've heard good things about mithril, but I've never tried it. [0] https://stimulus.hotwired.dev/ https://stimulus.hotwired.dev/
- mekoka 4y agoRegarding htmx https://htmx.org/docs/#security https://htmx.org/docs/#security > htmx allows you to define logic directly in your DOM. [...] One concern with this approach, however, is security. This is especially the case if you are injecting user-created content into your site without any sort of HTML escaping discipline. You should, of course, escape all 3rd party untrusted content that is injected into your site to prevent, among other issues, XSS attacks. Attributes starting with hx- and data-hx, as well as inline <script> tags should be filtered. That's been SOP for web development for aeons. I'd say the rewards vastly outweigh the costs. Not a deal breaker for me.
- marto1234 4y agoIve been waiting for a new JavaScript framework
- smitty1e 4y ago...all week.
- gremlinsinc 4y ago...5 minutes. (however alpine is hardly new, not sure why it's even posted unless it was a changlog, but it was the main page, etc...)
- bearmode 4y agoAlpine is a few years old already
- moonchrome 4y agoMods can we add [legacy] to the title please ?
- junon 4y agoNever seen that be a convention here, and what makes you think this is legacy? who decides that? Is Linux legacy too?
- commitpizza 4y agoI think he/she is joking
- naasking 4y agoBecause of JS churn. A few years old? Might as well be jQuery!
- nigamanth 4y agoDAMN. Saves a lot of code and effort for most websites. Very easy to use too!
- brap 4y agoThis is pretty neat. I think being lightweight, and having a very small API is important. But honestly, at this point function-style JSX/TSX is hard to beat in terms of API. There's basically no DSL to learn (`x-for`, `x-if` and so on), you just use plain old JS with all of its existing capabilities. And it's just a JS function, in come props, out comes HTML. You technically don't even need JSX to do this, it's just syntactic sugar over plain JS function calls. Sure, React is bloated, its full API has huge surface area (that you almost never need), and there are some icky parts (state, side effects, basically the whole hook system is ehh and doesn't fit the functional paradigm). There's definitely room for improvement there, but I think it will probably not come from React, but some other JSX-based framework (Preact and SolidJS suffer from some of the same issues). In any case JSX is going to be super hard to beat IMO.
- Kinrany 4y agoThere seems to be an unoccupied design space for a mix between JSX and shell languages. Both effectively compose concurrent processes.
- junon 4y agoI don't see how JSX has anything to do with processes, much less concurrency.
- brap 4y agoI think what he meant was that the style of JSX does lend itself well to parallelism (e.g different subtrees can execute in parallel), though to the best of my knowledge no such implementation exists (maybe something can be done with WASM or WebWorkers)
- junon 4y agoAny componentized UI system can do that though, in theory. JSX isn't special in that regard.
- 4y ago
- FlyingSnake 4y agoAlLpine.js is a great jQuery replacement. I'm using Alpine.js in a Django project and it does what it says on the tin. It blends well with the MVT style of Django along with HTMX and makes the client side interactions really easy. This is now my favorite JS framework for doing light client side JS magic.
- rajasimon 4y agoI agree with your point. Django + HTMX + AlpineJS makes the web application development completes.
- nop_slide 4y agoIf you add in Tailwind you get the PHAT stack (python htmx alpine tailwind)
- deleted 4y ago[deleted]
- insane_dreamer 4y agowhat's wrong with jQuery?
- imacrayon 4y agoNothing, if it works for you great! I’ve found Alpine code to be more declarative and easier to reason about. Also, jQuery often requires you to write more code to do the exact same thing in Alpine. One thing jQuery does better is AJAX, Alpine doesn’t have builtin AJAX tools. I’ve made a plug-in to bridge that gap though: https://imacrayon.github.io/alpine-ajax/ https://imacrayon.github.io/alpine-ajax/
- fantasizr 4y agoWe use alpine as well, very simple to add in for some simple reactive uis. Flask+Alpine seems to work well for us.
- nikolay 4y agoOh, I remember it now - this is the free framework with paid components [0]. Thanks, but no thanks! [0]: https://alpinejs.dev/components https://alpinejs.dev/components
- kugelblitz 4y agoWhat's so bad about charging for a service or for tools? Just because some of the MegaCorps offer Javascript frameworks + tools for free, why shouldn't a small competitor be charging a bit of money for work that is built on top of the free framework to keep it financially sustainable? It's an optional purchase for people who might want to rather save time, e.g. when you're a small dev team trying to build something, you need to calculate the opportunity costs. Furthermore, sometimes I will even refuse to use a service if they don't charge money. The interests of a service will often be aligned to those who give the money. So if the money is not coming from me, the supposed customer, it has to come from someone else.
- Mrdarknezz 4y agoWhat is wrong with that?
- aliswe 4y agoIndeed, i dont get the "i want to make money by using software by peple who are not allowed to make money"
- nerdawson 4y agoNothing is stopping you from building those components yourself. Or, you can pay the author who’s already done the work for you. I think it’s a great way to monetise an open source project.
- aatd86 4y agoWhy are some dev so cheap? Everywhere else you want great tools, you are willing to pay the price. Especially if they help you earn something in return. That's a strange mindset *pondering*
- 4y ago
- rosszurowski 4y agoI'm glad alternatives like Alpine exist for small sites that don't need many additional behaviours. But having used Alpine for a medium-sized art gallery website last year, I can't really recommend it for anything larger than a very simple site. The fact that all your code is scattered as strings everywhere without error checking or types makes it really hard to debug and work with long-term. For lightweight front-end tooling, nothing has unseated the ease of Preact for me.
- victorvosk 4y agoBasically the problem with every framework that think its fine to sprinkle in strings as code in html.
- lordgroff 4y agoAnd for Alpine it's sort of fine. People have taken Alpine a lot further than the original intent, which is to add sprinkles of JS to what is otherwise a server rendered page. For anything even slightly more complicated we have things like Mithril or Preact, and yeah they avoid this sort of thing for a reason.
- blowski 4y agoI’m glad when tools like Alpine remain in their niche. Too many of them grow with their userbase, adding every requested feature, until they become as unwieldy as the framework they originally tried replacing. If you outgrow Alpine, migrate to another framework instead.
- _cenw 4y agoThis feels like a less mature version of riot.js.
- Moissanite 4y agoFor someone with no JS experience who wants to learn, is this framework a good place to start? (I don't mean learning JS/TS syntax, more the general paradigm for user-facing code which does something meaningful besides printing "hello world")
- qhungg289 4y agoI would say you better start with using JS to manually manipulate the DOM, then start with a framework later.
- earthboundkid 4y agoI agree that you should start with vanilla JS, but Alpine is a good follow up because it just adds conventions around what you'd be doing in vanilla JS anyway.
- code_runner 4y agoEvery framework will teach you a different way of looking at things… can’t go wrong picking one and going really deep, then exploring some others. I wouldn’t recommend one specifically, but definitely start with one and stick with it for a bit
- vagrantJin 4y ago> but definitely start with one and stick with it for a bit Devs are paid quite a bit of money to play russian roulette with their tool choices. Frameworks are an abstraction and not really key to understanding anything. And devs need to understand, at least conceptually what the framework is doing (or attempting to do) without too much magical BS. Ideally that starts with working with the dom and seeing first-hand the pains and joys of adding and removing elements, functions, sync and async behaviours, data handling, objects etc. Then only will frameworks click and make sense, and so choose the right tool for the right job.
- code_runner 4y ago
- napkid 4y agoFor lightweight JS interaction, I prefer the Stimulus controller approach that doesn't mess too much with your HTML, and doesn't try to sell you "components"... https://stimulus.hotwired.dev/ https://stimulus.hotwired.dev/
- saluki 4y agoThe real power of Alpine.js is part of the TALL stack. Tailwind, Alpine, Laravel + Livewire.
- jostylr 4y agoThere is also the PETAL stack: Phoenix, Elixir, Tailwind, Alpine, and Liveview. The ordering doesn't make sense, but it is a nice acronym. I haven't actually gotten around to developing with it, but I am excited to do so.
- nberkman 4y agoIt's now the LEPT stack: https://fly.io/phoenix-files/plucking-the-a-from-petal/ https://fly.io/phoenix-files/plucking-the-a-from-petal/
- skerit 4y agoAm I the only one who doesn't add random attributes to the existing HTML elements? I use the dataset `data-` attributes for that. You can go nuts with custom elements though.
- blowski 4y agoWhat practical difference does it make?
- rglover 4y agoMaintainability and browser compatibility (less of an issue these days). If you use a non-standard attribute and hand your code off to someone else, they likely won't catch it and can introduce bugs.
- blowski 4y agoRegarding the bugs, what makes it more of a problem in HTML attributes than with any other code?
- rglover 4y agoNothing makes it more of a problem. Same as any other code. Just the idea of introducing non-standard things (especially if used inconsistently) increases the likelihood that things will break and be harder to fix. For example, if someone binds an event listener to an element in two different places with something like clicker="button" in one place and then uses data-clickable="true" in another place, it can introduce confusion. Not only does the original developer have to remember all of their custom attributes but so does anybody else who comes on to the project.
- snshn 4y agoYet another JS framework. How fast is it really? The learning curve may not be worth it if it's slower than https://www.solidjs.com/ https://www.solidjs.com/ and according to https://krausest.github.io/js-framework-benchmark/current.html https://krausest.github.io/js-framework-benchmark/current.ht... few things are faster than a very little-known framework called Mikado (https://github.com/nextapps-de/mikado https://github.com/nextapps-de/mikado). That being said, I would never sacrifice performance of my software for how trendy or popular something is, e.g. React is #1 in popularity yet it's overcomplicated, massive, slow as hell.
- devops000 4y ago68 code lines for a basic drodown https://alpinejs.dev/component/dropdown https://alpinejs.dev/component/dropdown
- fudgefactorfive 4y agoLooks interesting, I might take a deeper look into using this. One thing to note, on my mobile browser (Firefox Nightly) the site is a bit broken. The page has the wrong width so periods etc are off screen and for the code example blocks I can't scroll them to see the whole line.
- conradfr 4y agoIf you know (and like) Vue, look at petite-vue as an alternative. https://github.com/vuejs/petite-vue https://github.com/vuejs/petite-vue
- earthboundkid 4y agoPetite Vue appears to be dead. ISTM that Evan You saw Alpine, made a quick proof of concept version using Vue, then handed the project off, and it died. Nothing wrong with that! But I don't think it makes sense to use it over Alpine for anything serious.
- Semaphor 4y agoSadly. It would have been perfect for me, I wrote an SPA and some smaller widgets with Vue, would have loved to use petite Vue elsewhere.
- johnmw 4y ago“petite-vue is indeed intended to fill the gap for progressive enhancement cases where Vue 3 would be too heavy-handed. It is not abandoned, but rather it is considered "done" because the scope is well defined. I don't think it needs more features (as that would defeat the purpose of being lean and minimal). If you find yourself needing more than what petite-vue provides, you can either go up to Vue proper, or try https://alpinejs.dev/ https://alpinejs.dev/. That said, I should update the README to indicate this more clearly.” Github discussion: https://github.com/vuejs/petite-vue/discussions/53 https://github.com/vuejs/petite-vue/discussions/53
- earthboundkid 4y agoAlpine is great. I've been using it in production since 2020, and I've never had any complaints. I like that it embraces JS instead of pretending that JS doesn't exist, like other microframeworks do. (I'm thinking of Stimulus and HTMX specifically.) Alpine's job is simple: it adds encapsulation and reactivity around JS. Other than that, it's more or less vanilla JS, and you can use it the same way you'd do things if you were doing document.querySelector yourself.
- aka878 4y agoHorizontal scroll doesn’t work for code blocks in iOS Safari.
- foreigner 4y agoI've been using Alpine.js in production and love it. I wish it was easier to debug when things go wrong though. Also the CSP limitation is a bummer.
- typeofhuman 4y agoI still use Knockout[0] for almost an identical experience. 0: https://knockoutjs.com/ https://knockoutjs.com/
- capitanazo77 4y agoOne word: SEO Alpine uses the dom so it’s perfect for public sites with anonymous users with mostly static content. Makes google happy. It’s not meant for GUIs.
- tomhallett 4y agoTo make sure I'm understanding you correctly: you are saying that sites which use alpine will use progressive enhancement, where most of the static content is vanilla html, and then little alpine snippets are used to improve the page (nav bars, widgets, etc). Versus full SPA where all of the content is clientside rendered. At first, I (incorrectly) thought you were saying that the alpine templates are stored "in the dom", so then google would parse them. For example: <div> Copyright © <span x-text="new Date().getFullYear()"></span> </div> But, once I figured out you were talking about progressive enhancement, I came back to reality that google will only see "Copyright (C) " and won't see "2023". Note: I think it can be used for CRUD based GUIs, if paired with "Html over the wire" (rails hotwire, phoenix liveview, laravel livewire), but that's a different point.
- 0xCMP 4y agoFor Google in particular they can see the 2023 just fine[0], but I think it's needless complexity when you could just do it server side. Maybe it's nice for a rarely updated static site though. [0]: https://searchengineland.com/tested-googlebot-crawls-javascript-heres-learned-220157 https://searchengineland.com/tested-googlebot-crawls-javascr...
- moneywoes 4y agoThe benefit over Next Js would be small bundle sizes?
- brodouevencode 4y agoAnyone else have "new hot javascript framework" fatigue?
- kokizzu2 4y agome me me but I like svelte a lot :3 prefect fit for my projects for past 5 years https://github.com/kokizzu/svelte-mpa https://github.com/kokizzu/svelte-mpa
- dubcanada 4y agoThat's a fair comment, but I will say alpinejs is like 4-5 years old at this point. Not exactly "new" for JS
- tomhallett 4y agoYeah, I was hoping the HN title would give some context on why it was being posted now (2023). My guess: "TIL about a really cool js framework which has been around for years"? Which in my opinion is totally fair, but others might disagree. :)
- bern4444 4y agoI would never use a library that invents its own DSL. We already have JS and HTML native to every browser with all the capabilities we need. React/Solid/Preact and others build off this. Alpine, Vue, Angular and company do not (x-for, v-for, ngFor etc) I have no desire to learn a DSL specific to a single library.
- simonlc 4y agoSame, this in my opinion was one of React's biggest "innovations".
- frankfrank13 4y agohard agree. also especially brutal if you have to enclose it in strings like this, meaning you need to write an extension/language server to get this to have any semblance of a good developer experience
- aposm 4y agoThat's precisely my issue with React though - JSX is a DSL that looks almost like normal HTML, but differs in small uncanny-valley ways. Sure, it's fine once you're used to it. But it's very much not standard HTML.
- nickbauman 4y agoI was just going to say this too: JSX is a DSL. And a particularly screwy one!
- k4rli 4y agoWhat's screwy about it? In my experience it's been painless to switch between html+vanillajs and React projects.
- nickbauman 4y agoJSX has an Async "tag"... Let that sink in. <Async></Async>
- aporetics 4y agoHow is this different/better than htmx?
- etewiah 4y agoBTW, I feel some affinity for this just because I've been listening to Caleb Porzio's awesome "no plans to merge" podcast for so long. Good blend of entertaining and educational.
- jsmith45 4y agoInteresting that it markets itself as: > 15 attributes, 6 properties, and 2 methods. But when you check the documentation you get: > 18 "directives", 9 "magics", and 3 "globals". Not only are the names used in the documentation different from what the main page says, but not a single one of the numbers match. Not sure what to make of that. Its slightly misleading, but not really an outright lie. The extra items don't look at first glace like they are adding a ton of complexity or anything. Some of the unmarketed ones do look like they are more advanced features that are not needed most of the time.
- remram 4y agoCalling this a "JavaScript framework" doesn't seem accurate, it goes to great length to not use JavaScript. This is an HTML framework implemented in JS, the same way numpy is a Python framework implemented in C.
- carabiner 4y agoAlpine.js, Alpine Linux. What is next in the Alpine ecosystem besides marmots?
- fks 4y agoShameless plug: Astro supports Alpine.js and is a great dev environment for trying out Alpine for the first time. It runs in the browser as well, via Stackblitz! - Astro: https://astro.build/ https://astro.build/ - Try Alpine in the browser: https://astro.new/framework-alpine?on=stackblitz https://astro.new/framework-alpine?on=stackblitz
- clementmas 4y agoPeople comparing Alpine to React, Vue, Svelte, etc. are missing the point. Not everything has to be a SPA. There are so many Reacts devs out there that now blogs and landing pages are fully rendered client-side. With a backend only used for its API. Instead of simply sending HTML. Alpine is great for the simple dropdowns, navigation drawers, etc. The things we used jQuery for before we started to do everything in JS. I still love writing SPAs but I'll stick to boring HTML with JS sparkled whenever I can.
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]