-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
classnames.js
58 lines (56 loc) · 1.57 KB
/
classnames.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* eslint-disable no-continue */
/* eslint-disable no-plusplus */
/* eslint-disable import/prefer-default-export */
/* eslint-disable @typescript-eslint/no-unused-expressions */
/* eslint-disable no-restricted-syntax */
/** Copy pasted from
* https://github.com/emotion-js/emotion/blob/23f43ab9f24d44219b0b007a00f4ac681fe8712e/packages/react/src/class-names.js#L17-L63
* */
export const classnames = (args) => {
const len = args.length;
let i = 0;
let cls = '';
for (; i < len; i++) {
const arg = args[i];
if (arg == null) {
continue;
}
let toAdd;
switch (typeof arg) {
case 'boolean':
break;
case 'object': {
if (Array.isArray(arg)) {
toAdd = classnames(arg);
} else {
if (
process.env.NODE_ENV !== 'production' &&
arg.styles !== undefined &&
arg.name !== undefined
) {
console.error(
'You have passed styles created with `css` from `@emotion/react` package to the `cx`.\n' +
'`cx` is meant to compose class names (strings) so you should convert those styles to a class name by passing them to the `css` received from <ClassNames/> component.',
);
}
toAdd = '';
for (const k in arg) {
if (arg[k] && k) {
toAdd && (toAdd += ' ');
toAdd += k;
}
}
}
break;
}
default: {
toAdd = arg;
}
}
if (toAdd) {
cls && (cls += ' ');
cls += toAdd;
}
}
return cls;
};