Rest
...inputs: ClassValue[]Class strings or clsx
conditional class objects to merge. Tailwind classes
specified later in the arguments overwrite similar Tailwind classes specified earlier in the
arguments
Class string containing all applicable classes from the arguments based on the rules described above
const borderShouldBeBlue = true;
const textShouldBeRed = true;
const heightShouldBe20 = false;
const classString = cn(
'tw-bg-primary tw-h-10 tw-text-primary-foreground',
'tw-bg-secondary',
{
'tw-border-blue-500': borderShouldBeBlue,
'tw-text-red-500': textShouldBeRed,
'tw-h-20': heightShouldBe20,
},
'some-class',
);
The resulting classString
is 'tw-h-10 tw-bg-secondary tw-border-blue-500 tw-text-red-500 some-class'
'tw-bg-secondary'
, specified later, overwrote 'tw-bg-primary'
, specified earlier,
because they are Tailwind classes that affect the same css property'tw-text-red-500'
, specified later, overwrote 'tw-text-primary-foreground'
,
specified earlier, because they are Tailwind classes that affect the same css property'tw-h-20'
, specified later, did not overwrite 'tw-h-10'
, specified earlier,
because 'tw-h-20'
is part of a conditional class object and its value evaluated to false
;
therefore it was not applied'some-class'
was applied. This function is not limited only to Tailwind classes.
Tailwind and CSS class application helper function. Uses
clsx
to make it easy to apply classes conditionally using object syntax, and usestailwind-merge
to make it easy to merge/overwrite Tailwind classes in a programmer-logic-friendly way.Note:
tailwind-merge
is configured to use the prefixtw-
, so you must use the same prefix with any Tailwind classes you use with this function to successfully overwrite other Tailwind classes.platform-bible-react
is configured to usetw-
as its Tailwind prefix, so any Tailwind classes you pass intoplatform-bible-react
components will be compared using thetw-
prefix.This function was popularized by shadcn/ui. See ByteGrad's explanation video for more information.