ui: ScrollArea doesn't work in Dialog

When using ScrollArea inside Dialog gesture scrolling doesn’t work only way to navigate is by grabbing the scroll indicator

https://github.com/shadcn/ui/assets/62795688/ea1abc45-19f7-4969-812e-5b376e1a3f9a

To reproduce this bug index.ts

import { Button } from "@/components/ui/button"
import { ComboBox } from "@/components/ui/combo-box"
import { Dialog, DialogContent, DialogTrigger } from "@/components/ui/dialog"

const Demo = () => {
    const languages = [
        { label: "English", value: "en" },
        { label: "French", value: "fr" },
        { label: "German", value: "de" },
        { label: "Spanish", value: "es" },
        { label: "Portuguese", value: "pt" },
        { label: "Russian", value: "ru" },
        { label: "Japanese", value: "ja" },
        { label: "Korean", value: "ko" },
        { label: "Chinese", value: "zh" },
        { label: "Italian", value: "it" },
        { label: "Dutch", value: "nl" },
        { label: "Swedish", value: "sv" },
        { label: "Greek", value: "el" },
        { label: "Czech", value: "cs" },
        { label: "Polish", value: "pl" },
        { label: "Hungarian", value: "hu" },
        { label: "Turkish", value: "tr" },
        { label: "Arabic", value: "ar" },
        { label: "Hebrew", value: "he" },
        { label: "Hindi", value: "hi" }
    ] as const;
    
  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button variant="outline">Test</Button>
      </DialogTrigger>
      <DialogContent className="sm:max-w-[400px] px-0">
        <ComboBox
          options={languages.map((country) => {
            return {
              value: country.value,
              label: `${country.label}`,
            }
          })}
          placeholder={"Search Country"}
          defaultValue={"en"}
          onValueChange={() => {}}
        />
      </DialogContent>
    </Dialog>
  )
}

export default Demo

ComboBox.ts


import { cn } from "../../lib/utils"
import { Button } from "./button"
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
} from "./command"
import { Popover, PopoverContent, PopoverTrigger } from "./popover"
import { ScrollArea } from "./scroll-area"
import { Check, ChevronsUpDown } from "lucide-react"
import * as React from "react"

interface Item {
  value: string
  label: string
}

interface ComboBoxProps {
  options: Item[]
  defaultValue?: string
  placeholder: string
  className?: string
  onValueChange: (value: string) => void
}

export const ComboBox: React.FC<ComboBoxProps> = ({
  options,
  defaultValue = "",
  placeholder,
  className,
  onValueChange,
}) => {
  const [open, setOpen] = React.useState(false)

  const handleSelect = (currentValue: string) => {
    setOpen(false)
    onValueChange(currentValue)
  }

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant="outline"
          role="ComboBox"
          aria-expanded={open}
          className={cn("w-[350px] justify-between", className)}
        >
          {defaultValue
            ? options.find((option) => option.value === defaultValue)?.label
            : placeholder}
          <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent className="w-[350px] p-0">
        <Command>
          <CommandInput
            placeholder={`Search ${placeholder.toLowerCase()}...`}
          />
          <ScrollArea className="h-[200px]">
            <CommandEmpty>No {placeholder.toLowerCase()} found.</CommandEmpty>
            <CommandGroup>
              {options.map((option) => (
                <CommandItem
                  key={option.value}
                  onSelect={() => handleSelect(option.value)}
                >
                  <Check
                    className={cn(
                      "mr-2 h-4 w-4",
                      defaultValue === option.value
                        ? "opacity-100"
                        : "opacity-0"
                    )}
                  />
                  {option.label}
                </CommandItem>
              ))}
            </CommandGroup>
          </ScrollArea>
        </Command>
      </PopoverContent>
    </Popover>
  )
}

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 25 (1 by maintainers)

Most upvoted comments

Set modal={true} in popover like this <Popover open={open} modal={true}> ✔ Thanks 😎

You can make a couple of things:

  1. Not render the Portal component in popover (Maybe you have z-index issues)
  2. Set modal={false} on Dialog (The overlay will no be rendered)
  3. Wrap Dialog.Content with Dialog.Overlay (You can not set modal={false} because the overlay will not be rendered)

could you post the working code

I had this issue with the DropdownMenu component. In the end I managed to get this working by simply setting max-h- and overflow-scroll as tailwind classes on the DropdownMenuContent element, also tried it with the PopOverContent and it works, so i would assume this would also work on dialog and commandList

<DropdownMenu>
    <DropdownMenuTrigger asChild>
        <Button variant="outline" className="px-4">
            {itemId > 0 ? 'Selected: ' + itemId : 'Select Range Item'}
        </Button>
    </DropdownMenuTrigger>
    <DropdownMenuContent className="max-h-80 overflow-scroll">
        {rangeItems.map((item) => (
             <DropdownMenuItem
             className={'text-sm' + (item.id == itemId ? ' bg-gray-200' : '')}
             onClick={() => {
                 setItemId(item.id);
             }}
             >
                {item.id + ' | ' + item.allocation}
              </DropdownMenuItem>
            ))}
    </DropdownMenuContent>
</DropdownMenu>

I was able to get the ScrollArea to work with the Dialog by setting a max height to the DialogContent component and then using the ScrollArea component nested within.

<Dialog
      open={isOpen}
      onOpenChange={setIsOpen}
    >
      <DialogContent className='h-full max-h-[96%] p-4'>
        <ScrollArea className='p-4'>
          <DialogHeader>
            <DialogTitle asChild>
              <h2>{title}</h2>
            </DialogTitle>
            <DialogDescription>{description}</DialogDescription>
          </DialogHeader>

          {children}

          <DialogFooter className=''>
            <div className='flex justify-center gap-x-2'>
              <DialogClose asChild>
                <Button
                  variant='outline'
                  className='w-full'
                >
                  Cancel
                </Button>
              </DialogClose>
              <Button
                className='w-full'
              >
                confirm
              </Button>
            </div>
          </DialogFooter>
        </ScrollArea>
      </DialogContent>
    </Dialog>

Isn’t there a straightforward way to show a scroll bar on a Dialog if it contains too much content or is too big for the screen to fit?

PS: I am not using popover just pure input fields in the dialog

Set modal={true} in popover like this <Popover open={open} modal={true}> ✔ Thanks 😎

Legend!