-
Notifications
You must be signed in to change notification settings - Fork 53
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
react-components: support external state for grid #442
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@giphy/react-components': patch | ||
--- | ||
|
||
- Grid supports editableGifs | ||
- make fetchpriority lowercase to avoid runtime warning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/react-components/stories/grid-editable.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import styled from '@emotion/styled' | ||
import { giphyPurple } from '@giphy/colors' | ||
import { GiphyFetch } from '@giphy/js-fetch-api' | ||
import { IGif } from '@giphy/js-types' | ||
import { Meta, StoryObj } from '@storybook/react' | ||
import fetchMock from 'fetch-mock' | ||
import React, { useEffect, useState } from 'react' | ||
import { throttle } from 'throttle-debounce' | ||
import { GifOverlayProps, Grid as GridComponent } from '../src' | ||
import inTestsRunner from './in-tests-runner' | ||
import mockGifsResult from './mock-data/gifs.json' | ||
|
||
const apiKey = 'sXpGFDGZs0Dv1mmNFvYaGUvYwKX0PWIh' | ||
const gf = new GiphyFetch(apiKey) | ||
|
||
const OverlayContainer = styled.div` | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
right: 0; | ||
bottom: 0; | ||
display: flex; | ||
color: white; | ||
justify-content: center; | ||
align-items: center; | ||
` | ||
const ButtonContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 2px; | ||
align-items: center; | ||
h4 { | ||
text-align: center; | ||
} | ||
` | ||
const Button = styled.div` | ||
cursor: pointer; | ||
background: ${giphyPurple}; | ||
padding: 6px; | ||
border-radius: 4px; | ||
max-width: fit-content; | ||
` | ||
|
||
type GridProps = Partial<React.ComponentProps<typeof GridComponent>> | ||
|
||
const Grid = ({ loader, ...other }: GridProps) => { | ||
const [width, setWidth] = useState(innerWidth - 24) | ||
const [externalGifs, setExternalGifs] = useState<IGif[] | undefined>() | ||
const Overlay = ({ gif, isHovered }: GifOverlayProps) => ( | ||
<OverlayContainer> | ||
{isHovered && ( | ||
<ButtonContainer> | ||
<h4>{gif.title}</h4> | ||
<Button | ||
onClick={() => { | ||
setExternalGifs(externalGifs?.filter((g) => g.id !== gif.id)) | ||
}} | ||
> | ||
Delete Me | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
const result = prompt('Edit Title', gif.title) || 'New Title' | ||
if (externalGifs) { | ||
const editGif = externalGifs?.find((g) => g.id === gif.id) | ||
if (editGif) { | ||
editGif.title = result | ||
setExternalGifs([...externalGifs]) | ||
} | ||
} | ||
}} | ||
> | ||
Edit Title | ||
</Button> | ||
</ButtonContainer> | ||
)} | ||
</OverlayContainer> | ||
) | ||
const onResize = throttle(500, () => setWidth(innerWidth - 24)) | ||
useEffect(() => { | ||
window.addEventListener('resize', onResize, false) | ||
return () => window.removeEventListener('resize', onResize, false) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
const fetchGifs = async (offset: number) => { | ||
if (inTestsRunner()) { | ||
fetchMock.restore().getOnce(`begin:https://api.giphy.com/v1/gifs/search`, { body: mockGifsResult }) | ||
} | ||
const result = await gf.trending({ offset, limit: 10 }) | ||
fetchMock.restore() | ||
setExternalGifs(result.data) | ||
return result | ||
} | ||
return ( | ||
<GridComponent | ||
width={width} | ||
noLink | ||
columns={3} | ||
loader={loader} | ||
externalGifs={externalGifs} | ||
fetchGifs={fetchGifs} | ||
overlay={Overlay} | ||
onGifsFetched={(gifs) => setExternalGifs(gifs)} | ||
{...other} | ||
/> | ||
) | ||
} | ||
|
||
const meta: Meta<typeof Grid> = { | ||
component: Grid, | ||
title: 'React Components/Grid/Editable', | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof meta> | ||
|
||
export const EditableGrid: Story = {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
externalGifs
exists, is this ever going to befalse
? i can't remember if===
works with arrays of objects or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sketchybones yes this should be referentially false most of the time, the state in the grid is set to the external state. When you want to update the grid, pass in a new array. I do that in the Story when deleting a gif. If you edit the title, I don't update the array, although I probably should. But since the
Gif
component rerenders on hover, it grabs the latest title.I guess if you want it to update instantly, you'd want to clone the array after changing the title: