<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Sarin M</title>
    <description>The latest articles on Forem by Sarin M (@sarinmsari).</description>
    <link>https://forem.com/sarinmsari</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F548832%2Fcdde754f-0fa1-4b7c-9b0d-bd6317c37e1c.jpg</url>
      <title>Forem: Sarin M</title>
      <link>https://forem.com/sarinmsari</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sarinmsari"/>
    <language>en</language>
    <item>
      <title>Tutorial: Giphy gif picker (Vue3)</title>
      <dc:creator>Sarin M</dc:creator>
      <pubDate>Mon, 17 Jun 2024 06:47:41 +0000</pubDate>
      <link>https://forem.com/sarinmsari/tutorial-giphy-gif-picker-vue3-4fm4</link>
      <guid>https://forem.com/sarinmsari/tutorial-giphy-gif-picker-vue3-4fm4</guid>
      <description>&lt;p&gt;I hope you have a basic understanding of Vue3 and Tailwind CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Setup Vue 3 Project
&lt;/h2&gt;

&lt;p&gt;Ensure you have a basic Vue 3 project set up. If not, you can create one using Vue CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;vue create my-project
cd my-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Install Giphy Packages
&lt;/h2&gt;

&lt;p&gt;Install the required Giphy packages using npm or yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Using npm
npm install @giphy/js-fetch-api @giphy/js-components

# Or using yarn
yarn add @giphy/js-fetch-api @giphy/js-components
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Create Home Page Component
&lt;/h2&gt;

&lt;p&gt;Create a Home.vue component to serve as the main page for the GIF picker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Home.vue
&amp;lt;script setup&amp;gt;
import {ref} from 'vue'
import GifPicker from '@/components/giphy/GifPicker.vue';

let showGifPicker = ref(false);
let gifUrl = ref('');

const handleGifSelection = (gif) =&amp;gt; {
    showGifPicker.value = false;
    gifUrl.value = gif;
}
&amp;lt;/script&amp;gt;

&amp;lt;template&amp;gt;
    &amp;lt;div class="relative flex flex-col items-center justify-center w-full"&amp;gt;

        &amp;lt;GifPicker v-if="showGifPicker"
            @handleGifSelect="handleGifSelection"
            class="absolute text-black bottom-14"/&amp;gt;
        &amp;lt;button @click="showGifPicker = !showGifPicker"
            class="bg-green-700 p-4 rounded-2xl text-white"&amp;gt;Pick Gif&amp;lt;/button&amp;gt;
        &amp;lt;img v-if="gifUrl"
            :src="gifUrl"
            class="w-40 h-auto rounded-2xl absolute top-20"/&amp;gt;

    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Create the GifPicker Component
&lt;/h2&gt;

&lt;p&gt;Create a GifPicker.vue component to handle GIF selection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script setup&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;template&amp;gt;
    &amp;lt;div class="flex flex-col items-center justify-center w-[280px] h-[350px] bg-white shadow-lg rounded-2xl border p-4"&amp;gt;
        &amp;lt;div class="flex items-center justify-between w-full"&amp;gt;
            &amp;lt;input
                type="text"
                v-model="searchTerm"
                @input="handleGifSearch"
                class="w-full text-xl p-2 border rounded-xl"
                placeholder="Search gif"/&amp;gt;
            &amp;lt;span @click="handleTrendingClick" class=" ml-2 text-xl p-2 bg-white border flex items-center justify-center rounded-xl hover:bg-gray-100 cursor-pointer"&amp;gt;🔥&amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;div class="flex flex-wrap items-center justify-center w-full h-full overflow-y-auto"&amp;gt;
            &amp;lt;div class="mt-2" ref="gifs"/&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's work on the gif picker scripting&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup Imports and Initialization&lt;/strong&gt;&lt;br&gt;
Import necessary modules and initialize required variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script setup&amp;gt;
import {ref,onMounted} from 'vue'
import { renderGrid } from '@giphy/js-components'
import { GiphyFetch } from '@giphy/js-fetch-api'
import { debounce } from 'lodash';

const emit = defineEmits(['handleGifSelection'])

let gifs = ref(null),
    searchTerm = ref(''),
    grid = null;

const gf = new GiphyFetch('your Web SDK key') // update your giphy key

const fetchGifs = (offset) =&amp;gt; {
    if (searchTerm.value) {
        return gf.search(searchTerm.value, { offset, limit: 25 })
    }
    return gf.trending({ offset, limit: 25 })
}
Create Grid Rendering Function
Define a function to render the GIF grid:

const makeGrid = (targetEl) =&amp;gt; {
    const render = () =&amp;gt; {
        return renderGrid(
            {
                width: 226,
                fetchGifs,
                columns: 2,
                gutter: 6,
                noLink: true,
                hideAttribution: true,
                onGifClick,
            },
            targetEl
        )
    }
    const remove = render()
    return {
        remove: () =&amp;gt; {
            remove()
        },
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Handle GIF Clicks&lt;/strong&gt;&lt;br&gt;
Define a function to handle GIF clicks and emit the selected GIF URL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const onGifClick = (gif, e) =&amp;gt; {
    e.preventDefault();
    emit('handleGifSelection', gif.images.fixed_height.url);
}
Handle Grid Refresh and Fetch New GIFs
Define functions to refresh the grid and fetch new GIFs based on search or trending clicks:

const clearGridAndFetchGifs = () =&amp;gt; {
    grid.remove();
    grid=makeGrid(gifs.value)
}

const handleGifSearch = debounce(() =&amp;gt; {
    clearGridAndFetchGifs();
},500)

const handleTrendingClick = () =&amp;gt; {
    searchTerm.value = '';
    clearGridAndFetchGifs();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Initialize Grid on Component Mount&lt;/strong&gt;&lt;br&gt;
Initialize the grid when the component is mounted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onMounted(() =&amp;gt; {
    grid = makeGrid(gifs.value)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final GifPicker Component&lt;/strong&gt;&lt;br&gt;
Combine all the pieces into the final GifPicker component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script setup&amp;gt;
import {ref,onMounted} from 'vue'
import { renderGrid } from '@giphy/js-components'
import { GiphyFetch } from '@giphy/js-fetch-api'
import { debounce } from 'lodash';

const emit = defineEmits(['handleGifSelection'])

let gifs = ref(null),
    searchTerm = ref(''),
    grid = null;

const gf = new GiphyFetch('your Web SDK key') // update your giphy key

onMounted(() =&amp;gt; {
    grid = makeGrid(gifs.value)
})

const fetchGifs = (offset) =&amp;gt; {
    if (searchTerm.value) {
        return gf.search(searchTerm.value, { offset, limit: 25 })
    }
    return gf.trending({ offset, limit: 25 })
}

const makeGrid = (targetEl) =&amp;gt; {
    const render = () =&amp;gt; {
        return renderGrid(
            {
                width: 226,
                fetchGifs,
                columns: 2,
                gutter: 6,
                noLink: true,
                hideAttribution: true,
                onGifClick,
            },
            targetEl
        )
    }
    const remove = render()
    return {
        remove: () =&amp;gt; {
            remove()
        },
    }
}

const onGifClick = (gif, e) =&amp;gt; {
    e.preventDefault();
    emit('handleGifSelection', gif.images.fixed_height.url);
}

const handleGifSearch = debounce(() =&amp;gt; {
    clearGridAndFetchGifs();
},500)

const handleTrendingClick = () =&amp;gt; {
    searchTerm.value = '';
    clearGridAndFetchGifs();
}

const clearGridAndFetchGifs = () =&amp;gt; {
    grid.remove();
    grid=makeGrid(gifs.value)
}

&amp;lt;/script&amp;gt;

&amp;lt;template&amp;gt;
    &amp;lt;div class="flex flex-col items-center justify-center w-[280px] h-[350px] bg-white shadow-lg rounded-2xl border p-4"&amp;gt;
        &amp;lt;div class="flex items-center justify-between w-full"&amp;gt;
            &amp;lt;input
                type="text"
                v-model="searchTerm"
                @input="handleGifSearch"
                class="w-full text-xl p-2 border rounded-xl"
                placeholder="Search gif"/&amp;gt;
            &amp;lt;span @click="handleTrendingClick" class=" ml-2 text-xl p-2 bg-white border flex items-center justify-center rounded-xl hover:bg-gray-100 cursor-pointer"&amp;gt;🔥&amp;lt;/span&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;div class="flex flex-wrap items-center justify-center w-full h-full overflow-y-auto"&amp;gt;
            &amp;lt;div class="mt-2" ref="gifs"/&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/template&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Final gif picker result&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqozesyg9nftxg8mcb464.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqozesyg9nftxg8mcb464.png" alt="Giphy gif picker final result image with a button to open gif picker, gif picker listing gifs with options to get trending gifs and search gifs." width="774" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;GitHub Gist: &lt;a href="https://gist.github.com/sarinmsari/e2a47f457a1b5924ba4e5791b954ca88"&gt;https://gist.github.com/sarinmsari/e2a47f457a1b5924ba4e5791b954ca88&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>giphy</category>
    </item>
  </channel>
</rss>
