我有一个我猜是一个相当常见的设置在NextJS 13/14应用程序路由.我有一个layout.tsx文件,它在屏幕顶部显示了我的粘性导航栏,用于导航目的:

import "./globals.css";
export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode;
}>) {
  return (
    <html lang="en">
      <body>
        <div className="relative">
          <!-- My navbar is here --> 
          <div className="sticky top-0 z-50 h-16 w-full bg-blue-200" />;
          <div className="px-4">
            <div className="flex-grow p-6 md:overflow-y-auto ">{children}</div>
          </div>
        </div>
      </body>
    </html>
  );
}

然后我创建了我的page.tsx文件,其中包含一个模式:

import Modal from "./Modal";

export default function Home() {
  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-4">
      <div className="mx-auto">
        <p>Hello world!</p>
        <div className="w-full h-full">
          <Modal />
        </div>
      </div>
    </main>
  );
}

以下是Modal Code:

"use client";

import { Dialog, Transition } from "@headlessui/react";
import { Fragment, useState } from "react";

export default function Modal() {
  const [isOpen, setIsOpen] = useState(false);

  function toggleOpen() {
    setIsOpen(!isOpen);
  }

  return (
    <>
      <button
        className=" border-2 rounded-lg border-gray-400"
        onClick={toggleOpen}
      >
        Open me!
      </button>
      <Transition appear show={isOpen} as={Fragment}>
        <Dialog as="div" className="relative z-10" onClose={toggleOpen}>
          <Transition.Child
            as={Fragment}
            enter="ease-out duration-300"
            enterFrom="opacity-0"
            enterTo="opacity-100"
            leave="ease-in duration-200"
            leaveFrom="opacity-100"
            leaveTo="opacity-0"
          >
            <div className="fixed inset-0 bg-black/25" />
          </Transition.Child>

          <div className="fixed inset-0 overflow-y-auto">
            <div className="flex min-h-full items-center justify-center p-4 text-center">
              <Dialog.Panel className="text-center w-full max-w-md transform overflow-hidden rounded-2xl bg-white p-6 align-middle shadow-xl transition-all">
                <Dialog.Title
                  as="h3"
                  className={`text-black text-lg font-medium leading-6`}
                >
                  {"This is a header"}
                </Dialog.Title>

                <div className="flex mt-4 text-center w-full  justify-center">
                  <div className="ml-3">
                    <button className="rounded-md" onClick={toggleOpen}>
                      Close modal
                    </button>
                  </div>
                </div>
              </Dialog.Panel>
            </div>
          </div>
        </Dialog>
      </Transition>
    </>
  );
}

这给了我们这样的东西:

example-site

当我打开模态的时候,一些更丑的东西:

example-site-open-modal

My issue is that when I open the dialog, the modal transition does NOT obscure layout.tsx (e.g. the blue header), but it obscures the rest of the page not defined by the layout component (e.g. the children of the layout). I know this is because of the way that Nextjs nests components: NextJS component nesting

因此,我想我的问题是,任何全屏覆盖都没有超过layout.tsx中的代码的顶部,这难道不是使用layout.tsx用于任何类型的导航/永久fixture 目的的严重缺点吗?如果布局总是坐在任何模态覆盖的顶部,那不总是只是非常丑陋,无论如何?

推荐答案

我认为这可以用css来解决,而不是需要对next的项目 struct 做任何古怪的事情.看起来粘性头的z索引为50,所以转换组件需要有一个更高的z索引才能显示在顶部.

此外,粘性标题的宽度问题可能是由滚动条出现/消失后禁用滚动模式打开?在这种情况下,您可以将header的宽度设置为100vw而不是100%.

Reactjs相关问答推荐

无法使用Apollo客户端GraphQL设置Next.js 14

使用Thattle和Use Effect setTimeout进行搜索有什么不同

如何避免react 使用ESLINTreact 挂钩/穷举-deps进行第一次呈现

后端关闭时如何在REACTION AXIOS拦截器中模拟数据?

Reaction Axios多部分/表单-数据数组不工作

为多租户SSO登录配置Azure AD应用程序

如何从Reaction-Arborist树获取排序数据

Mantine DatePickerInput呈现错误

Gitlab CI和VITE环境变量出现问题

在迭代状态时无法读取未定义的属性(读取 map )

如何修改 react/jsx-no-useless-fragment 规则以允许 <>{children}

使用 react pro 侧边栏展开折叠菜单

添加新行时自动打开可扩展表格(Antd 表格,ReactJS)

在 Spring Cloud Gateway 中运行的 React SPA 页面刷新出现白标错误

需要帮助在 document.queryselector 的 nextjs 页面上插入 useEffect 状态

当状态改变时如何执行一些动作,但只针对第一次更新?

为什么 React 会多次调用我的应用程序的某些函数?

单击按钮时获取react 表中每一行的属性

在 Nextjs 中使用 useEffect 随机化一个数组

当上下文中的状态发生变化时,组件不会立即重新渲染