Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZUI Avatar #2535

Open
wants to merge 1 commit into
base: undocumented/new-design-system
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/zui/components/ZUIAvatar/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Meta, StoryObj } from '@storybook/react';

import ZUIAvatar from './index';

const meta: Meta<typeof ZUIAvatar> = {
component: ZUIAvatar,
title: 'Components/ZUIAvatar',
};

export default meta;
type Story = StoryObj<typeof ZUIAvatar>;

export const Small: Story = {
args: {
id: 1,
size: 'small',
text: 'Angela Davis',
variant: 'circular',
},
};

export const Medium: Story = {
args: {
id: 1,
size: 'medium',
text: 'Angela Davis',
variant: 'circular',
},
};

export const Large: Story = {
args: {
id: 1,
size: 'large',
text: 'Angela Davis',
variant: 'circular',
},
};

export const Circular: Story = {
args: {
id: 1,
size: 'large',
text: 'Angela Davis',
variant: 'circular',
},
};

export const Rounded: Story = {
args: {
id: 1,
size: 'large',
text: 'Angela Davis',
variant: 'rounded',
},
};
128 changes: 128 additions & 0 deletions src/zui/components/ZUIAvatar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { create } from 'random-seed';
import { FC } from 'react';

import palette from 'zui/theme/palette';
import typography from 'zui/theme/typography';

interface ZUIAvatarProps {
size?: 'small' | 'medium' | 'large';
variant?: 'rounded' | 'circular';
id: number;
text?: string;
}

const ZUIAvatar: FC<ZUIAvatarProps> = ({
size = 'medium',
variant = 'circular',
text = '',
id,
}) => {
const s = size == 'small' ? 24 : size == 'medium' ? 32 : 48;
const fontSize = size == 'small' ? 12 : size == 'medium' ? 16 : 20;

const idStr = '' + id;

const seededRand = create(idStr);
const rand = () => seededRand(1000) / 1000;
let shortText = '';
const textParts = text.split(' ');
if (textParts.length >= 2 && textParts[1].length != 0) {
shortText = textParts[0][0] + textParts[1][0];
} else {
shortText = textParts[0].slice(0, 2);
}

shortText = shortText.toUpperCase();

const colors = Object.keys(palette.swatches).reduce((acc, swatch) => {
type Palette = {
swatches: {
[key: string]: {
light: { color: string };
medium: { color: string };
};
};
};
if (
typeof (palette as unknown as Palette).swatches[swatch].light !=
'undefined'
) {
acc.push((palette as unknown as Palette).swatches[swatch].light.color);
}
if (
typeof (palette as unknown as Palette).swatches[swatch].medium !=
'undefined'
) {
acc.push((palette as unknown as Palette).swatches[swatch].medium.color);
}
return acc;
}, [] as string[]);
const getColor = (): string => colors[seededRand(colors.length)];
// `hsl(${Math.floor(rand() * 360)}, ${Math.floor(
// rand() * 20 + 40
// )}%, ${Math.floor(rand() * 40 + 50)}%)`;

return (
<svg height={s} version="1.1" width={s} xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient
gradientTransform={'rotate(' + rand() * 360 + ')'}
id="gradient"
>
<stop offset="5%" stopColor={getColor()} />
<stop offset="95%" stopColor={getColor()} />
</linearGradient>

<filter id="blurMe">
<feGaussianBlur
edgeMode="wrap"
in="SourceGraphic"
stdDeviation="10"
/>
</filter>

<clipPath id={size + 'circleClip'}>
<circle cx={s / 2} cy={s / 2} r={s / 2} />
</clipPath>
<clipPath id={size + 'roundedClip'}>
<rect height={s}rx="4" ry="4" width={s} />
</clipPath>
</defs>

<g
clipPath={
variant == 'circular'
? 'url(#' + size + 'circleClip)'
: 'url(#' + size + 'roundedClip)'
}
id="content"
>
<rect fill="url(#gradient)" height={s} width={s} />
<g filter="url(#blurMe)">
<rect fill={getColor()} height={s} width={s} x={-s / 2} y={-s / 2} />
<line
stroke={getColor()}
strokeWidth={15}
x1={s * 1.5}
x2={s / 2}
y1={s * 1.5}
y2={s / 3}
/>
</g>

<text
dominantBaseline="middle"
fontFamily={typography.bodyMdRegular?.fontFamily}
fontSize={fontSize}
textAnchor="middle"
x={s / 2}
y={s / 2}
>
{shortText}
</text>
</g>
</svg>
);
};

export default ZUIAvatar;
Loading