Components
Img3

Img3

<Img3 /> is a fundamental components for Web3 Apps. It extends HTML <img /> with Web3 decentralization storage, like IPFS. With <Img3 /> you can put ipfs:// in the src and render the image from IPFS with the fastest gateway. Will support ar soon.

Features

  • Supports the ipfs:// protocol.
  • Automatically uses the gateway with the highest speed, And reuse it the next request.
const defaultGateways = [
  'https://nftstorage.link/ipfs/',
  'https://ipfs-gateway.cloud/ipfs/',
  'https://gateway.pinata.cloud/ipfs/',
  'https://4everland.io/ipfs/',
];

Usage

import { Img3 } from '@lxdao/img3';

Props

PropTypeDescriptionDefault
srcstringimage source, support ipfs:// protocol
altstringimage alt text
gatewaysstring[]Web3 decentralization storage gatewaysdefaultGateways
timeoutnumbertimeout of gateway request2000
iconIconstyle of loading or error icons{size: 30, color: '#c0c0c0'}

Img3Provider

@lxdao/img3 provides an Img3Provider provider component that should be used to wrap around your components.

Img3Provider's responsibility is to pass the default gateway value to all of the <Img3 /> components in your application. Usually, your application only needs one provider. You should place it as high as possible in your React tree.

PropTypeDescriptionDefault
defaultGatewaysstring[]Web3 decentralization storage gateways
import React from 'react';
import { Img3Provider } from '@lxdao/img3';
 
export default function Demo() {
  return (
    <Img3Provider defaultGateways={["https://nftstorage.link/ipfs/"]}>
      <YourApp />
    </Img3Provider>
  );
}

Types reference

Icon

type Icon = {
  /** icon size. */
  size?: number;
  /** icon color. */
  color?: string;
  /** error icon size. */
  errorSize?: number;
  /** error icon color. */
  errorColor?: string;
};

Examples

Basic

Like <img /> tag, set the src to render the image.

Initializing...
import React from 'react';
import { Img3 } from '@lxdao/img3';

export default function App() {
  return (
    <div style={{padding: 10}}>
      <Img3
        src="https://bafkreifrusdcwh3w7d5uzehf3vvm6fzsrg7xdabvbzbfe6wt4jiy5y6kfy.ipfs.nftstorage.link/"
        style={{ height: 200, width: 200, borderRadius: 5, background: '#f2f4f6' }}
      />
    </div>
  )
};

IPFS protocol

ipfs:// protocol will be automatically replaced with the fastest gateway.

Initializing...
import React from 'react';
import { Img3 } from '@lxdao/img3';

export default function App() {
  return (
    <div style={{padding: 10}}>
      <Img3
        src="ipfs://bafkreifrusdcwh3w7d5uzehf3vvm6fzsrg7xdabvbzbfe6wt4jiy5y6kfy"
        style={{ height: 200, width: 200, borderRadius: 5, background: '#f2f4f6' }}
      />
    </div>
  )
};

Overwrite gateways

You can overwrite the default gateways, will be used in the request, and the fastest gateway will be cached.

Initializing...
import React from 'react';
import { Img3 } from '@lxdao/img3';

export default function App() {
  return (
    <div style={{padding: 10}}>
      <Img3
        src="ipfs://bafkreifrusdcwh3w7d5uzehf3vvm6fzsrg7xdabvbzbfe6wt4jiy5y6kfy"
        gateways={['https://ipfs.io/ipfs/']}
        style={{ height: 200, width: 200, borderRadius: 5, backgroundColor: '#f2f4f6' }}
        timeout={5000}
        icon={{
          size: 50,
          color: '#000',
          errorColor: '#f00',
          errorSize: 50
        }}
      />
    </div>
  )
};