YouTubevideo· Aug 7, 2024· 19:38

5 AlpineJS Plugins You Need To Know About (& bonus tool)

Watch on YouTube ↗

transcript

46 moments
  1. I've always loved Alpine JS but a lot of the magic that Alpine JS has and all the features that you can do with alpine JS are kind of hidden from me because of using Livewire if you don't know Caleb porio the creator of Livewire is also the creator of Alpine JS and because of that especially in Live Wire V3 the latest version Alpine JS and Live Wire are so connected where you don't really necessarily know what parts are Live

  2. Wire and what parts are Alpine JS they're one and the same but Pine GS has a whole bunch of really cool features that even if you're using it outside of Livewire outside of Lille that are really incredible to take a look at so I figured I'd make a video here's five of my favorite plugins and maybe a bonus one to use Alpine JS I didn't even realize but I even got the the shirt on today so there we go ripping Alpine

  3. Alpine JS if you don't know Alpine JS is just a lightweight JavaScript framework very similar in syntax to something like View but the premise is it's just all contained in line of course you can definitely separate this out but here are some uh extensions that you can use within Alpine JS in this case Alpine JS makes it really easy to have custom

  4. extensions or plugins that you can use and input into whether you're creating them yourselves like a custom directive or you have different plugins and that's what we're going to look at today the first one I want to take a look at is this create Alpine app and this is not a plugin this is like that bonus one before we actually get into five of my favorite plugins for Alpine JS but this is built by Dev Dojo ton Leia who

  5. oversees everything at Dev Dojo MPX create Al Alpine app and then you can just use whatever app you want so this is really similar to if you were to create a v app for example a lot of times this is the go-to way to do anything within react felt or view if you're not not using something like nextjs or next and now Alpine has the same thing too of course you could probably use like an astro if you want

  6. to mess around with alpine 2 but this is a great way to just just get things started I to pull open my terminal and just say MPX create Alpine app and then let's just go ahead and call this for the video there we go it will install everything that you need to get Alpine up and running for you so if I was to go into this folder

  7. and let's go ahead and run mpm run dev then we just get this out of the box so this is great it has hot reloading it we can just modify the page of index.html and we can have everything good to go so we're going to use this create Alpine app starter for kind of playing around with some of these plugins that we're going to take a look at first up of the five plugins I think

  8. you should know about within Alpine JS is Alpine clipboard created by Ryan Chandler Ryan Chandler is phenomenal in the live wire laravel alpine JS just scen in general and this is incredibly simple so you can install most plugins with an Aline JS you can install with a script tag or with mpm I'm just going to do it through mpm right now let's go

  9. ahead and do that here and then we can go ahead and add this this clipboard plugin so open up vs code index.js so now we can add that and then Alpine do clipboard or Plugin do is clipboard and then it looks like we can add some data and then we can copy and

  10. use this dollar sign clipboard initiative to copy whatever is in the data to the clipboard so I just go ahead and take this itself and we'll go ahead and say this right here okay it's a little unstyled but let's see what this looks like so we can write anything in here hello there YouTube and I copied a clipboard if I was to paste this up here

  11. hello there YouTube okay fantastic incredibly easy way to just get like a clipboard up and running in your Alpine app and this is just I I use this plugin a lot even in my laral applications my Livewire applications as well next up Alpine Auto size so this is fantastic for text areas that need to be Auto sized incredibly simple you just place

  12. this x- Auto siiz directive on any text area and then all of a sudden you have that input it's incredibly simple so let's go ahead and install this show it off mpm install again you can use the CDN for most of all these plugins if this is something that you're just looking to have maybe on one page that's probably a good way to do it within any application l whatever have you and then we'll just go

  13. ahead and import that auto siiz plugin so go into index.js again and we'll put that down there and now let's just have a text area so in our index. HTML we'll just go ahead and delete this and we'll have uh a text area maybe we'll just add some class to it too just here let's see so type something here so it's not

  14. there's no way to Auto siize it right now if I was to copy and paste then all a sudden it just starts Auto sizing so if I have one line of code and of course we can change the Styles if we need to this is great for any kind of note application that you have but you can see how that's really simple we don't have to do anything else it just works it just changes the size of that text area to go with

  15. the flow now this next plugin is actually a first party plugin but you still need to install it's not included out of the box mostly just because stuff like this you don't necessarily need to have installed out of the box so let's install it but then I want to show you what the difference is between using this the collapse plugin and there's a couple of different other plugins that Alpine JS has that are first-party

  16. plugins things like resize or persist Focus anchor morph a lot of these were built in order to have that under line magic for a live wire but they're also just great on their own as well but the collapse one is just really interesting and beautiful I think so let's go ahead and install it mpm install and then we

  17. need to import it index.js place that there and now what you need to do is just add the x- collapse to whatever you are showing and usually within Alpine JS you have this data and if you need to show something when that data has changed in this case if expanded is false this is very simple Alpine ja in the sense that this is what

  18. 90% of use cases are you're opening a hamburger menu or you're or doing like a FAQ drop down whatever that might be so let's go ahead and I just want to show this and what this looks like without the x- collap collapse first so we go ahead and place this here and we'll remove move x- collapse why don't we style this a little bit behind the

  19. scenes all right that's been styled one of the cool things to also note for this Alpine Jaz like the create Alpine app is it has Tailwind out of the box so you don't have to mess around with any other styles or if you do you can easily change that when you're creating a new Alpine app but here's just a button and this is again most of what you might use Alpine for Alpine's incredibly flexible this is what I use it for 80 plus% of

  20. the time Taco content and we see it this button just turns that content on and off but what does the x- collapse plug-in do so now I can just add x- collapse and very nice huh that's CR that's I love it and the only reason it's like jumping at the end is because of the where the uh scroll bar is I deleted everything just because I don't know why the padding was making it a

  21. little stut I let me see if I add some margin let's see here so yeah like that looks a lot smoother I wonder if it's because the x- collapse has the padding so if I like had a if I was set this to a div and then like here we have some

  22. padding if we were like class padding 20 maybe yeah so it looks like it might just be I had the padding in the element that was being collapsed and shown again this was mostly just to show off the plugin itself but it's nice that you can have something like this kind of Animation without having to do much the

  23. next one is Alpine Auto animate so if you don't know a form Auto animate an incredible little library that basically just gives you these kind of uh anim ations whenever you are doing anything on the page you're adding something to a list you're moving things around on a list uh in Native Java Script for example it just changes the order but if you use Auto animate again this is not

  24. changing any of the underlying functions this is just placing this Auto animate tag on the whole list now you have this animation where it keeps tracks of all keeps track of all the elements within that list and then animates it accordingly and it's pretty easy to install even if you don't use the opine JS plugin because it's there's a direct native JavaScript implementation where you just add this script do all of that

  25. of course there is one for react view preact solids felt Etc but with an Alpine Auto animate it's even easier so let's go ahead and install it and then we can add the plugin and then anything that we add this x- Auto animate directive to will automatically apply those animations okay with the help of Claud I Just Whipped up this to-do list very quickly and this is an implementation

  26. with an Alpine JS where we're just saving these to-dos to in an array and we have some functions to be able to add those so this works out of the box but it's not very flash they're just added there's no animations and this is where we can showcase how incredibly simple uh this form kit animate works again there's things within if you're using

  27. larl for example larl live wires animate has a lot of these kind of underline helpers for you so we probably will add this to each of the Allies maybe we can say x- Auto animate I believe it was called that let's go ahead and see so it's x- auto- animate let's see

  28. here perfect let's try that let's try adding it to the parent div okay and I was mistaken it's not best in like the list item it's best in the parent div and the template is something that Alpine JS uses for looping through elements so it's best to be put in the UL so let's go ahead and see what this

  29. looks like so now if I add one add another remove remove again very simple animations but incredibly easy to add where we we probably would want to clean up like the removal aspect of it so like specific easing functions or how long you would like it to be animated for and then you also have the ability to toggle specific animations so maybe

  30. for the delete we wouldn't want the animate to be enabled for example and it looks like it's also additionally configured with the auto animate do configure example so it's just using Auto animate under the hood it's just wrapping it then last but not least I put out a tweet about Alpine Ajax something that is really similar to HDMX but specific for Alpine and holds a lot

  31. of maybe initiatives and directives that Alpine JS uses so it's not an additional plug-in additional library that you would have to install for your frontend Ajax inspired requests and in this case Alpine Ajax uses a lot of the functionality or the syntax of Alpine but specific for making a lot of those front end to backend calls for example

  32. after you install it you can uh Target a specific element on your page in this case comments and when you have an action towards the SL common API route it will just automatically add that common so I already installed this and set this up within a lay of applications so why don't we take a look at what that looks like with with alpine Ajax hey there editing Josh wanting to clarify a

  33. couple of things implementing or adding an Alpine JS plugin in a laral application is a little different because Livewire uses so much Alpine under the hood so it's not the same as just adding an Alpine plug-in using Import in your app.js instead you have to import Livewire and Alpine as well as the plugin you're looking to implement from this distributed live wire. ES M

  34. and then you don't have to call Alpine dostart Live wire. start starts both Alpine and Livewire you would just have to initially initiate that plugin here and then wherever you are using Alpine JS and live wire in your application in this case I'm just in the welcome. blade. PHP you would have to input this Livewire script config this just tells it that you want to use a custom Livewire script or custom Alpine script

  35. this is all in the doc but something that's really easy to overlook so back to Alpine Ajax so this is the Alpine Ajax version of the to-do list that we created in strict Alpine but this has Lille behind the scenes where we're interacting with the routes within Lille and I'll probably create a separate video on this of why you might use something like HDMX or Alpine Ajax over Live Wire and while I definitely have my own personal biases there are benefits

  36. in this case if we were to turn off JavaScript with the implementation that I use that I created here if we were to turn off JavaScript it would still work in the exact same way but Alpine Ajax kind of enhances it so it's a way of doing straight blade requests but making it easier for you and this isn't perfect because you could use laravel's fragments to do some of this but here's just what it looks like if I was create a new to-do say Hello There

  37. YouTube submit it it's automatically added we can see that there's no refresh if you just take a look up here if I was to add a new one there's no refresh to the page It's Just Happening behind the scenes using a Jack's request so if I was to create a new one submit and there's this to-do payload or to-do that is being sent off in Ajax request and

  38. the payload is simply has this token then some text and then we're given back a new Li item in this case one two submit into this options here so if I was to take a look we have this IDE of to-dos in this unordered list and then in our Alpine Ajax we're saying I am initiating this Alpine component and I

  39. want to Target the to-dos item so that when the action of Slash Todo is hit we can push whatever is being received back into this UL of to-do in this case this x merge is saying we want to append appended to whatever else is here so now if I say Test new one all of that's being appended to that UL if I was to

  40. refresh we see exactly the same because we're just passing that in as the laravel prop or the data to this particular component but I'll show you why this is incredibly useful within not only is the syntax very similar so you don't necessarily have to learn a whole new syntax for HDMX for example you don't have the HX the X nit X Target is very similar to what you might see in

  41. Alpine and we also have this ack success where we've been are able to reset the text for example but this works without JavaScript as well so if I was say disable JavaScript okay and we'll refresh the page and then I'll say hi there submit it still works and you saw that there's a page refresh because we're not necessarily sending Ajax anymore what

  42. we're doing is in our laral web route we're saying if there is a header or if there's not a header of the X Alpine request and that's what Alpine Ajax kind of sends over with that request has a header of X Alpine request then you can do whatever you want within this route and this would probably be best to be in a controller but you can do whatever you want to say okay if I don't necessarily have that request then maybe I can still

  43. still create the to-do but now I'll just refresh the page or send it to a new page whatever that might be and you can do this is just returning an HTML response of that UL with the to-do title added to it but this would be the great case for using the fragment options within laravel so instead of UL we could just return the view of fragment and we

  44. could probably just set that up as like here so fragment uh to does for example then you could just say I want to only return this fragment from the page but if x Alpine request is not in the header if this is an actually an Alpine request and someone with JavaScript disabled then I'm just going to return the whole page you can see here how this is a great way of adding this flexibility

  45. adding this interactivity without necessarily jumping complete into full JavaScript page again I would say complete apples and oranges comparison to something like Livewire it's really just an HDMX alternative for people who are already using Alpine and that's it there's so many more plugins for Alpine JS that I would advise you to take a look at awesome Alpine JS which is going to be in the description as well and

  46. then so many ways for you to you expand Alpine js2 creating your own directive instead of adding a new plugin but Alpine JS makes that incredibly simple and it's makes it incredibly easy for people who have created plugins so give the plugin some love star them on GitHub until then keep creating

just you here