mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
26 lines
912 B
JavaScript
26 lines
912 B
JavaScript
import { isPlainObject, isString } from "../predicate/predicate.js";
|
|
//#region src/style/cn.ts
|
|
/**
|
|
* A (very basic) utility to merge class names and make them a little easier to read.
|
|
* Aims to replicate the API of popular libraries like `clsx` and `classnames` but with a much simpler implementation.
|
|
* This is not intended to be a full replacement for those libraries, but it should be sufficient for our use case.
|
|
* It also allows us to avoid adding an additional dependency to our packages.
|
|
*
|
|
* @example
|
|
* ```ts
|
|
* cn('foo', { bar: true, baz: false }, 'qux');
|
|
* // => 'foo bar qux'
|
|
* ```
|
|
*/
|
|
function cn(...classes) {
|
|
const result = [];
|
|
for (const value of classes.flat()) if (isString(value) && value) result.push(value);
|
|
else if (isPlainObject(value)) {
|
|
for (const key in value) if (value[key]) result.push(key);
|
|
}
|
|
return result.join(" ");
|
|
}
|
|
//#endregion
|
|
export { cn };
|
|
|
|
//# sourceMappingURL=cn.js.map
|