OpenReplay
Navigate back to the homepage
BLOG
Browse Repo
Back

Build a Movie Search App with Petite-Vue: A lightweight alternative to VueJS

Uma Victor
November 10th, 2021 · 3 min read

As a developer, you might want to create a little project and not want to burden yourself with installing any library/framework. Petite-vue is a new library which acts as a lightweight alternative to Vue. This light-weight library has syntax just like Vue with the same template and the way the reactivity model is designed. It’s a plug and play library as you can progressively add it to your HTML site to provide little interactions on your site. Petite-vue was created so you can add those little interactions in your HTML site by just loading it from a CDN and adding the needed interactivity. It’s a very light library of only 6kb and it is driven by @vue/reactivity. Let’s take a look at how this library works with a practical example!

How petite-vue works

Take a look at the following example:

1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <script src="https://unpkg.com/petite-vue" defer init></script>
5</head>
6<body>
7 <div v-scope="{ count: 0 }">
8 {{ count }}
9 <button @click="count++">inc</button>
10 </div>
11</body>
12</html>

We can import the library using a CDN and use it in our body by marking it’s boundry with a v-scope. In the v-scope, we set a reactive count and we can see how it uses vue curly brace template syntax. The code above doesn’t look clean so let’s see how we can use petite-vue with the ES module build.

1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <title>Petite-vue Example</title>
5</head>
6<body>
7 <div id="countapp">
8 {{count}}
9 <button @click="incrementCount">Increment</button>
10 </div>
11 <script type="module">
12 import { createApp } from "https://unpkg.com/petite-vue?module";
13 function countFunction() {
14 return {
15 count: 0,
16 incrementCount() {
17 this.count++
18 }
19 }
20 }
21 createApp(countFunction()).mount("#countapp");
22 </script>
23</body>
24</html>

We created a script tag for the type module and imported createApp from petite-vue. In the createApp function, we accept a countFunction that returns a object where all our reactivity takes place and we mount the div with the id we want to make reactive. Now we can carry out reactive operations just like we can in VueJS

Creating Components

In petite-vue, we can use templates as a way to create components. In the return statement of our function, we use the $template syntax and we set it to the id of our app we will want interactivity to take place. Now we can remove it from where we mounted it and have the template do the job of specifying the id we want to work on.

1<body>
2 <template id="countapp">
3 {{count}}
4 <button @click="incrementCount(2)">Increment</button>
5 </template>
6 <div v-scope="countFunction()"></div>
7 <div v-scope="countFunction()"></div>
8 <div v-scope="countFunction()"></div>
9 <script type="module">
10 import { createApp } from "https://unpkg.com/petite-vue?module";
11 function countFunction() {
12 return {
13 $template: "#countapp",
14 count: 20,
15 incrementCount(prop) {
16 this.count = this.count + prop;
17 }
18 };
19 }
20 createApp({ countFunction }).mount();
21 </script>
22 </body>

Above we see that we changed the div we want to turn a component into a template tag, now we can reference that particulat template again and again by using petite-vue v-scope to an passing the countFunction() to it.

Setting Up our App

Let’s explore what we can use petite-vue to do by building a movie app. Here is what we will be building:

null

For the UI, we will be using TailwindCSS, which we will install using a CDN in our head.

1<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">

Another thing we will be needing is our movie API. For this we will use the TMDB movie API. You can create an account and get your API key to make a request. Since this is a tutorial I will not be hiding my API key.

How our App works

We want to be able to select the movie type we want from our header and then we make a request for that movie type and display all of the movies under that type. The type of movie we want will be stored in the store we create and dynamically bind with our request. In order to write cleaner code, we will move all the app logic to an script.js file, so our file structure looks like this

1PETITE-VUE-MOVIEAPP
2 -index.html
3 -script.js

In the script.js file, we import createApp and reactive from petite-vue and in the app reactive object, we have two data points in our app i.e the movieType and results that stores the array of objects we get from our API request.

1//script.js
2import { createApp, reactive } from "https://unpkg.com/petite-vue?module";
3const app = reactive({
4 movieType: "popular",
5 results: "",
6 async search(){
7 const movieSearch = await fetch(`https://api.themoviedb.org/3/movie/${this.movieType}?api_key=fbb0c92eb459272c7afd323360fb1146`)
8 this.results = await movieSearch.json()
9
10 console.log(results);
11 }
12})
13createApp({app}).mount("#movieapp");

We have set the default movie type to popular and as we can see in our fetch request, we dynamically bind the movie type to the URL. Now in our index.html this is what the code looks like:

1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta http-equiv="X-UA-Compatible" content="IE=edge">
6 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 <script type="module" src="script.js"></script>
8 <link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
9 <title>Petite-vue Example</title>
10</head>
11<body>
12 <div id="movieapp">
13 <nav class="bg-gray-800">
14 <div class="max-w-7xl mx-auto px-2 sm:px-6 lg:px-8">
15 <div class="relative flex items-center justify-between h-16">
16 <div>
17 <h1 class="text-white text-3xl">Movie App</h1>
18 </div>
19 <div>
20 <select v-model="app.movieType" v-on:input="app.search"
21 className="ml-auto my-2 py-2 px-4 shadow-md rounded-md font-medium bg-blue-400 text-gray-700">
22 <option value="">Filter by movie</option>
23 <option value="popular">Most Popular</option>
24 <option value="top_rated">Top Rated</option>
25 <option value="upcoming">Upcoming Movies</option>
26 </select>
27 </div>
28 </div>
29 </div>
30 </nav>
31 <!-- display our movies -->
32 <div class="mt-6 mx-20 grid md:grid-cols-2 lg:grid-cols-4 gap-x-6 gap-y-8">
33 <article v-for="(result, id) in app.results.results" :key="id"
34 class="bg-white group relative rounded-lg overflow-hidden shadow-lg hover:shadow-2xl transform duration-200">
35 <div class="relative w-full h-80 md:h-64 lg:h-44">
36 <div :style="{
37 backgroundImage: `url(https://image.tmdb.org/t/p/w500${result.backdrop_path})`
38 }" class="bg-gray-300 h-64 w-full rounded-lg shadow-md bg-cover bg-center"></div>
39 </div>
40 <div class="px-3 py-4">
41 <h3 class="text-sm text-gray-500 pb-2">
42 <a class="bg-indigo-600 py-1 px-2 text-white rounded-lg" href="#">
43 <span class="absolute inset-0"></span>
44 {{result.original_title}}
45 </a>
46 </h3>
47 <p class="text-base font-semibold text-gray-900">
48 {{result.overview}}</p>
49 <p class="text-indigo-400 mt-3 font-medium">More info</p>
50 </div>
51 </article>
52 </div>
53 </div>
54</body>
55</html>

There are quite a few things going on above. You can see the nav tag that contains our header/navigation. In the header we have the app name then we have a select input tag with options, so that we can create a dropdown list of the movie type to select. Any movie type we select from the dropdown is modeled to the movieType store value in our script, using Vue’s v-model. At the same time we have a *v-on:input* directive that calls the search function in our script each time the value of movieType changes.

Displaying the Movies

When we make a request to the movie API, the data we get back is stored in our results array in our script. To display the movies, we loop over the results array and add the information we want to display.

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder. OpenReplay is the only open-source alternative currently available.

OpenReplay

Happy debugging, for modern frontend teams - Start monitoring your web app for free.

Conclusion

In this tutorial, we looked at the new light-weight library, how it works and how we can incrementally adopt it in a project. In the process we built an example movie app. I hope you give petite-vue a try after following this tutorial. To learn more about this library, you can check out their GitHub repo. You can also reach out to me on twitter.

More articles from OpenReplay Blog

Building an Encyclopedia with React and Wikipedia Search API

Learn how to query a REST API using Fetch with this practical tutorial

November 8th, 2021 · 3 min read

Fetching Data from GraphQL APIs with Apollo React

Query your GraphQL database from your React App with a few lines of code thanks to Apollo React

November 8th, 2021 · 2 min read
© 2021 OpenReplay Blog
Link to $https://twitter.com/OpenReplayHQLink to $https://github.com/openreplay/openreplayLink to $https://www.linkedin.com/company/18257552