5.13.0 • Published 11 days ago

@giphy/js-components v5.13.0

Weekly downloads
8,509
License
MIT
Repository
-
Last release
11 days ago

@giphy/js-components

A lightweight set of components, focused on ease-of-use and performance.

Try it out:

Edit @giphy/js-components

A note about pingbacks

This SDK sends analytics events back to GIPHY in the form of pingbacks to help us improve the quality of search results for your users.

Grid

Use renderGrid(props, target) to render a grid to a target element

Bare Bones Example

// use @giphy/js-fetch-api to fetch gifs
// apply for a new Web SDK key. Use a separate key for every platform (Android, iOS, Web)
const gf = new GiphyFetch('your Web SDK key')
// fetch 10 gifs at a time as the user scrolls (offset is handled by the grid)
const fetchGifs = (offset: number) => gf.trending({ offset, limit: 10 })
// render a grid
renderGrid({ width: 800, fetchGifs }, targetEl)

renderGrid options

proptypedefaultdescription
widthnumberundefinedThe width of the grid
fetchGifs(offset:number) => Promise<GifsResult>undefinedA function that returns a Promise. Use @giphy/js-fetch-api
columnsnumber3The number of columns in the grid
gutternumber6The space between columns and rows
noResultsMessagestring \| elementundefinedCustomise the "No results" message
noLinkbooleanfalseUse a div instead of an a tag for the Gif component, user defines functionality with onGifClick
hideAttributionbooleanfalseHide the user attribution that appears over a
Gif Events**see below

Thorough Example

import { throttle } from 'throttle-debounce'
import { renderGrid } from '@giphy/js-components'
import { GiphyFetch } from '@giphy/js-fetch-api'

// create a GiphyFetch with your api key
// apply for a new Web SDK key. Use a separate key for every platform (Android, iOS, Web)
const gf = new GiphyFetch('your Web SDK key')
// create a fetch gifs function that takes an offset
// this will allow the grid to paginate as the user scrolls
const fetchGifs = (offset: number) => {
    // use whatever end point you want,
    // but be sure to pass offset to paginate correctly
    return gf.trending({ offset, limit: 25 })
}

// Creating a grid with window resizing and remove-ability
const makeGrid = (targetEl: HTMLElement) => {
    const render = () => {
        // here is the @giphy/js-components import
        return renderGrid(
            {
                width: innerWidth,
                fetchGifs,
                columns: width < 500 ? 2 : 3,
                gutter: 6,
            },
            targetEl
        )
    }
    const resizeRender = throttle(500, render)
    window.addEventListener('resize', resizeRender, false)
    const remove = render()
    return {
        remove: () => {
            remove()
            window.removeEventListener('resize', resizeRender, false)
        },
    }
}

// Instantiate
const grid = makeGrid(document.querySelector('.grid'))

// To remove
grid.remove()

Carousel

renderCarousel options

propertytypedefaultdescription
gifHeightnumberundefinedThe height of the gifs and the carousel
gifWidthnumberundefinedThe width of the gifs and the carousel (you may want to set Gif.imgClassName to have object-fit: cover to avoid stretching)
fetchGifs(offset:number) => Promise<GifsResult>undefinedA function that returns a Promise. Use @giphy/js-fetch-api
gutternumber6The space between columns and rows
noResultsMessagestring \| elementundefinedCustomise the "No results" message
hideAttributionbooleanfalseHide the user attribution that appears over a
noLinkbooleanfalseUse a div instead of an a tag for the Gif component, user defines functionality with onGifClick
Gif Events**see below
import { renderCarousel } from '@giphy/js-components'
import { GiphyFetch } from '@giphy/js-fetch-api'

// create a GiphyFetch with your api key
// apply for a new Web SDK key. Use a separate key for every platform (Android, iOS, Web)
const gf = new GiphyFetch('your Web SDK key')

// Creating a grid with window resizing and remove-ability
const vanillaJSCarousel = (mountNode: HTMLElement) => {
    renderCarousel(
        {
            gifHeight: 200,
            fetchGifs: (offset: number) => gf.trending({ offset }),
            gutter: 6,
            onGifClick: (gif: IGif) => window.open(gif.url),
        },
        mountNode
    )
}

Gif

Gif props

proptypedefaultdescription
gifIGifundefinedThe gif to display
widthnumberundefinedThe width of the gif
backgroundColorstringrandom giphy colorThe background of the gif before it loads
hideAttributionbooleanfalseHide the user attribution that appears over a GIF
noLinkbooleanfalseUse a div instead of an a tag for the Gif component, user defines functionality with onGifClick
Gif Events**see below
import { renderGif } from '@giphy/js-components'
import { GiphyFetch } from '@giphy/js-fetch-api'

// create a GiphyFetch with your api key
// apply for a new Web SDK key. Use a separate key for every platform (Android, iOS, Web)
const gf = new GiphyFetch('your Web SDK key')

const vanillaJSGif = async (mountNode: HTMLElement) => {
    // render a single gif
    const { data: gif1 } = await gf.gif('fpXxIjftmkk9y')
    renderGif({ gif: gif1, width: 300 }, mountNode)
}

Video

Quick and easy way to play video. Just pass the video component a gif object that has a video property. This is true when using { type: 'videos' } in the fetch api type option.

If you want controls for the video player, use the controls property.

Video props

proptypedefaultdescription
gifIGifundefinedThe gif to display that contains video data
widthnumberundefinedThe width of the video
heightnumberundefinedThe height of the video
controlsbooleanundefinedShow transport controls
hideProgressBarbooleanundefinedif controls is true, hides progress bar
hideMutebooleanundefinedif controls is true, hides the mute button
hidePlayPausebooleanundefinedif controls is true, hides the play/pause button
persistentControlsbooleanundefineddon't hide controls when hovering away
onUserMuted(muted: boolean) => voidundefinedfired when the user toggles the mute state
import { renderVideo } from '@giphy/js-components'
import { GiphyFetch } from '@giphy/js-fetch-api'

// create a GiphyFetch with your api key
// apply for a new Web SDK key. Use a separate key for every platform (Android, iOS, Web)
const gf = new GiphyFetch('your Web SDK key')

const vanillaJSVideo = async (mountNode: HTMLElement) => {
    // render a video
    const { data: gif1 } = await gf.gif('D068R9Ziv1iCjezKzG')
    renderVideo({ gif: gif1, width: 300, controls: true }, mountNode)
}

Attribution Overlay

If a GIF has an associated user, an overlay with their avatar and display name will appear. This can be hidden with hideAttribution on any of the components.

Gif Events

propertytypedescription
onGifHover(gif: IGif, e: Event) => voidfired on desktop when hovered for
onGifVisible(gif: IGif, e: Event) => voidfired every time the gif is show
onGifSeen(gif: IGif, boundingClientRect: ClientRect &#124; DOMRect) => voidfired once after the gif loads and when it's completely in view
onGifClick(gif: IGif, e: Event) => voidfired when the gif is clicked
onGifRightClick(gif: IGif, e: Event) => voidfired when the gif is right clicked
onGifKeyPress(gif: IGif, e: Event) => voidfired when the a key is pressed on the gif

To stop fonts from loading set the environment variable GIPHY_SDK_NO_FONTS=true, this is not recommended as it could cause inconsistencies in the ui components

5.13.1-beta.0

11 days ago

5.14.0-charlie.0

19 days ago

5.14.0-beta.2

19 days ago

5.14.0-beta.0

20 days ago

5.14.0-beta.1

20 days ago

5.13.0

21 days ago

5.12.1

12 months ago

5.12.0

1 year ago

5.11.2-alpha.0

1 year ago

5.11.1

1 year ago

5.11.0

1 year ago

5.8.3

1 year ago

5.9.0

1 year ago

5.10.1

1 year ago

5.10.0

1 year ago

5.8.2

2 years ago

5.8.1

2 years ago

5.8.0

2 years ago

5.5.2

2 years ago

5.5.1

2 years ago

5.6.2

2 years ago

5.6.1

2 years ago

5.6.0

2 years ago

5.7.2

2 years ago

5.7.1

2 years ago

5.7.0

2 years ago

5.5.0

2 years ago

5.3.0

2 years ago

5.4.1

2 years ago

5.4.0

2 years ago

5.2.0

3 years ago

5.1.6

3 years ago

5.1.5

3 years ago

5.1.4

3 years ago

5.1.3

3 years ago

5.1.2

3 years ago

5.1.1

3 years ago

5.1.0

3 years ago

5.0.6

3 years ago

5.0.5

3 years ago

5.0.4

3 years ago

4.4.3-alpha.0

3 years ago

4.4.1-alpha.0

3 years ago

5.0.3

3 years ago

5.0.2

3 years ago

5.0.1

3 years ago

5.0.0

3 years ago

4.4.2-alpha.0

3 years ago

4.4.0

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.2

3 years ago

4.2.0

3 years ago

4.1.3

3 years ago

4.1.2

3 years ago

3.7.1

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.1

3 years ago

4.0.1-alpha.0

3 years ago

4.0.0-alpha.0

4 years ago

3.7.0

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.5.0

4 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.2

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.4.6

4 years ago

2.4.5

4 years ago

2.4.3

4 years ago

2.4.2

4 years ago

2.4.4

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.6

4 years ago

2.1.4

4 years ago

2.1.5

4 years ago

2.1.3

4 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago