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.

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

              
                <div id="app"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                
const cancelablePromise = promise => {
  let isCanceled = false;

  const wrappedPromise = new Promise((resolve, reject) => {
    promise.then(
      value => (isCanceled ? reject({ isCanceled, value }) : resolve(value)),
      error => reject({ isCanceled, error }),
    );
  });

  return {
    promise: wrappedPromise,
    cancel: () => (isCanceled = true),
  };
};

const noop = () => {};

const delay = n => new Promise(resolve => setTimeout(resolve, n));

const stopTriggerClicksOnDoubleClick = WrappedComponent => {
  class ComponentWrapper extends React.Component {
    componentWillUnmount() {
      // cancel all pending promises to avoid
      // side effects when the component is unmounted
      this.clearPendingPromises();
    }

    pendingPromises = [];

    appendPendingPromise = promise =>
      (this.pendingPromises = [...this.pendingPromises, promise]);

    removePendingPromise = promise =>
      (this.pendingPromises = this.pendingPromises.filter(p => p !== promise));

    clearPendingPromises = () => this.pendingPromises.map(p => p.cancel());

    handleClick = () => {
      // create the cancelable promise and add it to
      // the pending promises queue
      const waitForClick = cancelablePromise(delay(300));
      this.appendPendingPromise(waitForClick);

      return waitForClick.promise
        .then(() => {
          // if the promise wasn't cancelled, we execute
          // the callback and remove it from the queue
          this.removePendingPromise(waitForClick);
          this.props.onClick();
        })
        .catch(errorInfo => {
          // rethrow the error if the promise wasn't
          // rejected because of a cancelation
          this.removePendingPromise(waitForClick);
          if (!errorInfo.isCanceled) {
            throw errorInfo.error;
          }
        });
    };

    handleDoubleClick = () => {
      // all (click) pending promises are part of a 
      // dblclick event so we cancel them
      this.clearPendingPromises();
      this.props.onDoubleClick();
    };

    render() {
      return (
        <WrappedComponent
          {...this.props}
          onClick={this.handleClick}
          onDoubleClick={this.handleDoubleClick}
        />
      );
    }
  }

  ComponentWrapper.displayName = `withClickPrevention(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})`

  ComponentWrapper.defaultProps = {
    onClick: noop,
    onDoubleClick: noop,
  };

  return ComponentWrapper;
};

const ClickableBox = ({ onClick, onDoubleClick }) => (
  <button onClick={onClick} onDoubleClick={onDoubleClick}>
    Click or double click
  </button>
);

ClickableBox.defaultProps = {
  onClick: noop,
  onDoubleClick: noop,
};

const EnhancedClickableBox = stopTriggerClicksOnDoubleClick(ClickableBox)

const DoubleClickExample = () => (
  <EnhancedClickableBox
    onClick={() => console.log("on click")}
    onDoubleClick={() => console.log("on double click")}
  />
);

const App = () => {

  return (
    <DoubleClickExample />
  )
}

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

Console