Skip to content

Commit 825561b

Browse files
Erik RitterDhaiwat10
andauthored
Fix NFT sizes and add docs (#130)
* Fix NFT sizes and add docs * Add changeset Co-authored-by: Dhaiwat Pandya <[email protected]>
1 parent ecf3b20 commit 825561b

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

.changeset/eighty-icons-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web3-ui/components': minor
3+
---
4+
5+
Fix NFT sizes

packages/components/src/components/NFT/NFT.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ export const Audio = () => (
2525
<NFT contractAddress='0x0eede4764cfdfcd5dac0e00b3b7f4778c0cc994e' tokenId='1' />
2626
);
2727

28+
export const Big = () => (
29+
<NFT contractAddress='0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D' tokenId='1' size='lg' />
30+
);
31+
2832
export const Error = () => <NFT contractAddress='abcd' tokenId='1' />;

packages/components/src/components/NFT/NFT.tsx

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,18 @@ import {
1414
import fetch from 'cross-fetch';
1515

1616
export interface NFTProps {
17+
/**
18+
* The address of the NFT contract.
19+
*/
1720
contractAddress: string;
21+
/**
22+
* The id of the NFT.
23+
*/
1824
tokenId: string;
25+
/**
26+
* The size of the NFT card.
27+
*/
28+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
1929
}
2030

2131
export interface NFTData {
@@ -30,7 +40,7 @@ export interface NFTData {
3040
/**
3141
* Component to fetch and display NFT data
3242
*/
33-
export const NFT = ({ contractAddress, tokenId }: NFTProps) => {
43+
export const NFT = ({ contractAddress, tokenId, size = 'xs' }: NFTProps) => {
3444
const _isMounted = useRef(true);
3545
const [nftData, setNftData] = React.useState<NFTData>();
3646
const [errorMessage, setErrorMessage] = React.useState<string>();
@@ -54,8 +64,12 @@ export const NFT = ({ contractAddress, tokenId }: NFTProps) => {
5464
animationUrl: data.animation_url,
5565
});
5666
}
57-
} catch (error: any) {
58-
setErrorMessage(error.message);
67+
} catch (error) {
68+
if (error instanceof Error) {
69+
setErrorMessage(error.message);
70+
} else {
71+
setErrorMessage('An unknown error occurred');
72+
}
5973
}
6074
}, [contractAddress, tokenId]);
6175

@@ -67,7 +81,7 @@ export const NFT = ({ contractAddress, tokenId }: NFTProps) => {
6781
};
6882
}, [contractAddress, tokenId]);
6983

70-
return <NFTCard data={nftData} errorMessage={errorMessage} />;
84+
return <NFTCard data={nftData} errorMessage={errorMessage} size={size} />;
7185
};
7286

7387
/**
@@ -76,9 +90,11 @@ export const NFT = ({ contractAddress, tokenId }: NFTProps) => {
7690
export const NFTCard = ({
7791
data,
7892
errorMessage = '',
93+
size,
7994
}: {
8095
data: NFTData | undefined | null;
8196
errorMessage?: string | undefined;
97+
size: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
8298
}) => {
8399
const name = data?.name;
84100
const imageUrl = data?.imageUrl;
@@ -98,23 +114,25 @@ export const NFTCard = ({
98114
}
99115

100116
return (
101-
<Skeleton isLoaded={!!data} maxW='xs' h='md'>
102-
<Box maxW='xs' borderRadius='lg' borderWidth='1px' overflow='hidden'>
117+
<Skeleton isLoaded={!!data} maxW={size} h='md'>
118+
<Box maxW={size} borderRadius='lg' borderWidth='1px' overflow='hidden'>
103119
{animationUrl ? (
104120
animationUrl.endsWith('.mp3') ? (
105121
<VStack>
106-
<Image src={imageUrl} alt={displayName} borderRadius='lg' />
122+
<Image src={imageUrl} alt={displayName} borderRadius='lg' w={size} />
107123
<audio src={animationUrl} controls autoPlay muted style={{ borderRadius: '7px' }} />
108124
</VStack>
109125
) : (
110-
<video src={animationUrl} controls autoPlay muted />
126+
<Flex w={size} h={size} bg='black' justifyContent='center'>
127+
<video src={animationUrl} controls autoPlay muted />
128+
</Flex>
111129
)
112130
) : (
113-
<Image src={imageUrl} alt={displayName} borderRadius='lg' />
131+
<Image src={imageUrl} alt={displayName} borderRadius='lg' w={size} />
114132
)}
115133
<Box p='6'>
116134
<Flex alignItems='center' justifyContent='space-between' pb='2'>
117-
<Heading as='h3' size='sm'>
135+
<Heading as='h3' size='sm' style={{ overflowWrap: 'anywhere' }}>
118136
{displayName}
119137
</Heading>
120138
{assetContractSymbol && <Tag size='sm'>{assetContractSymbol}</Tag>}

packages/components/src/components/NFTGallery/NFTGallery.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
};
99

1010
export const nftsOwnedByAnAccount = () => (
11-
<NFTGallery address='0x1A16c87927570239FECD343ad2654fD81682725e' />
11+
<NFTGallery address='0x1A16c87927570239FECD343ad2654fD81682725e' gridWidth={2} />
1212
);
1313

1414
const nftsOwnedByAnENSStory = () => {

packages/components/src/components/NFTGallery/NFTGallery.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ import { NFTCard } from '../NFT';
66

77
export interface NFTGalleryProps {
88
/**
9-
* The owner of the NFTs
9+
* The owner of the NFTs. Can be a wallet address, or an ENS name. If the address is an ENS
10+
* name, then you must also provide the provider.
1011
*/
1112
address: string;
1213
/**
1314
* The number of columns in the grid
1415
*/
1516
gridWidth?: number;
17+
/**
18+
* A Web3Provider. Only needed if the address will be an ENS name.
19+
*/
1620
web3Provider?: ethers.providers.Web3Provider;
1721
}
1822

@@ -45,16 +49,16 @@ export const NFTGallery = ({ address, gridWidth = 4, web3Provider }: NFTGalleryP
4549
resolvedAddress = await web3Provider.resolveName(address);
4650
}
4751
fetch(`https://api.opensea.io/api/v1/assets?owner=${resolvedAddress}`)
48-
.then((res) => {
52+
.then(res => {
4953
if (!res.ok) {
5054
throw Error(
5155
`OpenSea request failed with status: ${res.status}. Make sure you are on mainnet.`
5256
);
5357
}
5458
return res.json();
5559
})
56-
.then((data) => setNfts(data.assets))
57-
.catch((err) => setErrorMessage(err.message));
60+
.then(data => setNfts(data.assets))
61+
.catch(err => setErrorMessage(err.message));
5862
}
5963
exec();
6064
}, [address, web3Provider]);
@@ -69,7 +73,7 @@ export const NFTGallery = ({ address, gridWidth = 4, web3Provider }: NFTGalleryP
6973
</Alert>
7074
)}
7175
<Grid templateColumns={`repeat(${gridWidth}, 1fr)`} gap={6}>
72-
{nfts.map((nft) => (
76+
{nfts.map(nft => (
7377
<NFTCard
7478
key={`${nft.asset_contract.symbol}-${nft.token_id}`}
7579
data={{
@@ -79,6 +83,7 @@ export const NFTGallery = ({ address, gridWidth = 4, web3Provider }: NFTGalleryP
7983
assetContractName: nft.asset_contract.name,
8084
assetContractSymbol: nft.asset_contract.symbol,
8185
}}
86+
size='xs'
8287
/>
8388
))}
8489
</Grid>

0 commit comments

Comments
 (0)