Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cls #15

Merged
merged 3 commits into from
Sep 11, 2023
Merged

cls #15

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
15 changes: 15 additions & 0 deletions src/dom/cls.ts
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(" ")
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export { shuffleArray } from "./array/shuffleArray"
// dom
export { getCssVariable } from "./dom/getCssVariable"
export { cls } from "./dom/cls"
// envs
export * from "./envs/envs"
// maths
Expand Down
23 changes: 23 additions & 0 deletions tests/dom/cls.test.ts
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("")
})
})
Loading