Skip to main content
View on YouTube to comment and like!

Syntax Video

Svelte 5: In Action | Build A Tasks App

Chapter 1 0:00

intro

spelt 5 was released about a month ago one of the biggest newest features being runes and in this video I'm going to show you spelt 5 in action by building a tasks application so this little app here allows us to add some tasks like so you can see we have some status if I check off some tasks that updates you can see the Styles update here we can remove tasks with some nice little animations we can filter them down to only show the too tasks or only the done tasks or display all of the tasks and as I walk through the build of this I'm going to point to areas where we're using some of the newer things that were introduced in spelt 5 and how those differ from how you might have done it in spelt 4 so if that sounds good to you let's start building now to start I have a basic app here that was generated using beat and we just have a Hello World message that we're rendering out if we look at it in the browser it looks like this I've also added in Pico CSS so we just have some default Styles now the first thing I want is a form so that way we can add

Chapter 2 0:42

create form

new tasks and so here in the compon onon directory I'm going to create a file called tasks form that's spelt and then inside of here I'll Define a form let's create an input with a label we'll give this input a name of new task and then I'll add a button here that will allow us to add that task and so now I want to bring an instance of this component into the app so over here in app. spel we're going to import in that task form and then now I can create an instance of it here in my template like that and while we're here let's just update our message to say the tasks app now if we take a look in the browser you can see we have a basic form I just want to push this add button over to the right so let's add some Styles and in spelt your styles are automatically scoped to the component so any styles that I Define inside of here are only going to apply to this specific component now I'm going to wrap this button in a div I'll give it a class of button container and then for this I'm just going to make it a flex box that pushes the button over to the right so set justify content to end and now our button should be over on the right great one last thing is I want some larger margin on the right and the

left so I'm actually just going to set some styles on my main element here inside of app so first I'm going to set a Max width of let's say 800 pixels and then I'm going to give it an automatic margin on the left and right and that looks good to me now let's set up some states so we can keep track of what the user enters into this text box and so in our task form component we're going to add a script tag you'll notice also set the language to typescript now I'd like to set up a state variable to keep track of that new task value and so for this I'll create a variable called new task we're going to set this equal to dollar sign state so this is the very first Rune that we're using and I'll ini ize it to an empty string and so this is a signal to this felt compiler that we want to keep track of the value of new task and now I want to bind that value down to this input so I can say bind colon value and then pass in the new task now I want to listen for the submit event of the form so on the form I can say on submit and then I'll pass in a function reference and let's create a function called form submitted so this will just be a basic function the main thing to note here is in spell five all of your Dom events have a new way of

specifying them because it used to be that you specified it as on ho and submit you can see now in spel 5 if you do that you will get a deprecated notice so all of your events should be converted to on submit instead and this is nice because it's exactly the same way that we Define events if you're just working in plain old HTML and so now there's no special Syntax for that in spell 5 now another thing that has changed in Sp 5 is we actually don't have access to event helpers anymore so you used to be able to add a helper like prevent default and this would automatically prevent the default action but now you can just handle that inside of the function itself so if we head back to what we had before now get access to that event in this case it's an instance of a submit event and then inside of our function we'll just prevent the default action so that the page doesn't refresh now inside this function let's just log out that new task and we'll make sure the value is coming through when that form is submitted so if I type in a new task and hit enter we can see that it gets logged out and so now I want a way to pass this task up to the parent component because the parent component is going to be in charge of the list of tasks so we need

to be able to pass this back up and so inside the task form we're going to Define some and we'll accept a prop called add task and in spel 5 we can now use the dollar sign props Rune if we Define a type we can say add task is a function and in this case I'm going to say that it takes in a string called new task and then just returns nothing and so now add task has the proper type and whenever the form is submitted we can actually invoke add task and pass in our current new task value and then after the form is submitted I also want to clear out the form so in this case I'll just say new task is equal to the empty string and now that we're we're accepting this prop we need to actually pass it down inside of our app component so if we head back over to app dosel you can see we're getting a type error because we're not passing down that add task function and so here inside of my app dots felt I'm going to create an add task function takes in that new task string and for now we'll just log it out to make sure that everything is hooked up and then we'll pass it down like so and so now if I try adding a new task you can see that it logs out so we're actually passing it

back up to the parent component and this is actually the preferred way to communicate between components inside of spel 5 so in spel 4 we had access to create event dispatcher and so essentially your components could dispatch custom events but now the recommendation is to just use call back props instead and so that's essentially what we're doing here we're actually passing down a function which can accept that data and so now inside of our app component let's keep track of that list of tasks so I'll create a state variable here called tasks and then we're going to use the dollar sign State Rune and initialize it to an empty array now let's define a type for our tasks and so each task will have an ID which is a string it'll also have a title which is a string and then we'll also have a done property which is a booing and this dollar sign State Rune can actually accept a type here and so we'll say that this is an array of tasks and so now whenever add task gets called we'll actually push into this array so here I can just say task. push and then we'll create a new object we'll give it an ID the title is going to be that new task

Chapter 3 5:15

tasks list

and done is going to default to false now this is a new feature in spell 5 where it actually tracks any changes made to to an array so this call to. Push will actually trigger a rerender whereas in spelt 4 typically you had to reassign the variable to get spelt to render but in spelt 5 under the hood these collections are actually using proxies so they can detect when that push method is called or any other array method that might modify the array now that we have a list of tasks let's actually list them out in the UI so here I'll create a section and then we'll iterate over each of the tasks for each of these I'll create a nice little card here and for now we'll just render out to the title and so with that now whenever we add a task we should see it on the page like so and we can say learn runes like that so this looks great but let's add a little bit of spacing below the form here and so in our tasks form component I'm just going to Target that form and set a margin bottom of one rim and now there's a little bit of breathing room now let's do a little bit of refactoring I'm going to move this

list of tasks into its own component so here I'll create a file called the tasks list felt and then in my app. felt I'm going to pull out this whole section here we going to move it into the task list but now I need to define a prop so inside the script tag here we can Define our prop we'll use that dollar sign props Rune and then we'll specify the type so tasks is a task array and I'll need to Define this task type in a common spot so that way I can use it in multiple places so I'll create a new file just called types. TS and then move this task type into that file make sure to export it and then everywhere I need it I'll import from my types and then I'll do the same thing over here we can import that type in so that looks good now we need to actually create an instance of the task list so in our app dovel we'll pull in the task list so and then just pass down our tasks and so now the app should behave in the exact same way but uh it's just a little bit more modular because we have things separated out into their own components so now I'd

Chapter 4 7:50

mark as done

like to add a checkbox on each of our tasks so if we check them then it marks that specific task as done and then also strikes a line through the title of the task so here inside of our task list we're going to add a checkbox to each of these tasks so I'm going to create a label that has an input of type checkbox and then we'll put the task title inside of that label and so now each of these has a nice little checkbox but I'd like to bind the checked attribute to whether or not this specific task is done and so we can bind the checked property to task DOD and so if we give this a try we can see that seems like it's working but we get a warning that says we have an invalid mutation because we're actually modifying a prop that was passed down to us so this is really nice that we're getting a very clear error here that tells us that we probably shouldn't be binding directly to that value and we essentially need to modify the state in the place that it was defined so instead of binding the value we're still going to set check equal to the current value

of task. done but now we can tap into the onchange event and essentially I just want to call a method that can toggle the done property so here inside of the onchange call back we'll call a method called toggle done and we'll pass in the entire task and so now I need to Define this as a prop so we'll pass it in here and then set the type to be a function that takes in a task and then returns nothing and so now we need to actually pass down this method so in our app component we're going to Define another function called toggle done that takes in a task and in this case it's just going to set task. dun to be the inverted task dotun and then now we'll pass down that prop as well and so instead of attempting to modify a prop that was passed down to the component now we're modifying that state in the same component that it was defined in so now if we give this a try and add a few tasks you can see that I can toggle but we're not getting any warnings in the console but now let's use the value of done to add a custom style to the title here so right here where we render out the title I'm just going to wrap this in

a span and then I want to add the done class if task. dun is true and so I'll Define that specific done CSS class and this is just going to set the text decoration to line through like so and so now if a task is done you'll see a line through the title like that next up I'd like to show just a little message right here that tells me how many of the tasks are currently marked done so if we head to our app. spelt let's add a message right above the task list that says X Out of X tasks completed now the total number of tasks is just going to be the length of the tasks array so we can do task. length but to get the number of tasks that are done we can actually use a derived value so here I'll Define a variable called total done and we're going to use dollar sign derived so this is new in spelt 5 and it's essentially a computed property so we can add an expression inside of here and it's going to get recalculated anytime its dependencies change so in this case we want to count up how many

Chapter 5 10:08

total status

tasks are done so I'll just do a simple little reduce and so the total starts off as zero and then for each task I'll turn that done property into a Boolean so true becomes one false becomes zero we'll add them all up and this will give us the total number of done tasks but now we can use this in our template so right here we'll render out total done and for this specific derive value anytime its dependencies change and in this case it's dependent on the tasks array whenever that changes this will rerun so if we add a few more tasks here now as I check the tasks you can see that that total is being updated automatically now let's also add a remove button right here so that way we can remove any tasks from the list so if we head over to the task list let's add a button to each of these tasks that just says remove and I want this to just be pushed to the right side so I'm going to make the task itself a flex box so I'm going to give this a class of task and then a task will be display flex and

Chapter 6 11:24

remove task

I'm going to do justify content space between so that will push the button all the way to the end and then we'll also do align item Center so now the title is nice and lined up great now let's just uh change the look of this button a little bit so built into PCO CSS there's an outline class that you can add to buttons and so yeah this kind of just differentiates this button from our our add button but now let's hook it up so when this button is clicked we want to call remove task and in this case I want to pass in the index of the task so that way we can just immediately remove it from the array based on its index so to get access to the index we'll pass it into the each here and pass that into remove task but I do need to Define remove task as a prop as well so remove task is is a method that takes in the index which is a number and then returns nothing and so now in the parent component we can define a method called remove task takes in that index and then we can just remove it from the tasks array directly so if I say tasks. splice we can pass in that index and say we'd

like to remove one item starting at that index and then finally we'll pass that method down to the task list great give it a try so we'll add some tasks here and now if I click remove it gets removed and you'll notice that our total number of tasks also takes into account if anything is removed so right now one of the two tasks is completed but if I remove that task now zero of one total tasks are completed now lastly just a little bit of cleanup I only want to show this message if there are actually tasks in the array so let's just add a conditional statement so right here we'll just say if the total tasks is greater than zero then we will render this out otherwise we'll just say add a task to get started like that and so now by default it just says add a task to get started but if we add a task now it shows us the total number of tasks completed now I'd like to add some filter buttons so right now by default we always show all tasks but it would be nice if we could filter between showing all tasks maybe only showing the tasks that are done and then only showing the

Chapter 7 13:34

filter tasks

tasks that are not done so right below the message here I'm going to add a section for some buttons and I want a button that just says all this will be the default then we'll have another button that says Todo and one last button that says done and so that gives us these three buttons but I want to change the Styles a little bit so built into Pico CSS as a class called secondary I'm going to add this to each that so that looks good and then I only want to show these buttons If there are actually tasks on the page so we'll wrap this in an if as well so I'll move this in here so now the buttons don't show by default but if we add some tasks now those buttons actually show up let's add a little bit of styling to the button container here so let's give this a class of button container and I will make this a flex box we're going to push everything to the end and then we'll also give it some margin bottom of one Rim so so if we add some tasks we can see that the buttons get pushed to the

side and there's a little bit of breathing room we'll also add a gap so that way these buttons are slightly separated We'll add a gap of 0.5 Rim as well and that looks good to me so now let's hook these buttons up so by default we want to show all of the tasks on the page so I'm going to create another derived value based on the task array that essentially filters the list down depending on which of these is selected so first I'll create a type so if we head over to our types I'm going to create a type called filter and this will just be a union of all Todo or done like so now we'll Define a state variable that is of that type so let's say we have a state variable called current filter and we'll use our dollar sign State Rune but we will say that it should be of type filter and we'll bring that in from our types and then we'll set the default default value as all and now I want another derived value that actually looks at the filter value and then filters this list of tasks accordingly and so if you need a derived value that is more than just a single expression you can use derived by so

I'll create a variable here called the filtered tasks and we'll use dollar sign derived. bu now this accepts a function and whatever is returned from this function will be the derived value in this case we want to switch over the current filter and in the case that the current filter is all we're just going to return all the tasks and then by default if none of the filters match here we're just going to return all the tasks as well and so you can see the type of this is a task array because essentially whatever is returned from this function is going to be that derived value so this is very useful if you need a computed property but it's a bit more complex than just a single expression and so now let's actually use this derived value in our template so instead of passing down the full tasks array to our task list we're just going to pass down the filtered tasks now while while I'm thinking about it we do need to update our remove task method so before we were accepting the index but now as the task list changes depending on the selected filter that index isn't going to make sense anymore so let's

head back into the task list component and instead of accepting an index let's actually just take in the ID of the task which is of type string and then when we call the method we'll pass in the task. ID we don't need the index anymore here and then we'll also update our remove task method in the parent component here and so this will take in ID which is a string and then we'll find that index based on the specific task ID so if the task ID is equal to that specific ID we have the index and then we'll remove it from the array so now let's add our other filter cases so in the case of done we want to return all the tasks that are done so we'll just use a filter and we'll say give me every task where the task is done that and then we'll also add the case for to do and so this should give us every task where that task is not done like so and so now let's actually hook up those filter buttons and so whenever we click the all button we want to set the current filter

Chapter 8 17:34

filter buttons

to all so we'll add an on click and we'll say the current filter is all and then for the to-do button we'll say the current filter is to-do and here we'll say the current filter is done now I want to change the look of the button if it is the currently selected filter so I'm going to add a class binder in for the contrast class and we'll set the contrast class if the current filter is equal to the filter for this specific button so here if it's equal to all here if it's equal to to-do and then here if it's equal to done so now let's give it a try if we add a few tasks if I Mark something as done and then head over to too we can see that it gets filtered out and then if we head to done we only see the tasks that are marked as done and then if I uncheck it here it disappears but it reappears onto do and then also on all so the filter buttons are working as intended so everything's working but you can see here we have quite a bit of code duplication now one way to clean

this up would be to create maybe a separate component called filter button that like accepts the type of the filter but in spelt 5 they introduce Snippets and I think this is a really good use case for that so if you check out their docs essentially a snippet can be defined inside of your template you give that snippet a name and that snippet can also accept arguments and then that snippet can be reused inside of your template so let's actually turn this filter button into a snippet I'll just copy one of our buttons here for the base code and then at the top of our template we will Define a snippet we'll give it a name I'll call it the filter button and then it can accept some arguments in this case I'm going to accept the filter type which is of type filter we'll throw in the code here and now let's use that type so here we'll set it to be the filter argument and then same thing for whenever we're setting the class like so and then we'll also display that value here now the type doesn't capitalize the first letter so I'm actually going to going to add an extra class here let's create a filter button class that just capitalizes the text inside of it so here in the Styles We'll add a text transform of capitalize

so so now in order to use this snippet I can use a render block so right here I can replace this with an at render and then specify my template name which is filter button and then pass in a specific filter type in this case I want a button for all just like that and you can see how this cleaned things up nicely so now I also want a filter button for too and then I also want a filter button for done and so now if if I add a few tasks buttons should work as intended and they do and so this is a really great way to kind of remove some duplicate code and especially in situations where it maybe doesn't make as much sense to create a brand new component now one last bit of Polish I want to add is some animations so whenever I add a new task or let's say I'm on a filtered View and then check something off and it disappears instead of just immediately disappearing I'd like that element to fade out and similarly if I remove an element I want it to fade out instead of immediately disappearing so if we head to our task list I'm actually going to import fade from spelt transitions like so and then

Chapter 9 20:11

transition animations

on the specific task element I can set the transition to fate and just like that we should get some nice animations and so if I add some more tasks you can see that they fade in if I am on a filtered View and then check something off you can see it fades out so that's looking good so in this video we really just scratched the surface of some of the newer things inside of spell 5 if you want to dive deeper definitely check out the video spelt 5 Basics here on the channel where Scott dives into all of the new stuff inside of spel 5 so this is a really great introduction to all the new things introduced in spel 5 and all the things that you can do all right that's it for this video if you found it useful definitely drop a like and if you want to support us definitely hit that subscribe button all right I'll see you in the next one

Chapter 10 20:53

thanks!

[Music]

Quote it with credit and a link. Quoting Syntax