# cn

> Joins class names and resolves conflicting Tailwind CSS utilities.

`cn` combines class names from strings, arrays, objects and conditionals, then resolves conflicting Tailwind CSS utilities so the last one wins. It is the [`cn`](https://github.com/shadcn-ui/cn) package, a drop-in replacement for `twMerge(clsx(...))`, re-exported through a Dify UI subpath.

```tsx
import { cn } from '@langgenius/dify-ui/cn'
```

## Usage guidelines

- **Put defaults first**: when a component merges its own classes with an incoming `className`, pass the defaults before the caller's value so the caller can override them.
- **Resolve state callbacks before merging**: a `className(state)` callback is a function, not a class value. Call it with the part's state and pass the resulting string to `cn`.
- **Override within the same utility family**: `cn` resolves `px-2` against `px-3`, but not a physical utility against a logical one. `pl-*` and `ps-*` both stay in the output, so caller overrides use the same family as the defaults. See [Styling](/guides/styling.md).
- **Prefer data attributes for state**: before writing a conditional class from React state, check whether the primitive exposes a data attribute such as `data-checked` or `data-disabled` that a Tailwind variant can read.
- **Skip it for a single static string**: a literal `className` with no caller override and no conditions does not need `cn`.

Class values can be strings, numbers, bigints, booleans, nullish values, nested arrays or dictionaries. Falsy values are dropped, object keys are kept when their value is truthy, and arrays are flattened.

## Examples

### Defaults with a caller override

```tsx
import type * as React from 'react'
import { cn } from '@langgenius/dify-ui/cn'

function PanelSection({ className, ...props }: React.ComponentProps<'section'>) {
  return <section className={cn('flex flex-col gap-2 px-4 py-3', className)} {...props} />
}

// The caller's px-6 replaces px-4: "flex flex-col gap-2 py-3 px-6"
const wideSection = <PanelSection className="px-6" />
```

### Conditional classes

```tsx
<div
  className={cn(
    'rounded-lg border border-components-panel-border',
    highlighted && 'border-state-accent-solid',
  )}
/>
```

## API reference

### cn

Merge Tailwind CSS classes with clsx-style arguments (strings, arrays, objects, conditionals). Drop-in replacement for `twMerge(clsx(...))`.

```ts
cn(...inputs: ClassValue[]): string
```

**Parameters**

| Parameter   | Type           | Description |
| ----------- | -------------- | ----------- |
| `...inputs` | `ClassValue[]` | —           |

**Returns:** `string`
