Skip to content

Commit 56a5056

Browse files
Merge pull request #23 from code4rena-dev/develop
Major version - Add avatar and reduce bundle size
2 parents f4699bf + 4cc33a0 commit 56a5056

19 files changed

+274
-75
lines changed

.storybook/main.ts

+20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { StorybookConfig } from "@storybook/react-webpack5";
2+
import path from "path";
3+
24
const config: StorybookConfig = {
35
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
46
staticDirs: ["../public"],
@@ -22,5 +24,23 @@ const config: StorybookConfig = {
2224
docs: {
2325
autodocs: "tag",
2426
},
27+
webpackFinal: async (config) => {
28+
if (config.resolve) {
29+
config.resolve = {
30+
...config.resolve,
31+
alias: {
32+
...config.resolve.alias,
33+
"/fonts": path.resolve(__dirname, "../public/fonts"),
34+
},
35+
};
36+
} else {
37+
config.resolve = {
38+
alias: {
39+
"/fonts": path.resolve(__dirname, "../public/fonts"),
40+
},
41+
};
42+
}
43+
return config;
44+
},
2545
};
2646
export default config;

package-lock.json

+3-54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@code4rena/components-library",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"description": "Code4rena's official components library ",
55
"types": "./dist/lib.d.ts",
66
"exports": {
@@ -71,7 +71,6 @@
7171
"container-query-polyfill": "^1.0.2",
7272
"date-fns": "^2.30.0",
7373
"luxon": "^3.3.0",
74-
"react-avatar": "^5.0.3",
7574
"react-select": "^5.7.4"
7675
},
7776
"peerDependencies": {
@@ -85,4 +84,4 @@
8584
"@babel/preset-react"
8685
]
8786
}
88-
}
87+
}

src/lib/Avatar/Avatar.scss

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.avatar {
2+
position: relative;
3+
display: inline-block;
4+
overflow: hidden;
5+
vertical-align: middle;
6+
7+
.avatar__image {
8+
width: 100%;
9+
height: 100%;
10+
object-fit: cover;
11+
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
12+
}
13+
14+
.avatar__initials {
15+
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
16+
17+
.avatar__initials-text {
18+
display: flex;
19+
align-items: center;
20+
justify-content: center;
21+
width: 100%;
22+
height: 100%;
23+
color: #fff;
24+
font-weight: 600;
25+
}
26+
}
27+
}
28+
29+
.widget__avatar-container {
30+
display: flex;
31+
flex-direction: row;
32+
align-items: center;
33+
gap: 10px;
34+
flex-wrap: wrap;
35+
}
36+

src/lib/Avatar/Avatar.stories.tsx

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from "react";
2+
import { Meta, StoryObj } from "@storybook/react";
3+
import { Avatar } from "./Avatar";
4+
5+
const meta: Meta<typeof Avatar> = {
6+
component: Avatar,
7+
title: "Avatar",
8+
tags: ["autodocs"],
9+
argTypes: {
10+
size: { control: "number" },
11+
round: { control: "number" },
12+
},
13+
};
14+
export default meta;
15+
16+
type Story = StoryObj<typeof Avatar>;
17+
18+
export const ImageAvatar: Story = (args) => <Avatar {...args} />;
19+
ImageAvatar.args = {
20+
imgElement: <img src="/images/default-avatar.png" alt="Placeholder" />,
21+
name: "0xJohnWithALongName",
22+
size: 50,
23+
round: 25,
24+
};
25+
26+
export const InitialsAvatar: Story = (args) => <Avatar {...args} />;
27+
InitialsAvatar.args = {
28+
name: "John-With-A-Long-Name",
29+
size: 50,
30+
round: 25,
31+
};

src/lib/Avatar/Avatar.test.tsx

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from "react";
2+
import { Avatar } from "./Avatar";
3+
import { render, screen } from "@testing-library/react";
4+
import "@testing-library/jest-dom";
5+
6+
const defaultArgs = {
7+
name: "John Doe",
8+
size: 50,
9+
round: 25,
10+
};
11+
12+
describe("========== Avatar Component - RUNNING TESTS ==========", () => {
13+
test("Renders with image avatar", () => {
14+
const imgElement = (
15+
<img src="/images/default-avatar.png" alt="Placeholder" />
16+
);
17+
render(<Avatar imgElement={imgElement} {...defaultArgs} />);
18+
const avatar = screen.getByRole("img");
19+
expect(avatar).toHaveAttribute("src", "/images/default-avatar.png");
20+
});
21+
22+
test("Renders with initials avatar", () => {
23+
render(<Avatar {...defaultArgs} />);
24+
const avatar = screen.getByText("JD");
25+
expect(avatar).toBeInTheDocument();
26+
});
27+
28+
test("Sets alt text for image avatar", () => {
29+
const imgElement = (
30+
<img src="/images/default-avatar.png" alt="User avatar" />
31+
);
32+
render(<Avatar imgElement={imgElement} {...defaultArgs} />);
33+
const avatar = screen.getByAltText("User avatar");
34+
expect(avatar).toBeInTheDocument();
35+
});
36+
});

src/lib/Avatar/Avatar.tsx

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import React, { cloneElement } from "react";
2+
import { AvatarProps } from "./Avatar.types";
3+
import "./Avatar.scss";
4+
5+
// Parse the initials of the user from their name
6+
const parseInitials = (name: string) => {
7+
if (!name) return "✷"; // If no name then return "✷"
8+
if (name.length <= 2) return name; // If name is 2 characters or less then return it whole
9+
if (name.substring(0, 2).toLowerCase() === "0x") name = name.substring(2); // If starts with "0x" then remove it
10+
let nameArray = name.split(" "); // Initials by 'space' separator
11+
if (nameArray.length <= 1) nameArray = name.split("."); // Initials by 'dot' separator
12+
if (nameArray.length <= 1) nameArray = name.split("_"); // Initials by 'underscore' separator
13+
if (nameArray.length <= 1) nameArray = name.split("-"); // Initials by 'dash' separator
14+
if (nameArray.length <= 1) {
15+
const nameString = name.replace(/([A-Z])/g, " $1").trim(); // Initials by case-change
16+
nameArray = nameString.split(" ");
17+
}
18+
if (nameArray.length <= 1) return name.substring(0, 1); // Fallback to first letter of name
19+
return `${nameArray[0].substring(0, 1)}${nameArray[1].substring(0, 1)}`;
20+
};
21+
22+
// Array of dark pastel colors suitable as bg for the white initials
23+
const pastelColors = [
24+
"#9B4DCA",
25+
"#8E44AD",
26+
"#2980B9",
27+
"#3498DB",
28+
"#1ABC9C",
29+
"#16A085",
30+
"#27AE60",
31+
"#2ECC71",
32+
"#F1C40F",
33+
"#F39C12",
34+
"#E67E22",
35+
"#D35400",
36+
"#E74C3C",
37+
"#C0392B",
38+
"#EC407A",
39+
"#D81B60",
40+
"#8E24AA",
41+
"#6A1B9A",
42+
"#4A148C",
43+
"#4527A0",
44+
];
45+
46+
// Generate a random color from the name string
47+
const generateColor = (str: string) => {
48+
const index = str.length % pastelColors.length;
49+
return pastelColors[index];
50+
};
51+
52+
/**
53+
* A stylized Avatar component for displaying user avatars.
54+
* This component supports displaying an image avatar or a fallback avatar with initials.
55+
* The fallback avatar is a colored circle with the user's initials.
56+
*
57+
* @param imgElement - An optional image element to use as the avatar.
58+
* @param name - The name of the user. Used to generate initials for the fallback avatar.
59+
* @param size - The size of the avatar in pixels.
60+
* @param round - The border-radius of the avatar in pixels. Use this to make the avatar round.
61+
*/
62+
export const Avatar: React.FC<AvatarProps> = ({
63+
imgElement,
64+
name,
65+
size,
66+
round,
67+
}) => {
68+
const clonedImgElement = imgElement
69+
? cloneElement(imgElement, {
70+
className: "avatar__image",
71+
width: size,
72+
height: size,
73+
})
74+
: null;
75+
76+
return (
77+
<div
78+
className={"avatar"}
79+
style={{
80+
borderRadius: round ? `${round}px` : "none",
81+
width: `${size}px`,
82+
height: `${size}px`,
83+
}}
84+
>
85+
{clonedImgElement ? (
86+
clonedImgElement
87+
) : (
88+
<div
89+
className={"avatar__initials"}
90+
style={{
91+
width: `${size}px`,
92+
height: `${size}px`,
93+
backgroundColor: generateColor(name),
94+
}}
95+
>
96+
<span
97+
className={"avatar__initials-text"}
98+
style={{
99+
lineHeight: `${size}px`,
100+
fontSize: `${size * 0.45}px`,
101+
}}
102+
>
103+
{parseInitials(name)}
104+
</span>
105+
</div>
106+
)}
107+
</div>
108+
);
109+
};

0 commit comments

Comments
 (0)