ui: [bug]: Sheet error for screen reader users

Describe the bug

When using the Sheet component I get two errors:

DialogContent requires a DialogTitle for the component to be accessible for screen reader users. If you want to hide the DialogTitle, you can wrap it with our VisuallyHidden component.

Warning: Missing Description or aria-describedby={undefined} for {DialogContent}.

Affected component/components

Sheet

How to reproduce

Using the https://ui.shadcn.com/blocks#dashboard-05 sample. Reduce width so that the Sheet shows; warnings in the console.

Codesandbox/StackBlitz link

No response

Logs

`DialogContent` requires a `DialogTitle` for the component to be accessible for screen reader users.

If you want to hide the `DialogTitle`, you can wrap it with our VisuallyHidden component.

For more information, see https://radix-ui.com/primitives/docs/components/dialog 

---

Warning: Missing `Description` or `aria-describedby={undefined}` for {DialogContent}. 
    at DescriptionWarning (webpack-internal:///(app-pages-browser)/./node_modules/@radix-ui/react-dialog/dist/index.mjs:457:11)

System Info

All browsers.

Before submitting

  • I’ve made research efforts and searched the documentation
  • I’ve searched for existing issues

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 14
  • Comments: 17

Commits related to this issue

Most upvoted comments

Also works only adding sr-only class to SheetHeader.

    if (isMobile) {
      return (
        <Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
          <SheetContent
            data-sidebar="sidebar"
            data-mobile="true"
            className="w-[--sidebar-width] bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
            style={
              {
                "--sidebar-width": SIDEBAR_WIDTH_MOBILE,
              } as React.CSSProperties
            }
            side={side}
          >
++++        <SheetHeader className="sr-only">
++++          <SheetTitle>Sidebar</SheetTitle>
++++        </SheetHeader>
            <div className="flex h-full w-full flex-col">{children}</div>
          </SheetContent>
        </Sheet>
      );
    }

This issue again becomes relevant with the recently released sidebar templates (on mobile). Slightly modified fix from @Maqed still works fine though.

Install @radix-ui/react-visually-hidden & Add this in your sidebar.tsx:

    if (isMobile) {
      return (
        <Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
          <SheetContent
            data-sidebar="sidebar"
            data-mobile="true"
            className="w-[--sidebar-width] bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
            style={
              {
                '--sidebar-width': SIDEBAR_WIDTH_MOBILE,
              } as React.CSSProperties
            }
            side={side}
          >
++++        <VisuallyHidden>
++++          <SheetHeader>
++++            <SheetTitle>Sidebar</SheetTitle>
++++            <SheetDescription>Sidebar</SheetDescription>
++++          </SheetHeader>
++++        </VisuallyHidden>
            <div className="flex h-full w-full flex-col">{children}</div>
          </SheetContent>
        </Sheet>
      )
    }

Proposed fix in Sheet component:

const SheetContent = React.forwardRef<
  React.ElementRef<typeof SheetPrimitive.Content>,
  SheetContentProps
>(({ side = "right", className, children, ...props }, ref) => (
  <SheetPortal>
    <SheetOverlay />
    <SheetPrimitive.Content
      ref={ref}
      className={cn(sheetVariants({ side }), className)}
      {...props}
    >
      <VisuallyHidden>
        <SheetTitle>Sheet Content</SheetTitle>
        <SheetDescription>
          This is a hidden description for screen readers.
        </SheetDescription>
      </VisuallyHidden>
      {children}
      <SheetPrimitive.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-secondary">
        <X className="h-4 w-4" />
        <span className="sr-only">Close</span>
      </SheetPrimitive.Close>
    </SheetPrimitive.Content>
  </SheetPortal>
));
SheetContent.displayName = SheetPrimitive.Content.displayName;

First thing first you should install visually hidden component from RadixUI as Shadcn is built on top of it.

npm install @radix-ui/react-visually-hidden

Then use it as following:

          <Sheet>
            <SheetTrigger asChild>
              {/* Trigger Logic */}
            </SheetTrigger>
            <SheetContent>
              <SheetHeader>
                <VisuallyHidden>
                  <SheetTitle>Nav Content</SheetTitle>
                </VisuallyHidden>
                <SheetDescription className="flex items-center flex-col justify-between h-[300px]">
                 {/* Descrciption Logic */}
                </SheetDescription>
              </SheetHeader>
            </SheetContent>
          </Sheet>

I hope that helps!

This issue again becomes relevant with the recently released sidebar templates (on mobile). Slightly modified fix from @Maqed still works fine though.

Install @radix-ui/react-visually-hidden & Add this in your sidebar.tsx:

    if (isMobile) {
      return (
        <Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
          <SheetContent
            data-sidebar="sidebar"
            data-mobile="true"
            className="w-[--sidebar-width] bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
            style={
              {
                '--sidebar-width': SIDEBAR_WIDTH_MOBILE,
              } as React.CSSProperties
            }
            side={side}
          >
++++        <VisuallyHidden>
++++          <SheetHeader>
++++            <SheetTitle>Sidebar</SheetTitle>
++++            <SheetDescription>Sidebar</SheetDescription>
++++          </SheetHeader>
++++        </VisuallyHidden>
            <div className="flex h-full w-full flex-col">{children}</div>
          </SheetContent>
        </Sheet>
      )
    }

It’s work

This is happening with the new Sidebar component because of the SheetContent too.

same thing happened when I used CommandDialog

the sr-only solution worked for me, but i also had to add <DialogDescription /> to avoid the warning Warning: Missing Description or aria-describedby={undefined} for {DialogContent}.

<CommandDialog
        open={open}
        onOpenChange={setOpen}
        aria-labelledby='undefined'
      >

        <DialogHeader>
          <DialogTitle className='sr-only'>Search...</DialogTitle>
          <DialogDescription />
        </DialogHeader>
...
</CommandDialog>

This happens with CommandDialog component as well, but there’s no CommandDialogTitle to fix this. It also happens in the shadcn site in the Command Dialog example (https://ui.shadcn.com/docs/components/command):

Screenshot 2024-08-10 at 18 43 47 Screenshot 2024-08-10 at 18 43 56

For the command, I found a temporary solution for my project:

import { DialogTitle, type DialogProps } from "@radix-ui/react-dialog";
....
const CommandDialog = ({ children, ...props }: CommandDialogProps) => {
  return (
    <Dialog {...props}>
      <DialogTitle></DialogTitle>
      <DialogContent className="overflow-hidden p-0">
        ....
      </DialogContent>
    </Dialog>
  );
};

Passing an empty DialogTitle worked for me and stopped the error, so I’ll use it until an official fix is released.

If you are using the Sidebar, open Sidebar definition and add

<SheetTitle></SheetTitle>

in this block of code

if (isMobile) {
      return (
        <Sheet open={openMobile} onOpenChange={setOpenMobile} {...props}>
          <SheetContent
            data-sidebar="sidebar"
            data-mobile="true"
            className="w-[--sidebar-width] bg-sidebar p-0 text-sidebar-foreground [&>button]:hidden"
            style={
              {
                "--sidebar-width": SIDEBAR_WIDTH_MOBILE,
              } as React.CSSProperties
            }
            side={side}
          >
            <SheetTitle></SheetTitle>
            <div className="flex h-full w-full flex-col">{children}</div>
          </SheetContent>
        </Sheet>
      );
    }

This will fix the issue and you don’t need to provide a title unless you want to. If that’s what you are looking for.

@AsazuTaiga that works perfectly