idyll-loader-component

This document uses idyll-loader-component display content that, for one reason or another, doesn’t get displayed on initial page load. You can find the document source here.

To start things off with a contrived example, the blockquote below uses the Loader component.

Loading...
Loading...
Loading...
Figure 1.

Just a plot of y=x+1y = x + 1

This could easily be used to lazy-load images. Figure 4 at the bottom of the page does just that. The more interesting case though is using Loader to add content that pulls in sorta expensive external scripts without blocking page load. In particular, by specifying a script source, you can either load an external script on page loads or once the first component that needs it loads. Since it manages load events, you don’t need to worry about ensuring the library is loaded before you use it. Figure 1 shows a plot of y=x+1y = x + 1 . It’s accomplished by creating a custom component with the following code:

class Lineplot extends PlotlyComponent {
  plot (gd) {
    Plotly.plot(gd, {
      data: [{
        y: [1, 2, 3]
      }], layout: {
        margin: {t: 40, r: 30, b: 30, l: 30}
      }
    });
  }
}

The plotly.js component just uses the loader, which is also very simple to use:

render () {
  return <Loader src="..." onLoad={this.onLoad}>
    I'm content added after the script loads!
  </Loader>
}

You can find the source here. It’s quite a bit more complicated than it needs to be because CORS didn’t work out for the external resources I was trying to load. Using the loader was simple though.

Loading...
Figure 2.

Second figure, same as the first. Except this one has a long caption that wraps nicely onto the next line because I mean captions are trivial and all, but it actually takes like twenty minutes to sit down and sort out the block/inline CSS centering stuff, plus including an equation wraps the whole thing in a p tag so that it’s actually really really nice if it all Just Works™.

And for some reason Figure 2 shows the same plot again, but hey at least it only loads the plotly cdn script once and the end-user doesn’t have to worry about including the script at all. Hooray! 😸

And of course it wouldn’t be complete without good old json loading. In this case I downloaded the data from plot.ly/~rreusser/99.json and dropped it in my data/ folder.

Loading...
Figure 3.

Visitors to Joshua Tree National Park from 1979 through the present. A new brochure in 2014, possibly? Source: NPS Stats (https://irma.nps.gov/Stats/)

Loading...
Figure 4.Pika!