-
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.
- Loading branch information
Showing
4 changed files
with
101 additions
and
122 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
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 |
---|---|---|
@@ -1,49 +1,64 @@ | ||
export type ClassValue = string | undefined | null | boolean; | ||
export type ClassMap = Record<string, boolean | string | number>; | ||
export type ClassValue = string | boolean | ClassValueArray | ClassMap; | ||
export type ClassValueArray = ClassValue[]; | ||
export type ClassMap = { [key: string]: string | boolean }; | ||
|
||
export function isString(value: unknown): value is string { | ||
return typeof value === "string"; | ||
} | ||
export function classNames(...args: ClassValue[]): string { | ||
const classes: string[] = []; | ||
|
||
export function isBooleanString(value: unknown): boolean { | ||
return ["true", "1"].includes(value as string); | ||
} | ||
for (let i = 0; i < args.length; i++) { | ||
processArg(args[i], classes); | ||
} | ||
|
||
export function classNames(...args: (ClassValue | ClassMap | string | string[])[]): string { | ||
function processArg (arg: ClassValue | ClassMap | string): string[] { | ||
if (arg === undefined || arg === null || typeof arg === "boolean") { | ||
return []; | ||
} | ||
let finalClasses = ''; | ||
let isFirstClass = true; | ||
|
||
if (Array.isArray(arg)) { | ||
return arg.filter(isString); | ||
for (let i = 0; i < classes.length; i++) { | ||
// Ignore boolean 'true' | ||
if (classes[i] === 'true') { | ||
continue; | ||
} | ||
|
||
if (typeof arg === "object") { | ||
return Object.keys(arg) | ||
.map((key) => { | ||
const value = arg[key]; | ||
if (value === true) { | ||
return key; | ||
} | ||
if (isString(value) && isBooleanString(value)) { | ||
return key; | ||
} | ||
return null; | ||
}) | ||
.filter(Boolean) | ||
.filter(isString); | ||
// Trim each class | ||
const trimmedClass = classes[i].trim(); | ||
|
||
if (trimmedClass) { | ||
if (!isFirstClass) { | ||
finalClasses += ' '; | ||
} else { | ||
isFirstClass = false; | ||
} | ||
finalClasses += trimmedClass; | ||
} | ||
} | ||
|
||
if (isString(arg)) { | ||
return [arg]; | ||
return finalClasses; | ||
} | ||
|
||
function processArg(arg: ClassValue, classes: string[]): void { | ||
if (arg === undefined || arg === null || arg === false) { | ||
return; | ||
} | ||
|
||
if (Array.isArray(arg)) { | ||
for (let i = 0; i < arg.length; i++) { | ||
processArg(arg[i], classes); | ||
} | ||
return; | ||
} | ||
|
||
return []; | ||
}; | ||
if (typeof arg === 'object') { | ||
for (const key in arg) { | ||
if (Object.prototype.hasOwnProperty.call(arg, key)) { | ||
const value = arg[key]; | ||
if (value === true || value === 'true' || value === '1') { | ||
classes.push(key); | ||
} | ||
} | ||
} | ||
return; | ||
} | ||
|
||
return args | ||
.flatMap(processArg) | ||
.filter(Boolean) | ||
.join(" "); | ||
if (typeof arg === 'string' || typeof arg === 'boolean') { | ||
classes.push(arg.toString()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export {classNames, ClassValue, ClassMap} from "./classnames" | ||
export {classNames, ClassValue, ClassMap, ClassValueArray} from "./classnames" |
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