0

Want to ask, when I click on the button above, the Dropdown menu can be closed with Tailwind CSS - ReactJS in the button above, but meanwhile I want to be able to click close the Dropdown menu via a web page other than the button, as an example in the picture with circle number 1, how do I close it? Dropdown menu besides button in my code?

Screenshot Website Dropdown menu

1

Code: `

import React, { useState } from "react";

const App = () => {
  const [showOptions, setShowOptions] = useState(false);
  const handleClick = () => {
    setShowOptions(!showOptions);
  };
  return (

    <div class="relative inline-block text-left" onClick={handleClick} >
      <div>
        <button
          onClick={handleClick}
          type="button"
          id="menu-button"
          aria-expanded="true"
          aria-haspopup="true"
        >
          <div>
            <section className="inline-flex gap-1 items-center text-left cursor-pointer">
              <h3 className="text-xs text-[#797979]">Welcome</h3>
              <svg class="-mr-1 ml-2 h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
                <path fill-rule="evenodd" d="M5.23 7.21a.75.75 0 011.06.02L10 11.168l3.71-3.938a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z" clip-rule="evenodd" />
              </svg>
            </section>
          </div>
        </button>
      </div>

      {showOptions ? (
        <div class=" absolute right-0 z-10 mt-2 w-56 origin-top-right rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none fixed overflow-y-auto z-[1000]" role="menu" aria-orientation="vertical" aria-labelledby="menu-button" tabindex="-1">
          <div class="py-1" role="none">
            <div className="items-center">
              <div className="text-gray-700 block px-4 py-2 text-sm hover:bg-gray-100">
                <a href="">Info 1</a>
              </div>
            </div>

            <div className="items-center">
              <div className="text-gray-700 block px-4 py-2 text-sm hover:bg-gray-100">
                <a href="">Info 2</a>
              </div>
            </div>

            <div className="items-center">
              <div className="text-gray-700 block px-4 py-2 text-sm hover:bg-gray-100">
                <a href="">Info 3</a>
              </div>
            </div>
          </div>
        </div>
      ) : null}
    </div>

  );
};

export default App;

`

Clicking to close the Dropdown menu via the button above has been successful and I hope that I can also close it by clicking on a button other than a web page

1 Answers1

0

Bro, you can write setShowOptions(!showOptions) line as setShowOptions(current=>!current