@avatar-generator/core 1.0.1 → 1.1.1
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.js +21 -6
- package/dist/types/index.d.ts +7 -3
- package/package.json +1 -1
- package/src/index.ts +34 -6
package/dist/index.js
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
const getInitials = (name) => {
|
|
2
|
+
if (!name.trim())
|
|
3
|
+
return "?";
|
|
2
4
|
return name
|
|
3
5
|
.split(" ")
|
|
6
|
+
.filter(Boolean)
|
|
4
7
|
.slice(0, 2)
|
|
5
8
|
.map((part) => part[0].toUpperCase())
|
|
6
9
|
.join("");
|
|
7
|
-
}
|
|
8
|
-
export function createAvatar({ name, backgroundColor = "#ccc", textColor = "#fff", fontSize = "40px", shape = "circle", }) {
|
|
10
|
+
};
|
|
11
|
+
export function createAvatar({ name, backgroundColor = "#ccc", gradientDirection = "vertical", textColor = "#fff", fontSize = "40px", shape = "circle", width = "100px", height = "100px", tooltip = false, additionalClasses = "", }) {
|
|
9
12
|
const initials = getInitials(name);
|
|
10
13
|
const avatar = document.createElement("div");
|
|
11
|
-
avatar.style.width =
|
|
12
|
-
avatar.style.height =
|
|
13
|
-
|
|
14
|
+
avatar.style.width = width;
|
|
15
|
+
avatar.style.height = height;
|
|
16
|
+
if (Array.isArray(backgroundColor)) {
|
|
17
|
+
const direction = gradientDirection === "vertical" ? "to bottom" : "to right";
|
|
18
|
+
avatar.style.background = `linear-gradient(${direction}, ${backgroundColor.join(", ")})`;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
avatar.style.backgroundColor = backgroundColor;
|
|
22
|
+
}
|
|
14
23
|
avatar.style.color = textColor;
|
|
15
24
|
avatar.style.display = "flex";
|
|
16
25
|
avatar.style.justifyContent = "center";
|
|
@@ -18,5 +27,11 @@ export function createAvatar({ name, backgroundColor = "#ccc", textColor = "#fff
|
|
|
18
27
|
avatar.style.fontSize = fontSize;
|
|
19
28
|
avatar.style.borderRadius = shape === "circle" ? "50%" : "0";
|
|
20
29
|
avatar.textContent = initials;
|
|
30
|
+
if (tooltip) {
|
|
31
|
+
avatar.title = name;
|
|
32
|
+
}
|
|
33
|
+
if (additionalClasses) {
|
|
34
|
+
avatar.className = additionalClasses;
|
|
35
|
+
}
|
|
21
36
|
return avatar;
|
|
22
37
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
export declare function getInitials(name: string): string;
|
|
2
1
|
export interface AvatarOptions {
|
|
3
2
|
name: string;
|
|
4
|
-
backgroundColor?: string;
|
|
3
|
+
backgroundColor?: string | string[];
|
|
4
|
+
gradientDirection?: "vertical" | "horizontal";
|
|
5
5
|
textColor?: string;
|
|
6
6
|
fontSize?: string;
|
|
7
7
|
shape?: "circle" | "square";
|
|
8
|
+
width?: string;
|
|
9
|
+
height?: string;
|
|
10
|
+
tooltip?: boolean;
|
|
11
|
+
additionalClasses?: string;
|
|
8
12
|
}
|
|
9
|
-
export declare function createAvatar({ name, backgroundColor, textColor, fontSize, shape, }: AvatarOptions): HTMLElement;
|
|
13
|
+
export declare function createAvatar({ name, backgroundColor, gradientDirection, textColor, fontSize, shape, width, height, tooltip, additionalClasses, }: AvatarOptions): HTMLElement;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,32 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
const getInitials = (name: string): string => {
|
|
2
|
+
if (!name.trim()) return "?";
|
|
3
|
+
|
|
2
4
|
return name
|
|
3
5
|
.split(" ")
|
|
6
|
+
.filter(Boolean)
|
|
4
7
|
.slice(0, 2)
|
|
5
8
|
.map((part) => part[0].toUpperCase())
|
|
6
9
|
.join("");
|
|
7
|
-
}
|
|
10
|
+
};
|
|
8
11
|
|
|
9
12
|
export interface AvatarOptions {
|
|
10
13
|
name: string;
|
|
11
|
-
backgroundColor?: string;
|
|
14
|
+
backgroundColor?: string | string[];
|
|
15
|
+
gradientDirection?: "vertical" | "horizontal"
|
|
12
16
|
textColor?: string;
|
|
13
17
|
fontSize?: string;
|
|
14
18
|
shape?: "circle" | "square";
|
|
19
|
+
width?: string;
|
|
20
|
+
height?: string;
|
|
21
|
+
tooltip?: boolean;
|
|
22
|
+
additionalClasses?: string;
|
|
15
23
|
}
|
|
16
24
|
|
|
17
25
|
export function createAvatar({
|
|
18
26
|
name,
|
|
19
27
|
backgroundColor = "#ccc",
|
|
28
|
+
gradientDirection = "vertical",
|
|
20
29
|
textColor = "#fff",
|
|
21
30
|
fontSize = "40px",
|
|
22
31
|
shape = "circle",
|
|
32
|
+
width = "100px",
|
|
33
|
+
height = "100px",
|
|
34
|
+
tooltip = false,
|
|
35
|
+
additionalClasses = "",
|
|
23
36
|
}: AvatarOptions): HTMLElement {
|
|
24
37
|
const initials = getInitials(name);
|
|
25
38
|
|
|
26
39
|
const avatar = document.createElement("div");
|
|
27
|
-
avatar.style.width =
|
|
28
|
-
avatar.style.height =
|
|
29
|
-
|
|
40
|
+
avatar.style.width = width;
|
|
41
|
+
avatar.style.height = height;
|
|
42
|
+
|
|
43
|
+
if (Array.isArray(backgroundColor)) {
|
|
44
|
+
const direction = gradientDirection === "vertical" ? "to bottom" : "to right";
|
|
45
|
+
avatar.style.background = `linear-gradient(${direction}, ${backgroundColor.join(", ")})`;
|
|
46
|
+
} else {
|
|
47
|
+
avatar.style.backgroundColor = backgroundColor;
|
|
48
|
+
}
|
|
49
|
+
|
|
30
50
|
avatar.style.color = textColor;
|
|
31
51
|
avatar.style.display = "flex";
|
|
32
52
|
avatar.style.justifyContent = "center";
|
|
@@ -35,5 +55,13 @@ export function createAvatar({
|
|
|
35
55
|
avatar.style.borderRadius = shape === "circle" ? "50%" : "0";
|
|
36
56
|
avatar.textContent = initials;
|
|
37
57
|
|
|
58
|
+
if (tooltip) {
|
|
59
|
+
avatar.title = name;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (additionalClasses) {
|
|
63
|
+
avatar.className = additionalClasses;
|
|
64
|
+
}
|
|
65
|
+
|
|
38
66
|
return avatar;
|
|
39
67
|
}
|