Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container">
  <div class="demo">
    <div id="root"></div>
  </div>
  
  <div class="explanation">
    <p>
      The above react component will render different elements 
      based on the user's connection quality:
    </p>
    <dl>
      <dt>offline:</dt>
      <dd>placeholder with alt text</dd>
      <dt>2g:</dt>
      <dd>low resolution image</dd>
      <dt>3g:</dt>
      <dd>high resolution image</dd>
      <dt>4g:</dt>
      <dd>video player</dd>
    </dl>
    <figure>
      <img 
         src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1256430/network.png" 
         alt="chrome devtools network tab" 
      />
      <figcaption>Try setting different connection speeds in devtools > network tab</figcaption>
    </figure>
    <p>
      It uses the <a href="https://wicg.github.io/netinfo/">Network Information API</a>, 
      which currently is <a href="https://caniuse.com/#search=Network%20Information%20API">only supported in Chrome</a>. Read more about this <a href="https://mxb.at/blog/connection-aware-components/">on my blog</a>.
    </p>
  </div>
</div>

              
            
!

CSS

              
                body {
  padding:2rem 0;
  background-color: #EAEAEA;
}

.demo {
  text-align:center;
  margin-bottom:2rem;
}

.explanation {
  background-color:#FFF;
  padding: 2rem;
  box-shadow:0 0 24px rgba(0,0,0,0.07);
}

.placeholder {
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
  height:300px;
  padding:2rem;
  text-align:center;
  background-color:#BFBFBF;
  color:#2A2A2A;
  
  &__caption {
    font-weight:bold;
    font-size:1.5rem;
    margin-bottom:1rem;
  }
}

figcaption {
  padding:.5rem 0;
  font-style:italic;
}

img, video {
  max-width:100%;
  height:auto;
}
              
            
!

JS

              
                /* Higher Order Component, injects "connectionType" prop */
function withConnectionType(WrappedComponent, respondToChange = false) {
    return class extends React.Component {
        constructor(props) {
            super(props)
            this.state = {
                connectionType: undefined
            }
            this.hasNetworkInfoSupport = Boolean(
              navigator.connection && navigator.connection.effectiveType
            )
            this.setConnectionType = this.setConnectionType.bind(this)
        }

        componentWillMount() {
            this.setConnectionType()
        }

        componentDidMount() {
            if (respondToChange && this.hasNetworkInfoSupport) {
                navigator.connection.addEventListener('change', this.setConnectionType)
            }
        }

        componentWillUnmount() {
            if (respondToChange && this.hasNetworkInfoSupport) {
                navigator.connection.removeEventListener('change', this.setConnectionType)
            }
        }
      
        getConnectionType() {
            const connection = navigator.connection
            // check if we're offline first...
            if (!navigator.onLine) {
                return 'offline'
            }
            // ...or if reduced data is preferred.
            if (connection.saveData) {
                return 'saveData'
            }
            return connection.effectiveType
        }

        setConnectionType() {
            if (this.hasNetworkInfoSupport) {
                const connectionType = this.getConnectionType()
                this.setState({
                    connectionType
                })
            }
        }

        render() {
            const { connectionType } = this.state
            return (
              <React.Fragment>
                <WrappedComponent
                    connectionType={connectionType}
                    {...this.props}
                />
                <p className="caption">
                  effective connection type: <strong>{connectionType || "undefined"}</strong>
                </p>
              </React.Fragment>
            )
        }
    }
}


/* Example Media Component, renders different things based on connection */
const Media = (props) => {
  const { imageSrc, videoSrc, connectionType, alt } = props
  
  // fallback if API is not supported
  if (!connectionType) {
    return <Image src={imageSrc.hiRes} alt={alt} />
  }

  // render different subcomponents based on network speed.
  switch(connectionType) {
    case 'offline':
      return <Placeholder text={alt} />

    case '4g':
      return <Video mp4={videoSrc.mp4} webm={videoSrc.webm} alt={alt} />

    case '3g':
      return <Image src={imageSrc.hiRes} alt={alt} />

    default:
      return <Image src={imageSrc.lowRes} alt={alt} />
  }
}


/* Some Demo Sub-Components */
const Placeholder = (props) => (
  <div className="placeholder">
    <p className="placeholder__caption">{props.text}</p>
    <p>(Image currently not available)</p>
  </div>
)
const Image = (props) => (
  <img src={props.src} alt={props.alt} />
)
const Video = (props) => (
  <video preload="true" controls="true" alt={props.alt}>
     <source src={props.mp4} type="video/mp4" />
    <source src={props.webm} type="video/webm" />
  </video>
)


/* Enhance Media with our HoC.
 * Re-render on connection change (for demo purpuses only)
 */
const ConnectionAwareMedia = withConnectionType(Media, true)


/* Mount It */
const ExampleApp = () => {
  const imageSrc = {
    lowRes: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1256430/chewie.lowres.jpg',
    hiRes: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1256430/chewie.hires.jpg'
  }
  const videoSrc =  {
    mp4: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1256430/chewie.mp4',
    webm: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1256430/chewie.webm'
  }
  return (
    <ConnectionAwareMedia
      imageSrc={imageSrc}
      videoSrc={videoSrc}
      alt="Chewie the Alpaca"
    />
  )
}

const rootElement = document.querySelector('#root')
ReactDOM.render(<ExampleApp />, rootElement)
              
            
!
999px

Console