0
0
Tailwindmarkup~20 mins

Conditional classes with clsx and twMerge in Tailwind - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Classes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2: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 });
A"btn"
B"btn btn-disabled"
C"btn btn-active"
D"btn btn-active btn-disabled"
Attempts:
2 left
💡 Hint
clsx adds class names only if their condition is true.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in clsx usage
Which option contains a syntax error when using clsx to conditionally add classes?

Code snippet:
clsx('card', { 'card-highlighted': isHighlighted, 'card-error' isError })
Aclsx('card', { 'card-highlighted': isHighlighted, 'card-error': isError })
Bclsx('card', { 'card-highlighted': isHighlighted, 'card-error' isError })
Cclsx('card', { 'card-highlighted': isHighlighted && true, 'card-error': isError })
Dclsx('card', ['card-highlighted', isHighlighted], ['card-error', isError])
Attempts:
2 left
💡 Hint
Check the object syntax for key-value pairs.
rendering
advanced
2:00remaining
What is the rendered class string after twMerge with conflicting Tailwind classes?
Given this code:
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?
A"text-lg text-blue-500"
B"text-lg text-red-500"
C"text-sm text-red-500"
D"text-sm text-red-500 text-lg text-blue-500"
Attempts:
2 left
💡 Hint
twMerge removes conflicting Tailwind classes, keeping the last one.
selector
advanced
2:00remaining
Which option correctly combines clsx and twMerge for conditional Tailwind classes?
You want to conditionally add bg-red-500 if isError is true, and bg-green-500 if false, ensuring no conflicting classes remain.
Which code correctly does this?
AtwMerge(clsx('p-4', isError ? 'bg-red-500' : 'bg-green-500'))
Bclsx(twMerge('p-4', isError ? 'bg-red-500' : 'bg-green-500'))
CtwMerge('p-4', clsx(isError && 'bg-red-500', !isError && 'bg-green-500'))
Dclsx('p-4', twMerge(isError ? 'bg-red-500' : 'bg-green-500'))
Attempts:
2 left
💡 Hint
twMerge should be called last to resolve conflicts.
accessibility
expert
3: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?
AOnly change background color with clsx and twMerge, no ARIA attributes needed.
BUse clsx to add 'opacity-50' when disabled and remove keyboard focus with <code>tabIndex={-1}</code>.
CDisable the button element with <code>disabled</code> attribute and add conditional classes without ARIA.
DUse clsx and twMerge to add 'bg-gray-400' and 'cursor-not-allowed' when disabled, and add <code>aria-disabled="true"</code> on the button.
Attempts:
2 left
💡 Hint
Accessibility means signaling state to assistive tech and visual cues.