Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ button({ w: 350 });
button({ w: "w-full" });
```

## Merging External `className` Safely (wcn)

WindCtrl resolves Tailwind class conflicts **inside** `windctrl()` using `tailwind-merge`.
However, in real applications you often need to merge **additional `className` values** at the component boundary.

A simple string concat can reintroduce conflicts:

```tsx
// ⚠️ Can cause subtle Tailwind conflicts (e.g. p-2 vs p-4)
className={`${result.className} ${className}`}
```

WindCtrl exports a small helper for this use case:

```tsx
import { wcn } from "windctrl";

// ✅ Conflict-safe merge
className={wcn(result.className, className)}
```

`wcn()` is equivalent to `twMerge(clsx(...))` and matches WindCtrl’s internal conflict resolution behavior.
This keeps the “last one wins” behavior consistent across both generated and user-supplied classes.

## Core Concepts

### Variants
Expand Down
4 changes: 2 additions & 2 deletions examples/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { windctrl, dynamic as d } from "../src/index";
import { windctrl, dynamic as d, wcn } from "../src/index";
import type { ComponentPropsWithoutRef, ElementType } from "react";

const button = windctrl({
Expand Down Expand Up @@ -71,7 +71,7 @@ export function Button<T extends ElementType = "button">({

return (
<Component
className={`${buttonClassName} ${className || ""}`}
className={wcn(buttonClassName, className)}
style={{ ...buttonStyle, ...style }}
{...props}
>
Expand Down
16 changes: 15 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
import { describe, it, expect } from "vitest";
import { windctrl, wc, dynamic as d } from "./";
import { windctrl, wc, dynamic as d, wcn } from "./";

describe("wc", () => {
it("should be the same as windctrl", () => {
expect(wc).toBe(windctrl);
});
});

describe("wcn", () => {
it("should merge class names with clsx behavior", () => {
expect(wcn("a", false && "b", null as any, undefined, "c")).toBe("a c");
});

it("should resolve Tailwind conflicts using tailwind-merge (last one wins)", () => {
expect(wcn("p-2", "p-4")).toBe("p-4");
});

it("should handle arrays and objects like clsx", () => {
expect(wcn(["a", ["b"]], { c: true, d: false })).toBe("a b c");
});
});

describe("windctrl", () => {
describe("Base classes", () => {
it("should apply base classes when provided", () => {
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,7 @@ export function windctrl<
}

export const wc = windctrl;

export function wcn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}