-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update name to clx * rename to cls * Add import
- Loading branch information
Willy Brauner
authored
Sep 11, 2023
1 parent
20fd707
commit 9615f2a
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Returns a string with all the classes passed as arguments | ||
* Useful to compose classes in jsx | ||
* | ||
* @param classes: List of classes | ||
* @returns string | ||
* @example | ||
* | ||
* cls("a", "b") // "a b" | ||
* cls(false && "a", "b") // "b" | ||
* | ||
*/ | ||
export function cls(...classes: (boolean | string)[]): string { | ||
return classes?.filter(Boolean).join(" ") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, it, expect } from "vitest" | ||
import { cls } from "../../src/dom/cls" | ||
|
||
describe("cls", () => { | ||
it("should compose cls list", () => { | ||
// simple | ||
expect(cls("a", "b")).toBe("a b") | ||
|
||
// with boolean | ||
expect(cls(false && "a", "b")).toBe("b") | ||
expect(cls(true && "foo", true && "bar")).toBe("foo bar") | ||
expect(cls(false && "foo", true && "bar")).toBe("bar") | ||
|
||
// ternary operator | ||
expect(cls(false ? "foo" : "voo", true && "bar")).toBe("voo bar") | ||
|
||
// return empty string if no cls | ||
expect(cls(false)).toBe("") | ||
expect(cls(false && "foo")).toBe("") | ||
expect(cls(undefined)).toBe("") | ||
expect(cls(null)).toBe("") | ||
}) | ||
}) |