123

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 id="root"></div>
<img class="hacky-asset-prefetch" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/820793/cursor-pointer-clicked.png" />
            
          
!
            
              body {
  margin: 0;
  cursor: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/820793/cursor-auto.png), auto;
  -webkit-tap-highlight-color: transparent;
}

/* 
  Explore how to prefetch asset instead of waiting for it to be loaded 
  when the render method calls it (see CustomPointerComponent -> state.isClicked)
  This is inspired by https://github.com/JasonBoy/prefetch-image
*/
.hacky-asset-prefetch {
  display: none;
  width: 0;
  height: 0;
  overflow: hidden;
}
            
          
!
            
              const renderTheScene = () =>
  ReactDOM.render(
    <Maestro>
      {({ elements, toggleDay}) => (
        <Sky ref={elements.sky}>
          <Sun ref={elements.sun} onClick={toggleDay} />
          <Mountain ref={elements.bigMountain} />
          <Mountain onClick={toggleDay} />
        </Sky>  
      )}
    </Maestro>,
    document.getElementById('root')
  );

const Mountain = React.forwardRef((props, ref) => (
  <InteractivePath 
    fill="#FFF"
    d="M121.284 114l28.285 28.284H93z"
    ref={ref}
    onClick={props.onClick} 
  />
));

const Sun = React.forwardRef((props, ref) => (
  <InteractivePath
    fill="#FFBD2D"
    d="M149.284 86l28.285 28.284-28.285 28.285L121 114.284z"
    ref={ref}
    onClick={props.onClick}
  />
));

const Sky = React.forwardRef((props, ref) => (
  <div 
    style={{
      background: "#000",
      height: "100vh",
      display: "flex",
      flexDirection: "column",
      justifyContent: "center",
      alignItems: "center"
    }}
    ref={ref}
  >
    <svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
      {props.children}
    </svg>
  </div>
));

class Maestro extends React.Component {
  elements = {
    bigMountain: React.createRef(),
    sky: React.createRef(),
    sun: React.createRef()
  };
  timeline = new TimelineMax();  

  state = {
    isDay: false
  };

  componentDidMount() {
    const [bigMountain, sky, sun] = Object.values(this.elements).map(ref => ref.current);
    
    TweenMax.set(bigMountain, { transformOrigin: "0% 100%" });
    TweenMax.set(sun, { visibility: "hidden" });
    
    this.timeline
      .to(bigMountain, 0.4, { scale: 2, fill: "#9B9B9B", ease: Sine.easeOut })
      .add("lighten-up")
      .set(sun, { visibility: "visible" })
      .to(sun, 0.3, { x: -40, y: -40, ease: Sine.easeOut }, "lighten-up")
      .to(sky, 0.4, { backgroundColor: "#4A4EF7", ease: Sine.easeOut }, "lighten-up")
      .to(bigMountain, 0.4, { fill: "#44D9F4", ease: Sine.easeOut }, "lighten-up")
    
    this.timeline.pause();
  }

  componentDidUpdate() {
    if (!this.state.isDay) {
      this.timeline.reverse();
    } else { 
      this.timeline.play();
    }
  }

  toggleDay = () => {
    this.setState(state => ({ isDay: !state.isDay }));
  };

  render() {
    return this.props.children({
      elements: this.elements,
      toggleDay: this.toggleDay
    });
  }
}

const createCustomPointerComponent = Component => {
  
  class CustomPointerComponent extends React.Component {
    state = {
      isClicked: false
    };

    render() {
      const { forwardedRef, ...rest } = this.props;
      
      return (
        <Component
          ref={forwardedRef}
          style={rest.onClick && { cursor: `url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/820793/cursor-pointer${this.state.isClicked ? "-clicked" : ""}.png), pointer`}}
          onMouseDown={() => this.setState(() => ({ isClicked: true }))}
          onMouseUp={() => this.setState(() => ({ isClicked: false }))}
          {...rest}
        />
      );
    }
  }

  const forwardRef = (props, ref) => (
    <CustomPointerComponent {...props} forwardedRef={ref} />
  );

  forwardRef.displayName = typeof Component === "string" ? Component : (Component.displayName || Component.name);

  return React.forwardRef(forwardRef);
}

const InteractivePath = createCustomPointerComponent("path");

renderTheScene();
            
          
!
999px
🕑 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.

Console