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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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

              
                #app
              
            
!

CSS

              
                *
  box-sizing border-box

:root
  --hue 190
  
body
  background 'hsl(%s, 20%, 65%)' % var(--hue)
  color 'hsl(%s, 20%, 35%)' % var(--hue)
  display grid
  place-items center
  min-height 100vh
  
  &:after
    content "Tap"
    font-weight bold
    font-family sans-serif
    position fixed
    bottom 1rem
    right 2rem
    font-size 2rem
  
.flippy-snap
  --count 0
  height 50vmin
  width 50vmin
  display grid
  grid-template-columns repeat(var(--grid-size, 10), 1fr)
  grid-template-rows repeat(var(--grid-size, 10), 1fr)
  perspective 50vmin
  transform-style preserve-3d
  appearance none
  border none
  background none
  padding 0
  cursor pointer
    
.flippy-card
  --hovered 1
  height 100%
  width 100%
  position relative
  transform translate3d(0, 0, calc((1 - (var(--hovered, 1))) * 5vmin)) rotateX(calc(var(--count) * -180deg))
  transform-style preserve-3d

  &__front
  &__rear
    position absolute
    height 100%
    width 100%
    left 0
    top 0
    backface-visibility hidden
    -webkit-backface-visibility hidden
    background-image var(--current-image)
    background-position calc(var(--x, 0) * -100%) calc(var(--y, 0) * -100%)
    background-size calc(var(--grid-size, 10) * 100%)

  &__rear
    background-image var(--next-image)
    transform rotateY(180deg) rotate(180deg)
              
            
!

JS

              
                import React, { useState, useEffect, useRef } from 'https://cdn.skypack.dev/react'
import gsap from 'https://cdn.skypack.dev/gsap'
import { render } from 'https://cdn.skypack.dev/react-dom'

const ROOT_NODE = document.querySelector('#app')

const FlippySnap = ({ disabled, gridSize, onFlip, snaps }) => {
  const CELL_COUNT = Math.pow(gridSize, 2)
  const count = useRef(0)
  const containerRef = useRef(null)
  const flipping = useRef(false)

  const flip = e => {
    if (disabled || flipping.current) return
    const x = parseInt(e.target.parentNode.getAttribute('data-snap-x'), 10)
    const y = parseInt(e.target.parentNode.getAttribute('data-snap-y'), 10)
    count.current = count.current + 1
    gsap.to(containerRef.current.querySelectorAll('.flippy-card'), {
      '--count': count.current,
      delay: gsap.utils.distribute({
        from: [x / gridSize, y / gridSize],
        amount: gridSize / 20,
        base: 0,
        grid: [gridSize, gridSize],
        ease: 'power1.inOut',
      }),
      duration: 0.2,
      onStart: () => {
        flipping.current = true
      },
      onComplete: () => {
        // At this point update the images
        flipping.current = false
        if (onFlip) onFlip(count)
      },
    })
  }
  
  const indicate = e => {
    const x = parseInt(e.currentTarget.getAttribute('data-snap-x'), 10)
    const y = parseInt(e.currentTarget.getAttribute('data-snap-y'), 10)
    gsap.to(containerRef.current.querySelectorAll('.flippy-card'), {
      '--hovered': gsap.utils.distribute({
        from: [x / gridSize, y / gridSize],
        base: 0,
        amount: 1,
        grid: [gridSize, gridSize],
        ease: 'power1.inOut'
      }),
      duration: 0.1,
    })
  }
  
  const reset = () => {
    gsap.to(containerRef.current.querySelectorAll('.flippy-card'), {
      '--hovered': 1,
      duration: 0.1,
    })
  }

  return (
    <button
      className="flippy-snap"
      ref={containerRef}
      onPointerLeave={reset}
      style={{
        '--grid-size': gridSize,
        '--count': count.current,
        '--current-image': `url('${snaps[0]}')`,
        '--next-image': `url('${snaps[1]}')`,
      }}
      onClick={flip}>
      {new Array(CELL_COUNT).fill().map((cell, index) => {
        const x = index % gridSize
        const y = Math.floor(index / gridSize)
        return (
          <span
            onPointerOver={indicate}
            className="flippy-card"
            data-snap-x={x}
            data-snap-y={y}
            style={{
              '--x': x,
              '--y': y,
            }}>
            <span className="flippy-card__front"></span>
            <span className="flippy-card__rear"></span>
          </span>
        )
      })}
    </button>
  )
}


const App = () => {
  const [snaps, setSnaps] = useState([])
  const [disabled, setDisabled] = useState(false)
  const [gridSize, setGridSize] = useState(9)
  const snapRef = useRef(null)

  const grabPic = async () => {
    const pic = await fetch('https://source.unsplash.com/random/1000x1000')
    return pic.url
  }

  useEffect(() => {
    const setup = async () => {
      const url = await grabPic()
      const nextUrl = await grabPic()
      setSnaps([url, nextUrl])
      setDisabled(false)
    }
    setup()
  }, [])

  const setNewImage = async count => {
    const newSnap = await grabPic()
    setSnaps(
      count.current % 2 !== 0 ? [newSnap, snaps[1]] : [snaps[0], newSnap]
    )
    setDisabled(false)
  }

  const onFlip = async count => {
    setDisabled(true)
    setNewImage(count)
  }

  if (snaps.length !== 2) return <h1 className="loader">Loading...</h1>
  return (
    <FlippySnap
      gridSize={gridSize}
      disabled={disabled}
      snaps={snaps}
      onFlip={onFlip}
      snapRef={snapRef}
    />
  )
}

render(<App/>, ROOT_NODE)
              
            
!
999px

Console