Skip to content

Commit

Permalink
Merge pull request #434 from Giphy/feat/react-components-fetch-priority
Browse files Browse the repository at this point in the history
Feat/react components fetch priority
  • Loading branch information
giannif authored Feb 22, 2024
2 parents c413823 + ce8bb01 commit 9cbbf87
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-jeans-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@giphy/react-components': minor
---

add fetchpriority prop to Gif component
5 changes: 4 additions & 1 deletion packages/react-components/src/components/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { gifPaginator, GifsResult } from '@giphy/js-fetch-api'
import { IGif, IUser } from '@giphy/js-types'
import { getGifWidth } from '@giphy/js-util'
import React, { ElementType, PureComponent } from 'react'
import React, { ComponentProps, ElementType, PureComponent } from 'react'
import styled from 'styled-components'
import { debounce } from 'throttle-debounce'
import ObserverShared from '../util/observer'
Expand Down Expand Up @@ -64,6 +64,7 @@ type Props = {
borderRadius?: number
tabIndex?: number
loaderConfig?: IntersectionObserverInit
fetchPriority?: ComponentProps<typeof Gif>[`fetchPriority`]
} & EventProps

const defaultProps = Object.freeze({ gutter: 6, user: {}, initialGifs: [] })
Expand Down Expand Up @@ -144,6 +145,7 @@ class Carousel extends PureComponent<Props, State> {
borderRadius,
tabIndex = 0,
loaderConfig,
fetchPriority,
} = this.props
const { gifs, isDoneFetching } = this.state
const showLoader = !isDoneFetching
Expand Down Expand Up @@ -171,6 +173,7 @@ class Carousel extends PureComponent<Props, State> {
noLink={noLink}
borderRadius={borderRadius}
backgroundColor={backgroundColor}
fetchPriority={fetchPriority}
/>
)
})}
Expand Down
5 changes: 5 additions & 0 deletions packages/react-components/src/components/gif.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type GifProps = {
borderRadius?: number
tabIndex?: number
style?: any
fetchPriority?: 'auto' | 'high' | 'low'
}

type Props = GifProps & EventProps
Expand Down Expand Up @@ -102,6 +103,7 @@ const Gif = ({
borderRadius = 4,
style,
tabIndex,
fetchPriority,
}: Props) => {
// only fire seen once per gif id
const [hasFiredSeen, setHasFiredSeen] = useState(false)
Expand Down Expand Up @@ -273,6 +275,9 @@ const Gif = ({
<img
ref={image}
suppressHydrationWarning
// @ts-ignore - fetchPriority is not recognized by React typescript
// eslint-disable-next-line react/no-unknown-property
fetchPriority={fetchPriority}
className={[Gif.imgClassName, loadedClassname].join(' ')}
src={shouldShowMedia ? rendition.url : placeholder}
style={{ background }}
Expand Down
5 changes: 4 additions & 1 deletion packages/react-components/src/components/grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { gifPaginator, GifsResult } from '@giphy/js-fetch-api'
import { IGif, IUser } from '@giphy/js-types'
import { getGifHeight } from '@giphy/js-util'
import React, { ElementType, GetDerivedStateFromProps, PureComponent } from 'react'
import React, { ComponentProps, ElementType, GetDerivedStateFromProps, PureComponent } from 'react'
import styled from 'styled-components'
import { debounce } from 'throttle-debounce'
import Observer from '../util/observer'
Expand Down Expand Up @@ -35,6 +35,7 @@ type Props = {
tabIndex?: number
loaderConfig?: IntersectionObserverInit
loader?: ElementType
fetchPriority?: ComponentProps<typeof Gif>[`fetchPriority`]
} & EventProps

const Loader = styled.div<{ $isFirstLoad: boolean }>`
Expand Down Expand Up @@ -152,6 +153,7 @@ class Grid extends PureComponent<Props, State> {
tabIndex = 0,
layoutType = 'GRID',
loader: LoaderVisual = DotsLoader,
fetchPriority,
} = this.props
const { gifWidth, gifs, isError, isDoneFetching } = this.state
const showLoader = !isDoneFetching
Expand Down Expand Up @@ -186,6 +188,7 @@ class Grid extends PureComponent<Props, State> {
hideAttribution={hideAttribution}
noLink={noLink}
borderRadius={borderRadius}
fetchPriority={fetchPriority}
/>
))}
</MasonryGrid>
Expand Down

0 comments on commit 9cbbf87

Please sign in to comment.