javascript - 为什么我的 React + Tailwind 汉堡菜单没有关闭?

标签 javascript reactjs hamburger-menu tailwind-css

我正在使用 React 和 TailwindCSS 构建一个汉堡菜单。单击“X”按钮后,我将状态 isOpen 更改为 false。包装菜单的 div 类会对状态更改使用react,但菜单不会滑出。我在这里缺少什么?

侧边栏.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是我的脚本的代码笔(如果有帮助的话):

最佳答案

根据 Tailwind 文档,您需要添加 transform 实用程序。
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.

因此在类中添加transform

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"
        }`}

关于javascript - 为什么我的 React + Tailwind 汉堡菜单没有关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60888133/

相关文章:

ios - 在没有 IBOutlets 的情况下以编程方式更新布局约束 - Swift

javascript重新定义一个变量

javascript - 在 IE 9 中刷新 fb IFRAME - 不起作用

listview - React Native ListView 部分标题不在列表顶部?

javascript - 如何在 Material-UI 中覆盖卡片组件中的 "MuiPaper-elevation1"属性?

android - 将后退按钮添加到工具栏,用于打开抽屉导航的主页 fragment 以外的所有 fragment

android - Xamarin Forms - IOS - 汉堡包菜单标题颜色与状态栏不同

javascript - 阿里巴巴面试: print a sentence with min spaces

javascript - 如何在 famo.us 中加载表面的动态内容 (ajax)?

reactjs - Gatsby 没有生成正确的静态 HTML 文件