I_A
I_A

Reputation: 341

Why Is My React + Tailwind Hamburger Menu Not Closing?

I am building a hamburger menu with React and TailwindCSS. On clicking the 'X' button, I am changing the state, isOpen to false. The class of the div that wraps the menu reacts to the state change, yet the menu does not slide out. What am I missing here?

Sidebar.js:

const Sidebar = () => {
  const [isOpen, toggleSidebar] = React.useState(true);
  return (
    <div className="h-screen flex">
      <div
        className={`fixed z-30 inset-y-0 left-0 w-64 px-8 py-4 bg-gray-100 border-r overflow-auto lg:static  lg:inset-auto lg:translate-x-0 ${
          isOpen
            ? "translate-x-0 ease-out transition-medium"
            : "-translate-x-full ease-in transition-medium"
        }`}
      >
        <div className="-mx-3 pl-3 pr-1 flex items-center justify-between">
          <span>
            <p className="h-10 w-10">Logo</p>
          </span>
          <button
            onClick={() => toggleSidebar(false)}
            className="text-gray-700 lg:hidden"
          >
            <svg fill="none" viewBox="0 0 24 24" className="h-6 w-6">
              <path
                d="M6 18L18 6M6 6L18 18"
                strokeLinecap="round"
                strokeLinejoin="round"
                strokeWidth="2"
                stroke="currentColor"
              />
            </svg>
          </button>
        </div>
        <nav className="mt-8">
          <h3 className="text-xs font-semibold text-gray-600 uppercase tracking-wide">
            STUFF
          </h3>
          <button className="mt-2 -ml-1 flex items-center text-sm font-medium text-gray-600">
            <svg
              className="h-5 w-5 text-gray-500"
              viewBox="0 0 24 24"
              fill="none"
            >
              <path
                d="M12 7v10m5-5H7"
                stroke="currentColor"
                strokeWidth="2"
                strokeLinecap="round"
              />
            </svg>
            <span className="ml-1">Add Stuff</span>
          </button>
        </nav>
      </div>
    </div>
  );
};

ReactDOM.render(<Sidebar />, document.getElementById("root"));

This is a codepen with my script if that is helpful:

Upvotes: 1

Views: 4758

Answers (1)

dance2die
dance2die

Reputation: 36895

According to the Tailwind documentation, you need to add, transform utility.
https://tailwindcss.com/docs/translate/#usage

Translate an element by first enabling transforms with the transform utility, then specifying the translate direction and distance using the translate-x-{amount} and translate-y-{amount} utilities.

So add transform in the class.

className={`
fixed z-30 inset-y-0 left-0 w-64 px-8 py-4 bg-gray-100 border-r 
overflow-auto lg:static  lg:inset-auto lg:translate-x-0 
šŸ‘‡
transform ${
          isOpen
            ? "translate-x-0 ease-out transition-medium"
            : "-translate-x-full ease-in transition-medium"
        }`}

Upvotes: 6

Related Questions