How to make an animated "Wizard" component - WotW
April 25, 2018
Welcome to the Widget of the Week series, where I take gifs or videos of awesome UI/UX components, and bring them to life with code.
This week is the turn for a progress indicator or “Wizard” component, that can help you with the on-boarding process of your app. The inspiration comes from this UI movement submission and looks like this:
Preparations
For this widget I’ll be using Vue.js for the interactions and Sass for styling.
The basic markup
As usual I like to start with the markup that I’ll be needing for the component. We will need a root div with id="app"
so we can later mount our Vue app.
Inside it should be a container with the progress elements (the background, the bar and the ball), and also the cards with the steps to follow.
You’ll notice in the code bellow, that the cards have 3 states: done, active and normal, those classes will help us with the styling in the next step.
Styling
We will start to match the background color and the main container. I used the grid display property to have better control over the layout and the items, if you want to know more about it you can check this guide. For better understanding I added inline comments clarifying key decisions:
Now we need to style the progress elements, they will be absolute positioned so they don’t get in the way of the cards. Also I will declare a couple of scss variables for the color and height of the progress elements.
After that, to style the cards, we will make a general card rule and special rules for the done and active cards:
And now we have something like this:
The interactions
We have a pretty component but it doesn’t have any actions, let’s start by initializing Vue in our #app
and adding some basic data for our cards:
Before binding that data to our HTML cards, first we need a way to generate the classes .done
and .active
. Let’s make a method to compare the currentStep
to each card and return the corresponding class:
Now we can replace the cards with the next snippet that uses v-for
to iterate over our data and will set the corresponding class to each one:
You can manually change the currentStep
data to se how the cards change, but it will be better to have a way to change it quicker, in the JS I’ll add a computed property and these two methods:
and then in our HTML (outside the container) we can add a couple of buttons and a paragraph to display the currentStep
:
Looks almost done, but the progress bar is not moving yet.
We actually don’t need to do something with the progress background, but the progress bar needs to change its width and the ball its left position.
Those are just style changes so it is a matter of just a couple of bindings like this:
The barStyle
and ballStyle
will be computed properties because they will change depending on the currentStep
and the number of cards.
We will start with the barStyle
, it just needs to returns an object with a width
property, thanks to the binding that will be changes to inline styles.
The tricky part here is how that width needs to be calculated, because it depends on the number of cards:
On the line where we assign the target
value, if we didn’t subtract the stepHalf
it would set the bar at the end of the card instead of the middle.
Now for the ball style, we can reuse the barWidth
property and just center the circle like we did before in the CSS.
Notice that when the currentStep
is 0
or the last step allStepsDone
, we just make the circle disappear by setting its opacity to 0.
The progress bar should be working by now, but our work is not done, if we look close to the original animation it has different colors for each “done” card. Using an array with all the possible shades of color a card can get is not practical, instead I used HSL colors.
HSL stands for Hue, Saturation and Lightness. Colors in this format can be declared like this hsl(0, 100%, 50%) // red
.
Let’s declare the base color at the beginning of our js
:
Now similar to the getCardClass
method, we will create a getCardColor
one. In there we will check first if the step is done and then depending on how close to the currentStep
it is, we will add more lightness to the base color:
And now the final result!
That’s it for today’s Widget of the Week.
If you’re hungry for more you can check other WotW: