通过 React 组件而不是按钮触发基数对话框(或 shadcn 对话框)

问题描述 投票:0回答:1

我正在使用 typescript 和 tailwind 构建一个 nextjs 应用程序,还使用 shadcn 组件。

我正在尝试创建一种行为,并且在大多数情况下我能够做到,但前端不会没有水合错误。

我想要创建的行为:

  • 我有一个包含 Markdown 文本编辑器的自定义组件。
  • 我将该组件放在Dialog中,作为触发器,这样当用户单击文本编辑器上的任意位置时,它就会打开一个弹出窗口。

我得到的错误:

`未处理的运行时错误 错误:水合失败,因为初始 UI 与服务器上呈现的内容不匹配。

警告:期望服务器 HTML 包含 .

中的匹配项

在此处查看更多信息:https://nextjs.org/docs/messages/react-Hydration-error`

Unhandled Runtime Error Error: There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.

我知道为什么会发生这种情况,但不知道如何解决。基数 DialogTrigger 的预期输入是一个按钮。 shadcn DialogTrigger 的预期输入只是纯文本(然后成为按钮上的文本)。我正在将整个组件传递给它。 shadcn 对话框的实现方式似乎应该允许它,但它并没有完全起作用。

我如何将我的编辑器传递到 shadcn 对话框:

<Dialog>
            <DialogTrigger >
              <LLEditor/>        
            </DialogTrigger>
            <DialogContent>
              <DialogHeader>
                <DialogTitle>What's top of mind for you?</DialogTitle>
                <DialogDescription>
                  <LLEditor/>
                </DialogDescription>
              </DialogHeader>
            </DialogContent>
          </Dialog>

shadcn对话框是如何实现的:

"use client"

import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"

import { cn } from "@/lib/utils"

const Dialog = DialogPrimitive.Root

const DialogTrigger = DialogPrimitive.Trigger

const DialogPortal = DialogPrimitive.Portal

const DialogClose = DialogPrimitive.Close

const DialogOverlay = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Overlay>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Overlay
    ref={ref}
    className={cn(
      "fixed  inset-0 z-50 bg-black/80  data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
      className
    )}
    {...props}
  />
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName

const DialogContent = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
  <DialogPortal>
    <DialogOverlay />
    <DialogPrimitive.Content
      ref={ref}
      className={cn(
        "fixed left-[50%] top-[50%] z-50 grid w-full max-w-[60%] translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
        className
      )}
      {...props}
    >
      {children}
      <DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
        <X className="h-4 w-4" />
        <span className="sr-only">Close</span>
      </DialogPrimitive.Close>
    </DialogPrimitive.Content>
  </DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName

const DialogHeader = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      "flex flex-col space-y-1.5 text-center sm:text-left",
      className
    )}
    {...props}
  />
)
DialogHeader.displayName = "DialogHeader"

const DialogFooter = ({
  className,
  ...props
}: React.HTMLAttributes<HTMLDivElement>) => (
  <div
    className={cn(
      "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
      className
    )}
    {...props}
  />
)
DialogFooter.displayName = "DialogFooter"

const DialogTitle = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Title>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Title
    ref={ref}
    className={cn(
      "text-lg font-semibold leading-none tracking-tight",
      className
    )}
    {...props}
  />
))
DialogTitle.displayName = DialogPrimitive.Title.displayName

const DialogDescription = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Description>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
  <DialogPrimitive.Description
    ref={ref}
    className={cn("text-sm text-muted-foreground", className)}
    {...props}
  />
))
DialogDescription.displayName = DialogPrimitive.Description.displayName

export {
  Dialog,
  DialogPortal,
  DialogOverlay,
  DialogClose,
  DialogTrigger,
  DialogContent,
  DialogHeader,
  DialogFooter,
  DialogTitle,
  DialogDescription,
}

我怎样才能实现我想要的行为?

非常感谢你

reactjs next.js dialog shadcnui radix-ui
1个回答
0
投票

我建议您管理对话框的打开状态,例如。反冲状态,反应状态。所以你不一定需要使用DialogTrigger来打开对话框,并且对话框可以放置在任何地方,而不是嵌套在编辑器中。

© www.soinside.com 2019 - 2024. All rights reserved.