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

New Prop Placeholder to make a progressive image #76

Closed
CharlyJazz opened this issue Jun 5, 2020 · 4 comments
Closed

New Prop Placeholder to make a progressive image #76

CharlyJazz opened this issue Jun 5, 2020 · 4 comments

Comments

@CharlyJazz
Copy link
Author

I can make a pull request if you want!

@DaddyWarbucks
Copy link
Collaborator

DaddyWarbucks commented Jun 5, 2020

I believe you can handle this already with the onGenerate prop. Change the component from the article that you linked to something like this (not tested)

export default class ProgresiveLoading extends PureComponent {
  static propTypes = {
    thumb: string.isRequired
  }

  state = { ready: false };

  handleOnGenerate(src) {
    const buffer = new Image(); // Here is the secret! :)
    buffer.onload = () => this.setState({ ready: true });
    buffer.src = src;
  }

  render() {
    const {  thumb } = this.props;
    const { ready } = this.state;

    return (
      <div className="progressive-loading">
        <div className="progressive-loading-wrapper">
          <img src={thumb} className={classNames("thumb", { hide: ready })} />
          <StaticGoogleMap onGenerate={this.handleOnGenerate} style={{ display: ready ? '' : 'none' }}>
            <Marker location="6.4488387,3.5496361" />
         </StaticGoogleMap>
        </div>
      </div>
    )
  }
}

@DaddyWarbucks
Copy link
Collaborator

I am going to close this and then open a new issue for "Add progressive image loading" to the docs.

@DaddyWarbucks
Copy link
Collaborator

I have recently implemented this in my own apps and I wanted to revisit this issue. The trick is to actually use the onLoad prop rather than the onGenerate prop. With onGenerate the URL has been constructed and added to the img tag's src, but the image has not actually loaded yet. Instead, use the built in onLoad prop which fires after the image is downloaded.

To accomplish this, you want to hide (display: 'none') the StaticGoogleMap and show your placeholder image until onLoad is fired, then switch the images.

Below is a bit more complex example. This example uses Bootstrap 4 classes, but could use any CSS/style. There is some crafty CSS going on here with the "intrinsic placeholder" (the div with paddingBottom set to the aspectRatio), rather than a placeholder image. That div acts like an img because it maintains aspect ratio like the image will when it is loaded. The example also implements a fallback in case the image does not load at all.

export class MapImage extends React.Component {
  state = { status: 'loading' };
  render() {
    const { status } = this.state;
    const [width, height] = this.props.size.split('x').map(Number);
    const aspectRatio = (height / width) * 100;
    const { className, ...rest } = this.props;
    return (
      <div
        className="bg-light"
        style={{
          paddingBottom: `${aspectRatio}%`,
          position: 'relative'
        }}
      >
        <div
          style={{
            position: 'absolute',
            left: 0,
            top: 0,
            right: 0,
            bottom: 0
          }}
        >
          {status === 'error' && (
            <div className="d-flex h-100 w-100 align-items-center justify-content-center animated fadeIn fast">
              <p className="text-muted mb-0">Image not available</p>
            </div>
          )}
          <StaticGoogleMap
            apiKey={process.env.REACT_APP_GOOGLE_MAPS_KEY}
            className={`${className} ${
              status !== 'loaded' ? 'd-none' : 'animated fadeIn faster'
            }`}
            onLoad={() => this.setState({ status: 'loaded' })}
            onError={() => this.setState({ status: 'error' })}
            {...rest}
          />
        </div>
      </div>
    );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants