@behivetech/get-class-name 0.0.7 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +45 -37
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -7
- package/dist/index.d.ts +23 -7
- package/dist/index.js +45 -27
- package/dist/index.js.map +1 -1
- package/package.json +7 -10
package/dist/index.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// index.ts
|
|
@@ -34,40 +24,58 @@ __export(index_exports, {
|
|
|
34
24
|
});
|
|
35
25
|
module.exports = __toCommonJS(index_exports);
|
|
36
26
|
|
|
27
|
+
// class-names.ts
|
|
28
|
+
var objectToString = Object.prototype.toString;
|
|
29
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
30
|
+
var append = (joined, next) => {
|
|
31
|
+
let result = joined;
|
|
32
|
+
if (next) {
|
|
33
|
+
result = joined ? `${joined} ${next}` : next;
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
var hasCustomToString = (value) => value.toString !== objectToString && !value.toString.toString().includes("[native code]");
|
|
38
|
+
var parseValue = (value) => {
|
|
39
|
+
let result = "";
|
|
40
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
41
|
+
result = String(value);
|
|
42
|
+
} else if (Array.isArray(value)) {
|
|
43
|
+
result = classNames(...value);
|
|
44
|
+
} else if (typeof value === "object" && value !== null) {
|
|
45
|
+
if (hasCustomToString(value)) {
|
|
46
|
+
result = value.toString();
|
|
47
|
+
} else {
|
|
48
|
+
Object.keys(value).forEach((key) => {
|
|
49
|
+
if (hasOwn.call(value, key) && value[key]) {
|
|
50
|
+
result = append(result, key);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return result;
|
|
56
|
+
};
|
|
57
|
+
var classNames = (...args) => args.reduce(
|
|
58
|
+
(joined, value) => value ? append(joined, parseValue(value)) : joined,
|
|
59
|
+
""
|
|
60
|
+
);
|
|
61
|
+
|
|
37
62
|
// get-class-name.ts
|
|
38
|
-
var import_classnames = __toESM(require("classnames"), 1);
|
|
39
63
|
var getClassName = ({
|
|
40
64
|
className,
|
|
41
65
|
modifiers = {},
|
|
42
66
|
rootClass,
|
|
43
67
|
styles = {}
|
|
44
68
|
}) => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return updatedModifiers;
|
|
56
|
-
}
|
|
57
|
-
function getChildClass(childClassName) {
|
|
58
|
-
const classKey = `${rootClass}__${childClassName}`;
|
|
59
|
-
return styles[classKey] || classKey;
|
|
60
|
-
}
|
|
61
|
-
function getRootStyles() {
|
|
62
|
-
return (0, import_classnames.default)(
|
|
63
|
-
className,
|
|
64
|
-
{
|
|
65
|
-
...getModifiers()
|
|
66
|
-
},
|
|
67
|
-
styles[rootClass] || rootClass
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
return [getRootStyles(), getChildClass];
|
|
69
|
+
const resolve = (classKey) => styles[classKey] || classKey;
|
|
70
|
+
const activeModifiers = Object.keys(modifiers).reduce((resolved, modKey) => {
|
|
71
|
+
resolved[resolve(`${rootClass}--${modKey}`)] = modifiers[modKey] ?? false;
|
|
72
|
+
return resolved;
|
|
73
|
+
}, {});
|
|
74
|
+
const getChildClass = (childClassName) => resolve(`${rootClass}__${childClassName}`);
|
|
75
|
+
return [
|
|
76
|
+
classNames(className, activeModifiers, resolve(rootClass)),
|
|
77
|
+
getChildClass
|
|
78
|
+
];
|
|
71
79
|
};
|
|
72
80
|
// Annotate the CommonJS export names for ESM import in node:
|
|
73
81
|
0 && (module.exports = {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts","../get-class-name.ts"],"sourcesContent":["export { getClassName } from
|
|
1
|
+
{"version":3,"sources":["../index.ts","../class-names.ts","../get-class-name.ts"],"sourcesContent":["export { getClassName } from \"./get-class-name.js\";\nexport type {\n GetChildClass,\n GetClassNameProps,\n IGetClassNameProps,\n} from \"./get-class-name.js\";\n","/** One argument of `classNames`: what it contributes to the class list. */\nexport type ClassValue =\n | string\n | number\n | bigint\n | boolean\n | null\n | undefined\n | ClassDictionary\n | ClassArray;\n\n/** An object whose keys are class names, kept when their value is truthy. */\nexport interface ClassDictionary {\n [className: string]: unknown;\n}\n\n/** A list of class values, nested as deep as needed. */\nexport type ClassArray = ClassValue[];\n\nconst objectToString = Object.prototype.toString;\nconst hasOwn = Object.prototype.hasOwnProperty;\n\n/** Joins `next` to `joined` with one space, skipping an empty `next`. */\nconst append = (joined: string, next: string): string => {\n let result = joined;\n\n if (next) {\n result = joined ? `${joined} ${next}` : next;\n }\n\n return result;\n};\n\n/** Whether an object brings its own `toString` (not `Object`'s, and not a built-in one). */\nconst hasCustomToString = (value: object): boolean =>\n value.toString !== objectToString &&\n !value.toString.toString().includes(\"[native code]\");\n\n/** The class string one truthy value contributes. */\nconst parseValue = (value: ClassValue): string => {\n let result = \"\";\n\n if (typeof value === \"string\" || typeof value === \"number\") {\n result = String(value);\n } else if (Array.isArray(value)) {\n result = classNames(...value);\n } else if (typeof value === \"object\" && value !== null) {\n if (hasCustomToString(value)) {\n result = value.toString();\n } else {\n Object.keys(value).forEach((key) => {\n if (hasOwn.call(value, key) && (value as ClassDictionary)[key]) {\n result = append(result, key);\n }\n });\n }\n }\n\n return result;\n};\n\n/**\n * Builds a class string from strings, numbers, arrays (nested to any depth)\n * and objects whose keys are kept when their value is truthy. Falsy arguments\n * are ignored, and nothing is de-duplicated or trimmed. Its output is the same\n * as the `classnames` package's for the same arguments, which this replaces.\n *\n * @param args The class values to join\n * @returns The class names, separated by single spaces\n */\nexport const classNames = (...args: ClassValue[]): string =>\n args.reduce<string>(\n (joined, value) => (value ? append(joined, parseValue(value)) : joined),\n \"\",\n );\n","import { classNames } from \"./class-names.js\";\n\n/** Options of `getClassName`. */\nexport interface GetClassNameProps {\n /** className prop generally passed in from the parent component */\n className?: string;\n /**\n * Modifiers of the root element, by name. A modifier whose value is true adds\n * `<rootClass>--<name>` (or the CSS module's name for it); a false or missing\n * value adds nothing.\n */\n modifiers?: { [key: string]: boolean | undefined };\n /** className for the component */\n rootClass: string;\n /** A CSS module's class map: each BEM name is looked up here first and kept as written when it is not there */\n styles?: { [key: string]: string };\n}\n\n/** Same options under the name the package has always exported. */\nexport type IGetClassNameProps = GetClassNameProps;\n\n/** Builds the class name of a child element from its kebab-case name. */\nexport type GetChildClass = (childClassName: string) => string;\n\n/**\n * Builds the BEM class names of a component: its root class (with the parent's\n * `className` first, then the active modifiers, then the block itself) and a\n * function for the classes of its child elements. With a CSS module's `styles`,\n * each name is swapped for the module's generated one when it has one.\n *\n * @param props The block name, optional modifiers, parent className and CSS module map\n * @returns The root class string and the child class builder\n */\nexport const getClassName = ({\n className,\n modifiers = {},\n rootClass,\n styles = {},\n}: GetClassNameProps): [string, GetChildClass] => {\n const resolve = (classKey: string): string => styles[classKey] || classKey;\n\n const activeModifiers = Object.keys(modifiers).reduce<\n Record<string, boolean>\n >((resolved, modKey) => {\n resolved[resolve(`${rootClass}--${modKey}`)] = modifiers[modKey] ?? false;\n\n return resolved;\n }, {});\n\n const getChildClass: GetChildClass = (childClassName) =>\n resolve(`${rootClass}__${childClassName}`);\n\n return [\n classNames(className, activeModifiers, resolve(rootClass)),\n getChildClass,\n ];\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmBA,IAAM,iBAAiB,OAAO,UAAU;AACxC,IAAM,SAAS,OAAO,UAAU;AAGhC,IAAM,SAAS,CAAC,QAAgB,SAAyB;AACvD,MAAI,SAAS;AAEb,MAAI,MAAM;AACR,aAAS,SAAS,GAAG,MAAM,IAAI,IAAI,KAAK;AAAA,EAC1C;AAEA,SAAO;AACT;AAGA,IAAM,oBAAoB,CAAC,UACzB,MAAM,aAAa,kBACnB,CAAC,MAAM,SAAS,SAAS,EAAE,SAAS,eAAe;AAGrD,IAAM,aAAa,CAAC,UAA8B;AAChD,MAAI,SAAS;AAEb,MAAI,OAAO,UAAU,YAAY,OAAO,UAAU,UAAU;AAC1D,aAAS,OAAO,KAAK;AAAA,EACvB,WAAW,MAAM,QAAQ,KAAK,GAAG;AAC/B,aAAS,WAAW,GAAG,KAAK;AAAA,EAC9B,WAAW,OAAO,UAAU,YAAY,UAAU,MAAM;AACtD,QAAI,kBAAkB,KAAK,GAAG;AAC5B,eAAS,MAAM,SAAS;AAAA,IAC1B,OAAO;AACL,aAAO,KAAK,KAAK,EAAE,QAAQ,CAAC,QAAQ;AAClC,YAAI,OAAO,KAAK,OAAO,GAAG,KAAM,MAA0B,GAAG,GAAG;AAC9D,mBAAS,OAAO,QAAQ,GAAG;AAAA,QAC7B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AACT;AAWO,IAAM,aAAa,IAAI,SAC5B,KAAK;AAAA,EACH,CAAC,QAAQ,UAAW,QAAQ,OAAO,QAAQ,WAAW,KAAK,CAAC,IAAI;AAAA,EAChE;AACF;;;ACzCK,IAAM,eAAe,CAAC;AAAA,EAC3B;AAAA,EACA,YAAY,CAAC;AAAA,EACb;AAAA,EACA,SAAS,CAAC;AACZ,MAAkD;AAChD,QAAM,UAAU,CAAC,aAA6B,OAAO,QAAQ,KAAK;AAElE,QAAM,kBAAkB,OAAO,KAAK,SAAS,EAAE,OAE7C,CAAC,UAAU,WAAW;AACtB,aAAS,QAAQ,GAAG,SAAS,KAAK,MAAM,EAAE,CAAC,IAAI,UAAU,MAAM,KAAK;AAEpE,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AAEL,QAAM,gBAA+B,CAAC,mBACpC,QAAQ,GAAG,SAAS,KAAK,cAAc,EAAE;AAE3C,SAAO;AAAA,IACL,WAAW,WAAW,iBAAiB,QAAQ,SAAS,CAAC;AAAA,IACzD;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
/** Options of `getClassName`. */
|
|
2
|
+
interface GetClassNameProps {
|
|
2
3
|
/** className prop generally passed in from the parent component */
|
|
3
4
|
className?: string;
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
* Modifiers of the root element, by name. A modifier whose value is true adds
|
|
7
|
+
* `<rootClass>--<name>` (or the CSS module's name for it); a false or missing
|
|
8
|
+
* value adds nothing.
|
|
9
|
+
*/
|
|
8
10
|
modifiers?: {
|
|
9
|
-
[key: string]: boolean;
|
|
11
|
+
[key: string]: boolean | undefined;
|
|
10
12
|
};
|
|
11
13
|
/** className for the component */
|
|
12
14
|
rootClass: string;
|
|
15
|
+
/** A CSS module's class map: each BEM name is looked up here first and kept as written when it is not there */
|
|
13
16
|
styles?: {
|
|
14
17
|
[key: string]: string;
|
|
15
18
|
};
|
|
16
19
|
}
|
|
17
|
-
|
|
20
|
+
/** Same options under the name the package has always exported. */
|
|
21
|
+
type IGetClassNameProps = GetClassNameProps;
|
|
22
|
+
/** Builds the class name of a child element from its kebab-case name. */
|
|
23
|
+
type GetChildClass = (childClassName: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Builds the BEM class names of a component: its root class (with the parent's
|
|
26
|
+
* `className` first, then the active modifiers, then the block itself) and a
|
|
27
|
+
* function for the classes of its child elements. With a CSS module's `styles`,
|
|
28
|
+
* each name is swapped for the module's generated one when it has one.
|
|
29
|
+
*
|
|
30
|
+
* @param props The block name, optional modifiers, parent className and CSS module map
|
|
31
|
+
* @returns The root class string and the child class builder
|
|
32
|
+
*/
|
|
33
|
+
declare const getClassName: ({ className, modifiers, rootClass, styles, }: GetClassNameProps) => [string, GetChildClass];
|
|
18
34
|
|
|
19
|
-
export { getClassName };
|
|
35
|
+
export { type GetChildClass, type GetClassNameProps, type IGetClassNameProps, getClassName };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
/** Options of `getClassName`. */
|
|
2
|
+
interface GetClassNameProps {
|
|
2
3
|
/** className prop generally passed in from the parent component */
|
|
3
4
|
className?: string;
|
|
4
5
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
* Modifiers of the root element, by name. A modifier whose value is true adds
|
|
7
|
+
* `<rootClass>--<name>` (or the CSS module's name for it); a false or missing
|
|
8
|
+
* value adds nothing.
|
|
9
|
+
*/
|
|
8
10
|
modifiers?: {
|
|
9
|
-
[key: string]: boolean;
|
|
11
|
+
[key: string]: boolean | undefined;
|
|
10
12
|
};
|
|
11
13
|
/** className for the component */
|
|
12
14
|
rootClass: string;
|
|
15
|
+
/** A CSS module's class map: each BEM name is looked up here first and kept as written when it is not there */
|
|
13
16
|
styles?: {
|
|
14
17
|
[key: string]: string;
|
|
15
18
|
};
|
|
16
19
|
}
|
|
17
|
-
|
|
20
|
+
/** Same options under the name the package has always exported. */
|
|
21
|
+
type IGetClassNameProps = GetClassNameProps;
|
|
22
|
+
/** Builds the class name of a child element from its kebab-case name. */
|
|
23
|
+
type GetChildClass = (childClassName: string) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Builds the BEM class names of a component: its root class (with the parent's
|
|
26
|
+
* `className` first, then the active modifiers, then the block itself) and a
|
|
27
|
+
* function for the classes of its child elements. With a CSS module's `styles`,
|
|
28
|
+
* each name is swapped for the module's generated one when it has one.
|
|
29
|
+
*
|
|
30
|
+
* @param props The block name, optional modifiers, parent className and CSS module map
|
|
31
|
+
* @returns The root class string and the child class builder
|
|
32
|
+
*/
|
|
33
|
+
declare const getClassName: ({ className, modifiers, rootClass, styles, }: GetClassNameProps) => [string, GetChildClass];
|
|
18
34
|
|
|
19
|
-
export { getClassName };
|
|
35
|
+
export { type GetChildClass, type GetClassNameProps, type IGetClassNameProps, getClassName };
|
package/dist/index.js
CHANGED
|
@@ -1,37 +1,55 @@
|
|
|
1
|
+
// class-names.ts
|
|
2
|
+
var objectToString = Object.prototype.toString;
|
|
3
|
+
var hasOwn = Object.prototype.hasOwnProperty;
|
|
4
|
+
var append = (joined, next) => {
|
|
5
|
+
let result = joined;
|
|
6
|
+
if (next) {
|
|
7
|
+
result = joined ? `${joined} ${next}` : next;
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
var hasCustomToString = (value) => value.toString !== objectToString && !value.toString.toString().includes("[native code]");
|
|
12
|
+
var parseValue = (value) => {
|
|
13
|
+
let result = "";
|
|
14
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
15
|
+
result = String(value);
|
|
16
|
+
} else if (Array.isArray(value)) {
|
|
17
|
+
result = classNames(...value);
|
|
18
|
+
} else if (typeof value === "object" && value !== null) {
|
|
19
|
+
if (hasCustomToString(value)) {
|
|
20
|
+
result = value.toString();
|
|
21
|
+
} else {
|
|
22
|
+
Object.keys(value).forEach((key) => {
|
|
23
|
+
if (hasOwn.call(value, key) && value[key]) {
|
|
24
|
+
result = append(result, key);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
var classNames = (...args) => args.reduce(
|
|
32
|
+
(joined, value) => value ? append(joined, parseValue(value)) : joined,
|
|
33
|
+
""
|
|
34
|
+
);
|
|
35
|
+
|
|
1
36
|
// get-class-name.ts
|
|
2
|
-
import classnames from "classnames";
|
|
3
37
|
var getClassName = ({
|
|
4
38
|
className,
|
|
5
39
|
modifiers = {},
|
|
6
40
|
rootClass,
|
|
7
41
|
styles = {}
|
|
8
42
|
}) => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return updatedModifiers;
|
|
20
|
-
}
|
|
21
|
-
function getChildClass(childClassName) {
|
|
22
|
-
const classKey = `${rootClass}__${childClassName}`;
|
|
23
|
-
return styles[classKey] || classKey;
|
|
24
|
-
}
|
|
25
|
-
function getRootStyles() {
|
|
26
|
-
return classnames(
|
|
27
|
-
className,
|
|
28
|
-
{
|
|
29
|
-
...getModifiers()
|
|
30
|
-
},
|
|
31
|
-
styles[rootClass] || rootClass
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
return [getRootStyles(), getChildClass];
|
|
43
|
+
const resolve = (classKey) => styles[classKey] || classKey;
|
|
44
|
+
const activeModifiers = Object.keys(modifiers).reduce((resolved, modKey) => {
|
|
45
|
+
resolved[resolve(`${rootClass}--${modKey}`)] = modifiers[modKey] ?? false;
|
|
46
|
+
return resolved;
|
|
47
|
+
}, {});
|
|
48
|
+
const getChildClass = (childClassName) => resolve(`${rootClass}__${childClassName}`);
|
|
49
|
+
return [
|
|
50
|
+
classNames(className, activeModifiers, resolve(rootClass)),
|
|
51
|
+
getChildClass
|
|
52
|
+
];
|
|
35
53
|
};
|
|
36
54
|
export {
|
|
37
55
|
getClassName
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../get-class-name.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../class-names.ts","../get-class-name.ts"],"sourcesContent":["/** One argument of `classNames`: what it contributes to the class list. */\nexport type ClassValue =\n | string\n | number\n | bigint\n | boolean\n | null\n | undefined\n | ClassDictionary\n | ClassArray;\n\n/** An object whose keys are class names, kept when their value is truthy. */\nexport interface ClassDictionary {\n [className: string]: unknown;\n}\n\n/** A list of class values, nested as deep as needed. */\nexport type ClassArray = ClassValue[];\n\nconst objectToString = Object.prototype.toString;\nconst hasOwn = Object.prototype.hasOwnProperty;\n\n/** Joins `next` to `joined` with one space, skipping an empty `next`. */\nconst append = (joined: string, next: string): string => {\n let result = joined;\n\n if (next) {\n result = joined ? `${joined} ${next}` : next;\n }\n\n return result;\n};\n\n/** Whether an object brings its own `toString` (not `Object`'s, and not a built-in one). */\nconst hasCustomToString = (value: object): boolean =>\n value.toString !== objectToString &&\n !value.toString.toString().includes(\"[native code]\");\n\n/** The class string one truthy value contributes. */\nconst parseValue = (value: ClassValue): string => {\n let result = \"\";\n\n if (typeof value === \"string\" || typeof value === \"number\") {\n result = String(value);\n } else if (Array.isArray(value)) {\n result = classNames(...value);\n } else if (typeof value === \"object\" && value !== null) {\n if (hasCustomToString(value)) {\n result = value.toString();\n } else {\n Object.keys(value).forEach((key) => {\n if (hasOwn.call(value, key) && (value as ClassDictionary)[key]) {\n result = append(result, key);\n }\n });\n }\n }\n\n return result;\n};\n\n/**\n * Builds a class string from strings, numbers, arrays (nested to any depth)\n * and objects whose keys are kept when their value is truthy. Falsy arguments\n * are ignored, and nothing is de-duplicated or trimmed. Its output is the same\n * as the `classnames` package's for the same arguments, which this replaces.\n *\n * @param args The class values to join\n * @returns The class names, separated by single spaces\n */\nexport const classNames = (...args: ClassValue[]): string =>\n args.reduce<string>(\n (joined, value) => (value ? append(joined, parseValue(value)) : joined),\n \"\",\n );\n","import { classNames } from \"./class-names.js\";\n\n/** Options of `getClassName`. */\nexport interface GetClassNameProps {\n /** className prop generally passed in from the parent component */\n className?: string;\n /**\n * Modifiers of the root element, by name. A modifier whose value is true adds\n * `<rootClass>--<name>` (or the CSS module's name for it); a false or missing\n * value adds nothing.\n */\n modifiers?: { [key: string]: boolean | undefined };\n /** className for the component */\n rootClass: string;\n /** A CSS module's class map: each BEM name is looked up here first and kept as written when it is not there */\n styles?: { [key: string]: string };\n}\n\n/** Same options under the name the package has always exported. */\nexport type IGetClassNameProps = GetClassNameProps;\n\n/** Builds the class name of a child element from its kebab-case name. */\nexport type GetChildClass = (childClassName: string) => string;\n\n/**\n * Builds the BEM class names of a component: its root class (with the parent's\n * `className` first, then the active modifiers, then the block itself) and a\n * function for the classes of its child elements. With a CSS module's `styles`,\n * each name is swapped for the module's generated one when it has one.\n *\n * @param props The block name, optional modifiers, parent className and CSS module map\n * @returns The root class string and the child class builder\n */\nexport const getClassName = ({\n className,\n modifiers = {},\n rootClass,\n styles = {},\n}: GetClassNameProps): [string, GetChildClass] => {\n const resolve = (classKey: string): string => styles[classKey] || classKey;\n\n const activeModifiers = Object.keys(modifiers).reduce<\n Record<string, boolean>\n >((resolved, modKey) => {\n resolved[resolve(`${rootClass}--${modKey}`)] = modifiers[modKey] ?? false;\n\n return resolved;\n }, {});\n\n const getChildClass: GetChildClass = (childClassName) =>\n resolve(`${rootClass}__${childClassName}`);\n\n return [\n classNames(className, activeModifiers, resolve(rootClass)),\n getChildClass,\n ];\n};\n"],"mappings":";AAmBA,IAAM,iBAAiB,OAAO,UAAU;AACxC,IAAM,SAAS,OAAO,UAAU;AAGhC,IAAM,SAAS,CAAC,QAAgB,SAAyB;AACvD,MAAI,SAAS;AAEb,MAAI,MAAM;AACR,aAAS,SAAS,GAAG,MAAM,IAAI,IAAI,KAAK;AAAA,EAC1C;AAEA,SAAO;AACT;AAGA,IAAM,oBAAoB,CAAC,UACzB,MAAM,aAAa,kBACnB,CAAC,MAAM,SAAS,SAAS,EAAE,SAAS,eAAe;AAGrD,IAAM,aAAa,CAAC,UAA8B;AAChD,MAAI,SAAS;AAEb,MAAI,OAAO,UAAU,YAAY,OAAO,UAAU,UAAU;AAC1D,aAAS,OAAO,KAAK;AAAA,EACvB,WAAW,MAAM,QAAQ,KAAK,GAAG;AAC/B,aAAS,WAAW,GAAG,KAAK;AAAA,EAC9B,WAAW,OAAO,UAAU,YAAY,UAAU,MAAM;AACtD,QAAI,kBAAkB,KAAK,GAAG;AAC5B,eAAS,MAAM,SAAS;AAAA,IAC1B,OAAO;AACL,aAAO,KAAK,KAAK,EAAE,QAAQ,CAAC,QAAQ;AAClC,YAAI,OAAO,KAAK,OAAO,GAAG,KAAM,MAA0B,GAAG,GAAG;AAC9D,mBAAS,OAAO,QAAQ,GAAG;AAAA,QAC7B;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO;AACT;AAWO,IAAM,aAAa,IAAI,SAC5B,KAAK;AAAA,EACH,CAAC,QAAQ,UAAW,QAAQ,OAAO,QAAQ,WAAW,KAAK,CAAC,IAAI;AAAA,EAChE;AACF;;;ACzCK,IAAM,eAAe,CAAC;AAAA,EAC3B;AAAA,EACA,YAAY,CAAC;AAAA,EACb;AAAA,EACA,SAAS,CAAC;AACZ,MAAkD;AAChD,QAAM,UAAU,CAAC,aAA6B,OAAO,QAAQ,KAAK;AAElE,QAAM,kBAAkB,OAAO,KAAK,SAAS,EAAE,OAE7C,CAAC,UAAU,WAAW;AACtB,aAAS,QAAQ,GAAG,SAAS,KAAK,MAAM,EAAE,CAAC,IAAI,UAAU,MAAM,KAAK;AAEpE,WAAO;AAAA,EACT,GAAG,CAAC,CAAC;AAEL,QAAM,gBAA+B,CAAC,mBACpC,QAAQ,GAAG,SAAS,KAAK,cAAc,EAAE;AAE3C,SAAO;AAAA,IACL,WAAW,WAAW,iBAAiB,QAAQ,SAAS,CAAC;AAAA,IACzD;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@behivetech/get-class-name",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"private": false,
|
|
6
6
|
"exports": {
|
|
@@ -13,15 +13,12 @@
|
|
|
13
13
|
"files": [
|
|
14
14
|
"dist"
|
|
15
15
|
],
|
|
16
|
-
"dependencies": {
|
|
17
|
-
"classnames": "^2.5.1"
|
|
18
|
-
},
|
|
19
16
|
"devDependencies": {
|
|
17
|
+
"@behivetech/eslint-config": "0.1.1",
|
|
18
|
+
"@behivetech/typescript-config": "0.0.2",
|
|
20
19
|
"eslint": "^9.39.5",
|
|
21
20
|
"tsup": "^8.5.1",
|
|
22
|
-
"typescript": "^6.0.3"
|
|
23
|
-
"@behivetech/eslint-config": "0.1.0",
|
|
24
|
-
"@behivetech/typescript-config": "0.0.2"
|
|
21
|
+
"typescript": "^6.0.3"
|
|
25
22
|
},
|
|
26
23
|
"repository": {
|
|
27
24
|
"type": "git",
|
|
@@ -29,10 +26,10 @@
|
|
|
29
26
|
"directory": "packages/utils/get-class-name"
|
|
30
27
|
},
|
|
31
28
|
"publishConfig": {
|
|
32
|
-
"registry": "https://
|
|
33
|
-
"access": "public"
|
|
34
|
-
"provenance": true
|
|
29
|
+
"registry": "https://npm.pkg.github.com",
|
|
30
|
+
"access": "public"
|
|
35
31
|
},
|
|
32
|
+
"description": "A utility for generating BEM-style class names with support for CSS modules, modifiers, and child elements.",
|
|
36
33
|
"scripts": {
|
|
37
34
|
"build": "tsup",
|
|
38
35
|
"dev": "tsup --watch",
|