Skip to content

Commit e350c12

Browse files
committed
💄(ui) provide more avatar colors (test before proposing to be in the library)
1 parent b28f304 commit e350c12

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

client/src/ui/User/User.jsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import PropTypes from 'prop-types';
33
import { UserAvatar } from '@gouvfr-lasuite/ui-kit';
44

@@ -9,9 +9,39 @@ const SIZES = {
99
LARGE: 'large',
1010
};
1111

12+
const AVATAR_COLORS = [
13+
'gray',
14+
'brand',
15+
'red',
16+
'orange',
17+
'brown',
18+
'green',
19+
'blue-1',
20+
'blue-2',
21+
'pink',
22+
'yellow',
23+
'purple',
24+
];
25+
26+
const AVATAR_SHADES = ['primary', 'secondary']; // tertiary is too light
27+
28+
const getAvatarColor = (name) => {
29+
let hash = 0;
30+
for (let i = 0; i < name.length; i += 1) {
31+
hash = name.charCodeAt(i) + hash * 31;
32+
}
33+
34+
const index = Math.abs(hash) % (AVATAR_COLORS.length * AVATAR_SHADES.length);
35+
const color = AVATAR_COLORS[index % AVATAR_COLORS.length];
36+
const shade = AVATAR_SHADES[Math.floor(index / AVATAR_COLORS.length)];
37+
38+
return `var(--c--contextuals--background--palette--${color}--${shade})`;
39+
};
40+
1241
// eslint-disable-next-line no-unused-vars
1342
const User = React.memo(({ name, avatarUrl, size, isDisabled, onClick }) => {
14-
const contentNode = <UserAvatar fullName={name} size={size} />;
43+
const forceColor = useMemo(() => getAvatarColor(name), [name]);
44+
const contentNode = <UserAvatar fullName={name} size={size} forceColor={forceColor} />;
1545

1646
return onClick ? (
1747
<button type="button" disabled={isDisabled} onClick={onClick}>

0 commit comments

Comments
 (0)