Bird
Raised Fist0
Tailwindmarkup~20 mins

Component library patterns (Headless UI) in Tailwind - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Headless UI Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main advantage of using Headless UI components?
Headless UI components provide unstyled, accessible UI primitives. What is the biggest benefit of this approach?
AThey restrict customization to maintain uniform design.
BThey come with built-in styles that cannot be changed.
CThey allow developers to fully control styling while ensuring accessibility.
DThey automatically generate CSS styles for you.
Attempts:
2 left
💡 Hint
Think about how Headless UI separates logic from style.
📝 Syntax
intermediate
1:30remaining
Which Tailwind class correctly applies a responsive grid layout?
You want a grid with 1 column on small screens and 3 columns on medium and larger screens. Which class string achieves this?
Agrid-cols-1 grid md:grid-cols-3 gap-4
Bgrid grid-cols-1 md:grid-cols-3 gap-4
Cgrid grid-cols-3 md:grid-cols-1 gap-4
Dgrid-cols-3 grid grid-cols-1 md:gap-4
Attempts:
2 left
💡 Hint
Remember Tailwind uses mobile-first approach and responsive prefixes like md:
❓ rendering
advanced
2:00remaining
What will be the visible result of this Headless UI Menu component snippet?
Consider this React code using Headless UI Menu and Tailwind CSS:
import { Menu } from '@headlessui/react';

export default function Example() {
  return (
    
      Options
      
        
          {({ active }) => (
            Account
          )}
        
        
          {({ active }) => (
            Settings
          )}
        
      
    
  );
}
ATwo buttons labeled 'Account' and 'Settings' always visible below a gray 'Options' label.
BA blue button labeled 'Options' with no dropdown menu visible or interactive.
CA white button labeled 'Options' that shows a blue dropdown with two links in gray text.
DA blue button labeled 'Options' that toggles a white dropdown with two links: 'Account' and 'Settings'. Hovering a link highlights it blue with white text.
Attempts:
2 left
💡 Hint
Headless UI Menu.Button toggles Menu.Items visibility with styling applied.
❓ selector
advanced
1:30remaining
Which Tailwind selector targets only the focused state of a Headless UI button?
You want to style a Headless UI button only when it is focused (keyboard or programmatically). Which Tailwind class applies this style correctly?
Afocus:outline-none focus:ring-2 focus:ring-blue-500
Bhover:outline-none hover:ring-2 hover:ring-blue-500
Cactive:outline-none active:ring-2 active:ring-blue-500
Dvisited:outline-none visited:ring-2 visited:ring-blue-500
Attempts:
2 left
💡 Hint
Focus styles apply when the element is focused, not hovered or active.
❓ accessibility
expert
2:00remaining
What ARIA role does Headless UI assign to its Dialog component by default?
Headless UI Dialog component manages accessibility automatically. Which ARIA role is assigned to the main dialog container?
Adialog
Balertdialog
Cregion
Dcomplementary
Attempts:
2 left
💡 Hint
Dialogs usually have a specific ARIA role to identify them as modal windows.

Practice

(1/5)
1. What is the main purpose of Headless UI in web development?
easy
A. To create animations for UI elements automatically
B. To offer pre-styled UI components ready to use
C. To replace Tailwind CSS for styling
D. To provide accessible component behavior without styling

Solution

  1. Step 1: Understand Headless UI's role

    Headless UI provides the logic and accessibility for components but no styles.
  2. Step 2: Compare with styling tools

    Styling is added separately, often with Tailwind CSS, so Headless UI focuses on behavior only.
  3. Final Answer:

    To provide accessible component behavior without styling -> Option D
  4. Quick Check:

    Headless UI = behavior only [OK]
Hint: Headless UI = logic + accessibility, no styles [OK]
Common Mistakes:
  • Thinking Headless UI provides styles
  • Confusing Headless UI with Tailwind CSS
  • Assuming it creates animations automatically
2. Which of the following is the correct way to import a Headless UI component in a React project?
easy
A. import { Menu } from '@headlessui/react';
B. import Menu from 'headless-ui';
C. import { Menu } from 'tailwindcss';
D. import Menu from '@headlessui/css';

Solution

  1. Step 1: Identify the correct package name

    The official Headless UI React package is '@headlessui/react'.
  2. Step 2: Use named import for components

    Components like Menu are imported as named imports from '@headlessui/react'.
  3. Final Answer:

    import { Menu } from '@headlessui/react'; -> Option A
  4. Quick Check:

    Correct import syntax = import { Menu } from '@headlessui/react'; [OK]
Hint: Headless UI React imports use named imports from '@headlessui/react' [OK]
Common Mistakes:
  • Using default import instead of named import
  • Importing from 'tailwindcss' instead of '@headlessui/react'
  • Using wrong package names like 'headless-ui' or '@headlessui/css'
3. Given this React code using Headless UI's Menu component, what will be the visible output when the button is clicked?
import { Menu } from '@headlessui/react';

export default function Example() {
  return (
    
      Options
      
        
          {({ active }) => (
            <a href='#' className={`${active ? 'bg-blue-500 text-white' : 'text-black'}`}>Account</a>
          )}
        
        
          {({ active }) => (
            <a href='#' className={`${active ? 'bg-blue-500 text-white' : 'text-black'}`}>Settings</a>
          )}
        
      
    
  );
}
medium
A. An error because Menu.Items is missing a wrapper element
B. A button labeled 'Options' that toggles a dropdown with 'Account' and 'Settings' links
C. A button labeled 'Options' with no dropdown appearing on click
D. Only a static list of 'Account' and 'Settings' links visible without a button

Solution

  1. Step 1: Understand Menu component behavior

    The Menu.Button toggles visibility of Menu.Items as a dropdown.
  2. Step 2: Check rendering of Menu.Items and Menu.Item

    Menu.Items contains two Menu.Item links that show with styling on hover (active).
  3. Final Answer:

    A button labeled 'Options' that toggles a dropdown with 'Account' and 'Settings' links -> Option B
  4. Quick Check:

    Menu.Button toggles dropdown = A button labeled 'Options' that toggles a dropdown with 'Account' and 'Settings' links [OK]
Hint: Menu.Button toggles Menu.Items dropdown visibility [OK]
Common Mistakes:
  • Thinking dropdown is always visible
  • Assuming Menu.Items needs extra wrapper
  • Expecting error due to JSX structure
4. What is the main issue with this Headless UI Disclosure usage in React?
import { Disclosure } from '@headlessui/react';

export default function Example() {
  return (
    <Disclosure>
      <Disclosure.Button>Toggle</Disclosure.Button>
      <div>Hidden content</div>
    </Disclosure>
  );
}
medium
A. The hidden content should be wrapped in Disclosure.Panel for proper behavior
B. Disclosure.Button must be outside Disclosure component
C. The import statement is incorrect
D. Disclosure requires a state hook to work

Solution

  1. Step 1: Check Disclosure structure

    Disclosure requires hidden content inside Disclosure.Panel for toggle functionality.
  2. Step 2: Identify missing wrapper

    The <div>Hidden content</div> should be replaced with <Disclosure.Panel>Hidden content</Disclosure.Panel>.
  3. Final Answer:

    The hidden content should be wrapped in Disclosure.Panel for proper behavior -> Option A
  4. Quick Check:

    Disclosure.Panel wraps hidden content [OK]
Hint: Hidden content must be inside Disclosure.Panel [OK]
Common Mistakes:
  • Placing hidden content directly without Disclosure.Panel
  • Moving Disclosure.Button outside Disclosure
  • Incorrect import or missing hooks
5. You want to create a custom dropdown menu using Headless UI's Menu component styled with Tailwind CSS. You want the menu items to close the menu when clicked and highlight on keyboard navigation. Which approach correctly combines Headless UI behavior with Tailwind styling?
hard
A. Use Menu.Button and style it only; menu items are plain HTML links without Headless UI components
B. Use Menu.Items without Menu.Button and add Tailwind hover classes manually
C. Use Menu.Button to toggle, wrap items in Menu.Items, and style Menu.Item using the active state with Tailwind classes
D. Use only Tailwind CSS dropdown utilities without Headless UI components

Solution

  1. Step 1: Use Headless UI components for behavior

    Menu.Button toggles menu visibility; Menu.Items contains menu options; Menu.Item handles keyboard navigation and active state.
  2. Step 2: Apply Tailwind CSS for styling active states

    Use the active render prop in Menu.Item to conditionally apply Tailwind classes for highlight on keyboard focus or hover.
  3. Step 3: Ensure menu closes on item click

    Headless UI automatically closes the menu when a Menu.Item is clicked, preserving accessibility.
  4. Final Answer:

    Use Menu.Button to toggle, wrap items in Menu.Items, and style Menu.Item using the active state with Tailwind classes -> Option C
  5. Quick Check:

    Headless UI + Tailwind styling = Use Menu.Button to toggle, wrap items in Menu.Items, and style Menu.Item using the active state with Tailwind classes [OK]
Hint: Style Menu.Item active state with Tailwind inside Headless UI Menu [OK]
Common Mistakes:
  • Styling only Menu.Button and ignoring Menu.Item active states
  • Not using Menu.Item components for menu options
  • Trying to build dropdown without Menu.Button
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard