Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 1696c5c

Browse files
authored
Change avatarsetting componment to use a menu (#12585)
* New user profile UI in User Settings Using new Edit In Place component. * Show avatar upload error * Fix avatar upload error * Wire up errors & feedback for display name setting * Implement avatar upload / remove progress toast * Add 768px breakpoint * Fix display of no avatar in avatar setting controls There was supposed to be a person icon but it was invisible, and also would have been inappropriate for room avatars anyway. This makes it match the designs by being the same as whatever the default avatar is. * Fix room profile display * Update edit icon on avatarsetting comnponent * Change avatarsetting componment to use a menu As per the designs, remove the 'remove' link and instead have a menu pop up to either upload a new file or remove the avatar. This also changes the room profile viw, since that uses the same view. * Update to released compund-web with required components / fixes * Require compound-web 4.4.0 because we do need it * Update snapshots Because of course all the auto-generated IDs of unrelated things have changed. * Fix duplicate import * Fix CSS comment * Update snapshot * Run all the tests so the ids stay the same * Start of a test for ProfileSettings * More tests * Test that a toast appears * Test ToastRack * Update snapshots * Add the usernamee control * Fix playwright tests * New compound version for editinplace fixes * Fix useId to not just generate a constant ID * Use the label in the username component * Fix widths of test boxes * Update screenshots * Put ^ back on compound-web version * Split CSS for room & user profile settings and name the components correspondingly * Fix playwright test * Update room settings screenshot * Use original screenshot instead * Add required props in test * Fix test * Also here * Update screenshots * Remove user icon ...which is unused now, as far as I can see. * Fix styling of unrelated buttons Needed to be added in other places otherwise the specificity changes. Also put the old screenshots back. * Add copyright year * Fix copyright year * Switch to useMatrixClientContext * Fix other test * Make clickable with no avatar again and fix tests and renmove a test for the remove button which is no longer there * Put back missing CSS to make the menu entry red * Fix type error * Fix tests * Supply open / onOpenChange props * Fix tests * There is no hover anymore * Use the computed name, not the name which may be null * Fix room avatar remove behaviour * Remove redundant else
1 parent c0cbd68 commit 1696c5c

File tree

12 files changed

+106
-133
lines changed

12 files changed

+106
-133
lines changed

playwright/e2e/settings/general-user-settings-tab.spec.ts

+9-18
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,6 @@ test.describe("General user settings tab", () => {
4545
// Assert that a userId is rendered
4646
expect(uut.getByLabel("Username")).toHaveText(user.userId);
4747

48-
// Check avatar setting
49-
const avatar = profile.locator(".mx_AvatarSetting_avatar");
50-
await avatar.hover();
51-
52-
// Hover effect
53-
await expect(avatar.locator(".mx_AvatarSetting_hoverBg")).toBeVisible();
54-
await expect(avatar.locator(".mx_AvatarSetting_hover span").getByText("Upload")).toBeVisible();
55-
5648
// Wait until spinners disappear
5749
await expect(uut.getByTestId("accountSection").locator(".mx_Spinner")).not.toBeVisible();
5850
await expect(uut.getByTestId("discoverySection").locator(".mx_Spinner")).not.toBeVisible();
@@ -128,21 +120,20 @@ test.describe("General user settings tab", () => {
128120
await expect(uut).toMatchScreenshot("general-smallscreen.png");
129121
});
130122

131-
test("should support adding and removing a profile picture", async ({ uut }) => {
123+
test("should support adding and removing a profile picture", async ({ uut, page }) => {
132124
const profileSettings = uut.locator(".mx_UserProfileSettings");
133125
// Upload a picture
134126
await profileSettings.getByAltText("Upload").setInputFiles("playwright/sample-files/riot.png");
135127

136-
// Find and click "Remove" link button
137-
await profileSettings
138-
.locator(".mx_UserProfileSettings_profile")
139-
.getByRole("button", { name: "Remove" })
140-
.click();
128+
// Image should be visible
129+
await expect(profileSettings.locator(".mx_AvatarSetting_avatar img")).toBeVisible();
141130

142-
// Assert that the link button disappeared
143-
await expect(
144-
profileSettings.locator(".mx_AvatarSetting_avatar .mx_AccessibleButton_kind_link_sm"),
145-
).not.toBeVisible();
131+
// Open the menu & click remove
132+
await profileSettings.getByRole("button", { name: "Profile Picture" }).click();
133+
await page.getByRole("menuitem", { name: "Remove" }).click();
134+
135+
// Assert that the image disappeared
136+
await expect(profileSettings.locator(".mx_AvatarSetting_avatar img")).not.toBeVisible();
146137
});
147138

148139
test("should set a country calling code based on default_country_code", async ({ uut }) => {
Loading
Loading

res/css/views/settings/_AvatarSetting.pcss

+15-44
Original file line numberDiff line numberDiff line change
@@ -21,36 +21,6 @@ limitations under the License.
2121
margin-top: 8px;
2222
position: relative;
2323

24-
.mx_AvatarSetting_hover {
25-
transition: opacity var(--hover-transition);
26-
opacity: 0;
27-
28-
/* position to place the hover bg over the entire thing */
29-
position: absolute;
30-
inset: 0;
31-
32-
pointer-events: none; /* let the pointer fall through the underlying thing */
33-
34-
line-height: 90px;
35-
text-align: center;
36-
37-
> span {
38-
color: $primary-content;
39-
position: relative; /* tricks the layout engine into putting this on top of the bg */
40-
font-weight: 500;
41-
}
42-
43-
.mx_AvatarSetting_hoverBg {
44-
/* absolute position to lazily fill the entire container */
45-
position: absolute;
46-
inset: 0;
47-
48-
opacity: 0.5;
49-
background-color: $quinary-content;
50-
border-radius: 90px;
51-
}
52-
}
53-
5424
&.mx_AvatarSetting_avatarDisplay:hover .mx_AvatarSetting_hover {
5525
opacity: 1;
5626
}
@@ -77,25 +47,26 @@ limitations under the License.
7747
}
7848

7949
.mx_AvatarSetting_uploadButton {
80-
width: 32px;
81-
height: 32px;
50+
width: 28px;
51+
height: 28px;
8252
border-radius: 32px;
83-
background-color: $secondary-content;
53+
border: 1px solid var(--cpd-color-bg-canvas-default);
54+
background-color: var(--cpd-color-bg-subtle-primary);
8455

8556
position: absolute;
8657
bottom: 0;
8758
right: 0;
88-
}
59+
text-align: center;
60+
cursor: pointer;
8961

90-
.mx_AvatarSetting_uploadButton::before {
91-
content: "";
92-
display: block;
93-
width: 100%;
94-
height: 100%;
95-
mask-repeat: no-repeat;
96-
mask-position: center;
97-
mask-size: 55%;
98-
background-color: $quinary-content;
99-
mask-image: url("$(res)/img/feather-customised/edit.svg");
62+
svg {
63+
position: relative;
64+
top: 3px;
65+
}
10066
}
10167
}
68+
69+
.mx_AvatarSetting_removeMenuItem svg,
70+
.mx_AvatarSetting_removeMenuItem span {
71+
color: var(--cpd-color-text-critical-primary) !important;
72+
}

src/components/views/room_settings/RoomProfileSettings.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
218218
);
219219
}
220220

221+
const canRemove = this.state.profileFieldsTouched.avatar
222+
? Boolean(this.state.avatarFile)
223+
: Boolean(this.state.originalAvatarUrl);
224+
221225
return (
222226
<form onSubmit={this.saveProfile} autoComplete="off" noValidate={true} className="mx_RoomProfileSettings">
223227
<div className="mx_RoomProfileSettings_profile">
@@ -254,9 +258,9 @@ export default class RoomProfileSettings extends React.Component<IProps, IState>
254258
avatarAltText={_t("room_settings|general|avatar_field_label")}
255259
disabled={!this.state.canSetAvatar}
256260
onChange={this.onAvatarChanged}
257-
removeAvatar={this.removeAvatar}
261+
removeAvatar={canRemove ? this.removeAvatar : undefined}
258262
placeholderId={idNameForRoom(MatrixClientPeg.safeGet().getRoom(this.props.roomId)!)}
259-
placeholderName={this.state.displayName}
263+
placeholderName={MatrixClientPeg.safeGet().getRoom(this.props.roomId)!.name}
260264
/>
261265
</div>
262266
{profileSettingsButtons}

src/components/views/settings/AvatarSetting.tsx

+70-35
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,59 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import React, { createRef, useCallback, useEffect, useState } from "react";
17+
import React, { ReactNode, createRef, useCallback, useEffect, useState } from "react";
18+
import { Icon as EditIcon } from "@vector-im/compound-design-tokens/icons/edit.svg";
19+
import { Icon as UploadIcon } from "@vector-im/compound-design-tokens/icons/share.svg";
20+
import { Icon as DeleteIcon } from "@vector-im/compound-design-tokens/icons/delete.svg";
21+
import { Menu, MenuItem } from "@vector-im/compound-web";
1822

1923
import { _t } from "../../../languageHandler";
20-
import AccessibleButton from "../elements/AccessibleButton";
2124
import { mediaFromMxc } from "../../../customisations/Media";
2225
import { chromeFileInputFix } from "../../../utils/BrowserWorkarounds";
2326
import { useId } from "../../../utils/useId";
27+
import AccessibleButton from "../elements/AccessibleButton";
2428
import BaseAvatar from "../avatars/BaseAvatar";
2529

30+
interface MenuProps {
31+
trigger: ReactNode;
32+
onUploadSelect: () => void;
33+
onRemoveSelect?: () => void;
34+
}
35+
36+
const AvatarSettingContextMenu: React.FC<MenuProps> = ({ trigger, onUploadSelect, onRemoveSelect }) => {
37+
const [menuOpen, setMenuOpen] = useState(false);
38+
39+
const onOpenChange = useCallback((newOpen: boolean) => {
40+
setMenuOpen(newOpen);
41+
}, []);
42+
43+
return (
44+
<Menu
45+
trigger={trigger}
46+
title={_t("action|set_avatar")}
47+
showTitle={false}
48+
open={menuOpen}
49+
onOpenChange={onOpenChange}
50+
>
51+
<MenuItem
52+
as="div"
53+
Icon={<UploadIcon width="24px" height="24px" />}
54+
label={_t("action|upload_file")}
55+
onSelect={onUploadSelect}
56+
/>
57+
{onRemoveSelect && (
58+
<MenuItem
59+
as="div"
60+
Icon={<DeleteIcon width="24px" height="24px" />}
61+
className="mx_AvatarSetting_removeMenuItem"
62+
label={_t("action|remove")}
63+
onSelect={onRemoveSelect}
64+
/>
65+
)}
66+
</Menu>
67+
);
68+
};
69+
2670
interface IProps {
2771
/**
2872
* The current value of the avatar URL, as an mxc URL or a File.
@@ -136,48 +180,39 @@ const AvatarSetting: React.FC<IProps> = ({
136180
}
137181

138182
let uploadAvatarBtn: JSX.Element | undefined;
139-
if (uploadAvatar) {
140-
// insert an empty div to be the host for a css mask containing the upload.svg
183+
if (!disabled) {
141184
uploadAvatarBtn = (
142-
<>
143-
<AccessibleButton
144-
onClick={uploadAvatar}
145-
className="mx_AvatarSetting_uploadButton"
146-
aria-labelledby={a11yId}
147-
/>
148-
<input
149-
type="file"
150-
style={{ display: "none" }}
151-
ref={fileInputRef}
152-
onClick={chromeFileInputFix}
153-
onChange={onFileChanged}
154-
accept="image/*"
155-
alt={_t("action|upload")}
156-
/>
157-
</>
158-
);
159-
}
160-
161-
let removeAvatarBtn: JSX.Element | undefined;
162-
if (avatarURL && removeAvatar && !disabled) {
163-
removeAvatarBtn = (
164-
<AccessibleButton onClick={removeAvatar} kind="link_sm">
165-
{_t("action|remove")}
166-
</AccessibleButton>
185+
<div className="mx_AvatarSetting_uploadButton">
186+
<EditIcon width="20px" height="20px" />
187+
</div>
167188
);
168189
}
169190

170-
return (
191+
const content = (
171192
<div className="mx_AvatarSetting_avatar" role="group" aria-label={avatarAltText}>
172193
{avatarElement}
173-
<div className="mx_AvatarSetting_hover" aria-hidden="true">
174-
<div className="mx_AvatarSetting_hoverBg" />
175-
{!disabled && <span id={a11yId}>{_t("action|upload")}</span>}
176-
</div>
177194
{uploadAvatarBtn}
178-
{removeAvatarBtn}
179195
</div>
180196
);
197+
198+
if (disabled) {
199+
return content;
200+
}
201+
202+
return (
203+
<>
204+
<AvatarSettingContextMenu trigger={content} onUploadSelect={uploadAvatar} onRemoveSelect={removeAvatar} />
205+
<input
206+
type="file"
207+
style={{ display: "none" }}
208+
ref={fileInputRef}
209+
onClick={chromeFileInputFix}
210+
onChange={onFileChanged}
211+
accept="image/*"
212+
alt={_t("action|upload")}
213+
/>
214+
</>
215+
);
181216
};
182217

183218
export default AvatarSetting;

src/components/views/settings/UserProfileSettings.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ const UserProfileSettings: React.FC = () => {
151151
avatar={avatarURL ?? undefined}
152152
avatarAltText={_t("common|user_avatar")}
153153
onChange={onAvatarChange}
154-
removeAvatar={onAvatarRemove}
154+
removeAvatar={avatarURL ? onAvatarRemove : undefined}
155155
placeholderName={displayName}
156156
placeholderId={client.getUserId() ?? ""}
157157
/>

src/i18n/strings/en_EN.json

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
"save": "Save",
110110
"search": "Search",
111111
"send_report": "Send report",
112+
"set_avatar": "Set profile picture",
112113
"share": "Share",
113114
"show": "Show",
114115
"show_advanced": "Show advanced",
@@ -132,6 +133,7 @@
132133
"update": "Update",
133134
"upgrade": "Upgrade",
134135
"upload": "Upload",
136+
"upload_file": "Upload file",
135137
"verify": "Verify",
136138
"view": "View",
137139
"view_all": "View all",

test/components/views/room_settings/RoomProfileSettings-test.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ describe("RoomProfileSetting", () => {
8585

8686
render(<RoomProfileSettings roomId="!floob:itty" />);
8787

88-
await user.click(screen.getByRole("button", { name: "Remove" }));
88+
await user.click(screen.getByRole("button", { name: "Room avatar" }));
89+
await user.click(screen.getByRole("menuitem", { name: "Remove" }));
8990
await user.click(screen.getByRole("button", { name: "Save" }));
9091

9192
await waitFor(() =>

test/components/views/settings/AvatarSetting-test.tsx

-32
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,6 @@ describe("<AvatarSetting />", () => {
4444
expect(imgElement).toBeInTheDocument();
4545
});
4646

47-
it("renders avatar with remove button", async () => {
48-
const { queryByText } = render(
49-
<AvatarSetting
50-
avatarAltText="Avatar of Peter Fox"
51-
avatar="mxc://example.org/my-avatar"
52-
removeAvatar={jest.fn()}
53-
placeholderId="blee"
54-
placeholderName="boo"
55-
/>,
56-
);
57-
58-
const removeButton = queryByText("Remove");
59-
expect(removeButton).toBeInTheDocument();
60-
});
61-
62-
it("renders avatar without remove button", async () => {
63-
const { queryByText } = render(
64-
<AvatarSetting
65-
placeholderId="blee"
66-
placeholderName="boo"
67-
disabled={true}
68-
avatarAltText="Avatar of Peter Fox"
69-
/>,
70-
);
71-
72-
const removeButton = queryByText("Remove");
73-
expect(removeButton).toBeNull();
74-
});
75-
7647
it("renders a file as the avatar when supplied", async () => {
7748
render(
7849
<AvatarSetting
@@ -102,9 +73,6 @@ describe("<AvatarSetting />", () => {
10273
/>,
10374
);
10475

105-
// not really necessary, but to follow the expected user flow as much as possible
106-
await user.click(screen.getByRole("button", { name: "Avatar of Peter Fox" }));
107-
10876
const fileInput = screen.getByAltText("Upload");
10977
await user.upload(fileInput, AVATAR_FILE);
11078

test/components/views/settings/UserProfileSettings-test.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe("ProfileSettings", () => {
8585
});
8686

8787
it("removes avatar", async () => {
88+
jest.spyOn(OwnProfileStore.instance, "avatarMxc", "get").mockReturnValue("mxc://example.org/my-avatar");
8889
renderProfileSettings(toastRack, client);
8990

9091
expect(await screen.findByText("Mocked AvatarSetting")).toBeInTheDocument();

0 commit comments

Comments
 (0)