GIPHY JS SDKs

repository·master·Indexed 41 days ago

https://github.com/giphy/giphy-js

A collection of JavaScript SDKs for integrating GIPHY into web applications. Includes @giphy/js-fetch-api for low-level API access, @giphy/js-components and @giphy/react-components for UI elements like grids and carousels, and @giphy/embed-sdk for embedding GIPHY Clips via GiphyVideo.

Tokens
31K
Snippets
114
Records
145
Agent score
50%

What's inside GIPHY

  1. Overview of @giphy/react-components

    master

    The @giphy/react-components SDK provides high-performance React components designed to integrate GIPHY's GIF library into web applications. It includes built-in support for common GIPHY endpoints such as Search, Trending, GIF by ID, Categories, Related GIFs, and Random GIFs.

    Note on Analytics: This SDK sends pingbacks to GIPHY to help improve search result quality. If you are using React and want a ready-to-use UI, this is the recommended package.

  2. Overview of @giphy/js-util

    master
    @giphy/js-util is a utility library providing helper functions for handling GIPHY media assets and object manipulation. It includes specialized logic for selecting the best image or video renditions based on dimensions, calculating GIF aspect ratios, and providing lightweight replacements for common Lodash collection methods.
  3. Choose the right GIPHY SDK for your project

    master

    The GIPHY SDK ecosystem provides different packages depending on your framework requirements and UI needs:

    • @giphy/react-components: Optimized React components designed for ease-of-use and high performance.
    • @giphy/js-components: Lightweight UI components that do not require a specific framework.
    • @giphy/js-fetch-api: A low-level API for fetching GIFs, stickers, categories, and more. Use this if you want to build your own custom UI and want to avoid built-in SDK analytics.
  4. Understand GIPHY SDK analytics (pingbacks)

    master
    By default, these SDKs send analytics events back to GIPHY in the form of pingbacks. This data is used to help GIPHY improve the quality of search results for users. If you require a version without SDK analytics, use the @giphy/js-fetch-api package.
  5. How to use GifOverlay for custom UI

    master

    The overlay prop is available on all components, allowing you to overlay a GIF with custom UI and respond to hover events (desktop only). The overlay component receives GifOverlayProps containing the gif object and the current isHovered state.

    // Overlay component gets GifOverlayProps
    const Overlay = ({ gif, isHovered }: GifOverlayProps) => {
        return <div className="overlay">{isHovered ? gif.id : ''}</div>
    }
    
    // React component
    ReactDOM.render(<Carousel gifHeight={200} fetchGifs={fetchGifs} overlay={Overlay} />, target)
  6. Implement a Search Experience using SearchContextManager

    master

    A complete search experience is built by wrapping components in a SearchContextManager. This manager uses React.Context to facilitate communication between the SearchBar and visual components like Grid or Carousel.

    Implementation Pattern

    1. Wrap your application (or search section) in <SearchContextManager apiKey={...} />.
    2. Use the useContext(SearchContext) hook in child components to access fetchGifs and searchKey.
    3. Pass the searchKey as a key prop to your results component (e.g., Grid) to ensure the component re-mounts and resets when the search term changes.

    Components in the Search Experience

    • SearchContextManager: The provider that manages the search state and API configuration.
    • SearchBar: The input field for user queries.
    • SuggestionBar: (Optional) Displays trending searches and username/channel matches (e.g., @nba).
    • Grid / Carousel: The visual components that consume fetchGifs to display results.
    import { useContext } from 'react'
    import {
        Grid,
        SearchBar,
        SearchContext,
        SearchContextManager,
        SuggestionBar,
    } from '@giphy/react-components'
    
    const SearchExperience = () => (
        <SearchContextManager apiKey={webSDKKey}>
            <Components />
        </SearchContextManager>
    )
    
    const Components = () => {
        const { fetchGifs, searchKey } = useContext(SearchContext)
        return (
            <>
                <SearchBar />
                <SuggestionBar />
                <Grid key={searchKey} columns={3} width={800} fetchGifs={fetchGifs} />
            </>
        )
    }
  7. Select optimal GIPHY renditions and dimensions

    master

    The utility provides several functions to help you choose the most efficient media asset (image or video) based on your UI requirements:

    Rendition Selection

    • _bestfit: Finds the image rendition that best fits a given width and height.
    • _getBestRendition: Returns the best rendition object based on a width and height (utilizes _bestfit).
    • _getBestVideo: Returns the best video rendition object based on a width and height (utilizes _bestfit).
    • _getBestRenditionUrl: Returns the best rendition URL based on width and height, factoring in WebP availability and video usage (utilizes _getBestRendition).
    • _getSpecificRendition: Retrieves a specific rendition while factoring in WebP, sticker, and video availability.

    Dimension Helpers

    • _getGifHeight: Quickly calculates the height for a GIF based on a provided width.
    • _getGifWidth: Quickly calculates the width for a GIF based on a provided height.

    Accessibility

    • _getAltText: Retrieves the alt text used for displaying GIFs.
  8. Refresh Grid or Carousel based on search terms

    master

    To refresh a Grid or Carousel when a search term changes (e.g., via an input field), use Svelte's {#key} block. By wrapping the component in {#key term}, Svelte will destroy and re-mount the component whenever the term variable changes, triggering a fresh fetchGifs call with the new term.

    When using {#key}, you should manage initialGifs carefully to ensure the component doesn't attempt to use stale data from the previous search.

    <script lang="ts">
        import { Grid } from '@giphy/svelte-components'
        import { GiphyFetch } from '@giphy/js-fetch-api'
        const gf = new GiphyFetch('YOUR_API_KEY')
        
        let term = 'initial term'
        let initialGifs = [] // data from SSR
    </script>
    
    <input bind:value={term} />
    
    {#key term}
        <Grid
            initialGifs={term === 'initial term' ? initialGifs : []}
            width={600}
            fetchGifs={(offset) => gf.search(term, { offset, limit: 10 })}
        />
    {/key}