Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
<div id="app"></div>
              
            
!

CSS

              
                
:root {
  // Base Orange
  --orange-h: 36;
  --orange-s: 69%;
  --orange-l: 50%;
  --orange-base: hsl(var(--orange-h), var(--orange-s), var(--orange-l));
}


body {
  margin: 0;
  text-align: center;
  font-size: 14px;
  font-family: 'Roboto', sans-serif;
  font-weight: 700;
  font-style: normal;
  background-image: linear-gradient(to top, #dfe9f3 0%, white 100%);
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100vw;
  height: 100vh;
  
}

.magnetic-button {
  border: none;
  padding: 0;
  background: transparent;
  cursor: pointer;
  position: relative;
  z-index: 1;
  touch-action: none;
  text-transform: uppercase;
  font-family: 'Roboto', sans-serif;
  letter-spacing: 0.5px;
  font-weight: 500 !important;
  
  span {
    display: inline-block;
  }
  
  &--hover {
    content: "";
    position: absolute;
    z-index: -1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
  }
}

.magnetic-button + .magnetic-button {
  margin-top: 60px;
}

.pill {
  background: var(--orange-base);
  color: white;
  border-radius: 30px;
  border: none;
  padding: 20px 40px;
  font-size: 16px;
  font-weight: 700;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  box-shadow: 
    inset 1px 1px 1px 0 rgba(255,255,255,.5),
    7px 7px 20px 0 rgba(0,0,0,0.07),
    4px 4px 5px 0 rgba(0,0,0,0.07);
}

.round {
  height: 150px;
  width: 150px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid var(--orange-base);
  color: var(--orange-base);
  display: block;
  font-size: 16px;
  font-weight: bold;
  margin: 0 auto;
  padding: 30px;
  box-shadow:
    -7px -7px 20px 0 rgba(255,255,255,.5),
    -4px -4px 5px 0 rgba(255,255,255,.5),
    7px 7px 20px 0 rgba(0,0,0,0.07),
    4px 4px 5px 0 rgba(0,0,0,0.07);
}
              
            
!

JS

              
                const { useRef, useState, useEffect, createRef } = React


/* --------------------------
* MAGNETIC BUTTON
* -------------------------- */

const MagneticButton = ({ 
  children,
  className,
  speed = 1,
  tollerance = 0.8,
  scale = 1.2,
  debug = false,
  ...props
}) => {
  const $root = useRef()
  const $item = useRef()
  const $hover = useRef()
  const rootBound = useRef()
  const itemBound = useRef()
  const diffBound = useRef({ x: 0, y: 0 })
  const splitTextTl = gsap.timeline({ 
    paused: true,
    onComplete: function() {
      splitTextTl.seek(0).pause();
      splitTextTl.tweenTo( splitTextTl.nextLabel() );
    }
  })
  
  useEffect(() => {
     let splitText = new SplitText($item.current, {type:"words,chars"})
     let chars = splitText.chars;
    
     splitTextTl
       .addLabel("start", "<")
       .set(chars, {opacity: 0, y: 10, transformOrigin:"0% 50% -50"})
       .to(chars, {duration: 0.3, opacity:1, y: 0, transformOrigin:"0% 50% -50", ease:"back", stagger: 0.015}, "+=0")
       .addLabel("visible", ">")
       .to(chars, {duration: 0.3, opacity:0, y: -10, transformOrigin:"0% 50% -50", ease:"back", stagger: 0.015}, "+=0")
       .addLabel("invisible", ">")
     ;
    
    splitTextTl.tweenFromTo('start', 'visible');
  });
  
  const handleMouseEnter = () => {
    splitTextTl.tweenTo(splitTextTl.nextLabel());
    
    gsap.killTweensOf($root.current)
    gsap.set($hover.current, {
      scale: scale,
      background: debug ? 'rgba(0, 125, 255, .4)' : 'transparent',
    })
    
    rootBound.current = $root.current.getBoundingClientRect()
    itemBound.current = $item.current.getBoundingClientRect()
    diffBound.current.x = (rootBound.current.width * scale - rootBound.current.width) / 2
    diffBound.current.y = (rootBound.current.height * scale - rootBound.current.height) / 2
  }
    
  const handleMouseLeave = () => {
    gsap.killTweensOf($root.current)
    gsap.to($root.current, {
      x: 0,
      y: 0,
      ease: 'elastic.out(1.1, .4)',
      duration: 1.2
    })
    gsap.set($hover.current, {
      scale: 1
    })
  }
  
  const handleMouseMove = (e) => {
    const x = e.clientX || e.touches[0].clientX
    const y = e.clientY || e.touches[0].clientY
    
    const maxX = (rootBound.current.width - itemBound.current.width) / 2 * tollerance
    const maxY = (rootBound.current.height - itemBound.current.height) / 2 * tollerance
    
    const newX = gsap.utils.mapRange(
      0,
      rootBound.current.width * scale,
      -maxX,
      maxX,
      x - rootBound.current.x + diffBound.current.x
    )
    
    const newY = gsap.utils.mapRange(
      0,
      rootBound.current.height * scale,
      -maxY,
      maxY,
      y - rootBound.current.y + diffBound.current.y
    )
    
    gsap.killTweensOf($root.current)
    gsap.to($root.current, {
      x: newX,
      y: newY,
      ease: 'power3.out',
      duration: speed
    })
  }
  
  return (
    <button 
      ref={$root}
      className={`magnetic-button ${className}`}
      onMouseEnter={handleMouseEnter}
      onMouseMove={handleMouseMove}
      onTouchMove={handleMouseMove}
      onTouchStart={handleMouseEnter}
      onMouseLeave={handleMouseLeave}
      onTouchEnd={handleMouseLeave}
      {...props}
    >
      <span
        ref={$item}
        className="magnetic-button--item"
      >
        {children}
      </span>
      <span
        ref={$hover}
        className="magnetic-button--hover"
      />
    </button>
  )
}


/* --------------------------
* APP
* -------------------------- */
const App = () => {
  return (
    <div className="App">
      
      <MagneticButton
        className="round"
        scale={2}
        tollerance={.8}
        speed={.3}
        onClick={() => {
          console.log('click')
        }}
      >
        View All Work
      </MagneticButton>
      
      <MagneticButton
        className="pill"
        scale={2}
        tollerance={.8}
        speed={.3}
        onClick={() => {
          console.log('click')
        }}
      >
        Load More Work
      </MagneticButton>
    </div>
  )
}


/* --------------------------
* RENDER
* -------------------------- */

ReactDOM.render(<App />,
document.getElementById("app"))
              
            
!
999px

Console