Complete the code to create a basic Headless UI <Menu> component with Tailwind CSS.
<Menu> <Menu.Button className="px-4 py-2 bg-blue-600 text-white rounded">Options</Menu.Button> <Menu.Items className="mt-2 w-48 bg-white shadow-lg rounded"> <Menu.Item> {({ active }) => ( <button className={`${active ? '[1]' : ''} block w-full text-left px-4 py-2`}>Account</button> )} </Menu.Item> </Menu.Items> </Menu>
The active state in Headless UI Menu.Item lets you style the item when hovered or focused. Using bg-blue-500 highlights the item with a blue background on active.
Complete the code to add keyboard accessibility to the Headless UI <Disclosure> component.
<Disclosure> <Disclosure.Button className="px-4 py-2 bg-gray-700 text-white rounded"> [1] </Disclosure.Button> <Disclosure.Panel className="p-4 bg-gray-100"> This is the hidden content revealed by the button. </Disclosure.Panel> </Disclosure>
Including an icon like <ChevronDownIcon /> inside the Disclosure.Button helps users understand the toggle action visually and supports keyboard navigation.
Fix the error in the Headless UI component by completing the missing prop.
<Tab.Group [1]> <Tab.List className="flex space-x-1 bg-blue-900/20 p-1 rounded"> <Tab className={({ selected }) => selected ? 'bg-white text-blue-700' : 'text-blue-100'}>Tab 1</Tab> <Tab className={({ selected }) => selected ? 'bg-white text-blue-700' : 'text-blue-100'}>Tab 2</Tab> </Tab.List> <Tab.Panels> <Tab.Panel className="p-4 bg-white">Content 1</Tab.Panel> <Tab.Panel className="p-4 bg-white">Content 2</Tab.Panel> </Tab.Panels> </Tab.Group>
selectedIndex which requires controlled state.initialIndex or index.The defaultIndex prop sets the initially selected tab in Headless UI's Tab.Group. This fixes the error by defining which tab is active on load.
Fill both blanks to create a Headless UI <Listbox> with Tailwind styling and accessibility.
<Listbox value={selected} onChange={setSelected}>
<Listbox.Button className="relative w-48 cursor-default rounded bg-white py-2 pl-3 pr-10 text-left shadow-md">
[1]
</Listbox.Button>
<Listbox.Options className="absolute mt-1 max-h-60 w-48 overflow-auto rounded bg-white py-1 text-base shadow-lg">
[2]
</Listbox.Options>
</Listbox>value or label inconsistently.The Listbox.Button shows the selected option's name with {selected.name}. The Listbox.Options renders all options by mapping over options and creating Listbox.Option elements.
Fill all three blanks to build a Headless UI <Popover> with accessible button, panel, and transition.
<Popover className="relative"> <Popover.Button className="inline-flex justify-center rounded-md bg-indigo-600 px-4 py-2 text-white"> [1] </Popover.Button> <Transition enter="transition duration-200 ease-out" enterFrom="opacity-0 scale-95" enterTo="opacity-100 scale-100" leave="transition duration-150 ease-in" leaveFrom="opacity-100 scale-100" leaveTo="opacity-0 scale-95" > <Popover.Panel className="absolute z-10 mt-3 w-screen max-w-sm rounded bg-white p-4 shadow-lg"> [2] [3] </Popover.Panel> </Transition> </Popover>
The Popover.Button needs a label like "Open Menu". The Popover.Panel contains the content paragraph and a close button for accessibility and keyboard control.