Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/react components fetch priority #434

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading