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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

            
              <button>Hover for a new image</button>
            
          
!

CSS

            
              html, body {
  min-height: 400px;
}

button {
  background: #ddd;
  border: none;
  font-size: 20px;
  padding: 10px 20px;
}

.tippy-tooltip.ajax-theme {
  position: absolute;
  width: 200px;
  padding: 0;
  overflow: hidden;
}

.tippy-tooltip.ajax-theme img {
  display: block;
  max-width: 100%;
}

/* 
We need to "pin" the tooltip absolutely to the bottom or top of the popper element depending on the placement.
*/
.tippy-tooltip.ajax-theme[x-placement^='top'] {
  top: auto !important;
  bottom: 0;
}

.tippy-tooltip.ajax-theme[x-placement^='bottom'] {
  bottom: auto !important;
  top: 0;
}
            
          
!

JS

            
              const INITIAL_CONTENT = '<div style="margin:5px 0;">Loading...</div>'

function applyStyles(popper, tooltip) {
  // Because the tooltip has `position: absolute`,
  // it no longer affects the parent popper's layout.
  // We need to explicitly give it a width.
  popper.style.width = '200px'

  // Setup transition styles on the tooltip itself
  tooltip.style.transitionDuration = '0.2s'
  tooltip.style.transitionProperty = 'visibility, opacity, height'
}

function animateHeight(instance, instanceContent) {
  const { popper } = instance
  const { tooltip, content } = instance.popperChildren
  
  function onTransitionEnd(event) {
    if (event.target === event.currentTarget) {
      content.style.opacity = '1'
      instance.setContent(instanceContent)
    }
  }
  
  // Wait until the height transition has finished before
  // fading the content in. Since we have `overflow: hidden`
  // on the tooltip this isn't actually needed, but if you
  // have an arrow element it will be.
  if (!instance._transitionEndListener) {
    instance._transitionEndListener = onTransitionEnd
  }

  tooltip.addEventListener('transitionend', onTransitionEnd)
  
  // Store the base height of the tooltip when it has the
  // initial Loading... content.
  if (!instance._baseHeight) {
    instance._baseHeight = tooltip.clientHeight
  }

  // Here is where we find out the height of the tooltip
  // when it has the content. We could technically hardcode
  // 200px as the value, but it's useful to know how to do
  // this with dynamic content.
  content.style.opacity = '0'
  
  // Temporarily set the image as the tooltip's content
  // so we can find out the final height of the tooltip.
  instance.setContent(instanceContent)
  const height = tooltip.clientHeight
  
  // Apply the height to the parent popper element.
  popper.style.height = height + 'px'
  
  // Reset the tooltip's height to the base height.
  tooltip.style.height = instance._baseHeight + 'px'
  
  // Cause reflow so we can start the height transition.
  void tooltip.offsetHeight
  
  // Start the transition.
  tooltip.style.height = height + 'px'
  
  // Remove the Loading... content and wait until the
  // transition finishes.
  instance.setContent('')
}

tippy('button', {
  content: INITIAL_CONTENT,
  animation: 'fade',
  animateFill: false,
  theme: 'ajax',
  async onShow(instance) {
    if (instance.state.isFetching === true || instance.state.canFetch === false) {
      return
    }

    instance.state.isFetching = true
    instance.state.canFetch = false
    
    applyStyles(instance.popper, instance.popperChildren.tooltip)
    
    try {
      const response = await fetch('https://unsplash.it/200/?random')
      const blob = await response.blob()
      const url = URL.createObjectURL(blob)
      
      // If the tooltip hid before finishing the request, stop further action
      if (!instance.state.isVisible) {
        return
      }
      
      const img = new Image()
      img.width = 200
      img.height = 200
      img.src = url
      
      animateHeight(instance, img)
    } catch (error) {
      instance.setContent('An error occurred')
    } finally {
      instance.state.isFetching = false
    }
  },
  onHidden(instance) {
    const { tooltip } = instance.popperChildren
    instance.state.canFetch = true
    instance.setContent(INITIAL_CONTENT)
    tooltip.style.height = null
    tooltip.removeEventListener('transitionend', instance._transitionEndListener)
    instance._transitionEndListener = null
  }
})

            
          
!
999px

Console