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

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

CSS

              
                html, body
  height: 100%
  
body
  background: #333
  display: flex
  justify-content: center
  align-items: center
  font-family: Helvetica Neue
  
h1
  font-size: 2em
  color: #eee
  display: inline-block

a
  color: white
p
  margin-top: 1em
  text-align: center
  color: #aaa
  
button
  padding: 10px
  background: #eee
  border: 3px solid #999
    
.copy-right
    font: normal 11px Tahoma
    position: absolute
    width: 100%
    text-align: center
    bottom: 0px
    left: 0
  
              
            
!

JS

              
                const {useState} = React;

const ClickManager = ({
  handleSingleClick,
  handleDoubleClick,
  children,
  ...props
}) => {
    const [clickCount, setClickCount] = useState(0);
    const [singleClickTimer, setSingleClickTimer] = useState(null);
    const delay = 280; // in ms

  const onSingleClick = (e) => {
    console.log('-------- is on click');

    // increase click count
    const currentClickCount = clickCount+1;
    setClickCount(currentClickCount); 

    if (currentClickCount === 1) {
        
      const timeoutId = setTimeout(() => {
        // reset click count
        setClickCount(0);

        // handle single click if handler is passed
        if(handleSingleClick) handleSingleClick(e);
      }, delay);
      setSingleClickTimer(timeoutId); // for have control to cancel single click in a delay to waiting for double-click

    } else {
      // click count is 2, it will be handled by `onDoubleClick` event, so do nothing!
      console.log('click count is:', currentClickCount);
    }
  };


  const onDoubleClick = (e) => {
    console.log('-------- is on DOUBBLE click');

    // -- first of all, cancel singleClick timeout and reset cilckCount
    clearTimeout(singleClickTimer);
    setClickCount(0);

    // handle double click if handler is passed
    if(handleDoubleClick) handleDoubleClick(e);
  };

  return (
    <div
      onClick={onSingleClick}
      onDoubleClick={onDoubleClick}
      {...props} 
    >
      {children} 
    </div>
  );
}

const Button = () => {
  const [singleClickCount, setSingleClickCount] = useState(0);
  const [doubleClickCount, setDoubleClickCount] = useState(0);

  return (<>
    <ClickManager
      handleSingleClick={() => setSingleClickCount(singleClickCount + 1)}
      handleDoubleClick={() => setDoubleClickCount(doubleClickCount + 1)}
    >
      <button>
        single Click {singleClickCount} times, double click {doubleClickCount} times
      </button>
    </ClickManager>
      
      
    <CopyRight />
  </>);
};

const CopyRight = () => {
  return (
    <p class="copy-right">Check the toturial here: <a href="https://night-coder.com/2022/01/18/reactjs-handle-single-click-and-doubleclick-at-the-same-time-click-manager/" target="_blank">Night-Coder.com</a></p>  
  );
};

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

Console