The max-h in headlessui ListboxOptions is not working as expected

I tried creating a listbox using headlessui. I simply copied the code from HeadlessUI Listbox and just increased the people count and added max-h-40 and overflow-y-auto to ListboxOptions element.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import {
Listbox,
ListboxButton,
ListboxOption,
ListboxOptions,
} from '@headlessui/react';
import { CheckIcon, ChevronDownIcon } from '@heroicons/react/20/solid';
import clsx from 'clsx';
import { useState } from 'react';
const people = [
{ id: 1, name: 'Tom Cook' },
{ id: 2, name: 'Wade Cooper' },
{ id: 3, name: 'Tanya Fox' },
{ id: 4, name: 'Arlene Mccoy' },
{ id: 5, name: 'Devon Webb' },
{ id: 11, name: 'Tom Cook' },
{ id: 12, name: 'Wade Cooper' },
{ id: 13, name: 'Tanya Fox' },
{ id: 14, name: 'Arlene Mccoy' },
{ id: 15, name: 'Devon Webb' },
];
export function App() {
const [selected, setSelected] = useState(people[1]);
return (
<div className='mx-auto h-screen w-52 pt-20'>
<Listbox value={selected} onChange={setSelected}>
<ListboxButton
className={clsx(
'relative block w-full rounded-lg bg-white/5 py-1.5 pr-8 pl-3 text-left text-sm/6 text-white',
'focus:outline-none data-[focus]:outline-2 data-[focus]:-outline-offset-2 data-[focus]:outline-white/25'
)}
>
{selected.name}
<ChevronDownIcon
className='group pointer-events-none absolute top-2.5 right-2.5 size-4 fill-white/60'
aria-hidden='true'
/>
</ListboxButton>
<ListboxOptions
anchor='bottom'
transition
className={clsx(
'w-[var(--button-width)] max-h-40 overflow-y-auto rounded-xl border border-white/5 bg-white/5 p-1 [--anchor-gap:var(--spacing-1)] focus:outline-none',
'transition duration-100 ease-in data-[leave]:data-[closed]:opacity-0'
)}
>
{people.map(person => (
<ListboxOption
key={person.name}
value={person}
className='group flex cursor-default items-center gap-2 rounded-lg py-1.5 px-3 select-none data-[focus]:bg-white/10'
>
<CheckIcon className='invisible size-4 fill-white group-data-[selected]:visible' />
<div className='text-sm/6 text-white'>{person.name}</div>
</ListboxOption>
))}
</ListboxOptions>
</Listbox>
</div>
);
}
</code>
<code>import { Listbox, ListboxButton, ListboxOption, ListboxOptions, } from '@headlessui/react'; import { CheckIcon, ChevronDownIcon } from '@heroicons/react/20/solid'; import clsx from 'clsx'; import { useState } from 'react'; const people = [ { id: 1, name: 'Tom Cook' }, { id: 2, name: 'Wade Cooper' }, { id: 3, name: 'Tanya Fox' }, { id: 4, name: 'Arlene Mccoy' }, { id: 5, name: 'Devon Webb' }, { id: 11, name: 'Tom Cook' }, { id: 12, name: 'Wade Cooper' }, { id: 13, name: 'Tanya Fox' }, { id: 14, name: 'Arlene Mccoy' }, { id: 15, name: 'Devon Webb' }, ]; export function App() { const [selected, setSelected] = useState(people[1]); return ( <div className='mx-auto h-screen w-52 pt-20'> <Listbox value={selected} onChange={setSelected}> <ListboxButton className={clsx( 'relative block w-full rounded-lg bg-white/5 py-1.5 pr-8 pl-3 text-left text-sm/6 text-white', 'focus:outline-none data-[focus]:outline-2 data-[focus]:-outline-offset-2 data-[focus]:outline-white/25' )} > {selected.name} <ChevronDownIcon className='group pointer-events-none absolute top-2.5 right-2.5 size-4 fill-white/60' aria-hidden='true' /> </ListboxButton> <ListboxOptions anchor='bottom' transition className={clsx( 'w-[var(--button-width)] max-h-40 overflow-y-auto rounded-xl border border-white/5 bg-white/5 p-1 [--anchor-gap:var(--spacing-1)] focus:outline-none', 'transition duration-100 ease-in data-[leave]:data-[closed]:opacity-0' )} > {people.map(person => ( <ListboxOption key={person.name} value={person} className='group flex cursor-default items-center gap-2 rounded-lg py-1.5 px-3 select-none data-[focus]:bg-white/10' > <CheckIcon className='invisible size-4 fill-white group-data-[selected]:visible' /> <div className='text-sm/6 text-white'>{person.name}</div> </ListboxOption> ))} </ListboxOptions> </Listbox> </div> ); } </code>
import {
  Listbox,
  ListboxButton,
  ListboxOption,
  ListboxOptions,
} from '@headlessui/react';
import { CheckIcon, ChevronDownIcon } from '@heroicons/react/20/solid';
import clsx from 'clsx';
import { useState } from 'react';

const people = [
  { id: 1, name: 'Tom Cook' },
  { id: 2, name: 'Wade Cooper' },
  { id: 3, name: 'Tanya Fox' },
  { id: 4, name: 'Arlene Mccoy' },
  { id: 5, name: 'Devon Webb' },
  { id: 11, name: 'Tom Cook' },
  { id: 12, name: 'Wade Cooper' },
  { id: 13, name: 'Tanya Fox' },
  { id: 14, name: 'Arlene Mccoy' },
  { id: 15, name: 'Devon Webb' },
];

export function App() {
  const [selected, setSelected] = useState(people[1]);

  return (
    <div className='mx-auto h-screen w-52 pt-20'>
      <Listbox value={selected} onChange={setSelected}>
        <ListboxButton
          className={clsx(
            'relative block w-full rounded-lg bg-white/5 py-1.5 pr-8 pl-3 text-left text-sm/6 text-white',
            'focus:outline-none data-[focus]:outline-2 data-[focus]:-outline-offset-2 data-[focus]:outline-white/25'
          )}
        >
          {selected.name}
          <ChevronDownIcon
            className='group pointer-events-none absolute top-2.5 right-2.5 size-4 fill-white/60'
            aria-hidden='true'
          />
        </ListboxButton>
        <ListboxOptions
          anchor='bottom'
          transition
          className={clsx(
            'w-[var(--button-width)] max-h-40 overflow-y-auto rounded-xl border border-white/5 bg-white/5 p-1 [--anchor-gap:var(--spacing-1)] focus:outline-none',
            'transition duration-100 ease-in data-[leave]:data-[closed]:opacity-0'
          )}
        >
          {people.map(person => (
            <ListboxOption
              key={person.name}
              value={person}
              className='group flex cursor-default items-center gap-2 rounded-lg py-1.5 px-3 select-none data-[focus]:bg-white/10'
            >
              <CheckIcon className='invisible size-4 fill-white group-data-[selected]:visible' />
              <div className='text-sm/6 text-white'>{person.name}</div>
            </ListboxOption>
          ))}
        </ListboxOptions>
      </Listbox>
    </div>
  );
}

The max height is not being set in the listboxoptions. Output in image below
result

An alternative method that worked was instead of max-h-40, I used h-40. The result was as expected. However, this creates another issue: when there are fewer elements, there is extra space. I decreased the people count and now there is extra space

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const people = [
{ id: 1, name: 'Tom Cook' },
{ id: 2, name: 'Wade Cooper' },
];
</code>
<code>const people = [ { id: 1, name: 'Tom Cook' }, { id: 2, name: 'Wade Cooper' }, ]; </code>
const people = [
  { id: 1, name: 'Tom Cook' },
  { id: 2, name: 'Wade Cooper' },
];
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><ListboxOptions
anchor='bottom'
transition
className={clsx(
'w-[var(--button-width)] h-40 overflow-y-auto rounded-xl border border-white/5 bg-white/5 p-1 [--anchor-gap:var(--spacing-1)] focus:outline-none',
'transition duration-100 ease-in data-[leave]:data-[closed]:opacity-0'
)}
>
</code>
<code><ListboxOptions anchor='bottom' transition className={clsx( 'w-[var(--button-width)] h-40 overflow-y-auto rounded-xl border border-white/5 bg-white/5 p-1 [--anchor-gap:var(--spacing-1)] focus:outline-none', 'transition duration-100 ease-in data-[leave]:data-[closed]:opacity-0' )} > </code>
<ListboxOptions
          anchor='bottom'
          transition
          className={clsx(
            'w-[var(--button-width)] h-40 overflow-y-auto rounded-xl border border-white/5 bg-white/5 p-1 [--anchor-gap:var(--spacing-1)] focus:outline-none',
            'transition duration-100 ease-in data-[leave]:data-[closed]:opacity-0'
          )}
        >

Please find the output of the above case below
result

what I expect to happen is if there are fewer options, it takes only enough space as needed and if there is overflow, there must be a max height and overflowing data must be scrollable.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật