How to make a š color picker with Vue!
Caution: colors may appear cuter than they are!
Before I get to the article, I just want to share that Iām building a product, and I would love to collect some data about how to better serve web developers. I created a short questionnaire to check out before or after reading this article. Please check it out ā thanks! And now, back to our regular scheduled programming.
Hello Internet!
Iām Zaydek and Iām newish to web development. I come from a background in graphic design and programming, so picking up the frontend has been fascinating for me.
The web is like the offspring of a graphic designer and programmer ā itās both visual and programmatic. So today, Iām going to introduce you to Vue.js and component-based web design ā that is, modern web development. Iāll take you from a newbie web developer to an all-powerful, all-knowing web developer seer!
How can you make such claims, sir?
ā Fancy you
Well, the thing is, most people learned how to Internet before we (as a whole) understood how to Internet! Including me! š Which, to be honest, is probably why I refused to learn web development for the longest time. It just felt so broken!
But times they are a-changinā and web development has never been more accessible or streamlined. So it is my great honor, and privilege, to introduce you to how to Internet in 2018, and perhaps even beyond!
I also teach how this šØ works and a whole lot more in the Vue course I just released. Learn Vue from the basics and how to build a few things, too! Click here to enroll for free!
Scrimba.com is a new and interactive website for learning and sharing how to code. Screencasts can be interrupted and edited, making learning active and fun to experiment with!
So, whatās the deal with Vue?
So why Vue? Itās a great and fair question you should be asking. Some people protest and scorn the idea of using any framework, and I think this is a dangerous idea. I do, however, suggest that for whatever tool it is that you use, to be very deliberate and thoughtful in why youāre using it.
I picked up Vue because I want to use newer tools that are not too mainstream. I wanted them to have learned from tools that have come before them (or in other words, are not too trailblazing). They should have best-in-class documentation, and have a critical mass of users.
Since Vue, in the last weeks, surpassed React is stars in GitHub, that is evidence to me that Vue has a critical mass. š
Furthermore, Vue is an extraordinary open-source project, is publicly-funded, and has a great developer-experience! Like user-experience but for developers. This has the wonderful consequence that the common developer can now create interactive websites intuitively. And Vue builds on one of the most successful ideas from the Angular-React wars which is the Virtual DOM. So letās talk about that now.
Virtual WUT?
ā Internet You
Virtual DOM, yo. LOL sorry. So letās back up ā DOM is short for document-object-model. I think of the DOM as a paradigm for how we think about text as data structures to compose what we call web pages. And a virtual DOM is a clever abstraction for dealing with the text that goes between the elements, like <p>hello, world!</p>
. In an idiomatic Vue-based website itās something like <p>{{ message }}</p>
, where the data is stored inside of the JavaScript instead!
Why rely on JavaScript for a simple website?
ā Innocent You
Thatās a great (and fair) point. But thereās a significant benefit to relying on some JavaScript to compose websites, static or dynamic. We can write and build websites programmatically instead of copypastaāing data around. Once the data is separate from the HTML, just like having CSS separate from the HTML, thatās when magical things can start to happen. š¦
The good news is that because weāve come to expect so much from websites, itās fair to assume that the majority of people will have JavaScript enabled by default. So weād have no reason to disable it. I might have disagreed with this a few years ago, but I can now appreciate just how much the benefits of using JavaScript far outweigh whatever possible concerns may arise.
Soā¦ what about that color picker, eh?
Sorry for blabbering! Itās hard for me not to talk about this at length, partly because itās so exciting. And also partly because of the gamut of possibilities that present themselves when a sole developer can be responsible for building beautiful and interactive web apps/businesses. Businesses you say? Yes ā Follow me on Twitter and Iāll be be sure to follow up! š
Without further ado, hereās the HTML for the website:
Psstā¦ the full code is available in the ninth screencast.
Feeling shocked? The thing is, letās think about the inherent complexity of the website Iāve shown you. Itās really just a box with two cells, one with an emoji and one with text, repeating 12 times. Yes, thereās some padding, there are some gradients, but the fundamental design is unchanged. So what if the codeās complexity was proportional to the designās complexity?
Here Iāve applied a CSS debugger to disambiguate the design, but this only goes as far as the CSS is concerned. Whatās more important here, the real significance of Vue, is how we can think about our website in terms of data and not in terms of HTML elements.
So look again at those images. If we think about our website purely in terms of data, how much data is there?
Allow me to now share the underlying data structure used:
The following sentence is possibly the most important: Vue frees us to think about our website in terms of data, separate from the HTML; this is a revolution for how we can build for the web!
To be explicit, what Iām showing you is an array of anonymous objects each with two keys: emoji
, and color
. Now that we can represent the website in terms of data, we just iterate over the data using Vueās v-for
and a custom component.
Yeah, right. So what about components?
Components ā yes! Second to separating the data from the HTML, one of the coolest things Vue offers is component-based design. Components can help us abstract blocks of HTML/CSS/JS into a reusable unit: a component.
A quick note: I decided it would be best to first learn how to use Vue without a build-process, meaning Iām not making use of single-file-components. But I am making use of components via Vue.component()
.
Remember the <swatch>
element I demoād earlier in the source code? Thatās a custom component I engineered using Vue to abstract the element from the implementation. This is a significant concept, because it means we can start to think more functionally than imperatively.
Whatās the difference? Functional design cares about the result, whereas imperative design cares about the result and the implementation. Designing a component is an imperative process, whereas using one is a functional. š§
This is the implementation for the swatch
element shown earlier. How it works is that Vue scans the DOM for instances of swatch
and replaces it with the quotes HTML inside of the componentās template
value. This means we can do major refactoring to better understand our website in terms of concepts, rather than being worried about how something ought to be engineered all the time.
Thereās a lot more to learning Vue, so I wrote two more articles on the subject matter. Please, after this article, have a look!
Well, youāve changed my mind..
I know this might be a lot to take in, especially for something appearing so harmless as a šØ color picker. But what Iāve shown you is (besides the CSS) 90% of the codebase. There are at least a few helper functions Iām omitting, but the point is that the techniques and ideologies discussed add up to a lot more than a cute web app. It means that a sole individual can create beautiful and functional web-based products and services for others.
Iām also suggesting that Vue is a lot more than a framework. If paired with the right backend language (letās hear it for Go!), learning and using Vue adds up to a lot more. Idiomatic Vue can also teach what modern software development means, and how to access the billions(!) of us that are now connected, without the same technical barriers that come with app development.