Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

My petite-vue review – daverupert.com


Road testing petite-vue with a simple category filter

July 21, 2021

petite-vue is a new cut of the Vue project specifically built with progressive enhancement in mind. At 5kb, petite-vue is a lightweight Alpine (or jQuery) alternative that can be “sprinkled” over your project requiring no extra bundling steps or build processes. Add a tag, set a v-scope, and you’re off to the races. This is up my alley.

Since it came out, I’ve been looking for a quick project to road test petite-vue with, but I’m a bit buried under client work and starting a new company to dream up some new toy projects. This weekend I was updating my bookshelf and realized petite-vue would be an ideal candidate for a feature I’ve wanted for awhile: Filter By Tags.

Sprinkling in petite-vue

Some background: My site is a statically generated Jekyll site with 790 bytes of first-party JavaScript. All the data that powers my bookshelf lives in a giant YAML file in _data/books.yml. This system has become less sustainable now that there are 200+ books on the bookshelf but that’s a project for another day. If you want a gist of how I built my bookshelf, here’s the gist of how I built my bookshelf.

For years now, I’ve dreamed about rewriting this whole thing in Vue so that I could add some interactive/reactive category filtering, but introducing a JS build chain into my Jekyll project always stopped me dead in my tracks. I don’t need the whole shebang, I need a place to store some state and some directives to make content show/hide based on that state. petite-vue is a perfect fit for this niche!

Step 1: Initialize petite-vue

As with most little tutorials, you start by adding the thing to the thing. I added this to the bottom of my bookshelf.html

Step 2: Add v-scope and my page state

Next we need to add a v-scope directive to the place we want Vue to mount. v-scope also does double duty by being the place where you set your components default state. Handy.

Step 3: Add v-show to each book

Next I wired up the show/hide logic on the book itself. If filter isn’t set, show the book, otherwise check if the book’s tags contain the current filter.

This looks a little bit weird because both Vue and Liquid use {{ curly_braces }} . In this line, {{ book.tags | jsonify }} is Liquid code that spits out a JavaScript array like ['graphic novel', 'biography']. If you were to view source, that line looks more like normal Vue code:

Step 4: Create the filtering mechanism

Next is wiring up the code to set the filter. I originally had a bunch of elements that set the filter, but I would have had to set aria-pressed and a bunch of other junk, I felt like a radio group semantically matched what I was trying to express. If this was a poor accessibility choice or this could be better, let me know, but I personally enjoy the free keyboard interactivity.

{% raw %}
  
{% endraw %}

In this situation, we don’t want Liquid to run, so we use the {% raw %} block to tell Liquid to ignore this block of code. From there it’s a standard Vue template. Selecting one of the radio buttons updates the filter state via the v-model directive. That update then cascades to show/hide each book based on that books v-show boolean statement. The end result is something that’s snappy and reactive and didn’t take much time at all.

One little piece of magic I’m proud of was this little hidden attribute flipper:




Source link

The post My petite-vue review – daverupert.com appeared first on Nexstair Technologies.



This post first appeared on Nexstair | Creative Website Design And Wordpress T, please read the originial post: here

Share the post

My petite-vue review – daverupert.com

×

Subscribe to Nexstair | Creative Website Design And Wordpress T

Get updates delivered right to your inbox!

Thank you for your subscription

×