Challenge - 5 Problems
Conditional Classes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Understanding clsx usage for conditional classes
What will be the value of
className after running this code?import clsx from 'clsx';
const isActive = true;
const isDisabled = false;
const className = clsx('btn', { 'btn-active': isActive, 'btn-disabled': isDisabled });
Tailwind
import clsx from 'clsx'; const isActive = true; const isDisabled = false; const className = clsx('btn', { 'btn-active': isActive, 'btn-disabled': isDisabled });
Attempts:
2 left
💡 Hint
clsx adds class names only if their condition is true.
✗ Incorrect
clsx combines class names. It adds 'btn-active' because isActive is true. It skips 'btn-disabled' because isDisabled is false. So the result is 'btn btn-active'.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in clsx usage
Which option contains a syntax error when using clsx to conditionally add classes?
Code snippet:
Code snippet:
clsx('card', { 'card-highlighted': isHighlighted, 'card-error' isError })Attempts:
2 left
💡 Hint
Check the object syntax for key-value pairs.
✗ Incorrect
Option B misses a colon between 'card-error' and isError, causing a syntax error. The others use correct syntax.
❓ rendering
advanced2:00remaining
What is the rendered class string after twMerge with conflicting Tailwind classes?
Given this code:
What is the value of
import { twMerge } from 'tailwind-merge';
const base = 'text-sm text-red-500';
const conditional = 'text-lg text-blue-500';
const merged = twMerge(base, conditional);
What is the value of
merged?Attempts:
2 left
💡 Hint
twMerge removes conflicting Tailwind classes, keeping the last one.
✗ Incorrect
twMerge removes earlier conflicting font size and color classes. It keeps 'text-lg' and 'text-blue-500' from the second string.
❓ selector
advanced2:00remaining
Which option correctly combines clsx and twMerge for conditional Tailwind classes?
You want to conditionally add
Which code correctly does this?
bg-red-500 if isError is true, and bg-green-500 if false, ensuring no conflicting classes remain.Which code correctly does this?
Attempts:
2 left
💡 Hint
twMerge should be called last to resolve conflicts.
✗ Incorrect
Option A calls clsx first to build the class string, then twMerge to remove conflicts. Others misuse the order or types.
❓ accessibility
expert3:00remaining
How to ensure accessible conditional styling with clsx and twMerge?
You have a button that changes background color based on
isDisabled. Which approach best ensures the button remains accessible (focusable and readable) regardless of styles?Attempts:
2 left
💡 Hint
Accessibility means signaling state to assistive tech and visual cues.
✗ Incorrect
Option D uses visual cues (gray background, cursor change) and ARIA attribute to inform screen readers. Option D disables the button but misses ARIA for custom controls. Option D removes keyboard focus which harms accessibility.