Your next page is taking shape...

Opening Your Next Page…

Loading the guide and preparing its examples for you to explore. The page will appear here automatically when it’s ready, so you can keep this view open without refreshing.

Made for Preact. Built to be yours.

Styling Utilities

cn Utility

cn combines clsx for conditional class lists with tailwind-merge to resolve conflicting Tailwind utilities. Keep class composition readable as defaults, visual state and consumer overrides meet on the same element. Import it from @kamod-ch/ui/utils.

Source · API reference · Theming · Styles


Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Installation

One Existing Dependency. cn ships with @kamod-ch/ui — no separate package or install step. Add @kamod-ch/ui once and import cn from @kamod-ch/ui/utils. If utilities do not appear in your app, check the CSS Setup Guide.

Package Root Alternative. You can also import cn from @kamod-ch/ui. Both paths provide the same helper; choose one import style for your file.

Import SetupTypeScript
import { cn } from "@kamod-ch/ui";
Install PackagesTerminal
pnpm add @kamod-ch/ui

Copy an example with @kamod-ch/ui or the matching @kamod-ch/ui/… subpath. The code block switches between those two installs. Connect The Global Theme CSS and Tailwind Source Detection before your first render.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Usage

Base, State, Consumer. Pass base classes first, then conditional values, arrays, or object maps. Put consumer class props last so callers can override defaults. clsx handles falsy values; tailwind-merge keeps the last recognized conflicting utility (for example px-4 wins over px-2).

Import SetupTypeScript
import { cn } from "@kamod-ch/ui/utils";
Usage PatternTypeScript
export function panelClasses(selected: boolean, className?: string) {
  return cn(
    "rounded-lg border bg-background p-4",
    selected && "border-primary",
    className,
  );
}

panelClasses(true, "p-6");

Component Examples & Variants

Explore cn Utility in Different Contexts, from the starting pattern to the compositions below. Begin with Basic Classes, then compare the examples that match your content, state and layout. These are working patterns to adapt, not a list of interchangeable variant values: check the API Reference before using a prop or combining behaviors. Look at the surrounding labels, supporting text and spacing as well as the component itself.

Read the Result and Its Implementation Together. Use each showcase’s Code view to inspect imports and composition, or its Prompt view to prepare a setup or adaptation brief. Try the narrow preview and local theme controls before connecting real data, and use Reset to return an example to its initial state. Keep layout changes in class utilities where appropriate; follow the Component Styles Guide for hierarchy and the Theming Guide for shared surfaces and colors. The Accessibility Notes explain what to preserve when making those changes.

Basic Classes

Keep Simple Class Composition Readable. Pass static class strings to cn() when you want to assemble a reusable styling value from named pieces. The returned string can be assigned to class, keeping the composition close to the component that uses it.

It returns a class string rather than applying styles itself, so the resulting utilities still need to be available in your Tailwind build.

src/components/cnUtilityExample.tsx7 lines
import { cn } from "@kamod-ch/ui/utils";

const surface = "rounded-lg border bg-background";
const spacing = "p-4";

export const panelClass = cn(surface, spacing, "text-sm");
// => "rounded-lg border bg-background p-4 text-sm"

Conditional Classes

Express Visual State Next to Its Condition. Use boolean expressions to include classes only when their state applies, such as selected && 'border-primary'. cn() ignores falsy inputs, allowing the condition and the visual change to remain together in one readable expression.

Keep unrelated behavior out of class composition, and inspect the final result when multiple states can be true together.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Arrays and Objects

Group Classes According to Their Purpose. Pass arrays for related fragments or object maps whose keys are included when their values are truthy. These forms let cn() collect styles from structured inputs without manually flattening every combination before rendering.

Avoid overly deep structures that obscure which styles win; a short named fragment is often clearer than a large inline map inside JSX.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Overriding Defaults

Let Callers Make Intentional Overrides. Place defaults before consumer overrides when calling cn(), for example cn('px-2', 'px-4'). Its merge step resolves recognized Tailwind conflicts so the later padding utility can replace the earlier value predictably.

Check the generated result for unusual utilities or custom class systems; merging class names cannot resolve every cascade rule defined elsewhere in your stylesheets.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Preact Component

Keep the Public Styling Hook Predictable. Expose a class prop on a reusable Preact component and pass it last to cn() after the built-in styles. The caller then has a deliberate override point without replacing the component's entire styling contract.

Forward the remaining attributes carefully, and document when a component has multiple surfaces so consumers do not assume one class changes every internal part.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

With Tailwind-Variants

Separate Supported Variants from One-Off Overrides. Generate supported variant classes first, then merge the consumer's class through cn(). A composition such as cn(variantFn(options), className) keeps the named design choices separate from a caller's local layout adjustments.

Keep variant names meaningful and avoid encoding application logic into an ever-growing collection of conditional utility strings.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width

Clsx vs cn

Choose Merging When Defaults Can Conflict. clsx joins conditional classes, while cn() also runs the result through tailwind-merge. Compare their outputs when defaults and overrides contain conflicting utilities; plain concatenation preserves both tokens rather than resolving that conflict.

Compare the output in this example before changing helpers; removing a conflicting utility can alter the result even when both inputs look valid individually.

Interactive Live Preview
Try it. Make it yours.
Setting the scene...

Interactive preview

Live Preact example Local preview theme · Responsive styles follow the preview widthFull width
API reference

Props and Data

Configure cn Utility using the contracts below. Read the documented options alongside the Source Definitions to understand which values your application supplies and which details the component owns. Start with a prop or helper, then follow its type link to inspect the declaration without leaving the page.

Props, slots and variants for this component.

API reference
Your API, from source

Type cards are extracted from this checkout’s TypeScript source at build time. Props inherited through HTMLAttributes, Omit or variant helpers remain references in those declarations; their fields are not flattened here. Match the reference to your installed version, and check a package’s exports before writing an import type. A type exported by a source file is not necessarily re-exported by @kamod-ch/ui.

Component Props

Read each option with its owning component. The
marker identifies required fields verified in that declaration. A ? means a field can be omitted; it does not promise a fallback value. Existing documented defaults remain alongside the descriptions. For callbacks, check the argument and return types before connecting your own state or services.

cn Utility

cn Utility component props and accepted values.

cn Utility documented props
Prop / typeDescription
...inputs
ClassValue[]

Class strings, arrays, conditional values and object maps accepted by clsx. Order inputs from defaults to intended overrides.

Default
—
returns
string

A class string with recognized Tailwind conflicts resolved. The function does not modify the DOM or generate CSS.

Default
merged class string

Data Type Reference

Expand a card to inspect and copy the exact declaration. The code header identifies its repository file; follow imported or inherited types in the Implementation Source when you need the complete dependency chain. Required-field summaries cover fields declared directly in that type. Source-only helpers and inferred schema outputs describe the local implementation, not additional props you can pass to every component.

Follow the implementation

This implementation does not declare a separate props or data type in its component folder. Follow the Source Reference and documented signatures above; no extra type is implied by the examples.

Accessibility

cn combines class names and has no DOM, roles, focus behavior or accessible name. Its accessibility impact comes from the styles you compose on actual controls and content.

Take the Next Useful Step

You have the pieces. Put them into one real interaction, keep its state in one place, and check the result with the content your application will actually show.

Connect the Foundation

Return to Set Up Styles and Tokens to connect this reference to a working screen. Keep global CSS in one entrypoint and import only the components that screen needs.

Build the Surrounding Screen

Find a neighboring interaction in the Component Library, or start with a complete Block. Replace sample content and destinations before refining the layout; a local class change can stay with the composition.

Make the Result Hold Up

Use shared theme roles for changes that belong everywhere. Then review the whole journey: keyboard focus, long labels, narrow screens and a clear way to recover when something fails.

Keep a link to the source and note your intentional changes. Your next edit should begin with a clear decision, not another search.