css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              <div class="container">
  <div class="demo">
    <div id="root"></div>
  <div class="explanation">
      The above react component will render different elements 
      based on the user's connection quality:
      <dd>placeholder with alt text</dd>
      <dd>low resolution image</dd>
      <dd>high resolution image</dd>
      <dd>video player</dd>
         alt="chrome devtools network tab" 
      <figcaption>Try setting different connection speeds in devtools > network tab</figcaption>
      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>.

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

.demo {

.explanation {
  padding: 2rem;
  box-shadow:0 0 24px rgba(0,0,0,0.07);

.placeholder {
  &__caption {

figcaption {
  padding:.5rem 0;

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

        componentWillMount() {

        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()

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

/* 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} />

      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>
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" />

/* 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 (
      alt="Chewie the Alpaca"

const rootElement = document.querySelector('#root')
ReactDOM.render(<ExampleApp />, rootElement)
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................