Skip to content

Commit

Permalink
Add random avatar colors
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbornhilland committed Dec 25, 2022
1 parent 6c5dabb commit 7b7bc26
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion components/avatar.module.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
.avatar {
--opacity: 50%;
--size: var(--size-8);
--background-color: var(--green-5-hsl);
--background: hsl(var(--background-color) / var(--opacity));

width: var(--size);
height: var(--size);
border-radius: var(--radius-round);
background: hsl(var(--green-5-hsl) / var(--opacity));
background: var(--background);
box-shadow: var(--shadow-1);
display: flex;
align-items: center;
Expand All @@ -14,6 +16,8 @@
font-size: var(--font-size-4);

transition: background 0.5s, transform 0.5s, box-shadow 0.5s;

color: white;
}

.avatar[data-is-rolling="true"] {
Expand Down
11 changes: 9 additions & 2 deletions components/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import styles from "./avatar.module.css";
export type AvatarProps = {
name: string;
isRolling: boolean;
color: string;
};

export function Avatar({ name, isRolling }: AvatarProps) {
export function Avatar({ name, isRolling, color }: AvatarProps) {
return (
<div className={styles.avatarWrapper} title={name}>
<div className={styles.avatar} data-is-rolling={isRolling}>
<div
className={styles.avatar}
style={{
["--background-color" as any]: color,
}}
data-is-rolling={isRolling}
>
{name.charAt(0).toUpperCase()}
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions pages/[roomId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import avatarStyles from "../components/avatar.module.css";
import globalStyles from "../styles/global.module.css";
import { Die } from "../components/die";
import {
getRandomHsl,
getRoll,
getRollerName,
RoomProvider,
Expand Down Expand Up @@ -74,6 +75,7 @@ function Main() {
<Avatar
name={self.presence.name}
isRolling={self.presence.id === roller}
color={self.presence.avatarColor}
></Avatar>
</li>
{others.map((other) => {
Expand All @@ -82,6 +84,7 @@ function Main() {
<Avatar
name={other.presence.name}
isRolling={other.presence.id === roller}
color={other.presence.avatarColor}
></Avatar>
</li>
);
Expand Down Expand Up @@ -174,6 +177,7 @@ export default function Home(props: HomeProps) {
initialPresence={{
name: props.name,
id: Math.floor(Math.random() * 1000),
avatarColor: getRandomHsl(),
}}
initialStorage={{
rolls: new LiveList([
Expand Down
9 changes: 9 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ const client = createClient({
export type Presence = {
name: string;
id: number;
avatarColor: string;
};

export type Storage = {
rolls: LiveList<Roll>;
roller: number | null;
};

export function rand(max: number, min: number) {
return Math.floor(Math.random() * (max - min)) + min;
}

export function getRandom(max = 24, min = 1) {
return (Math.floor(Math.random() * (max - min)) + min) * 90;
}
Expand All @@ -31,6 +36,10 @@ export function getRoll(): Roll {
return [getRandom(), getRandom()];
}

export function getRandomHsl() {
return `${rand(365, 0)} ${rand(100, 0)}% ${rand(100, 0)}%`;
}

export function getRollerName(
id: number,
self: User<Presence, BaseUserMeta>,
Expand Down

2 comments on commit 7b7bc26

@vercel
Copy link

@vercel vercel bot commented on 7b7bc26 Dec 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 7b7bc26 Dec 25, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.