-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
964f2ce
commit 688d0f0
Showing
17 changed files
with
568 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
|
||
module.exports = { | ||
"testEnvironment": "jsdom" | ||
"testEnvironment": "jsdom", | ||
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'], | ||
"moduleNameMapper": { | ||
"\\.(css|less|scss|sss|styl)$": "<rootDir>/node_modules/jest-css-modules" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ declare module "*.svg" { | |
React.SVGAttributes<SVGElement> | ||
>; | ||
} | ||
|
||
declare module "*.module.css"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2023 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
.avatar { | ||
display: inline-block; | ||
aspect-ratio: 1 / 1; | ||
width: var(--cpd-avatar-size); | ||
line-height: var(--cpd-avatar-size); | ||
text-align: center; | ||
font-size: min(calc(var(--cpd-avatar-size) * 0.75), 60px); | ||
border-radius: var(--cpd-avatar-radius); | ||
text-transform: uppercase; | ||
speak: none; | ||
pointer-events: none; | ||
font-weight: normal; | ||
overflow: hidden; | ||
user-select: none; | ||
} | ||
|
||
img.avatar { | ||
object-fit: cover; | ||
overflow: hidden; | ||
} | ||
|
||
.avatar:not([src]) { | ||
/* In the future we'd prefer to pass the HEX code as the data attr | ||
and use `attr(data-color)` to avoid the style declaration from below | ||
but this is currently not supported in all browsers */ | ||
background: var(--cpd-avatar-background); | ||
color: #fff; | ||
} | ||
|
||
.avatar[data-color] { | ||
--cpd-avatar-background: #368bd6; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="2"] { | ||
--cpd-avatar-background: #ac3ba8; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="3"] { | ||
--cpd-avatar-background: #03b381; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="4"] { | ||
--cpd-avatar-background: #e64f7a; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="5"] { | ||
--cpd-avatar-background: #ff812d; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="6"] { | ||
--cpd-avatar-background: #2dc2c5; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="7"] { | ||
--cpd-avatar-background: #5c56f5; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-color="8"] { | ||
--cpd-avatar-background: #74d12c; /* TODO: Replace with a compound color */ | ||
} | ||
|
||
.avatar[data-type="round"] { | ||
--cpd-avatar-radius: 50%; | ||
} | ||
|
||
.avatar[data-type="square"] { | ||
--cpd-avatar-radius: 25%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
Copyright 2023 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import React from "react"; | ||
import { Meta, StoryFn } from "@storybook/react"; | ||
|
||
import { Avatar as AvatarComponent } from "./Avatar"; | ||
|
||
export default { | ||
title: "Avatar", | ||
component: AvatarComponent, | ||
argTypes: {}, | ||
args: { | ||
name: "Bob", | ||
size: "64px", | ||
id: "@bob:example.org", | ||
src: "https://placekitten.com/200/200", | ||
type: "round", | ||
}, | ||
} as Meta<typeof AvatarComponent>; | ||
|
||
const Template: StoryFn<typeof AvatarComponent> = (args) => ( | ||
<AvatarComponent {...args} /> | ||
); | ||
|
||
export const Round = Template.bind({}); | ||
Round.args = { | ||
type: "round", | ||
}; | ||
|
||
export const Square = Template.bind({}); | ||
Square.args = { | ||
type: "square", | ||
}; | ||
|
||
export const NoImageFallback = Template.bind({}); | ||
NoImageFallback.args = { | ||
src: "", | ||
}; | ||
|
||
export const LargeNoImageFallback = Template.bind({}); | ||
NoImageFallback.args = { | ||
src: "", | ||
size: "128px", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2023 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { render, waitFor } from "@testing-library/react"; | ||
import React from "react"; | ||
|
||
import { Avatar } from "./Avatar"; | ||
|
||
const originalImage = global.Image; | ||
|
||
describe("Avatar", () => { | ||
beforeEach(() => { | ||
global.Image = class extends originalImage { | ||
constructor() { | ||
super(); | ||
setTimeout(() => { | ||
this.onload?.(new Event("load")); | ||
}, 50); | ||
} | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
global.Image = originalImage; | ||
}); | ||
|
||
it("renders the image-less avatar", () => { | ||
const { asFragment } = render(<Avatar name="Bob" id="@bob:example.org" />); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it("replaces the placeholder with the actual image", async () => { | ||
const { container } = render( | ||
<Avatar src="./mock.jpg" name="Bob" id="@bob:example.org" /> | ||
); | ||
|
||
expect(container).toHaveTextContent("B"); | ||
await waitFor(() => expect(document.querySelector("img")).not.toBeNull()); | ||
}); | ||
|
||
it("does not split emoji as first letter", () => { | ||
const { container } = render( | ||
<Avatar name="🤓 John" id="@john:example.org" /> | ||
); | ||
expect(container).toHaveTextContent("🤓"); | ||
}); | ||
|
||
it.each([ | ||
["@bob:example.org", "8"], | ||
["@alice:example.org", "3"], | ||
["@charlie:example.org", "5"], | ||
["@dan:example.org", "8"], | ||
["@elena:example.org", "2"], | ||
["@fanny:example.org", "1"], | ||
])( | ||
"has a deterministic color based on the id passed to it, %s", | ||
(id, colorNumber) => { | ||
const { container } = render(<Avatar name={id} id={id} />); | ||
expect(container.firstChild).toHaveAttribute("data-color", colorNumber); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright 2023 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import classnames from "classnames"; | ||
import React, { Suspense } from "react"; | ||
import { getInitialLetter } from "../../utils/string"; | ||
import { SuspenseImg } from "../../utils/SuspenseImg"; | ||
import styles from "./Avatar.module.css"; | ||
import { useUsernameColor } from "./useUsernameColor"; | ||
|
||
type AvatarProps = { | ||
src?: string; | ||
id: string; | ||
name: string; | ||
type?: "square" | "round"; | ||
className?: string; | ||
size?: CSSStyleDeclaration["height"]; | ||
}; | ||
|
||
export const Avatar = ({ | ||
src, | ||
id, | ||
name = "", | ||
type = "round", | ||
className = "", | ||
size, | ||
}: AvatarProps): JSX.Element => { | ||
const color = useUsernameColor(id); | ||
const style = { | ||
"--cpd-avatar-size": size, | ||
} as React.CSSProperties; | ||
const imagelessAvatar = ( | ||
<span | ||
role="img" | ||
aria-label="" | ||
data-type={type} | ||
data-color={color} | ||
className={classnames(styles.avatar, className)} | ||
style={style} | ||
title={id} | ||
> | ||
{getInitialLetter(name)} | ||
</span> | ||
); | ||
|
||
return !src ? ( | ||
imagelessAvatar | ||
) : ( | ||
<Suspense fallback={imagelessAvatar}> | ||
<SuspenseImg | ||
src={src} | ||
className={classnames(styles.avatar, className)} | ||
data-type={type} | ||
style={style} | ||
width={size} | ||
height={size} | ||
title={id} | ||
/> | ||
</Suspense> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Avatar renders the image-less avatar 1`] = ` | ||
<DocumentFragment> | ||
<span | ||
aria-label="" | ||
class="avatar" | ||
data-color="8" | ||
data-type="round" | ||
role="img" | ||
title="@bob:example.org" | ||
> | ||
B | ||
</span> | ||
</DocumentFragment> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright 2023 New Vector Ltd | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Determines a color to assign to a given username | ||
* @param id - a Matrix username ID | ||
* @returns a hexadecimal color | ||
*/ | ||
export function useUsernameColor(id: string): number { | ||
const MIN = 1; | ||
const MAX = 8; | ||
// Sum up the values of all the char codes in the string | ||
const charCodeSum = id.split("").reduce((sum, char) => { | ||
return sum + char.charCodeAt(0); | ||
}, 0); | ||
return (charCodeSum % MAX) + MIN; | ||
} |
Oops, something went wrong.