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

              
                
              
            
!

CSS

              
                body, html, .app {
  width: 100%;
  height: 100%;
}

.app {
  display: flex;
  align-items: center;
  justify-content: center;
  
  .particles {
    position: fixed;
    top: 0;
    left: 0;
    pointer-events: none;

    .particle {
      position: absolute;
      transition: all 5s ease-out;
      
      &.circular {
        background-color: var(--color);
        width: var(--size);
        height: var(--size);
        border-radius: var(--size);
        transform: translate(var(--x), var(--y));
      }
      
      &.rectangular {
        background-color: var(--color);
        width: var(--size);
        height: var(--size);
        transform: 
          translate(var(--x), var(--y)) 
          rotateX(var(--rotate)) 
          rotateY(var(--rotate));
      }
      
      &.squiggle {
        stroke: var(--color);
        stroke-width: 15px;
        stroke-linecap: round;
        overflow: visible;
        width: var(--size);
        height: var(--size);
        transform: 
          translate(var(--x), var(--y))
          rotateY(var(--rotate));
      }
    }
  }
  
  .button {
    height: 40px;
    width: 200px;
    font-family: Roboto;
    background-color: #34495e;
    color: white;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all .2s ease-out;
    user-select: none;
    
    &:hover {
      background-color: #8e44ad;
    }
    
    .popper {
      margin-right: 20px;
      margin-left: -20px;
      width: 64px;
      height: 64px;
      background-image: url('https://cdn.yoavik.com/codepen/react-confetti/confetti.png');
    }
  }
}
              
            
!

JS

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

const COLORS = ['#2ecc71','#3498db','#e67e22','#e67e22','#e74c3c'];
const TOP_OFFSET = window.innerHeight;
const LEFT_OFFSET = 150;

const randomNumber = (min, max) => min + Math.floor(Math.random()*(max - min));

const randomColor = () => COLORS[randomNumber(0,COLORS.length)];

const Particle = ({children, size}) => {
  const ref = useRef();
  const child = React.Children.only(children);
  const top = randomNumber(-200, -size[1]);
  
  useEffect(() => {
    ref.current.style.setProperty('--x', `${randomNumber(-LEFT_OFFSET, LEFT_OFFSET)}px`);
    ref.current.style.setProperty('--y', `${window.innerHeight - top + randomNumber(0, 300)}px`);
    ref.current.style.setProperty('--rotate', `${randomNumber(200, 3000)}deg`);
  }, []);
  
  return React.cloneElement(child, {ref, style: {
    '--color': randomColor(),
    '--size': `${randomNumber(...size)}px`,
    '--rotate': '0deg',
    '--x': '0px',
    '--y': '0px',
    top: top,
    left: randomNumber(0, window.innerWidth),
  }});
};

const CircularParticle = () => (
  <Particle size={[5, 10]}>
    <div className='particle circular'/>
  </Particle>
);

const RectangularParticle = () => (
  <Particle size={[5, 10]}>
    <div className='particle rectangular'/>
  </Particle>
);

const SquiggleParticle = () => (
  <Particle size={[15, 45]}>
    <svg className='particle squiggle'
      xmlns="http://www.w3.org/2000/svg" 
      viewBox="0 0 30 200">
      <path d="M15 0 Q 30 25 15 50 Q 0 75 15 100 Q 30 125 15 150 Q 0 175 15 200"/>
    </svg>
  </Particle>
);

const Particles = memo(({count: n}) => {
  
  const particles = [];
  const types = [SquiggleParticle, RectangularParticle, CircularParticle];

  while(n--) {
    const Particle = types[randomNumber(0, 3)];
    particles.push(
      <Particle key={n}/>
    );
  }
  
  return (
    <div className='particles'>
      {particles}
    </div>
  );
});

let id = 1;
const App = () => {
  const [particles, setParticles] = useState([]);
  const {innerWidth} = window;
  
  const handleOnClick = () => {
    const _id = id;
    id++;
    
    setParticles(particles => [...particles, _id]);
    setTimeout(() => {
      // Cleanup
      setParticles(particles => particles.filter(id => id !== _id));
    }, 5000);
  }
  
  return (
    <div className='app'>
      {particles.map(id => (
        <Particles key={id} count={Math.floor(innerWidth / 10)}/>
      ))}
      <div className='button' onClick={handleOnClick}>
        <div className='popper'/>
        CLICK ME!</div>
    </div>
  );
};

ReactDOM.render(<App/>, document.body);
              
            
!
999px

Console